Ë
    7^(hr  ã                   ó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      €úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/matrices/expressions/permutation.pyr   zPermutationMatrix.__new__;   sI   ø€ Ý@ä˜‹~ˆÜ˜$ Ô,ÜØ:×AÑAÀ$ÓGóIð Iô ‰w‰˜s DÓ)Ð)ó    c                 ó<   — | j                   d   j                  }||fS ©Nr   )ÚargsÚsize)Úselfr   s     r   ÚshapezPermutationMatrix.shapeE   s   € à�y‰y˜‰|× Ñ ˆØ�dˆ|Ðr   c                 ó4   — | j                   d   j                  S r   )r   Úis_Identity©r   s    r   r"   zPermutationMatrix.is_IdentityJ   s   € à�y‰y˜‰|×'Ñ'Ð'r   c                 óH   — | j                   rt        | j                  «      S | S )N)r"   r	   Úrows)r   Úhintss     r   ÚdoitzPermutationMatrix.doitN   s   € Ø×ÒÜ˜DŸI™IÓ&Ð&Øˆr   c                 óV   — | j                   d   }t        |j                  |«      |«      S r   )r   r   Úapply)r   ÚiÚjÚkwargsr   s        r   Ú_entryzPermutationMatrix._entryS   s$   € Ø�y‰y˜‰|ˆÜ˜dŸj™j¨›m¨QÓ/Ð/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?   € Ø�y‰y˜‰|×%Ñ%Ó'ˆØ�1Š9Ü—5‘5ˆLØ�RŠZÜ—=‘=Ð Ü!Ð!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à�y‰y˜‰|ˆØ×0Ñ0Ðàˆð ‰ˆˆ1ˆaØˆØ%ò !	ˆEÜ�E“
ˆAÜ�E“
ˆAáØ�q‘5˜1˜q™5’=Ø�DØ!˜7�DØ�AØ‘Aà ×'Ñ'¨¨Ô0Ø˜‘F‘Að �q’5Ø˜1‘u  A¡¨¡	Ò)ØŸ™ EÔ*Ø$×+Ñ+¨DÔ1Ø$˜Ø˜a™C™à˜ØŸ™ EÔ*Ø˜Q™™à˜1‘u  A¡¨¡	Ò)ØŸ™ EÔ*Ø$×+Ñ+¨DÔ1Ø$˜Ø˜a™C™àŸ™ EÔ*Ø˜Q™™ðC!	ðH ˆØˆØ ò 
	ˆDØˆJØˆAØò  �Ø,1Ö2 q˜Q ›UÐ2�	Ð2Ø×!Ñ! )Ô,Ø”S˜“Z‘‘ð ð �‰FˆAÙ˜zÓ*ˆDÜ# DÓ)ˆCØ�K‰K˜Õð
	ñ  Ð%Ð%ùò 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Ð3€O�mò"ö>&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  ø€ Ý@ä�s‹mˆØ�}Š}ÜØ5×<Ñ<¸TÓBóDð Dô ˜‹~ˆÜ�dÔ-Ô.Ø—9‘9˜Q‘<ˆDä˜$ Ô,Üðß!™6 $›<ó)ð )ô ˜‹~ˆØ�vÑÜÐ7Ó8Ð8à—9‘9˜T‘?ˆØ�t—y‘yÒ ð.Ø—{‘{ 8Ó,�ô ‰w‰˜s C¨¨tÓ4Ð4øô ò .Ü ð/÷ ‘V˜D # 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ß   € ØŸ)™)‰ˆˆT�4áØ�#—(‘(Ñ. Ð.¨Ñ.ˆCØ�4—9‘9Ñ0 $Ð0¨%Ñ0ˆDà×ÒØˆJà�?Š?Ø”q—v‘v‰~Ü(¨Ó.Ð.ØœŸ™‘Ü(¨¨r©Ó2Ð2ä�cœJ¬	Ð2Ô3ØˆJä�cœ=Ô)¨c¯h©h°q©k¸TÒ.AÜ  §¡¨!¡¨d°S·X±X¸a±[Ñ.@À$ÓGÐGàˆr   c                 ó4   — | j                   d   j                  S r   )r   r    r#   s    r   r    zMatrixPermute.shape  s   € à�y‰y˜‰|×!Ñ!Ð!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   € ØŸ)™)‰ˆˆT�4à�1Š9Ø�t—z‘z !“} aÐ'Ñ(Ð(Ø�QŠYØ�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   € Ý"àŸ)™)‰ˆˆT�4à�z‰z˜& $Ó'ˆáØ—+‘+˜fÓ%ˆCà�1Š9ÙÔ+¨DÓ1°3Ó7Ð7Ø�QŠYÙ˜#Ô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 &'§V¡Võ  5óDð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   