B
    [d                 @   s^  d dl mZmZ d dlmZmZ d dlZd dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlm Z  d dl!m"Z" d!ddZ#G dd deZ$G dd deZ%G dd de$Z&G dd de$Z'G dd de$Z(dd Z)ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 dd l2m3Z3 dS )"    )print_functiondivision)wrapsreduceN)SSymbolTupleIntegerBasicExprEq)call_highest_priority)range
SYMPY_INTSdefault_sort_key)SympifyErrorsympify)	conjugateadjoint)KroneckerDelta)
ShapeError)simplify)
filldedentc                s    fdd}|S )Nc                s   t   fdd}|S )Nc                s0   yt |dd} | |S  tk
r*   S X d S )NT)strict)r   r   )ab)funcretval Alib/python3.7/site-packages/sympy/matrices/expressions/matexpr.py__sympifyit_wrapper   s
    
z5_sympifyit.<locals>.deco.<locals>.__sympifyit_wrapper)r   )r   r    )r   )r   r   deco   s    z_sympifyit.<locals>.decor   )argr   r!   r   )r   r   
_sympifyit   s    r#   c               @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdd Zdd	 Zd
d Zedeeddd Zedeeddd Zedeeddd Zedeeddd Zedeeddd Zedeeddd Zedeeddd  Zedeedd!d" Zedeed#d$d% Zedeed&d'd( Z edeed)d*d+ Z!edeed,d-d. Z"e!Z#e"Z$e%d/d0 Z&e%d1d2 Z'e%d3d4 Z(d5d6 Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-d?d@ Z.dAdB Z/dCdD Z0dEdF Z1dGdH Z2dfdIdJZ3dKdL Z4dMdN Z5e%e5dddOZ6dPdQ Z7e7Z8e%dRdS Z9dTdU Z:dVdW Z;dXdY Z<dZd[ Z=d\d] Z>d^d_ Z?d`da Z@dbdc ZAeBdgdddeZCdS )h
MatrixExpra   Superclass for Matrix Expressions

    MatrixExprs represent abstract matrices, linear transformations represented
    within a particular basis.

    Examples
    ========

    >>> from sympy import MatrixSymbol
    >>> A = MatrixSymbol('A', 3, 3)
    >>> y = MatrixSymbol('y', 3, 1)
    >>> x = (A.T*A).I * A * y

    See Also
    ========
        MatrixSymbol
        MatAdd
        MatMul
        Transpose
        Inverse
    Fg      &@TNc             O   s   t t|}tj| f||S )N)mapr   r
   __new__)clsargskwargsr   r   r   r&   L   s    
zMatrixExpr.__new__c             C   s   t tj|  S )N)MatMulr   NegativeOnedoit)selfr   r   r   __neg__Q   s    zMatrixExpr.__neg__c             C   s   t d S )N)NotImplementedError)r-   r   r   r   __abs__T   s    zMatrixExpr.__abs__other__radd__c             C   s   t | | S )N)MatAddr,   )r-   r1   r   r   r   __add__W   s    zMatrixExpr.__add__r4   c             C   s   t ||  S )N)r3   r,   )r-   r1   r   r   r   r2   \   s    zMatrixExpr.__radd____rsub__c             C   s   t | |  S )N)r3   r,   )r-   r1   r   r   r   __sub__a   s    zMatrixExpr.__sub__r6   c             C   s   t ||   S )N)r3   r,   )r-   r1   r   r   r   r5   f   s    zMatrixExpr.__rsub____rmul__c             C   s   t | | S )N)r*   r,   )r-   r1   r   r   r   __mul__k   s    zMatrixExpr.__mul__c             C   s   t | | S )N)r*   r,   )r-   r1   r   r   r   
__matmul__p   s    zMatrixExpr.__matmul__r8   c             C   s   t ||  S )N)r*   r,   )r-   r1   r   r   r   r7   u   s    zMatrixExpr.__rmul__c             C   s   t ||  S )N)r*   r,   )r-   r1   r   r   r   __rmatmul__z   s    zMatrixExpr.__rmatmul____rpow__c             C   s\   | j std|  n>| jr| S |tjkr0t| S |tjkrDt| jS |tj	krR| S t
| |S )NzPower of non-square matrix %s)	is_squarer   is_Identityr   r+   InverseZeroIdentityrowsOneMatPow)r-   r1   r   r   r   __pow__   s    



