
    Edn                         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e          Z G d	 d
e          ZdS )    )S)_sympify)KroneckerDelta   )
MatrixExpr)
ZeroMatrixIdentity	OneMatrixc                   z     e Zd ZdZ fdZed             Zed             Zd Zd Z	d Z
d ZexZZd	 Zd
 Z x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                     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__s      Flib/python3.11/site-packages/sympy/matrices/expressions/permutation.pyr   zPermutationMatrix.__new__;   ss    @@@@@@~~$,, 	I:AA$GGI I I wwsD)))    c                 .    | j         d         j        }||fS Nr   )argssize)selfr   s     r   shapezPermutationMatrix.shapeE   s    y| d|r   c                 &    | j         d         j        S r   )r   is_Identityr   s    r   r"   zPermutationMatrix.is_IdentityJ   s    y|''r   c                 <    | j         rt          | j                  S | S )N)r"   r	   rows)r   hintss     r   doitzPermutationMatrix.doitN   s"     	'DI&&&r   c                 b    | j         d         }t          |                    |          |          S r   )r   r   apply)r   ijkwargsr   s        r   _entryzPermutationMatrix._entryS   s'    y|djjmmQ///r   c                 `    t          | j        d         |z                                            S r   )r   r   r'   )r   exps     r   _eval_powerzPermutationMatrix._eval_powerW   s'     1!455::<<<r   c                 <    t          | j        d         dz            S )Nr   )r   r   r#   s    r   _eval_inversezPermutationMatrix._eval_inverseZ   s     1!3444r   c                     | j         d                                         }|dk    rt          j        S |dk    rt          j        S t
          )Nr   r   r2   )r   	signaturer   OneNegativeOneNotImplementedError)r   signs     r   _eval_determinantz#PermutationMatrix._eval_determinant_   sF    y|%%''19 	!5LRZ 	!= !!r   c                    ddl m} ddlm} | j        d         }|j        }g }d\  }}	}
d}|D ]}t          |          }t          |          }|s2|dz   ||z   k    r
d}|g}|}	|}
9|                    |g           ||z  }U||	k    r^|dz   ||
z   |z   k    r2|                    |           |                    |           d}|dz   }|}	|                    |           |
|z  }
|	dz   ||
z   |z   k    r2|                    |           |                    |           d}|	dz   }|                    |           |
|z  }
dg }|D ]t}g }d}|D ]7}fd|D             }|                    |           |t          |          z  }8|z   ||          }t          |          }|                    |           u || S )	Nr   r   r   )BlockDiagMatrix)r   r   r   FTc                     g | ]}|z
  S  r>   ).0r*   ps     r   
<listcomp>zFPermutationMatrix._eval_rewrite_as_BlockDiagMatrix.<locals>.<listcomp>   s    222qQU222r   )
r   r   blockmatrixr<   r   full_cyclic_formlenmaxappendr   )r   r   r,   r   r<   r   rC   cycles_picksabcflagcyclelmtemppick
new_cycles	new_cyclematr@   s                       @r    _eval_rewrite_as_BlockDiagMatrixz2PermutationMatrix._eval_rewrite_as_BlockDiagMatrixg   sp   @@@@@@000000y|0 1a% !	 !	EE

AE

A q51q5= D!7DAAA ''000FAA q5 1uA	) E***$++D111$aCE***Q1uA	) E***$++D111$aCE***Q   
	 
	DJA    2222E222	!!),,,SZZFA;z**D#D))CKK%%r   )__name__
__module____qualname____doc__r   propertyr    r"   r'   r-   r0   r3   _eval_transpose_eval_adjointr:   rT   __classcell__r   s   @r   r   r   	   s        / /b* * * * *   X ( ( X(  
0 0 0= = =5 5 5 '43Om" " ">& >& >& >& >& >& >&r   r   c                   Z     e Zd ZdZej        f fd	ZddZed             Z	d Z
d Z xZS )	MatrixPermuteav  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 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                    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    rI	 |                    |          }n2# t          $ r% t	          d                    |||                    w xY 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   rS   r   axisr   mat_sizer   s         r   r   zMatrixPermute.__new__   sn   @@@@@@smm} 	D5<<TBBD D D ~~d-.. 	 9Q<D$,, 	)!6$<<) ) ) ~~v 	978889T?ty  	..{{8,, . . . / VD#t,,	. . .. wwsCt444s   C4 4/D#Tc                    | j         \  }}}|r |j        dd|i|} |j        dd|i|}|j        r|S |j        r=|t          j        u rt          |          S |t          j        u rt          |dz            S t          |t          t          f          r|S t          |t                    r;|j         d         |k    r*t          |j         d         ||j         d         z  |          S | S )Ndeepr2      r   r   r>   )r   r'   r"   r   Zeror   r6   r   r   r
   r_   )r   rf   r&   rS   r   rc   s         r   r'   zMatrixPermute.doit   s   )T4 	1#(.....C4900$0%00D 	J? 	3qv~ 3(... 3(r222cJ	233 	Jc=)) 	HchqkT.A 	H !dSXa[.@$GGGr   c                 &    | j         d         j        S r   )r   r    r#   s    r   r    zMatrixPermute.shape  s    y|!!r   c                     | j         \  }}}|dk    r||                    |          |f         S |dk    r|||                    |          f         S d S )Nr   r   )r   r)   )r   r*   r+   r,   rS   r   rc   s          r   r-   zMatrixPermute._entry  se    )T419 	)tzz!}}a'((QY 	)q$**Q--'((	) 	)r   c                    ddl m} | j        \  }}}|                    dd          }|r|                    |          }|dk    r |t          |          |          S |dk    r ||t          |dz                      S d S )Nr   )MatMulrf   Tr   r2   )matmulrl   r   getrewriter   )r   r   r,   rl   rS   r   rc   rf   s           r   _eval_rewrite_as_MatMulz%MatrixPermute._eval_rewrite_as_MatMul"  s    """""")T4zz&$'' 	&++f%%C19 	<6+D113777QY 	<6#0r::;;;	< 	<r   )T)rU   rV   rW   rX   r   rh   r   r'   rY   r    r-   rp   r\   r]   s   @r   r_   r_      s        2 2f &'V  5  5  5  5  5  5D   0 " " X") ) )< < < < < < <r   r_   N)
sympy.corer   sympy.core.sympifyr   sympy.functionsr   matexprr   specialr   r	   r
   r   r_   r>   r   r   <module>rv      s          ' ' ' ' ' ' * * * * * *       4 4 4 4 4 4 4 4 4 4\& \& \& \& \&
 \& \& \&~G< G< G< G< G<J G< G< G< G< G<r   