
    \hr                     t    S SK Jr  S SKJr  S SKJr  SSKJr  SSKJ	r	J
r
Jr   " S S\5      r " S	 S
\5      rg)    )S)_sympify)KroneckerDelta   )
MatrixExpr)
ZeroMatrixIdentity	OneMatrixc                   x   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       rS r	S r
S rS	 r\=rrS
 rS rSrU =r$ )PermutationMatrix	   a)  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SK Jn  [        U5      n[        X5      (       d  [	        SR                  U5      5      e[        TU ]  X5      $ )Nr   Permutationz({} must be a SymPy Permutation instance.) sympy.combinatorics.permutationsr   r   
isinstance
ValueErrorformatsuper__new__)clspermr   	__class__s      ^/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/expressions/permutation.pyr   PermutationMatrix.__new__;   sH    @~$,,:AA$GI I ws))    c                 :    U R                   S   R                  nX4$ Nr   )argssize)selfr    s     r   shapePermutationMatrix.shapeE   s    yy|  |r   c                 4    U R                   S   R                  $ r   )r   is_Identityr!   s    r   r%   PermutationMatrix.is_IdentityJ   s    yy|'''r   c                 R    U R                   (       a  [        U R                  5      $ U $ )N)r%   r	   rows)r!   hintss     r   doitPermutationMatrix.doitN   s    DII&&r   c                 V    U R                   S   n[        UR                  U5      U5      $ r   )r   r   apply)r!   ijkwargsr   s        r   _entryPermutationMatrix._entryS   s$    yy|djjmQ//r   c                 T    [        U R                  S   U-  5      R                  5       $ r   )r   r   r+   )r!   exps     r   _eval_powerPermutationMatrix._eval_powerW   s#     1!45::<<r   c                 8    [        U R                  S   S-  5      $ )Nr   )r   r   r&   s    r   _eval_inversePermutationMatrix._eval_inverseZ   s     1!344r   c                     U R                   S   R                  5       nUS:X  a  [        R                  $ US:X  a  [        R                  $ [
        e)Nr   r   r9   )r   	signaturer   OneNegativeOneNotImplementedError)r!   signs     r   _eval_determinant#PermutationMatrix._eval_determinant_   s?    yy|%%'1955LRZ== !!r   c                 V   SSK Jn  SSKJn  U R                  S   nUR
                  n/ nSu  pn
SnU H  n[        U5      n[        U5      nU(       d.  US-   X-   :  a  SnU/nUn	Un
M6  UR                  U/5        X-  nMN  X:  aR  US-   X-   U-   :X  a+  WR                  U5        UR                  U5        SnUS-   nM  Un	WR                  U5        X-  n
M  U	S-   X-   U-   :X  a+  WR                  U5        UR                  U5        SnU	S-   nM  WR                  U5        X-  n
M     Sn/ nU Hm  n/ nSnU H7  nU Vs/ s H  nUU-
  PM
     nnUR                  U5        U[        U5      -  nM9     UU-  nU" U5      n[        U5      nUR                  U5        Mo     U" U6 $ s  snf )Nr   r   r   )BlockDiagMatrix)r   r   r   FT)
r   r   blockmatrixrE   r   full_cyclic_formlenmaxappendr   )r!   r   r1   r   rE   r   rG   cycles_picksabcflagcyclelmtempppick
new_cyclesr/   	new_cyclemats                         r    _eval_rewrite_as_BlockDiagMatrix2PermutationMatrix._eval_rewrite_as_BlockDiagMatrixg   s   @0yy|00 a%EE
AE
Aq515=D!7DAA ''0FA 51u	)E*$++D1$aCE*1u	)E*$++D1$aCE*C &H  DJA,12EqQUE	2!!),SZ  FAz*D#D)CKK ! %% 3s    F& )__name__
__module____qualname____firstlineno____doc__r   propertyr"   r%   r+   r2   r6   r:   _eval_transpose_eval_adjointrB   rY   __static_attributes____classcell__r   s   @r   r   r   	   se    /b*   ( (
0=5 '43Om">& >&r   r   c                   l   ^  \ rS rSrSr\R                  4U 4S jjrS	S jr\	S 5       r
S rS rSrU =r$ )
MatrixPermute   a  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                 8  > SSK Jn  [        U5      nUR                  (       d  [	        SR                  U5      5      e[        U5      n[        U[        5      (       a  UR                  S   n[        X$5      (       d  [	        SR                  U5      5      e[        U5      nUS;  a  [	        S5      eUR                  U   nXRR                  :w  a   UR                  U5      n[        TU ]5  XX#5      $ ! [         a    [	        SR                  X!U5      5      ef = f)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   rX   r   axisr   mat_sizer   s         r   r   MatrixPermute.__new__   s   @sm}}5<<TBD D ~d-..99Q<D$,,!6$<) ) ~v78899T?yy .{{8, ws44  . / VDt,	. ..s   C3 3&Dc                 $   U R                   u  p4nU(       a(  UR                  " SSU0UD6nUR                  " SSU0UD6nUR                  (       a  U$ UR                  (       a?  U[        R                  L a  [        U5      $ U[        R                  L a  [        US-  5      $ [        U[        [        45      (       a  U$ [        U[        5      (       a<  UR                   S   U:X  a)  [        UR                   S   XCR                   S   -  U5      $ U $ )Ndeepr9      r   r   r[   )r   r+   r%   r   Zeror   r>   r   r   r
   rh   )r!   rq   r*   rX   r   rm   s         r   r+   MatrixPermute.doit   s    ))4((...C990$0%0DJ??qvv~(..(r22cJ	233Jc=))chhqkT.A !dXXa[.@$GGr   c                 4    U R                   S   R                  $ r   )r   r"   r&   s    r   r"   MatrixPermute.shape  s    yy|!!!r   c                     U R                   u  pEnUS:X  a  XER                  U5      U4   $ US:X  a  XAUR                  U5      4   $ g )Nr   r   )r   r.   )r!   r/   r0   r1   rX   r   rm   s          r   r2   MatrixPermute._entry  sN    ))419zz!}a'((QY$**Q-'(( r   c                     SSK Jn  U R                  u  pEnUR                  SS5      nU(       a  UR	                  U5      nUS:X  a  U" [        U5      U5      $ US:X  a  U" U[        US-  5      5      $ g )Nr   )MatMulrq   Tr   r9   )matmulrz   r   getrewriter   )r!   r   r1   rz   rX   r   rm   rq   s           r   _eval_rewrite_as_MatMul%MatrixPermute._eval_rewrite_as_MatMul"  sp    "))4zz&$'++f%C19+D1377QY#0r:;; r   r[   )T)r\   r]   r^   r_   r`   r   rs   r   r+   ra   r"   r2   r~   rd   re   rf   s   @r   rh   rh      s@    2f &'VV  5D0 " ")< <r   rh   N)
sympy.corer   sympy.core.sympifyr   sympy.functionsr   matexprr   specialr   r	   r
   r   rh   r[   r   r   <module>r      s4     ' *  4 4\&
 \&~G<J G<r   