a
    <bJ                     @   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__ v/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/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                 C   s   | j rt| jS | S )N)r   r   rowsr   r   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>       zFPermutationMatrix._eval_rewrite_as_BlockDiagMatrix.<locals>.<listcomp>)
r   r   Zblockmatrixr0   r   full_cyclic_formlenmaxappendr
   )r   r   r%   r   r0   r   r6   Zcycles_picksabcflagcyclelmtempZpickZ
new_cyclesZ	new_cyclematr   r2   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_adjointr/   rC   __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 )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              	      s   ddl m} t|}|js(td|t|}t|trD|jd }t||s\td|t|}|dvrttd|j	| }||j
krz||}W n$ ty   td|||Y n0 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   rB   r   axisr   Zmat_sizer   r   r   r      s:    





zMatrixPermute.__new__Tc                 C   s   | j \}}}|r(|j|d}|j|d}|jr2|S |jr`|tju rJt|S |tju r`t|d S t|tt	frr|S t|t
r|j d |krt
|j d ||j d  |S | S )N)deepr)      r   r   )r   r!   r   r   Zeror
   r,   r   r   r	   rJ   )r   rM   rB   r   rL   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%   rB   r   rL   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   )MatMulrM   Tr   r)   )matmulrP   r   getZrewriter
   )r   r   r%   rP   rB   r   rL   rM   r   r   r   _eval_rewrite_as_MatMul"  s    
z%MatrixPermute._eval_rewrite_as_MatMul)T)rD   rE   rF   rG   r   rO   r   r!   rH   r   r&   rS   rI   r   r   r   r   rJ      s   3"

rJ   N)Z
sympy.corer   Zsympy.core.sympifyr   Zsympy.functionsr   Zmatexprr   Zspecialr   r   r	   r
   rJ   r   r   r   r   <module>   s     