
    Edj                     v   d dl mZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZ d d	lmZmZ d d
lmZ d dlmZm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d,dZ, G d de          Z- e)e-e          d             Z. e)e-e-          d             Z.d Z/ e/e	          g e/e
          gdej0        e-<   d-dZ1d Z2 G d de          Z3 G d de-          Z4d  Z5 G d! d"          Z6d# Z7d$d%l8m9Z9 d$d&l:m;Z; d$d'l<m=Z= d$d(l>m?Z? d$d)l@mAZA d$d*lBmCZCmDZD d$d+lEmFZF dS ).    )Tuplewraps)SIntegerBasicMulAdd)check_assumptions)call_highest_priority)ExprExprBuilder)	FuzzyBool)StrDummysymbolsSymbol)SympifyError_sympify)
SYMPY_INTS)	conjugateadjoint)KroneckerDelta)NonSquareMatrixError)
MatrixKind
MatrixBase)dispatch)
filldedentNc                     fd}|S )Nc                 @     t                      fd            }|S )Nc                 `    	 t          |          } | |          S # t          $ r cY S w xY wN)r   r   )abfuncretvals     Blib/python3.11/site-packages/sympy/matrices/expressions/matexpr.py__sympifyit_wrapperz5_sympifyit.<locals>.deco.<locals>.__sympifyit_wrapper   sG    QKKtAqzz!   s    --r   )r%   r(   r&   s   ` r'   decoz_sympifyit.<locals>.deco   s:    	t	 	 	 	 	 
	 #"     )argr&   r)   s    ` r'   
_sympifyitr-      s#    	# 	# 	# 	# 	# Kr*   c                       e Zd ZU 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Z e            Zeed<   d Zed	eeef         fd
            Zed             Zed             Zd Zd Z ede            e!d          d                         Z" ede            e!d          d                         Z# ede            e!d          d                         Z$ ede            e!d          d                         Z% ede            e!d          d                         Z& ede            e!d          d                         Z' ede            e!d          d                         Z( ede            e!d          d                         Z) ede            e!d          d                         Z* ede            e!d           d!                         Z+ ede            e!d"          d#                         Z, ede            e!d$          d%                         Z-ed&             Z.ed'             Z/ed(             Z0d) Z1dJd*Z2d+ Z3d, Z4d- Z5d. Z6d/ Z7d0 Z8d1 Z9d2 Z: fd3Z;e<d4             Z=d5 Z>d6 Z?dKd7Z@d8 ZAd9 ZBed:             ZCd; ZDd< ZEd= ZFed>             ZGd? ZHd@ ZId	eJfdAZKdB ZLdC ZMdD ZNdE ZOdF ZPdG ZQeRdLdH            ZSdI ZT xZUS )M
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
    r+   Fg      &@TNkindc                 V    t          t          |          }t          j        | g|R i |S r"   )mapr   r   __new__)clsargskwargss      r'   r3   zMatrixExpr.__new__P   s1    8T""}S242226222r*   returnc                     t           r"   NotImplementedErrorselfs    r'   shapezMatrixExpr.shapeV   s    !!r*   c                     t           S r"   MatAddr;   s    r'   _add_handlerzMatrixExpr._add_handlerZ       r*   c                     t           S r"   MatMulr;   s    r'   _mul_handlerzMatrixExpr._mul_handler^   rB   r*   c                 Z    t          t          j        |                                           S r"   )rE   r   NegativeOnedoitr;   s    r'   __neg__zMatrixExpr.__neg__b   s     amT**//111r*   c                     t           r"   r9   r;   s    r'   __abs__zMatrixExpr.__abs__e   s    !!r*   other__radd__c                 F    t          | |                                          S r"   r@   rI   r<   rM   s     r'   __add__zMatrixExpr.__add__h         dE""'')))r*   rR   c                 F    t          ||                                           S r"   rP   rQ   s     r'   rN   zMatrixExpr.__radd__m         eT""'')))r*   __rsub__c                 H    t          | |                                           S r"   rP   rQ   s     r'   __sub__zMatrixExpr.__sub__r   s"     dUF##((***r*   rX   c                 H    t          ||                                            S r"   rP   rQ   s     r'   rV   zMatrixExpr.__rsub__w   s"     edU##((***r*   __rmul__c                 F    t          | |                                          S r"   rE   rI   rQ   s     r'   __mul__zMatrixExpr.__mul__|   rS   r*   c                 F    t          | |                                          S r"   r\   rQ   s     r'   