zMatrixExpr.__pow__rD   c             C   s   t dd S )NzMatrix Power not defined)r/   )r-   r1   r   r   r   r;      s    zMatrixExpr.__rpow____rdiv__c             C   s   | |t j  S )N)r   r+   )r-   r1   r   r   r   __div__   s    zMatrixExpr.__div__rF   c             C   s
   t  d S )N)r/   )r-   r1   r   r   r   rE      s    zMatrixExpr.__rdiv__c             C   s
   | j d S )Nr   )shape)r-   r   r   r   rA      s    zMatrixExpr.rowsc             C   s
   | j d S )N   )rG   )r-   r   r   r   cols   s    zMatrixExpr.colsc             C   s   | j | jkS )N)rA   rI   )r-   r   r   r   r<      s    zMatrixExpr.is_squarec             C   s$   ddl m} ddlm} ||| S )Nr   )Adjoint)	Transpose)"sympy.matrices.expressions.adjointrJ   $sympy.matrices.expressions.transposerK   )r-   rJ   rK   r   r   r   _eval_conjugate   s    zMatrixExpr._eval_conjugatec             C   s@   ddl m} tdd | |    }| |   d|  }||fS )Nr   )IrH      )sympyrO   r   rN   )r-   rO   realZimr   r   r   as_real_imag   s    zMatrixExpr.as_real_imagc             C   s   ddl m} || S )Nr   )r>   )Z"sympy.matrices.expressions.inverser>   )r-   r>   r   r   r   _eval_inverse   s    zMatrixExpr._eval_inversec             C   s   t | S )N)rK   )r-   r   r   r   _eval_transpose   s    zMatrixExpr._eval_transposec             C   s
   t | |S )N)rC   )r-   Zexpr   r   r   _eval_power   s    zMatrixExpr._eval_powerc                s(   | j r
| S | j fdd| jD  S d S )Nc                s   g | ]}t |f qS r   )r   ).0x)r)   r   r   
<listcomp>   s    z-MatrixExpr._eval_simplify.<locals>.<listcomp>)Zis_Atom	__class__r(   )r-   r)   r   )r)   r   _eval_simplify   s    zMatrixExpr._eval_simplifyc             C   s   ddl m} || S )Nr   )rJ   )rL   rJ   )r-   rJ   r   r   r   _eval_adjoint   s    zMatrixExpr._eval_adjointc                sx  t |tsd S ddlm}m m}m |d d\}}}}| j||dd} fdd}	|fd	d
|	}i }
| j	d dkrd|
|< | j	d dkrd|
|< |j	d dkrd|
|< |j	d dkrd|
|< |
|||f }||
}|dkr|S t|
dk r|}n:||
krt||}n"||
kr2t||}n
t|}||sl||sl||sl||rpd S |S d S )Nr   )symbolsDummyLambdaTracezi j m n)r'   F)expandc                s.    d}t | jd |d| jd jd d fS )NZd_ir   rH   )Sumr(   rG   )rX   Zdi)r^   r   r   getsum   s    z+MatrixExpr._eval_derivative.<locals>.getsumc                s
   t |  S )N)
