o
    8Va\                     @   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.matrices 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__ H/usr/lib/python3/dist-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                 C   s   | j rt| jS | S )N)r   r   Zrowsr    r   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   Z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   Z	signaturer   OneZ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   Zblockmatrixr/   r   full_cyclic_formlenmaxappendr
   )r   r   r&   r   r/   r   r4   Zcycles_picksabcflagcyclelmZtempZpickZ
new_cyclesZ	new_cyclematr   r1   r    _eval_rewrite_as_BlockDiagMatrixg   s`   













z2PermutationMatrix._eval_rewrite_as_BlockDiagMatrix)__name__
__module____qualname____doc__r   propertyr   r   r"   r'   r(   r*   Z_eval_transposeZ_eval_adjointr.   r@   __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 )MatrixPermutea  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.common.MatrixCommon.permute`.

    Examples
    ========

    >>> from sympy.matrices 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.common.MatrixCommon.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   Z	is_Matrixr   r   r   r
   r   r   r   Zresizer   r   )r   r?   r   axisr   Zmat_sizer   r   r   r      s8   





zMatrixPermute.__new__Tc                 C   s   | j \}}}|r|j|d}|j|d}|jr|S |jr0|tju r%t|S |tju r0t|d S t|tt	fr9|S t|t
rS|j d |krSt
|j d ||j d  |S | S )N)deepr)      r   r   )r   r"   r   r   Zeror
   r+   r   r   r	   rG   )r   rI   r?   r   rH   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&   r?   r   rH   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   )MatMulrI   Tr   r)   )matmulrL   r   getZrewriter
   )r   r   r&   rL   r?   r   rH   rI   r   r   r   _eval_rewrite_as_MatMul"  s   
z%MatrixPermute._eval_rewrite_as_MatMul)T)rA   rB   rC   rD   r   rK   r   r"   rE   r   r'   rO   rF   r   r   r   r   rG      s    3
"
rG   N)Z
sympy.corer   Zsympy.core.sympifyr   Zsympy.functionsr   Zmatexprr   Zspecialr   r   r	   r
   rG   r   r   r   r   <module>   s      