__matmul__zMatrixExpr.__matmul__   rS   r*   r]   c                 F    t          ||                                           S r"   r\   rQ   s     r'   rZ   zMatrixExpr.__rmul__   rU   r*   c                 F    t          ||                                           S r"   r\   rQ   s     r'   __rmatmul__zMatrixExpr.__rmatmul__   rU   r*   __rpow__c                 F    t          | |                                          S r"   )MatPowrI   rQ   s     r'   __pow__zMatrixExpr.__pow__   rS   r*   rf   c                      t          d          )NzMatrix Power not definedr9   rQ   s     r'   rc   zMatrixExpr.__rpow__   s     ""<===r*   __rtruediv__c                 &    | |t           j        z  z  S r"   )r   rH   rQ   s     r'   __truediv__zMatrixExpr.__truediv__   s     eQ]***r*   rj   c                     t                      r"   r9   rQ   s     r'   rh   zMatrixExpr.__rtruediv__   s     "###r*   c                     | j         d         S Nr   r=   r;   s    r'   rowszMatrixExpr.rows       z!}r*   c                     | j         d         S N   rn   r;   s    r'   colszMatrixExpr.cols   rp   r*   c                 "    | j         | j        k    S r"   ro   rt   r;   s    r'   	is_squarezMatrixExpr.is_square   s    yDI%%r*   c                 >    ddl m}  |t          |                     S Nr   )Adjoint)"sympy.matrices.expressions.adjointrz   	Transposer<   rz   s     r'   _eval_conjugatezMatrixExpr._eval_conjugate   s*    >>>>>>wy'''r*   c                 *    |                                  S r"   )_eval_as_real_imag)r<   deephintss      r'   as_real_imagzMatrixExpr.as_real_imag   s    &&(((r*   c                     t           j        | |                                 z   z  }| |                                 z
  dt           j        z  z  }||fS N   )r   Halfr~   ImaginaryUnit)r<   realims      r'   r   zMatrixExpr._eval_as_real_imag   sK    v 4 4 6 667T))+++a.?@bzr*   c                      t          |           S r"   Inverser;   s    r'   _eval_inversezMatrixExpr._eval_inverse       t}}r*   c                      t          |           S r"   Determinantr;   s    r'   _eval_determinantzMatrixExpr._eval_determinant   s    4   r*   c                      t          |           S r"   r|   r;   s    r'   _eval_transposezMatrixExpr._eval_transpose       r*   c                 "    t          | |          S )z
        Override this in sub-classes to implement simplification of powers.  The cases where the exponent
        is -1, 0, 1 are already covered in MatPow.doit(), so implementations can exclude these cases.
        re   )r<   exps     r'   _eval_powerzMatrixExpr._eval_power   s    
 dC   r*   c                 \    | j         r| S ddlm  | j        fd| j        D              S )Nr   )simplifyc                 "    g | ]} |fi S r+   r+   ).0xr6   r   s     r'   