isinstance)rX   )r`   r   r   <lambda>   s    z-MatrixExpr._eval_derivative.<locals>.<lambda>rH   rP   )rd   r$   rQ   r]   r^   r_   r`   _entryreplacerG   diffZxreplacelenfrom_index_summationhas)r-   vr]   r_   ijmnMrc   replresZparsedr   )r^   r`   r   _eval_derivative   s<    




0zMatrixExpr._eval_derivativec             K   s   t d| jj d S )NzIndexing not implemented for %s)r/   rZ   __name__)r-   rm   rn   r)   r   r   r   rf      s    zMatrixExpr._entryc             C   s   t | S )N)r   )r-   r   r   r   r      s    zMatrixExpr.adjointc             C   s
   t j| fS )z2Efficiently extract the coefficient of a product. )r   rB   )r-   Zrationalr   r   r   as_coeff_Mul  s    zMatrixExpr.as_coeff_Mulc             C   s   t | S )N)r   )r-   r   r   r   r     s    zMatrixExpr.conjugatec             C   s   ddl m} || S )Nr   )	transpose)rM   rw   )r-   rw   r   r   r   rw     s    zMatrixExpr.transposezMatrix transposition.c             C   s   |   S )N)rT   )r-   r   r   r   inverse  s    zMatrixExpr.inversec             C   s   |   S )N)rx   )r-   r   r   r   rO     s    zMatrixExpr.Ic             C   sV   dd }||oT||oT| j d ks<d|kdkoT|| j k dkoTd|kdkoT|| jk dkS )Nc             S   s   t | ttttfS )N)rd   intr	   r   r   )idxr   r   r   is_valid  s    z(MatrixExpr.valid_index.<locals>.is_validr   F)rA   rI   )r-   rm   rn   r{   r   r   r   valid_index  s
    
zMatrixExpr.valid_indexc             C   sV  t |ts,t |tr,ddlm} || |dS t |trt|dkr|\}}t |ts^t |trvddlm} || ||S t|t| }}| ||dkr| ||S t	d||f nt |t
tfr*| j\}}t |tst	tdt|}|| }|| }| ||dkr| ||S t	d| nt |ttfrFt	td	t	d
|  d S )Nr   )MatrixSlice)r   NrH   rP   FzInvalid indices (%s, %s)zo
                    Single indexing is only supported when the number
                    of columns is known.zInvalid index %szr
                    Only integers may be used when addressing the matrix
                    with a single index.zInvalid index, wanted %s[i,j])rd   tuplesliceZ sympy.matrices.expressions.slicer}   ri   r   r|   rf   
IndexErrorr   r	   rG   r   r   r   )r-   keyr}   rm   rn   rA   rI   r   r   r   __getitem__  s6    

zMatrixExpr.__getitem__c                s(   ddl m} | fddt jD S )a  
        Returns a dense Matrix with elements represented explicitly

        Returns an object of type ImmutableDenseMatrix.

        Examples
        ========

        >>> from sympy import Identity
        >>> I = Identity(3)
        >>> I
        I
        >>> I.as_explicit()
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])

        See Also
        ========
        as_mutable: returns mutable Matrix type

        r   )ImmutableDenseMatrixc                s&   g | ]  fd dt jD qS )c                s   g | ]} |f qS r   r   )rW   rn   )rm   r-   r   r   rY   [  s   z5MatrixExpr.as_explicit.<locals>.<listcomp>.<listcomp>)r   rI   )rW   )r-   )rm   r   rY   [  s   z*MatrixExpr.as_explicit.<locals>.<listcomp>)Zsympy.matrices.immutabler   r   rA   )r-   r   r   )r-   r   as_explicitB  s    zMatrixExpr.as_explicitc             C   s   |    S )a  
        Returns a dense, mutable matrix with elements represented explicitly

        Examples
        ========

        >>> from sympy import Identity
        >>> I = Identity(3)
        >>> I
        I
        >>> I.shape
        (3, 3)
        >>> I.as_mutable()
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])

        See Also
        ========
        as_explicit: returns ImmutableDenseMatrix
        )r   
as_mutable)r-   r   r   r   r   _  s    zMatrixExpr.as_mutablec             C   sZ   ddl m} || jtd}x:t| jD ],}x&t| jD ]}| ||f |||f< q6W q&W |S )Nr   )empty)Zdtype)Znumpyr   rG   objectr   rA   rI   )r-   r   r   rm   rn   r   r   r   	__array__x  s    zMatrixExpr.__array__c             C   s   |   |S )z
        Test elementwise equality between matrices, potentially of different
        types

        >>> from sympy import Identity, eye
        >>> Identity(3).equals(eye(3))
        True
        )r   equals)r-   r1   r   r   r   r     s    	zMatrixExpr.equalsc             C   s   | S )Nr   )r-   r   r   r   canonicalize  s    zMatrixExpr.canonicalizec             C   s   dt | fS )NrH   )r*   )r-   r   r   r   as_coeff_mmul  s    zMatrixExpr.as_coeff_mmulc       
   
      s   ddl mmm mm	m ddlm 	fddi f 	f	dd	| }t	| \}}
|}t|dkstt|dgkr|S |dkrx|D ]}|dk	r|}	P qW |f|	 S |||S dS )	a  
        Parse expression of matrices with explicitly summed indices into a
        matrix expression without indices, if possible.

        This transformation expressed in mathematical notation:

        `\sum_{j=0}^{N-1} A_{i,j} B_{j,k} \Longrightarrow \mathbf{A}\cdot \mathbf{B}`

        Optional parameter ``first_index``: specify which free index to use as
        the index starting the expression.

        Examples
        ========

        >>> from sympy import MatrixSymbol, MatrixExpr, Sum, Symbol
        >>> from sympy.abc import i, j, k, l, N
        >>> A = MatrixSymbol("A", N, N)
        >>> B = MatrixSymbol("B", N, N)
        >>> expr = Sum(A[i, j]*B[j, k], (j, 0, N-1))
        >>> MatrixExpr.from_index_summation(expr)
        A*B

        Transposition is detected:

        >>> expr = Sum(A[j, i]*B[j, k], (j, 0, N-1))
        >>> MatrixExpr.from_index_summation(expr)
        A.T*B

        Detect the trace:

        >>> expr = Sum(A[i, i], (i, 0, N-1))
        >>> MatrixExpr.from_index_summation(expr)
        Trace(A)

        More complicated expressions:

        >>> expr = Sum(A[i, j]*B[k, j]*A[l, k], (j, 0, N-1), (k, 0, N-1))
        >>> MatrixExpr.from_index_summation(expr)
        A*B.T*A.T
        r   )rb   MulAddr*   rw   trace)	bottom_upc                sR    fdd}|  |ddd } |  |dfdd} fdd}|| S )	Nc                s    fdd}|S )Nc                sV   t | tsdS | j  kr dS | jd  dkrR| jd jd  dkrNdS dS dS )NF   r   rP   rH   T)rd   MatrixElementr(   rG   )rX   )i1posr   r   r     s    
z\MatrixExpr.from_index_summation.<locals>.remove_matelement.<locals>.repl_match.<locals>.funcr   )r   r   )r   )r   r   
repl_match  s    zNMatrixExpr.from_index_summation.<locals>.remove_matelement.<locals>.repl_matchrH   c             S   s
   | j d S )Nr   )r(   )rX   r   r   r   re     s    zLMatrixExpr.from_index_summation.<locals>.remove_matelement.<locals>.<lambda>rP   c                s    | j d S )Nr   )r(   )rX   )rw   r   r   re     s    c                s"   t |  frtdd | jS | S )Nc             S   s   | | S )Nr   )r   r   r   r   r   re     s    z^MatrixExpr.from_index_summation.<locals>.remove_matelement.<locals>.<lambda>.<locals>.<lambda>)rd   r   r(   )rX   )r*   r   r   r   re     s    )rg   )exprr   i2r   Zrule)r*   r   r   rw   )r   r   remove_matelement  s    


z:MatrixExpr.from_index_summation.<locals>.remove_matelementc       '         s  | j rg }g }g }i }g }d}g }xT| jD ]J}	|	|}
t|
tsHtt|
trlx"|
D ]}|| qXW q,||
 q,W x|D ]\}}|d kr|| qt|tr|jd }|| || |d gt|  xVt|D ]J\}}||kr || }||| |< ||f||d  |d < ||f||< qW |d7 }qW d}i }x|t|k r@x0t|D ]$\}}d |kr^||	d f}P q^W |}g }||d  |d  }x|\}}|| dkr|d dkr|||  n|||  || d|  }|d7 }d||< |d kr2|| d|  }||||f< P |}qW qFW tdd |D }fdd|
 D }|d fgd	d
 |
 D  S | jr2fdd
| jD }tt}xh|D ]`}d}xT|D ]L\}} | d kr|}qtt| td} ||  ||f|    d}qW qW  fdd
|
 D S t| tr|| j\}!}"d k	r`td }#ntj}#t|#|!|"|!|"ffgS t| trh| j\}$}!}"|!|kr||! \}%}&|%dks|$jd |&d krtd|%|&f|$jd |"|kr0||" \}%}&|%dks|$jd |&d kr0td|%|&f|$jd |!|"krR|!|krR|$d fgS t|$|!|"|!|"ffgS t| r| jd dd | jdd  D dS | d fgS d S )Nr   rH   rP   )r   c             s   s   | ]}|D ]
}|V  q
qd S )Nr   )rW   rm   rn   r   r   r   	<genexpr>  s    zHMatrixExpr.from_index_summation.<locals>.recurse_expr.<locals>.<genexpr>c                s0   i | ](\}}t |d kr" |n|d |qS )rH   r   )ri   fromiter)rW   krl   )r*   r   r   
<dictcomp>  s    zIMatrixExpr.from_index_summation.<locals>.recurse_expr.<locals>.<dictcomp>c             S   s(   g | ] \\}}}t |||||ffqS r   )r   )rW   rm   rn   r   r   r   r   rY     s    zIMatrixExpr.from_index_summation.<locals>.recurse_expr.<locals>.<listcomp>c                s   g | ]} |qS r   r   )rW   rm   )recurse_exprr   r   rY     s    )r   c                s(   g | ] \}}t  |f| |fqS r   )r   r   )rW   r   rl   )r   r   r   rY   $  s    z&index range mismatch: {0} vs. (0, {1})c             S   s   i | ]}|d d |d qS )rH   Nr   r   )rW   rm   r   r   r   r   >  s    )index_ranges)Zis_Mulr(   rd   listAssertionErrorappendr   ri   	enumerateindexitemsr   Zis_Addcollectionsdefaultdictr~   sortedr   r   r@   r   rB   rG   
ValueErrorformat)'r   r   Z
nonmatargsZpos_argZpos_indZdlinksZlink_indZcounterZargs_indr"   retvalsrm   Z
arg_symbolZarg_indicesZindZother_iZcounter2lineseZline_start_indexZcur_ind_posZcur_lineZindex1drZnext_ind_posZindex2Zret_indicesrs   Z
res_addendZscalarelemindicesr   r   ZidentityZmatrix_symbolr1r2)	r   r*   r   rb   
dimensionsr   r   r   rw   r   r   r     s    

















