o
    ohr                     @   sh   d dl mZ d dlmZ d dlmZ ddlmZ ddlm	Z	m
Z
mZ G dd deZG d	d
 d
eZdS )    )S)_sympify)KroneckerDelta   )
MatrixExpr)
ZeroMatrixIdentity	OneMatrixc                       sp   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dd Z
dd Ze ZZdd Zdd Z  ZS )PermutationMatrixa  A Permutation Matrix

    Parameters
    ==========

    perm : Permutation
        The permutation the matrix uses.

        The size of the permutation determines the matrix size.

        See the documentation of
        :class:`sympy.combinatorics.permutations.Permutation` for
        the further information of how to create a permutation object.

    Examples
    ========

    >>> from sympy import Matrix, PermutationMatrix
    >>> from sympy.combinatorics import Permutation

    Creating a permutation matrix:

    >>> p = Permutation(1, 2, 0)
    >>> P = PermutationMatrix(p)
    >>> P = P.as_explicit()
    >>> P
    Matrix([
    [0, 1, 0],
    [0, 0, 1],
    [1, 0, 0]])

    Permuting a matrix row and column:

    >>> M = Matrix([0, 1, 2])
    >>> Matrix(P*M)
    Matrix([
    [1],
    [2],
    [0]])

    >>> Matrix(M.T*P)
    Matrix([[2, 0, 1]])

    See Also
    ========

    sympy.combinatorics.permutations.Permutation
    c                    s:   ddl m} t|}t||std|t | |S )Nr   Permutationz({} must be a SymPy Permutation instance.) sympy.combinatorics.permutationsr   r   
isinstance
ValueErrorformatsuper__new__)clspermr   	__class__ z/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/matrices/expressions/permutation.pyr   ;   s   
zPermutationMatrix.__new__c                 C   s   | j d j}||fS Nr   )argssize)selfr   r   r   r   shapeE   s   zPermutationMatrix.shapec                 C      | j d jS r   )r   is_Identityr   r   r   r   r   J      zPermutationMatrix.is_Identityc                 K   s   | j rt| jS | S )N)r   r   rows)r   hintsr   r   r   doitN   s   
zPermutationMatrix.doitc                 K   s   | j d }t|||S r   )r   r   apply)r   ijkwargsr   r   r   r   _entryS   s   
zPermutationMatrix._entryc                 C   s   t | jd |  S r   )r
   r   r$   )r   expr   r   r   _eval_powerW   s   zPermutationMatrix._eval_powerc                 C   s   t | jd d S )Nr   )r
   r   r    r   r   r   _eval_inverseZ   s   zPermutationMatrix._eval_inversec                 C   s.   | j d  }|dkrtjS |dkrtjS t)Nr   r   r,   )r   	signaturer   OneNegativeOneNotImplementedError)r   signr   r   r   _eval_determinant_   s   z#PermutationMatrix._eval_determinantc                    s  ddl m} ddlm} | jd }|j}g }d\}}	}
d}|D ]x}t|}t|}|sH|d || kr=d}|g}|}	|}
q||g ||7 }q||	krs|d ||
 | krg|| || d}|d }q|}	|| |
|7 }
q|	d ||
 | kr|| || d}|	d }q|| |
|7 }
qd g }|D ]0}g }d}|D ]} fdd	|D }|| |t|7 }q |7  ||}t	|}|| q|| S )
Nr   r   r   )BlockDiagMatrix)r   r   r   FTc                    s   g | ]}|  qS r   r   ).0r&   pr   r   
<listcomp>   s    zFPermutationMatrix._eval_rewrite_as_BlockDiagMatrix.<locals>.<listcomp>)
r   r   blockmatrixr4   r   full_cyclic_formlenmaxappendr
   )r   r   r(   r   r4   r   r:   cycles_picksabcflagcyclelmtemppick
new_cycles	new_cyclematr   r6   r    _eval_rewrite_as_BlockDiagMatrixg   s`   













z2PermutationMatrix._eval_rewrite_as_BlockDiagMatrix)__name__
__module____qualname____doc__r   propertyr   r   r$   r)   r+   r-   _eval_transpose_eval_adjointr3   rK   __classcell__r   r   r   r   r
   	   s    1


r
   c                       sL   e Zd ZdZejf fdd	ZdddZedd Z	d	d
 Z
dd Z  ZS )MatrixPermuteaz  Symbolic representation for permuting matrix rows or columns.

    Parameters
    ==========

    perm : Permutation, PermutationMatrix
        The permutation to use for permuting the matrix.
        The permutation can be resized to the suitable one,

    axis : 0 or 1
        The axis to permute alongside.
        If `0`, it will permute the matrix rows.
        If `1`, it will permute the matrix columns.

    Notes
    =====

    This follows the same notation used in
    :meth:`sympy.matrices.matrixbase.MatrixBase.permute`.

    Examples
    ========

    >>> from sympy import Matrix, MatrixPermute
    >>> from sympy.combinatorics import Permutation

    Permuting the matrix rows:

    >>> p = Permutation(1, 2, 0)
    >>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    >>> B = MatrixPermute(A, p, axis=0)
    >>> B.as_explicit()
    Matrix([
    [4, 5, 6],
    [7, 8, 9],
    [1, 2, 3]])

    Permuting the matrix columns:

    >>> B = MatrixPermute(A, p, axis=1)
    >>> B.as_explicit()
    Matrix([
    [2, 3, 1],
    [5, 6, 4],
    [8, 9, 7]])

    See Also
    ========

    sympy.matrices.matrixbase.MatrixBase.permute
    c              	      s   ddl m} t|}|jstd|t|}t|tr"|jd }t||s.td|t|}|dvr:td|j	| }||j
kr\z||}W n ty[   td|||w t | |||S )Nr   r   z#{} must be a SymPy matrix instance.z>{} must be a SymPy Permutation or a PermutationMatrix instance)r   r   zThe axis must be 0 or 1.zsSize does not match between the permutation {} and the matrix {} threaded over the axis {} and cannot be converted.)r   r   r   	is_Matrixr   r   r   r
   r   r   r   resizer   r   )r   rJ   r   axisr   mat_sizer   r   r   r      s8   





zMatrixPermute.__new__Tc                 K   s   | j \}}}|r|jdd|i|}|jdd|i|}|jr!|S |jr8|tju r-t|S |tju r8t|d S t|tt	frA|S t|t
r[|j d |kr[t
|j d ||j d  |S | S )Ndeepr,      r   r   r   )r   r$   r   r   Zeror
   r/   r   r   r	   rT   )r   rY   r#   rJ   r   rW   r   r   r   r$      s    

zMatrixPermute.doitc                 C   r   r   )r   r   r    r   r   r   r     r!   zMatrixPermute.shapec                 K   sD   | j \}}}|dkr||||f S |dkr ||||f S d S )Nr   r   )r   r%   )r   r&   r'   r(   rJ   r   rW   r   r   r   r)     s   zMatrixPermute._entryc                 O   sf   ddl m} | j\}}}|dd}|r||}|dkr$|t||S |dkr1||t|d S d S )Nr   )MatMulrY   Tr   r,   )matmulr\   r   getrewriter
   )r   r   r(   r\   rJ   r   rW   rY   r   r   r   _eval_rewrite_as_MatMul"  s   
z%MatrixPermute._eval_rewrite_as_MatMul)T)rL   rM   rN   rO   r   r[   r   r$   rP   r   r)   r`   rS   r   r   r   r   rT      s    3
"
rT   N)
sympy.corer   sympy.core.sympifyr   sympy.functionsr   matexprr   specialr   r   r	   r
   rT   r   r   r   r   <module>   s      