o
    §ƒðjr  ã                   @   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__© ú¨/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/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      