<listcomp>z-MatrixExpr._eval_simplify.<locals>.<listcomp>   s+    HHHxx44V44HHHr*   )is_Atomsympy.simplifyr   r%   r5   )r<   r6   r   s    `@r'   _eval_simplifyzMatrixExpr._eval_simplify   sO    < 	JK//////49HHHHHdiHHHIIr*   c                 $    ddl m}  ||           S ry   )r{   rz   r}   s     r'   _eval_adjointzMatrixExpr._eval_adjoint   s"    >>>>>>wt}}r*   c                 .    t          j        | ||          S r"   )r   _eval_derivative_n_times)r<   r   ns      r'   r   z#MatrixExpr._eval_derivative_n_times   s    -dAq999r*   c                     |                      |          r!t                                          |          S t          | j         S r"   )hassuper_eval_derivative
ZeroMatrixr=   )r<   r   	__class__s     r'   r   zMatrixExpr._eval_derivative   s:    88A;; 	+77++A...tz**r*   c                 v    t          |dd          }|du r"t          d                    |                    dS )z2Helper function to check invalid matrix dimensionsT)integernonnegativeFz?The dimension specification {} should be a nonnegative integer.N)r   
ValueErrorformat)r4   dimoks      r'   
_check_dimzMatrixExpr._check_dim   sP     sDdCCC; 	6))/6 6 6	6 	6r*   c                 :    t          d| j        j        z            )NzIndexing not implemented for %s)r:   r   __name__r<   ijr6   s       r'   _entryzMatrixExpr._entry   s$    !-0GGI I 	Ir*   c                      t          |           S r"   )r   r;   s    r'   r   zMatrixExpr.adjoint   r   r*   c                     t           j        | fS )z2Efficiently extract the coefficient of a product. )r   One)r<   rationals     r'   as_coeff_MulzMatrixExpr.as_coeff_Mul   s    ud{r*   c                      t          |           S r"   )r   r;   s    r'   r   zMatrixExpr.conjugate   r   r*   c                 $    ddl m}  ||           S )Nr   	transpose)$sympy.matrices.expressions.transposer   )r<   r   s     r'   r   zMatrixExpr.transpose   s"    BBBBBByr*   c                 *    |                                  S )zMatrix transpositionr   r;   s    r'   TzMatrixExpr.T   s     ~~r*   c                 V    | j         st          d          |                                 S )NzInverse of non-square matrix)rw   r   r   r;   s    r'   inversezMatrixExpr.inverse  s.    ~ 	G&'EFFF!!###r*   c                 *    |                                  S r"   r   r;   s    r'   invzMatrixExpr.inv	  s    ||~~r*   c                 $    ddl m}  ||           S )Nr   )det)&sympy.matrices.expressions.determinantr   )r<   r   s     r'   r   zMatrixExpr.det  s"    >>>>>>s4yyr*   c                 *    |                                  S r"   r   r;   s    r'   IzMatrixExpr.I  s    ||~~r*   c                     d } ||          oQ ||          oF| j         d u p|| j          k    dk    o|| j         k     dk    o|| j         k    dk    o|| j        k     dk    S )Nc                 R    t          | t          t          t          t          f          S r"   )
isinstanceintr   r   r   )idxs    r'   is_validz(MatrixExpr.valid_index.<locals>.is_valid  s    cC&$#?@@@r*   Frv   )r<   r   r   r   s       r'   valid_indexzMatrixExpr.valid_index  s    	A 	A 	A H Hd" HtyjU*GDI%/GH tyjU*H 12DI%/G	Ir*   c                    t          |t                    s(t          |t                    rddlm}  || |d          S t          |t                    rt          |          dk    r|\  }}t          |t                    st          |t                    rddlm}  || ||          S t          |          t          |          }}|                     ||          dk    r|                     ||          S t          d|d|d          t          |t          t          f          r| j        \  }}t          |t                    st          t          d	                    t          |          }||z  }||z  }|                     ||          dk    r|                     ||          S t          d
|z            t          |t          t          f          rt          t          d                    t          d| z            )Nr   )MatrixSlice)r   Nrs   r   FzInvalid indices (z, )zo
                    Single indexing is only supported when the number
                    of columns is known.zInvalid index %szj
                Only integers may be used when addressing the matrix
                with a single index.zInvalid index, wanted %s[i,j])r   tupleslice sympy.matrices.expressions.slicer   lenr   r   r   
IndexErrorr   r   r=   r   r   r   )r<   keyr   r   r   ro   rt   s          r'   __getitem__zMatrixExpr.__getitem__  s.   #u%% 	8*S%*@*@ 	8DDDDDD;tS,777c5!! 	*c#hh!m 	*DAq!U## /z!U';'; /HHHHHH"{4A...A;;qA1%%. F{{1a((( jqqq!!!!DEEEj'233 	*JD$dG,, .  -, "- "- . . . 3--CtAd
A1%%. ;{{1a((( !3c!9:::fd^,, 	*Z )( ) ) * * * 84?@@@r*   c                     t          | j        t          t          f           p!t          | j        t          t          f           S r"   )r   ro   r   r   rt   r;   s    r'   _is_shape_symboliczMatrixExpr._is_shape_symbolic?  s<    ty:w*?@@@ @di*g)>???	Ar*   c                                                        rt          d          ddlm}  | f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

        z<Matrix with symbolic shape cannot be represented explicitly.r   ImmutableDenseMatrixc                 T    g | ]#fd t          j                  D             $S )c                 $    g | ]}|f         S r+   r+   )r   r   r   r<   s     r'   r   z5MatrixExpr.as_explicit.<locals>.<listcomp>.<listcomp>`  s1     &7 &7 &7 ! '+1a4j &7 &7 &7r*   )rangert   r   r   r<   s    @r'   r   z*MatrixExpr.as_explicit.<locals>.<listcomp>`  sZ     %7 %7 %7 !&7 &7 &7 &7 &7%*49%5%5&7 &7 &7 %7 %7 %7r*   )r   r   sympy.matrices.immutabler   r   ro   )r<   r   s   ` r'   as_explicitzMatrixExpr.as_explicitC  s    0 ""$$ 	545 5 5 	BAAAAA## %7 %7 %7 %7%*49%5%5%7 %7 %7 8 8 	8r*   c                 N    |                                                                  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_mutabler;   s    r'   r   zMatrixExpr.as_mutabled  s"    . !!,,...r*   c                     ddl m}  || j        t                    }t	          | j                  D ](}t	          | j                  D ]}| ||f         |||f<   )|S )Nr   )empty)dtype)numpyr   r=   objectr   ro   rt   )r<   r   r#   r   r   s        r'   	__array__zMatrixExpr.__array__}  s}    E$*F+++ty!! 	% 	%A49%% % %q!t*!Q$%r*   c                 P    |                                                      |          S )z
        Test elementwise equality between matrices, potentially of different
        types

        >>> from sympy import Identity, eye
        >>> Identity(3).equals(eye(3))
        True
        )r   equalsrQ   s     r'   r   zMatrixExpr.equals  s$     !!((///r*   c                     | S r"   r+   r;   s    r'   canonicalizezMatrixExpr.canonicalize      r*   c                 8    t           j        t          |           fS r"   )r   r   rE   r;   s    r'   as_coeff_mmulzMatrixExpr.as_coeff_mmul  s    ufTll""r*   c                     ddl m} ddlm} g }||                    |           ||                    |            || |          } ||          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
        >>> 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   )convert_indexed_to_arrayconvert_array_to_matrixN)first_indices)4sympy.tensor.array.expressions.conv_indexed_to_arrayr   3sympy.tensor.array.expressions.conv_array_to_matrixr  append)exprfirst_index