z5MatrixExpr.from_index_summation.<locals>.recurse_exprN)rQ   rb   r   r   r*   rw   r   Zsympy.strategies.traverser   zipr   ri   r   set)
r   Zfirst_indexZ
last_indexr   r   Zfactorsr   Zretexprrm   Zind0r   )
r   r*   r   rb   r   r   r   r   r   rw   r   rj     s     *  k

zMatrixExpr.from_index_summation)F)NNN)Dru   
__module____qualname____doc__Z	_iterableZ_op_priority	is_MatrixZis_MatrixExprr=   Z
is_InverseZis_Transposeis_ZeroMatrixZ	is_MatAddZ	is_MatMulis_commutativeZ	is_number	is_symbolr&   r.   r0   r#   NotImplementedr   r4   r2   r6   r5   r8   r9   r7   r:   rD   r;   rF   rE   __truediv____rtruediv__propertyrA   rI   r<   rN   rS   rT   rU   rV   r[   r\   rt   rf   r   rv   r   rw   Trx   invrO   r|   r   r   r   r   r   r   r   staticmethodrj   r   r   r   r   r$   !   s   .
#r$   c               @   sT   e Zd Zedd Zedd Zedd ZdZdZdZ	dd Z
dd	 Zd
d ZdS )r   c             C   s
   | j d S )Nr   )r(   )r-   r   r   r   re   S  s    zMatrixElement.<lambda>c             C   s
   | j d S )NrH   )r(   )r-   r   r   r   re   T  s    c             C   s
   | j d S )NrP   )r(   )r-   r   r   r   re   U  s    Tc             C   s^   t t||f\}}ddlm} t||frB|jrB|jrB|||f S t|}t| |||}|S )Nr   )
