U
    ZÊ¹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__© úe/var/www/html/bwave/python/venv/lib/python3.8/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   s   | j d jS r   )r   Úis_Identity©r   r   r   r   r   J   s    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   s2   | j d  ¡ }|dkrtjS |dkr*tjS t‚d S )Nr   r   r*   )r   Ú	signaturer   ÚOneZ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 ]ò}t|ƒ}t|ƒ}|s�|d || krzd}|g}|}	|}
n| |g¡ ||7 }q>||	kræ|d ||
 | krÎ| |¡ | |¡ d}|d }n|}	| |¡ |
|7 }
q>|	d ||
 | k�r| |¡ | |¡ d}|	d }q>| |¡ |
|7 }
q>d‰ g }|D ]d}g }d}|D ].}‡ fdd	„|D ƒ}| |¡ |t|ƒ7 }�qNˆ |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   Zblockmatrixr1   r   Úfull_cyclic_formÚlenÚmaxÚappendr
   )r   r   r&   r   r1   r   r6   Zcycles_picksÚaÚbÚcÚflagÚcycleÚlÚmÚtempÚpickZ
new_cyclesZ	new_cycleÚmatr   r3   r   Ú _eval_rewrite_as_BlockDiagMatrixg   s`    













z2PermutationMatrix._eval_rewrite_as_BlockDiagMatrix)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r"   r'   r)   r+   Z_eval_transposeZ_eval_adjointr0   rD   Ú__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ƒrD|jd }t||ƒs\td |¡ƒ‚t|ƒ}|dkrttdƒ‚|j	| }||j
kr¾z| |¡}W n& tk
r¼   td |||¡ƒ‚Y nX 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   Z	is_Matrixr   r   r   r
   r   r   r   Úresizer   r   )r   rC   r   Úaxisr   Zmat_sizer   r   r   r   Ü   s>    ÿ


ÿÿ

  ýÿ
zMatrixPermute.__new__Tc                 K   sº   | j \}}}|r8|jf d|i|—Ž}|jf d|i|—Ž}|jrB|S |jrp|tjkrZt|ƒS |tjkrpt|d ƒS t|tt	fƒr‚|S t|t
ƒr¶|j d |kr¶t
|j d ||j d  |ƒS | S )NÚdeepr*   é   r   r   )r   r"   r   r   ÚZeror
   r-   r   r   r	   rK   )r   rN   r!   rC   r   rM   r   r   r   r"   þ   s     

zMatrixPermute.doitc                 C   s   | j d jS r   )r   r   r   r   r   r   r     s    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&   rC   r   rM   r   r   r   r'     s
    zMatrixPermute._entryc                 O   sf   ddl m} | j\}}}| dd¡}|r2| |¡}|dkrH|t|ƒ|ƒS |dkrb||t|d ƒƒS d S )Nr   )ÚMatMulrN   Tr   r*   )ÚmatmulrQ   r   ÚgetZrewriter
   )r   r   r&   rQ   rC   r   rM   rN   r   r   r   Ú_eval_rewrite_as_MatMul"  s    
z%MatrixPermute._eval_rewrite_as_MatMul)T)rE   rF   rG   rH   r   rP   r   r"   rI   r   r'   rT   rJ   r   r   r   r   rK   ¨   s   3"

rK   N)Z
sympy.corer   Zsympy.core.sympifyr   Zsympy.functionsr   Zmatexprr   Úspecialr   r   r	   r
   rK   r   r   r   r   Ú<module>   s     