last_index
dimensionsr   r  r  arrs           r'   from_index_summationzMatrixExpr.from_index_summation  s    T 	baaaaa______ 	.  --- 	-  ,,,&&t=III&&s+++r*   c                 &    ddl m}  |||           S )Nrs   )ElementwiseApplyFunction)	applyfuncr  )r<   r%   r  s      r'   r  zMatrixExpr.applyfunc  s'    777777''d333r*   )TF)NNN)Vr   
__module____qualname____doc__	__slots__	_iterable_op_priority	is_Matrixis_MatrixExpris_Identity
is_Inverseis_Transposeis_ZeroMatrix	is_MatAdd	is_MatMulis_commutative	is_number	is_symbol	is_scalarr   r0   __annotations__r3   propertytTupler   r=   rA   rF   rJ   rL   r-   NotImplementedr   rR   rN   rX   rV   r]   r_   rZ   rb   rf   rc   rj   rh   ro   rt   rw   r~   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   boolr   r   r   r   r   r   r   staticmethodr  r  __classcell__)r   s   @r'   r/   r/   $   s         $ I
 ILIMKJLMIINIII!z||D*###3 3 3 "vdDj) " " " X"   X   X2 2 2" " " Z((:&&* * '& )(* Z((9%%* * &% )(* Z((:&&+ + '& )(+ Z((9%%+ + &% )(+ Z((:&&* * '& )(* Z((:&&* * '& )(* Z((9%%* * &% )(* Z((9%%* * &% )(* Z((:&&* * '& )(* Z((9%%> > &% )(> Z((>**+ + +* )(+ Z((=))$ $ *) )($   X   X & & X&( ( () ) ) )  
  ! ! !  ! ! !J J J  : : :+ + + + + 6 6 [6I I I              X $ $ $
       XI I I!A !A !AFAD A A A A8 8 8B/ / /2  	0 	0 	0  # # # 1, 1, 1, \1,f4 4 4 4 4 4 4r*   r/   c                     dS )NFr+   lhsrhss     r'   _eval_is_eqr/    s    5r*   c                 B    | j         |j         k    rdS | |z
  j        rdS d S )NFT)r=   r  r,  s     r'   r/  r/    s6    
yCI uc	  t r*   c                       fd}|S )Nc                    t           t          t          t          i         }g }g }| j        D ]B}t          |t                    r|                    |           -|                    |           C|s                    |          S |rt           k    rbt          t          |                    D ]D}||         j        s5||                                                 |                    ||<   g } nEn0                    | ||                     d          gz             S |t          k    r ||                     d          S  |                    |          g|R                      d          S )NF)r   )r	   rE   r
   r@   r5   r   r/   r  