MatrixBase)r%   r   rQ   r   rd   Z
is_Integerr   r&   )r'   namerp   ro   r   objr   r   r   r&   Z  s    zMatrixElement.__new__c                sD     dd}|r& fdd| jD }n| j}|d |d |d f S )NdeepTc                s   g | ]}|j f  qS r   )r,   )rW   r"   )r)   r   r   rY   g  s    z&MatrixElement.doit.<locals>.<listcomp>r   rH   rP   )getr(   )r-   r)   r   r(   r   )r)   r   r,   d  s
    zMatrixElement.doitc             C   s>  ddl m}m}m} t|tsTddl m} t| j|rN| j|| j	| j
f S tjS | jd }||jd krt| jd |jd t| jd |jd  S t|tr"| jdd  \}}|d|d\}	}
|jd }|j\}}||||	f ||	|
f | ||
|f  |	d|d f|
d|d f S | |jd r8d S tjS )Nr   )rb   r]   r^   )r   rH   rP   zz1, z2)r'   )rQ   rb   r]   r^   rd   r   r   parentrh   rm   rn   r   r?   r(   r   r>   rG   rk   )r-   rl   rb   r]   r^   r   rq   rm   rn   r   r   Yr   r   r   r   r   rt   l  s$    

,

HzMatrixElement._eval_derivativeN)ru   r   r   r   r   rm   rn   	_diff_wrtr   r   r&   r,   rt   r   r   r   r   r   R  s   
r   c               @   st   e Zd ZdZdZdZdd Zdd Zedd	 Z	ed
d Z
dd Zdd Zdd Zedd Zdd Zdd ZdS )MatrixSymbola  Symbolic representation of a Matrix object

    Creates a SymPy Symbol to represent a Matrix. This matrix has a shape and
    can be included in Matrix Expressions

    >>> from sympy import MatrixSymbol, Identity
    >>> A = MatrixSymbol('A', 3, 4) # A 3 by 4 Matrix
    >>> B = MatrixSymbol('B', 4, 3) # A 4 by 3 Matrix
    >>> A.shape
    (3, 4)
    >>> 2*A*B + Identity(3)
    I + 2*A*B
    FTc             C   s&   t |t | }}t| |||}|S )N)r   r
   r&   )r'   r   rp   ro   r   r   r   r   r&     s    zMatrixSymbol.__new__c             C   s   | j | jfS )N)r   rG   )r-   r   r   r   _hashable_content  s    zMatrixSymbol._hashable_contentc             C   s   | j dd S )NrH   r   )r(   )r-   r   r   r   rG     s    zMatrixSymbol.shapec             C   s
   | j d S )Nr   )r(   )r-   r   r   r   r     s    zMatrixSymbol.namec             C   s"   t | j ||}t| jf| S )N)r   rG   Z_subsr   r   )r-   oldnewrG   r   r   r   
