
    sgr                     t    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y)    )S)_sympify)KroneckerDelta   )
MatrixExpr)
ZeroMatrixIdentity	OneMatrixc                   n     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j                  |            t        |   | |      S )Nr   Permutationz({} must be a SymPy Permutation instance.) sympy.combinatorics.permutationsr   r   
isinstance
ValueErrorformatsuper__new__)clspermr   	__class__s      Y/var/www/html/venv/lib/python3.12/site-packages/sympy/matrices/expressions/permutation.pyr   zPermutationMatrix.__new__;   sI    @~$,:AA$GI I wsD))    c                 <    | j                   d   j                  }||fS Nr   )argssize)selfr   s     r   shapezPermutationMatrix.shapeE   s    yy|  d|r   c                 4    | j                   d   j                  S r   )r   is_Identityr   s    r   r"   zPermutationMatrix.is_IdentityJ   s    yy|'''r   c                 H    | j                   rt        | j                        S | S )N)r"   r	   rows)r   hintss     r   doitzPermutationMatrix.doitN   s    DII&&r   c                 V    | j                   d   }t        |j                  |      |      S r   )r   r   apply)r   ijkwargsr   s        r   _entryzPermutationMatrix._entryS   s$    yy|djjmQ//r   c                 T    t        | j                  d   |z        j                         S r   )r   r   r'   )r   exps     r   _eval_powerzPermutationMatrix._eval_powerW   s#     1!45::<<r   c                 8    t        | j                  d   dz        S )Nr   )r   r   r#   s    r   _eval_inversezPermutationMatrix._eval_inverseZ   s     1!344r   c                     | j                   d   j                         }|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_   s?    yy|%%'1955LRZ== !!r   c                 8   ddl m} ddlm} | j                  d   }|j
                  }g }d\  }}	}
d}|D ]  }t        |      }t        |      }|s-|dz   ||z   kD  r
d}|g}|}	|}
0|j                  |g       ||z  }H||	kD  rQ|dz   ||
z   |z   k(  r*j                  |       |j                  |       d}|dz   }|}	j                  |       |
|z  }
|	dz   ||
z   |z   k(  r*j                  |       |j                  |       d}|	dz   }j                  |       |
|z  }
 d}g }|D ]g  }g }d}|D ]3  }|D cg c]  }||z
  	 }}|j                  |       |t        |      z  }5 ||z  } ||      }t        |      }|j                  |       i  || S c c}w )Nr   r   r   )BlockDiagMatrix)r   r   r   FT)
r   r   blockmatrixr<   r   full_cyclic_formlenmaxappendr   )r   r   r,   r   r<   r   r>   cycles_picksabcflagcyclelmtempppick
new_cyclesr*   	new_cyclemats                         r    _eval_rewrite_as_BlockDiagMatrixz2PermutationMatrix._eval_rewrite_as_BlockDiagMatrixg   s   @0yy|00 1a% !	EE
AE
Aq51q5=D!7DAA ''0FA q51uA	)E*$++D1$aCE*Q1uA	)E*$++D1$aCE*QC!	H   
	DJA  ,12qQU2	2!!),SZ  FAz*D#D)CKK
	 %% 3s   8F)__name__
__module____qualname____doc__r   propertyr    r"   r'   r-   r0   r3   _eval_transpose_eval_adjointr:   rP   __classcell__r   s   @r   r   r   	   s`    /b*   ( (
0=5 '43Om">&r   r   c                   ^     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 )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                    ddl m} t        |      }|j                  st	        dj                  |            t        |      }t        |t              r|j                  d   }t        ||      st	        dj                  |            t        |      }|dvrt	        d      |j                  |   }||j                  k7  r	 |j                  |      }t        | 5  | |||      S # t        $ r t	        dj                  |||            w xY w)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   rO   r   axisr   mat_sizer   s         r   r   zMatrixPermute.__new__   s   @sm}}5<<TBD D ~d-.99Q<D$,!6$<) ) ~v78899T?tyy .{{8, wsCt44  . / VD#t,	. ..s   C% %&Dc                    | 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   Zeror   r6   r   r   r
   r[   )r   rb   r&   rO   r   r_   s         r   r'   zMatrixPermute.doit   s    ))T4#((...C4990$0%0DJ??qvv~(..(r22cJ	23Jc=)chhqkT.A !dSXXa[.@$GGr   c                 4    | j                   d   j                  S r   )r   r    r#   s    r   r    zMatrixPermute.shape  s    yy|!!!r   c                     | j                   \  }}}|dk(  r||j                  |      |f   S |dk(  r|||j                  |      f   S y )Nr   r   )r   r)   )r   r*   r+   r,   rO   r   r_   s          r   r-   zMatrixPermute._entry  sT    ))T419tzz!}a'((QYq$**Q-'(( r   c                     ddl m} | j                  \  }}}|j                  dd      }|r|j	                  |      }|dk(  r |t        |      |      S |dk(  r ||t        |dz              S y )Nr   )MatMulrb   Tr   r2   )matmulri   r   getrewriter   )r   r   r,   ri   rO   r   r_   rb   s           r   _eval_rewrite_as_MatMulz%MatrixPermute._eval_rewrite_as_MatMul"  sr    "))T4zz&$'++f%C19+D1377QY#0r:;; r   )T)rQ   rR   rS   rT   r   re   r   r'   rU   r    r-   rm   rX   rY   s   @r   r[   r[      s;    2f &'VV  5D0 " ")<r   r[   N)
sympy.corer   sympy.core.sympifyr   sympy.functionsr   matexprr   specialr   r	   r
   r   r[   rd   r   r   <module>rs      s4     ' *  4 4\&
 \&~G<J G<r   