_from_argsr   r   r  r]   rI   )r  	mat_classnonmatricesmatricestermr   r4   s         r'   _postprocessorz)get_postprocessor.<locals>._postprocessor  s   &#v.s3	I 	) 	)D$
++ )%%%%""4(((( 	/>>+... 	]cz ]s8}}--  A#A;4  '/qk&9&9#..:U:U&V&V&( ~~kYY5I5N5NTY5N5Z5Z4[&[\\\ 	99h',,%,888y44@x@@@EE5EQQQr*   r+   )r4   r8  s   ` r'   get_postprocessorr9    s*    "R "R "R "R "RF r*   )r	   r
   Fc                     t          | t                    st          |t                    rd}|rt          | |          S ddlm} ddlm} ddlm}  ||           } |||          } ||          }|S )NTr   )convert_matrix_to_array)array_deriver  )	r   r    _matrix_derivative_old_algorithm3sympy.tensor.array.expressions.conv_matrix_to_arrayr;  4sympy.tensor.array.expressions.arrayexpr_derivativesr<  r  r  )	r  r   old_algorithmr;  r<  r  
array_exprdiff_array_exprdiff_matrix_exprs	            r'   _matrix_derivativerD    s    $
## z!Z'@'@  9/a888[[[[[[QQQQQQ[[[[[[((..J"l:q11O..??r*   c                 &  	
 ddl m} |                     |          }d |D             }ddlm	 	fd|D             }d fd

fd|D             }|d         }d	 |d
k    r t          j        fd|D                       S  || |          S )Nr   )ArrayDerivativec                 6    g | ]}|                                 S r+   )buildr   r   s     r'   r   z4_matrix_derivative_old_algorithm.<locals>.<listcomp>  s     &&&1QWWYY&&&r*   r  c                 ,    g | ]}fd |D             S )c                 &    g | ]} |          S r+   r+   )r   r   r  s     r'   r   z?_matrix_derivative_old_algorithm.<locals>.<listcomp>.<listcomp>"  s%    444Q%%a((444r*   r+   )r   r   r  s     r'   r   z4_matrix_derivative_old_algorithm.<locals>.<listcomp>"  s.    DDD4444!444DDDr*   c                 >    t          | t                    r| j        S dS )Nrs   rs   r   r/   r=   elems    r'   
_get_shapez4_matrix_derivative_old_algorithm.<locals>._get_shape$  s!    dJ'' 	:tr*   c                 :    t          fd| D                       S )Nc                 4    g | ]} |          D ]}|d vS ))rs   Nr+   )r   r   r   rQ  s      r'   r   zF_matrix_derivative_old_algorithm.<locals>.get_rank.<locals>.<listcomp>*  s3    MMM1zz!}}MM!AY&MMMMr*   )sum)partsrQ  s    r'   get_rankz2_matrix_derivative_old_algorithm.<locals>.get_rank)  s&    MMMMMMMNNNr*   c                 &    g | ]} |          S r+   r+   )r   r   rV  s     r'   r   z4_matrix_derivative_old_algorithm.<locals>.<listcomp>,  s!    (((QXXa[[(((r*   c                 d   t          |           dk    r| d         S | d d         \  }}|j        r|j        }|t          d          k    r|}n|t          d          k    r|}n||z  }t          |           dk    r|S |j        rt	          d          |t          j        | dd                    z  S )Nrs   r   r    )r   r  r   Identityr   r	   fromiter)rU  p1p2pbases       r'   contract_one_dimsz;_matrix_derivative_old_algorithm.<locals>.contract_one_dims/  s    u::? 	58O2A2YFB| TXa[[  x{{" 25zzQ 5? )$R..(S\%)4444r*   r   c                 &    g | ]} |          S r+   r+   )r   r   r_  s     r'   r   z4_matrix_derivative_old_algorithm.<locals>.<listcomp>D  s%    AAAa..q11AAAr*   )$sympy.tensor.array.array_derivativesrF  _eval_derivative_matrix_linesr  r  r
   r[  )r  r   rF  linesrU  ranksrankrQ  r_  r  rV  s          @@@@r'   r=  r=    s   DDDDDD..q11E&&&&&E[[[[[[DDDDeDDDE  
O O O O O )(((%(((E8D5 5 5( qy C|AAAA5AAABBB?4###r*   c                       e Zd Z ed           Z ed           Z ed           ZdZdZdZ	d Z
ed             Zd Zed             Zd	 Zd
S )MatrixElementc                     | j         d         S rm   r5   r;   s    r'   <lambda>zMatrixElement.<lambda>J  s    49Q< r*   c                     | j         d         S rr   ri  r;   s    r'   rj  zMatrixElement.<lambda>K      dil r*   c                     | j         d         S r   ri  r;   s    r'   rj  zMatrixElement.<lambda>L  rl  r*   Tc                    t          t          ||f          \  }}ddlm} t	          |t
                    rt          |          }nt	          ||          r(|j        r|j        r
|||f         S t          |          }n8t          |          }t	          |j        t                    st          d           t          |dd           ||          st          d          t          j        | |||          }|S )Nr   r   z2First argument of MatrixElement should be a matrixr   c                     dS )NTr+   )r   ms     r'   rj  z'MatrixElement.__new__.<locals>.<lambda>_  s    T r*   zindices out of range)r2   r   sympy.matrices.matricesr   r   strr   
is_Integerr0   r   	TypeErrorgetattrr   r   r3   )r4   namer   rq  r   objs         r'   r3   zMatrixElement.__new__Q  s   8aV$$1666666dC   	9$<<DD$
++ Z< &AL &1:%~~~~!$)Z88 Z#$XYYYB740A0ABB1aHH 9 !7888l3a++
r*   c                     | j         d         S rm   ri  r;   s    r'   symbolzMatrixElement.symbold  s    y|r*   c                                          dd          }|rfd| j        D             }n| j        }|d         |d         |d         f         S )Nr   Tc                 *    g | ]} |j         d i S )r+   )rI   )r   r,   r   s     r'   r   z&MatrixElement.doit.<locals>.<listcomp>k  s+    ;;;#HCH%%u%%;;;r*   r   rs   r   )getr5   )r<   r   r   r5   s    `  r'   rI   zMatrixElement.doith  s^    yy&& 	;;;;;;;DD9DAwtAwQ'((r*   c                      | j         dd          S rr   ri  r;   s    r'   indiceszMatrixElement.indicesp  s    y}r*   c                    t          |t                    sSddlm} t          | j        |          r,| j                            |          | j        | j        f         S t          j	        S | j
        d         }| j        j        \  }}||j
        d         k    rYt          | j
        d         |j
        d         d|dz
  f          t          | j
        d         |j
        d         d|dz
  f          z  S t          |t                    rddlm} | j
        dd          \  }}t!          dt"                    \  }	}