_eval_subs  s    zMatrixSymbol._eval_subsc             G   s   t d| j d S )Nz%s object is not callable)	TypeErrorrZ   )r-   r(   r   r   r   __call__  s    zMatrixSymbol.__call__c             K   s   t | ||S )N)r   )r-   rm   rn   r)   r   r   r   rf     s    zMatrixSymbol._entryc             C   s
   t | fS )N)r   )r-   r   r   r   free_symbols  s    zMatrixSymbol.free_symbolsc             K   sB   | ddr:t| | j| jd jf || jd jf |S | S d S )Nr   TrH   rP   )r   typer   r(   r,   )r-   Zhintsr   r   r   r,     s    zMatrixSymbol.doitc             K   s   | S )Nr   )r-   r)   r   r   r   r[     s    zMatrixSymbol._eval_simplifyN)ru   r   r   r   r   r   r&   r   r   rG   r   r   r   rf   r   r,   r[   r   r   r   r   r     s   r   c                   sx   e Zd ZdZdZ fddZedd Zedd Zed	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )r@   zThe Matrix Identity I - multiplicative identity

    >>> from sympy.matrices import Identity, MatrixSymbol
    >>> A = MatrixSymbol('A', 3, 5)
    >>> I = Identity(3)
    >>> I*A
    A
    Tc                s   t t| | t|S )N)superr@   r&   r   )r'   rp   )rZ   r   r   r&     s    zIdentity.__new__c             C   s
   | j d S )Nr   )r(   )r-   r   r   r   rA     s    zIdentity.rowsc             C   s
   | j d S )Nr   )r(   )r-   r   r   r   rI     s    zIdentity.colsc             C   s   | j d | j d fS )Nr   )r(   )r-   r   r   r   rG     s    zIdentity.shapec             C   s   | S )Nr   )r-   r   r   r   rU     s    zIdentity._eval_transposec             C   s   | j S )N)rA   )r-   r   r   r   _eval_trace  s    zIdentity._eval_tracec             C   s   | S )Nr   )r-   r   r   r   rT     s    zIdentity._eval_inversec             C   s   | S )Nr   )r-   r   r   r   r     s    zIdentity.conjugatec             K   s4   t ||}|tjkrtjS |tjkr*tjS t||S )N)r   r   truerB   Zfalser?   r   )r-   rm   rn   r)   eqr   r   r   rf     s    