|j
        d         }|j        \  }} ||||	f         ||	|
f                             |          z  ||
|f         z  |	d|dz
  f|
d|dz
  f           S |                     |j
        d                   rd S t          j	        S )Nr   ro  rs   r   )Sumzz1, z2)r4   )r   rg  rr  r   parentdiffr   r   r   Zeror5   r=   r   r   sympy.concrete.summationsr  r   r   r   )r<   vr   Mrq  r   r  r   r   i1i2Yr1r2s                 r'   r   zMatrixElement._eval_derivativet  s   !]++ 	::::::$+z22 ;{''**4646>::6MIaL{ 1q	> 	E!$)A,q	Aqs8DD!$)A,q	Aqs8DDE E a!! 	[5555559QRR=DAqX5111FBq	AWFBC!R%2r6!2!221RU8;b!RT]RQRTVWXTXMZZZZ88AF1I 	4vr*   N)r   r  r  r$  r  r   r   	_diff_wrtr!  r  r3   rz  rI   r  r   r+   r*   r'   rg  rg  I  s        X//00F**++A**++AIIN  &   X) ) )   X    r*   rg  c                   ~    e Zd ZdZdZdZdZd Zed             Z	ed             Z
d Zed             Zd	 Zd
 Z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

    Examples
    ========

    >>> 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                    t          |          t          |          }}|                     |           |                     |           t          |t                    rt	          |          }t          j        | |||          }|S r"   )r   r   r   rs  r   r   r3   )r4   rw  r   rq  rx  s        r'   r3   zMatrixSymbol.__new__  sr    {{HQKK1qqdC   	t99DmCq!,,