zIdentity._entryc             C   s   t jS )N)r   rB   )r-   r   r   r   _eval_determinant  s    zIdentity._eval_determinant)ru   r   r   r   r=   r&   r   rA   rI   rG   rU   r   rT   r   rf   r   __classcell__r   r   )rZ   r   r@     s   r@   c                   s~   e Zd ZdZdZ fddZedd Zede	e
dd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZeZ  ZS )
ZeroMatrixzThe Matrix Zero 0 - additive identity

    >>> from sympy import MatrixSymbol, ZeroMatrix
    >>> A = MatrixSymbol('A', 3, 5)
    >>> Z = ZeroMatrix(3, 5)
    >>> A+Z
    A
    >>> Z*A.T
    0
    Tc                s   t t| | ||S )N)r   r   r&   )r'   ro   rp   )rZ   r   r   r&     s    zZeroMatrix.__new__c             C   s   | j d | j d fS )Nr   rH   )r(   )r-   r   r   r   rG     s    zZeroMatrix.shaper1   r;   c             C   s@   |dkr| j std|  |dkr,t| jS |dk r<td| S )NrH   zPower of non-square matrix %sr   z Matrix det == 0; not invertible.)r<   r   r@   rA   r   )r-   r1   r   r   r   rD   
  s    
zZeroMatrix.__pow__c             C   s   t | j| jS )N)r   rI   rA   )r-   r   r   r   rU     s    zZeroMatrix._eval_transposec             C   s   t jS )N)r   r?   )r-   r   r   r   r     s    zZeroMatrix._eval_tracec             C   s   t jS )N)r   r?   )r-   r   r   r   r     s    zZeroMatrix._eval_determinantc             C   s   | S )Nr   )r-   r   r   r   r     s    zZeroMatrix.conjugatec             K   s   t jS )N)r   r?   )r-   rm   rn   r)   r   r   r   rf   !  s    zZeroMatrix._entryc             C   s   dS )NFr   )r-   r   r   r   __nonzero__$  s    zZeroMatrix.__nonzero__)ru   r   r   r   r   r&   r   rG   r#   r   r   rD   rU   r   r   r   rf   r   __bool__r   r   r   )rZ   r   r     s   

r   c             C   s   dd | j D S )Nc             S   s   g | ]}|j r|qS r   )r   )rW   Zsymr   r   r   rY   +  s    z"matrix_symbols.<locals>.<listcomp>)r   )r   r   r   r   matrix_symbols*  s    r   rH   )r*   )r3   )rC   )rK   )r>   )N)4Z
__future__r   r   	functoolsr   r   r   Z
sympy.corer   r   r   r	   r
   r   r   Zsympy.core.decoratorsr   Zsympy.core.compatibilityr   r   r   Zsympy.core.sympifyr   r   Zsympy.functionsr   r   Z(sympy.functions.special.tensor_functionsr   Zsympy.matricesr   Zsympy.simplifyr   Zsympy.utilities.miscr   r#   r$   r   r   r@   r   r   matmulr*   Zmataddr3   ZmatpowrC   rw   rK   rx   r>   r   r   r   r   <module>   s6   $
    55;35