r*   c                 6    | j         d         | j         d         fS )Nrs   r   ri  r;   s    r'   r=   zMatrixSymbol.shape  s    y|TYq\))r*   c                 &    | j         d         j        S rm   )r5   rw  r;   s    r'   rw  zMatrixSymbol.name  s    y|  r*   c                 $    t          | ||          S r"   )rg  r   s       r'   r   zMatrixSymbol._entry  s    T1a(((r*   c                     | hS r"   r+   r;   s    r'   free_symbolszMatrixSymbol.free_symbols  s	    vr*   c                     | S r"   r+   )r<   r6   s     r'   r   zMatrixSymbol._eval_simplify  r   r*   c                 N    t          | j        d         | j        d                   S Nr   rs   )r   r=   )r<   r   s     r'   r   zMatrixSymbol._eval_derivative  s    $*Q-A777r*   c                 >   | |k    r| j         d         dk    r&t          |j         d         | j         d                   nt          j        }| j         d         dk    r&t          |j         d         | j         d                   nt          j        }t	          ||g          gS | j         d         dk    rt          | j         d                   nt          j        }| j         d         dk    rt          | j         d                   nt          j        }t	          ||g          gS r  )r=   r   r   r  _LeftRightArgsrZ  r   )r<   r   firstseconds       r'   rb  z*MatrixSymbol._eval_derivative_matrix_lines  s   19 	=AZ]a=O[Jqwqz4:a=999UVU[E>Bjmq>P\Z
DJqM:::VWV\F"    04z!}/ALHTZ]+++quE04
10BMXdjm,,,F"   r*   N)r   r  r  r  r  r!  r  r3   r$  r=   rw  r   r  r   r   rb  r+   r*   r'   r  r    s           NII	 	 	 * * X* ! ! X!) ) )   X  8 8 8    r*   r  c                 $    d | j         D             S )Nc                      g | ]}|j         	|S r+   )r  )r   syms     r'   r   z"matrix_symbols.<locals>.<listcomp>  s    >>>C>C>>>r*   )r  r  s    r'   matrix_symbolsr    s    >>4,>>>>r*   c                       e Zd ZdZej        fdZed             Zej	        d             Zed             Z
e
j	        d             Z
d Zd Zed	             Zd
 Zd Zd Zd Zd Zd ZdS )r  a  
    Helper class to compute matrix derivatives.

    The logic: when an expression is derived by a matrix `X_{mn}`, two lines of
    matrix multiplications are created: the one contracted to `m` (first line),
    and the one contracted to `n` (second line).

    Transposition flips the side by which new matrices are connected to the
    lines.

    The trace connects the end of the two lines.
    c                     d |D             | _         | j         | _        d| _        d| _        | j         | _        d| _        d| _        || _        d S )Nc                     g | ]}|S r+   r+   rI  s     r'   r   z+_LeftRightArgs.__init__.<locals>.<listcomp>  s    (((Qq(((r*   r   rs   )_lines_first_pointer_parent_first_pointer_index_first_line_index_second_pointer_parent_second_pointer_index_second_line_indexhigher)r<   rc  r  s      r'   __init__z_LeftRightArgs.__init__  sV    ((%(((%)["$%!!"&*k#%&""#r*   c                 &    | j         | j                 S r"   r  r  r;   s    r'   first_pointerz_LeftRightArgs.first_pointer  s    ()BCCr*   c                 $    || j         | j        <   d S r"   r  r<   values     r'   r  z_LeftRightArgs.first_pointer  s    @E"4#<===r*   c                 &    | j         | j                 S r"   r  r  r;   s    r'   second_pointerz_LeftRightArgs.second_pointer  s    *4+EFFr*   c                 $    || j         | j        <   d S r"   r  r  s     r'   r  z_LeftRightArgs.second_pointer  s    BG#D$>???r*   c                 F      fd j         D             }d|d j        dS )Nc                 :    g | ]}                     |          S r+   _buildr   s     r'   r   z+_LeftRightArgs.__repr__.<locals>.<listcomp>  s#    555AQ555r*   z_LeftRightArgs(lines=z	, higher=r   )r  r  )r<   builts   ` r'   __repr__z_LeftRightArgs.__repr__  s:    5555555EEKKK
 	
r*   c                     | j         | j        c| _        | _         | j        | j        c| _        | _        | j        | j        c| _        | _        | S r"   )r  r  r  r  r  r  r;   s    r'   r   z_LeftRightArgs.transpose
  sR    BFB]_c_y?"D$?@D@Z\`\u=!4#=:>:QSWSi7 7r*   c                     t          | t                    r|                                 S t          | t                    r6t	          |           dk    r| d         S  | d         d | d         D              S | S )Nrs   r   c                 B    g | ]}t                               |          S r+   )r  r  rI  s     r'   r   z)_LeftRightArgs._build.<locals>.<listcomp>  s&     K K Ka!6!6q!9!9 K K Kr*   )r   r   rH  listr   r  s    r'   r  z_LeftRightArgs._build  sy    dK(( 	 ::<<dD!! 	4yyA~ MAwtAw K K47 K K KLLKr*   c                       fd j         D             } j        dk    r|                      j                  gz  }d |D             }|S )Nc                 :    g | ]}                     |          S r+   r  r   s     r'   r   z(_LeftRightArgs.build.<locals>.<listcomp>  s#    4441A444r*   rs   c                     g | ]}|S r+   r+   rI  s     r'   r   z(_LeftRightArgs.build.<locals>.<listcomp>   s       a   r*   )r  r  r  )r<   datas   ` r'   rH  z_LeftRightArgs.build  s`    4444444;! 	/T[[--..D  4   r*   c                    | j         dk    r| j        dk    rt          d          d } || j                   d          || j                  d         k    rf || j                  dk    r| j         | j        d         z  S  || j                   dk    r| j         d         | j        j        z  S t          d          | j         dk    r| j         | j        j        z  S | j        S )Nrs   z.higher dimensional array cannot be representedc                 >    t          | t                    r| j        S dS )N)NNrN  rO  s    r'   rQ  z._LeftRightArgs.matrix_form.<locals>._get_shape'  s!    $
++ "z!<r*   rM  )r   r   zincompatible shapes)r  r  r   r  r   )r<   rQ  s     r'   matrix_formz_LeftRightArgs.matrix_form#  s   :? 	Ot{a/ 	OMNNN	  	  	 
 :dj!!!$

4;(?(?(BB 	4 z$+&&&0 4z$+d"333z$*%%/ 6z$'552333:? 	:dkm++;r*   c                     d}| j         dk    r&|t          d | j         j        D                       z  }| j        dk    r&|t          d | j        j        D                       z  }| j        dk    r|dz  }|S )zl
        Number of dimensions different from trivial (warning: not related to
        matrix rank).
        r   rs   c                     g | ]}|d k    	S rs   r+   rI  s     r'   r   z'_LeftRightArgs.rank.<locals>.<listcomp>@  s    :::Aa:::r*   c                     g | ]}|d k    	S r  r+   rI  s     r'   r   z'_LeftRightArgs.rank.<locals>.<listcomp>B  s    ;;;Aa;;;r*   r   )r  rT  r=   r  r  )r<   re  s     r'   re  z_LeftRightArgs.rank9  s    
 :? 	<C::)9:::;;;D;! 	=C;;):;;;<<<D;! 	AIDr*   c                 p    ddl m} ddl m} t          |t          |||g          dg|j                  }|S )N   )ArrayTensorProduct)ArrayContraction)rs   r   )	validator)*tensor.array.expressions.array_expressionsr  r  r   	_validate)r<   pointerrM   r  r  subexprs         r'   _multiply_pointerz _LeftRightArgs._multiply_pointerG  sq    TTTTTTRRRRRR&  	 '0
 
 
 r*   c                 &    | xj         |z  c_         d S r"   )r  rQ   s     r'   append_firstz_LeftRightArgs.append_first\  s    e#r*   c                 &    | xj         |z  c_         d S r"   )r  rQ   s     r'   append_secondz_LeftRightArgs.append_second_  s    u$r*   N)r   r  r  r  r   r   r  r$  r  setterr  r  r   r)  r  rH  r  re  r  r  r  r+   r*   r'   r  r    sL         &'U     D D XD F F F G G XG H H H
 
 
   	 	 \	    ,    *$ $ $% % % % %r*   r  c                 V    ddl m} t          | t                    r| S  || gg          S )Nr   r   )r   r   r   r/   )r   r   s     r'   _make_matrixr  c  s@    ======!Z   !&&&r*   rs   rD   r?   r   r   r   )r   rZ  r   r"   r  )Gtypingr   r%  	functoolsr   
sympy.corer   r   r   r	   r
   sympy.core.assumptionsr   sympy.core.decoratorsr   sympy.core.exprr   r   sympy.core.logicr   sympy.core.symbolr   r   r   r   sympy.core.sympifyr   r   sympy.external.gmpyr   sympy.functionsr   r   (sympy.functions.special.tensor_functionsr   sympy.matrices.commonr   rr  r   r   sympy.multipledispatchr   sympy.utilities.miscr   r-   r/   r/  r9  "_constructor_postprocessor_mappingrD  r=  rg  r  r  r  r  matmulrE   mataddr@   matpowre   r   r|   r   r   specialr   rZ  determinantr   r+   r*   r'   <module>r     s   " " " " " "       2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 7 7 7 7 7 7 - - - - - - - - & & & & & & 9 9 9 9 9 9 9 9 9 9 9 9 5 5 5 5 5 5 5 5 * * * * * * . . . . . . . . C C C C C C 6 6 6 6 6 6 : : : : : : : : + + + + + + + + + + + +    h4 h4 h4 h4 h4 h4 h4 h4V 
*d   
*j!!  "!$ $ $P c""#c""#8 8 ( 4   &,$ ,$ ,$^F F F F FD F F FRB B B B B: B B BJ? ? ?E% E% E% E% E% E% E% E%P' ' '                                     ) ) ) ) ) ) ) ) $ $ $ $ $ $ $ $r*   