
    \hk                    N   S SK Jr  S SKJr  S SKJrJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJrJr  S SKJr  S SKJrJrJrJr  S S	KJrJr  S S
KJr  S SKJrJr  S SK J!r!  S SK"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+  S,S jr, " S S\5      r-\)" \-\5      S 5       r.\)" \-\-5      S 5       r.S r/\/" \5      /\/" \	5      /S.\R`                  \-'   S-S jr1S r2 " S S\5      r3 " S S\-5      r4S  r5 " S! S"5      r6S# r7S$S%K8J9r9  S$S&K:J;r;  S$S'K<J=r=  S$S(K>J?r?  S$S)K@JArA  S$S*KBJCrCJDrD  S$S+KEJFrF  g).    )annotations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                   ^ U4S jnU$ )Nc                4   >^  [        T 5      U U4S j5       nU$ )Nc                P   >  [        U5      nT" X5      $ ! [         a    Ts $ f = fN)r   r   )abfuncretvals     Z/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/expressions/matexpr.py__sympifyit_wrapper5_sympifyit.<locals>.deco.<locals>.__sympifyit_wrapper   s/    QKAz! s    %%r   )r%   r(   r&   s   ` r'   deco_sympifyit.<locals>.deco   s!    	t	 
	 #"     )argr&   r*   s    ` r'   
_sympifyitr/      s    	# Kr,   c                    ^  \ rS rSr% SrSrS\S'   SrSrSr	S	\S
'   Sr
S	\S'   SrS\S'   SrSrSrSrSrSrSrSrSr\" 5       rS\S'   S r\SSS j5       r\S 5       r\S 5       rS rS r\" S\5      \ " S5      S 5       5       r!\" S\5      \ " S5      S 5       5       r"\" S\5      \ " S5      S 5       5       r#\" S\5      \ " S5      S 5       5       r$\" S\5      \ " S 5      S! 5       5       r%\" S\5      \ " S 5      S" 5       5       r&\" S\5      \ " S#5      S$ 5       5       r'\" S\5      \ " S#5      S% 5       5       r(\" S\5      \ " S&5      S' 5       5       r)\" S\5      \ " S(5      S) 5       5       r*\" S\5      \ " S*5      S+ 5       5       r+\" S\5      \ " S,5      S- 5       5       r,\S. 5       r-\S/ 5       r.\STS0 j5       r/S1 r0SUS2 jr1S3 r2S4 r3S5 r4S6 r5S7 r6S8 r7S9 r8S: r9S; r:U 4S< jr;\<S= 5       r=S> r>S? r?SVS@ jr@SA rASB rB\SC 5       rCSD rDSE rESF rF\SG 5       rGSH rHSI rISWSJ jrJSK rKSL rL\MS4SM jrNSN rOSO rPSP rQ\RSXSQ j5       rSSR rTSrUU =rV$ )Y
MatrixExpr%   a`  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-   ztuple[str, ...]	__slots__Fg      &@Tbool	is_Matrixis_MatrixExprNr   is_Identityr   kindc                V    [        [        U5      n[        R                  " U /UQ70 UD6$ r"   )mapr   r   __new__)clsargskwargss      r'   r;   MatrixExpr.__new__Q   s'    8T"}}S242622r,   c                    [         er"   NotImplementedErrorselfs    r'   shapeMatrixExpr.shapeW   s    !!r,   c                    [         $ r"   MatAddrC   s    r'   _add_handlerMatrixExpr._add_handler[       r,   c                    [         $ r"   MatMulrC   s    r'   _mul_handlerMatrixExpr._mul_handler_   rL   r,   c                R    [        [        R                  U 5      R                  5       $ r"   )rO   r   NegativeOnedoitrC   s    r'   __neg__MatrixExpr.__neg__c   s    ammT*//11r,   c                    [         er"   rA   rC   s    r'   __abs__MatrixExpr.__abs__f   s    !!r,   other__radd__c                4    [        X5      R                  5       $ r"   rI   rT   rD   rZ   s     r'   __add__MatrixExpr.__add__i        d"''))r,   r_   c                4    [        X5      R                  5       $ r"   r]   r^   s     r'   r[   MatrixExpr.__radd__n        e"''))r,   __rsub__c                6    [        X* 5      R                  5       $ r"   r]   r^   s     r'   __sub__MatrixExpr.__sub__s   s     dF#((**r,   rg   c                6    [        X* 5      R                  5       $ r"   r]   r^   s     r'   re   MatrixExpr.__rsub__x   s     eU#((**r,   __rmul__c                4    [        X5      R                  5       $ r"   rO   rT   r^   s     r'   __mul__MatrixExpr.__mul__}   ra   r,   c                4    [        X5      R                  5       $ r"   rm   r^   s     r'   
__matmul__MatrixExpr.__matmul__   ra   r,   rn   c                4    [        X5      R                  5       $ r"   rm   r^   s     r'   rk   MatrixExpr.__rmul__   rd   r,   c                4    [        X5      R                  5       $ r"   rm   r^   s     r'   __rmatmul__MatrixExpr.__rmatmul__   rd   r,   __rpow__c                4    [        X5      R                  5       $ r"   )MatPowrT   r^   s     r'   __pow__MatrixExpr.__pow__   ra   r,   r{   c                    [        S5      e)NzMatrix Power not definedrA   r^   s     r'   rx   MatrixExpr.__rpow__   s     ""<==r,   __rtruediv__c                ,    X[         R                  -  -  $ r"   )r   rS   r^   s     r'   __truediv__MatrixExpr.__truediv__   s     Q]]***r,   r   c                    [        5       er"   rA   r^   s     r'   r   MatrixExpr.__rtruediv__   s     "##r,   c                     U R                   S   $ Nr   rE   rC   s    r'   rowsMatrixExpr.rows       zz!}r,   c                     U R                   S   $ N   r   rC   s    r'   colsMatrixExpr.cols   r   r,   c                    U R                   u  p[        U[        5      (       a  [        U[        5      (       a  X:H  $ X:X  a  gg NT)rE   
isinstancer   )rD   r   r   s      r'   	is_squareMatrixExpr.is_square   s9    ZZ
dG$$D')B)B<<r,   c                0    SSK Jn  U" [        U 5      5      $ Nr   )Adjoint)"sympy.matrices.expressions.adjointr   	TransposerD   r   s     r'   _eval_conjugateMatrixExpr._eval_conjugate   s    >y''r,   c                "    U R                  5       $ r"   )_eval_as_real_imag)rD   deephintss      r'   as_real_imagMatrixExpr.as_real_imag   s    &&((r,   c                    [         R                  X R                  5       -   -  nX R                  5       -
  S[         R                  -  -  nX4$ N   )r   Halfr   ImaginaryUnit)rD   realims      r'   r   MatrixExpr._eval_as_real_imag   sC    vv 4 4 667))++a.?@zr,   c                    [        U 5      $ r"   InverserC   s    r'   _eval_inverseMatrixExpr._eval_inverse       t}r,   c                    [        U 5      $ r"   DeterminantrC   s    r'   _eval_determinantMatrixExpr._eval_determinant   s    4  r,   c                    [        U 5      $ r"   r   rC   s    r'   _eval_transposeMatrixExpr._eval_transpose       r,   c                    g r"   r-   rC   s    r'   _eval_traceMatrixExpr._eval_trace   s    r,   c                    [        X5      $ )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.
rz   )rD   exps     r'   _eval_powerMatrixExpr._eval_power   s    
 d  r,   c           
         U R                   (       a  U $ SSKJn  U R                  " U R                   Vs/ s H  o2" U40 UD6PM     sn6 $ s  snf )Nr   )simplify)is_Atomsympy.simplifyr   r%   r=   )rD   r>   r   xs       r'   _eval_simplifyMatrixExpr._eval_simplify   s@    <<K/99diiHix4V4iHIIHs   A
c                    SSK Jn  U" U 5      $ r   )r   r   r   s     r'   _eval_adjointMatrixExpr._eval_adjoint   s    >t}r,   c                0    [         R                  " XU5      $ r"   )r   _eval_derivative_n_times)rD   r   ns      r'   r   #MatrixExpr._eval_derivative_n_times   s    --dq99r,   c                r   > U R                  U5      (       a  [        TU ]	  U5      $ [        U R                  6 $ r"   )hassuper_eval_derivative
ZeroMatrixrE   )rD   r   	__class__s     r'   r   MatrixExpr._eval_derivative   s/    88A;;7+A..tzz**r,   c                    UR                   (       + =(       a    [        USSS9nUSL a  [        SR                  U5      5      eg)z2Helper function to check invalid matrix dimensionsT)integernonnegativeFz?The dimension specification {} should be a nonnegative integer.N)is_Floatr   
ValueErrorformat)r<   dimoks      r'   
_check_dimMatrixExpr._check_dim   sK      1"34#1;))/6 6 r,   c                F    [        SU R                  R                  -  5      e)NzIndexing not implemented for %s)rB   r   __name__rD   ijr>   s       r'   _entryMatrixExpr._entry   s#    !-0G0GGI 	Ir,   c                    [        U 5      $ r"   )r   rC   s    r'   r   MatrixExpr.adjoint   r   r,   c                &    [         R                  U 4$ )z1Efficiently extract the coefficient of a product.)r   One)rD   rationals     r'   as_coeff_MulMatrixExpr.as_coeff_Mul   s    uud{r,   c                    [        U 5      $ r"   )r   rC   s    r'   r   MatrixExpr.conjugate  r   r,   c                    SSK Jn  U" U 5      $ )Nr   	transpose)$sympy.matrices.expressions.transposer   )rD   r   s     r'   r   MatrixExpr.transpose  s    Br,   c                "    U R                  5       $ )zMatrix transpositionr   rC   s    r'   TMatrixExpr.T	  s     ~~r,   c                V    U R                   SL a  [        S5      eU R                  5       $ )NFzInverse of non-square matrix)r   r   r   rC   s    r'   inverseMatrixExpr.inverse  s)    >>U"&'EFF!!##r,   c                "    U R                  5       $ r"   r   rC   s    r'   invMatrixExpr.inv  s    ||~r,   c                    SSK Jn  U" U 5      $ )Nr   )det)&sympy.matrices.expressions.determinantr  )rD   r  s     r'   r  MatrixExpr.det  s    >4yr,   c                "    U R                  5       $ r"   r   rC   s    r'   IMatrixExpr.I  s    ||~r,   c                $   S nU" U5      =(       a    U" U5      =(       ap    U R                   S L =(       d*    XR                   * :  S:g  =(       a    XR                   :  S:g  =(       a*    X R                  * :  S:g  =(       a    X R                  :  S:g  $ )Nc                B    [        U [        [        [        [        45      $ r"   )r   intr   r   r   )idxs    r'   is_valid(MatrixExpr.valid_index.<locals>.is_valid  s    cC&$#?@@r,   F)r   r   )rD   r   r   r  s       r'   valid_indexMatrixExpr.valid_index  s    	A H Hd" HyyjU*GII%/GH yyjU*H 12II%/G	Ir,   c                   [        U[        5      (       d$  [        U[        5      (       a  SSKJn  U" XS5      $ [        U[        5      (       a  [        U5      S:X  a  Uu  p4[        U[        5      (       d  [        U[        5      (       a  SSKJn  U" XU5      $ [        U5      [        U5      pCU R                  X45      S:w  a  U R                  X45      $ [        SU< SU< S35      e[        U[        [        45      (       a~  U R                  u  pV[        U[        5      (       d  [        [        S	5      5      e[        U5      nX-  nX-  nU R                  X45      S:w  a  U R                  X45      $ [        S
U-  5      e[        U[        [        45      (       a  [        [        S5      5      e[        SU -  5      e)Nr   )MatrixSlice)r   Nr   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   rE   r   r   r   )rD   keyr  r   r   r   r   s          r'   __getitem__MatrixExpr.__getitem__&  s   #u%%*S%*@*@Dt,77c5!!c#h!mDA!U##z!U';';H"4A..A;q%.{{1(( q!!DEEj'233JDdG,,  -, "- . . 3-CA
A%.{{1(( !3c!9::fd^,,Z )( ) * * 84?@@r,   c                    [        U R                  [        [        45      (       + =(       d%    [        U R                  [        [        45      (       + $ r"   )r   r   r   r   r   rC   s    r'   _is_shape_symbolicMatrixExpr._is_shape_symbolicI  s:    tyy:w*?@@ @dii*g)>??	Ar,   c                   U R                  5       (       a  [        S5      eSSKJn  U" [	        U R
                  5       VVs/ s H-  n[	        U R                  5       Vs/ s H	  nXU4   PM     snPM/     snn5      $ s  snf s  snnf )a8  
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   ImmutableDenseMatrix)r  r   sympy.matrices.immutabler   ranger   r   )rD   r   r   r   s       r'   as_explicitMatrixExpr.as_explicitM  s    0 ""$$45 5 	B#%*499%5%7%5 &+499%5&7%5 '+a4j%5&7%5%7 8 	8 &7 %7s   B
A=.B
=B
c                >    U R                  5       R                  5       $ )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_mutablerC   s    r'   r&  MatrixExpr.as_mutablen  s    . !,,..r,   c                    Ub  U(       d  [        S5      eSSKJn  U" U R                  [        S9n[        U R                  5       H)  n[        U R                  5       H  nXU4   XEU4'   M     M+     U$ )Nz=Cannot implement copy=False when converting Matrix to ndarrayr   )empty)dtype)	TypeErrornumpyr)  rE   objectr"  r   r   )rD   r*  copyr)  r#   r   r   s          r'   	__array__MatrixExpr.__array__  sg    D[\\$**F+tyy!A499%!t*Q$ & " r,   c                @    U R                  5       R                  U5      $ )z
Test elementwise equality between matrices, potentially of different
types

>>> from sympy import Identity, eye
>>> Identity(3).equals(eye(3))
True
)r#  equalsr^   s     r'   r2  MatrixExpr.equals  s     !((//r,   c                    U $ r"   r-   rC   s    r'   canonicalizeMatrixExpr.canonicalize      r,   c                8    [         R                  [        U 5      4$ r"   )r   r   rO   rC   s    r'   as_coeff_mmulMatrixExpr.as_coeff_mmul  s    uufTl""r,   c                    SSK Jn  SSKJn  / nUb  UR	                  U5        Ub  UR	                  U5        U" XS9nU" U5      $ )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_matrix)first_indices)4sympy.tensor.array.expressions.from_indexed_to_arrayr<  3sympy.tensor.array.expressions.from_array_to_matrixr>  append)exprfirst_index
last_index
dimensionsr<  r>  r?  arrs           r'   from_index_summationMatrixExpr.from_index_summation  sN    T 	b_"  -!  ,&tI&s++r,   c                    SSK Jn  U" X5      $ )Nr   )ElementwiseApplyFunction)	applyfuncrK  )rD   r%   rK  s      r'   rL  MatrixExpr.applyfunc  s    7'33r,   )returnztuple[Expr, Expr])rN  zbool | None)TF)rN  r4   )NNN)Wr   
__module____qualname____firstlineno____doc__r3   __annotations__	_iterable_op_priorityr5   r6   r7   
is_Inverseis_Transposeis_ZeroMatrix	is_MatAdd	is_MatMulis_commutative	is_number	is_symbol	is_scalarr   r8   r;   propertyrE   rJ   rP   rU   rX   r/   NotImplementedr   r_   r[   rg   re   rn   rq   rk   rv   r{   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r  r  r  r  r  r#  r&  r-  r/  r2  r5  r9  staticmethodrH  rL  __static_attributes____classcell__)r   s   @r'   r1   r1   %   s   $ "$I#
 ILItM4!K!JLMIINIII!|D*#3 " "    2" (:&* ' )* (9%* & )* (:&+ ' )+ (9%+ & )+ (:&* ' )* (:&* ' )* (9%* & )* (9%* & )* (:&* ' )* (9%> & )> (>*+ + )+ (=)$ * )$      ()
!!J:+ 6 6I    $
  I!AFA8B/2 %4 	0# 1, 1,f4 4r,   r1   c                    g)NFr-   lhsrhss     r'   _eval_is_eqrj    s    r,   c                b    U R                   UR                   :w  a  gX-
  R                  (       a  gg )NFT)rE   rY  rg  s     r'   rj  rj    s(    
yyCII	   !r,   c                   ^  U 4S jnU$ )Nc                  > [         [        [        [        0T   n/ n/ nU R                   H<  n[        U[        5      (       a  UR                  U5        M+  UR                  U5        M>     U(       d  TR                  U5      $ U(       a  T[         :X  aW  [        [        U5      5       H>  nX5   R                  (       a  M  X5   R                  TR                  U5      5      X5'   / n  O'   O$TR                  X!" U6 R                  SS9/-   5      $ U[        :X  a  U" U6 R                  SS9$ U" TR                  U5      /UQ76 R                  SS9$ )NF)r   )r	   rO   r
   rI   r=   r   r1   rB  
_from_argsr"  r  r6   rn   rT   )rC  	mat_classnonmatricesmatricestermr   r<   s         r'   _postprocessor)get_postprocessor.<locals>._postprocessor  s0   &#v.s3	IID$
++%""4(	  >>+..czs8}-A#;444 '/k&9&9#..:U&V&( . ~~kY5I5N5NTY5N5Z4[&[\\h',,%,884@x@EE5EQQr,   r-   )r<   rs  s   ` r'   get_postprocessorru    s    "RF r,   )r	   r
   c                    [        U [        5      (       d  [        U[        5      (       a  SnU(       a  [        X5      $ SSKJn  SSKJn  SSKJn  U" U 5      nU" Xa5      nU" U5      nU$ )NTr   )convert_matrix_to_array)array_deriver=  )	r   r    _matrix_derivative_old_algorithm3sympy.tensor.array.expressions.from_matrix_to_arrayrw  4sympy.tensor.array.expressions.arrayexpr_derivativesrx  rA  r>  )	rC  r   old_algorithmrw  rx  r>  
array_exprdiff_array_exprdiff_matrix_exprs	            r'   _matrix_derivativer    s\    $
##z!Z'@'@/88[Q[(.J":1O.?r,   c           
       ^ SSK Jn  U R                  U5      nU Vs/ s H  oDR                  5       PM     nnSSKJn  U VVs/ s H  oD Vs/ s H
  ov" U5      PM     snPM     nnnS mU4S jnU Vs/ s H
  oH" U5      PM     n	nU	S   n
S nU
S::  a,  [        R                  " U Vs/ s H
  oK" U5      PM     sn5      $ U" X5      $ s  snf s  snf s  snnf s  snf s  snf )Nr   )ArrayDerivativer=  c                F    [        U [        5      (       a  U R                  $ g)Nr   r   r   r1   rE   elems    r'   
_get_shape4_matrix_derivative_old_algorithm.<locals>._get_shape0  s    dJ''::r,   c                .   > [        U4S jU  5       5      $ )Nc              3  L   >#    U  H  nT" U5        H	  o"S ;  v   M     M     g7f)r   NNr-   ).0r   r   r  s      r'   	<genexpr>E_matrix_derivative_old_algorithm.<locals>.get_rank.<locals>.<genexpr>6  s!     Lu!jmI%m%us   !$)sum)partsr  s    r'   get_rank2_matrix_derivative_old_algorithm.<locals>.get_rank5  s    LuLLLr,   c                T   [        U 5      S:X  a  U S   $ U S S u  pUR                  (       a  UR                  nU[        S5      :X  a  UnOU[        S5      :X  a  UnOX-  n[        U 5      S:X  a  U$ UR                  (       a  [	        S5      eU[
        R                  " U SS  5      -  $ )Nr   r   r    )r  r5   r   Identityr   r	   fromiter)r  p1p2pbases       r'   contract_one_dims;_matrix_derivative_old_algorithm.<locals>.contract_one_dims;  s    u:?8O2AYFB||TTXa[ x{"5zQ??$R.(S\\%)444r,   r   )$sympy.tensor.array.array_derivativesr  _eval_derivative_matrix_linesbuildrA  r>  r
   r  )rC  r   r  linesr   r  r>  r   r  ranksrankr  r  s               @r'   ry  ry  &  s    D..q1E %&1WWYE&[>CDe!4!Q%a(!4eED
M #((%QXa[%E(8D5( qy||5A5a.q15ABB4##Q ' 5D )0 Bs)   C		CCC4C)CCc                      \ rS rSr\" S 5      r\" S 5      r\" S 5      rSrSr	Sr
S r\S 5       rS r\S	 5       rS
 rSrg)MatrixElementiU  c                     U R                   S   $ r   r=   rC   s    r'   <lambda>MatrixElement.<lambda>V  s    499Q<r,   c                     U R                   S   $ r   r  rC   s    r'   r  r  W      diilr,   c                     U R                   S   $ r   r  rC   s    r'   r  r  X  r  r,   Tc                   [        [        X#45      u  p#[        U[        5      (       a  [	        U5      nO[        U[
        5      (       a4  UR                  (       a  UR                  (       a  XU4   $ [        U5      nO5[        U5      n[        UR                  [        5      (       d  [        S5      e[        USS 5      " X#5      (       d  [        S5      e[        R                  " XX#5      nU$ )Nz2First argument of MatrixElement should be a matrixr  c                    gr   r-   )r   ms     r'   r  'MatrixElement.__new__.<locals>.<lambda>j  s    Tr,   zindices out of range)r:   r   r   strr   r   
is_Integerr8   r   r+  getattrr  r   r;   r<   namer   r  objs        r'   r;   MatrixElement.__new__]  s    8aV$dC  $<D$
++<<ALL1:%~~!$))Z88#$XYY40AB1HH !788ll3a+
r,   c                     U R                   S   $ r   r  rC   s    r'   symbolMatrixElement.symbolo  s    yy|r,   c                    UR                  SS5      nU(       a,  U R                   Vs/ s H  o3R                  " S0 UD6PM     nnOU R                  nUS   US   US   4   $ s  snf )Nr   Tr   r   r   r-   )getr=   rT   )rD   r   r   r.   r=   s        r'   rT   MatrixElement.doits  sc    yy&15;#HH%u%D;D99DAwtAwQ'(( <s   A"c                     U R                   SS  $ r   r  rC   s    r'   indicesMatrixElement.indices{  s    yy}r,   c                B   [        U[        5      (       d4  U R                  R                  U5      U R                  U R
                  4   $ U R                  S   nU R                  R                  u  p4X!R                  S   :X  aY  [        U R                  S   UR                  S   SUS-
  45      [        U R                  S   UR                  S   SUS-
  45      -  $ [        U[        5      (       a|  SSK
Jn  U R                  SS  u  pg[        S[        S9u  pUR                  S   n
U
R                  u  pU" X&U4   XU	4   R                  U5      -  X)U4   -  USUS-
  4U	SUS-
  45      * $ U R                  UR                  S   5      (       a  g [        R                   $ )Nr   r   r   )Sumzz1, z2)r<   )r   r  parentdiffr   r   r=   rE   r   r   sympy.concrete.summationsr  r   r   r   r   Zero)rD   vMr  r   r  r   r   i1i2Yr1r2s                r'   r   MatrixElement._eval_derivative  so   !]++;;##A&tvvtvv~66IIaL{{  q	>!$))A,q	Aqs8D!$))A,q	Aqs8DE E a!!599QR=DAX51FBq	AWWFBR%r6!221U8;b!RT]RQRTVWXTXMZZZ88AFF1Ivvr,   r-   N)r   rP  rQ  rR  r`  r  r   r   	_diff_wrtr^  r\  r;   r  rT   r  r   rd  r-   r,   r'   r  r  U  si    /0F*+A*+AIIN$  )  r,   r  c                  r    \ rS rSrSrSrSrSrS r\	S 5       r
\	S 5       rS r\	S	 5       rS
 rS rS rSrg)MatrixSymboli  av  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                    [        U5      [        U5      p2U R                  U5        U R                  U5        [        U[        5      (       a  [	        U5      n[
        R                  " XX#5      nU$ r"   )r   r   r   r  r   r   r;   r  s        r'   r;   MatrixSymbol.__new__  sT    {HQK1qqdC  t9DmmCq,
r,   c                >    U R                   S   U R                   S   4$ )Nr   r   r  rC   s    r'   rE   MatrixSymbol.shape  s    yy|TYYq\))r,   c                4    U R                   S   R                  $ r   )r=   r  rC   s    r'   r  MatrixSymbol.name  s    yy|   r,   c                    [        XU5      $ r"   )r  r   s       r'   r   MatrixSymbol._entry  s    Ta((r,   c                    U 1$ r"   r-   rC   s    r'   free_symbolsMatrixSymbol.free_symbols  s	    vr,   c                    U $ r"   r-   )rD   r>   s     r'   r   MatrixSymbol._eval_simplify  r7  r,   c                N    [        U R                  S   U R                  S   5      $ Nr   r   )r   rE   )rD   r   s     r'   r   MatrixSymbol._eval_derivative  s    $**Q-A77r,   c                P   X:w  a  U R                   S   S:w  a&  [        UR                   S   U R                   S   5      O[        R                  nU R                   S   S:w  a&  [        UR                   S   U R                   S   5      O[        R                  n[	        X#/5      /$ U R                   S   S:w  a  [        U R                   S   5      O[        R                  nU R                   S   S:w  a  [        U R                   S   5      O[        R                  n[	        X#/5      /$ r  )rE   r   r   r  _LeftRightArgsr  r   )rD   r   firstseconds       r'   r  *MatrixSymbol._eval_derivative_matrix_lines  s    9=AZZ]a=OJqwwqz4::a=9UVU[U[E>Bjjmq>PZ
DJJqM:VWV\V\F"   04zz!}/AHTZZ]+quuE04

10BXdjjm,F"  r,   r-   N)r   rP  rQ  rR  rS  r\  r^  r  r;   r`  rE   r  r   r  r   r   r  rd  r-   r,   r'   r  r    sm      NII	 * * ! !)  8r,   r  c                j    U R                    Vs/ s H  oR                  (       d  M  UPM     sn$ s  snf r"   )r  r5   )rC  syms     r'   matrix_symbolsr    s&    ,,>,CC,>>>s   00c                      \ rS rSrSr\R                  4S jr\S 5       r	\	R                  S 5       r	\S 5       r\R                  S 5       rS rS	 r\S
 5       rS rS rS rS rS rS rSrg)r  i  aq  
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                    [        U5      U l        U R                  U l        SU l        SU l        U R                  U l        SU l        SU l        X l        g r  )	list_lines_first_pointer_parent_first_pointer_index_first_line_index_second_pointer_parent_second_pointer_index_second_line_indexhigher)rD   r  r  s      r'   __init___LeftRightArgs.__init__  sJ    5k%)[["$%!!"&*kk#%&""#r,   c                4    U R                   U R                     $ r"   r  r  rC   s    r'   first_pointer_LeftRightArgs.first_pointer  s    ))$*C*CDDr,   c                4    XR                   U R                  '   g r"   r  rD   values     r'   r  r    s    @E""4#<#<=r,   c                4    U R                   U R                     $ r"   r  r  rC   s    r'   second_pointer_LeftRightArgs.second_pointer  s    **4+E+EFFr,   c                4    XR                   U R                  '   g r"   r  r  s     r'   r  r     s    BG##D$>$>?r,   c                    U R                    Vs/ s H  oR                  U5      PM     nnSU< SU R                  < S3$ s  snf )Nz_LeftRightArgs(lines=z	, higher=r  )r  _buildr  )rD   r   builts      r'   __repr___LeftRightArgs.__repr__  s;    )-5AQ5KK
 	
 6s   A c                    U R                   U R                  sU l        U l         U R                  U R                  sU l        U l        U R                  U R
                  sU l        U l        U $ r"   )r  r  r  r  r  r  rC   s    r'   r   _LeftRightArgs.transpose  sd    BFB]B]_c_y_y?"D$?@D@Z@Z\`\u\u=!4#=:>:Q:QSWSiSi7 7r,   c                
   [        U [        5      (       a  U R                  5       $ [        U [        5      (       aC  [	        U 5      S:X  a  U S   $ U S   " U S    Vs/ s H  n[
        R                  U5      PM     sn6 $ U $ s  snf )Nr   r   )r   r   r  r  r  r  r  )rC  r   s     r'   r  _LeftRightArgs._build  st    dK((::<dD!!4yA~AwAw47 K7a!6!6q!97 KLLK !Ls   B c                    U R                    Vs/ s H  oR                  U5      PM     nnU R                  S:w  a  X R                  U R                  5      /-  n[        U5      nU$ s  snf r   )r  r  r  r  )rD   r   datas      r'   r  _LeftRightArgs.build$  sW    (,41A4;;![[-..DDz	 5s   A&c                &   U R                   S:w  a  U R                  S:w  a  [        S5      eS nU" U R                   5      S   U" U R                  5      S   :w  ay  U" U R                  5      S:X  a  U R                   U R                  S   -  $ U" U R                   5      S:X  a&  U R                   S   U R                  R                  -  $ [        S5      eU R                   S:w  a#  U R                   U R                  R                  -  $ U R                  $ )Nr   z.higher dimensional array cannot be representedc                F    [        U [        5      (       a  U R                  $ g)N)NNr  r  s    r'   r  ._LeftRightArgs.matrix_form.<locals>._get_shape/  s    $
++zz!r,   r  )r   r   zincompatible shapes)r  r  r   r  r   )rD   r  s     r'   matrix_form_LeftRightArgs.matrix_form+  s    ::?t{{a/MNN	 
 djj!!$
4;;(?(BB $++&&0zz$++d"333$**%/zz$'55233::?::dkkmm++;;r,   c                   SnU R                   S:w  a)  U[        S U R                   R                   5       5      -  nU R                  S:w  a)  U[        S U R                  R                   5       5      -  nU R                  S:w  a  US-  nU$ )zT
Number of dimensions different from trivial (warning: not related to
matrix rank).
r   r   c              3  *   #    U  H	  oS :g  v   M     g7fr  r-   r  r   s     r'   r  &_LeftRightArgs.rank.<locals>.<genexpr>H  s     9(81Q(8   c              3  *   #    U  H	  oS :g  v   M     g7fr  r-   r  s     r'   r  r  J  s     :(91Q(9r  r   )r  r  rE   r  r  )rD   r  s     r'   r  _LeftRightArgs.rankA  sx    
 ::?C9

(8(8999D;;!C:(9(9:::D;;!AIDr,   c                d    SSK Jn  SSK Jn  [        U[        UUU/5      S/UR                  S9nU$ )N   )ArrayTensorProduct)ArrayContraction)r   r   )	validator)*tensor.array.expressions.array_expressionsr  r  r   	_validate)rD   pointerrZ   r  r  subexprs         r'   _multiply_pointer _LeftRightArgs._multiply_pointerO  sG    TR& 	 '00
 r,   c                .    U =R                   U-  sl         g r"   )r  r^   s     r'   append_first_LeftRightArgs.append_firstd  s    e#r,   c                .    U =R                   U-  sl         g r"   )r  r^   s     r'   append_second_LeftRightArgs.append_secondg  s    u$r,   )r  r  r  r  r  r  r  r  N)r   rP  rQ  rR  rS  r   r   r  r`  r  setterr  r  r   rc  r  r  r  r  r#  r&  r)  rd  r-   r,   r'   r  r    s     &'UU  E E F F G G H H
 	 	,*$%r,   r  c                P    SSK Jn  [        U [        5      (       a  U $ U" U //5      $ )Nr   r  )r!  r   r   r1   )r   r   s     r'   _make_matrixr-  k  s&    =!Z  !&&r,   r   rN   rH   r   r   r   )r   r  r   r"   rO  )G
__future__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.exceptionsr   sympy.matrices.kindr   sympy.matrices.matrixbaser   sympy.multipledispatchr   sympy.utilities.miscr   r/   r1   rj  ru  "_constructor_postprocessor_mappingr  ry  r  r  r  r  r-  matmulrO   mataddrI   matpowrz   r   r   r   r   specialr   r  determinantr   r-   r,   r'   <module>rE     s   "  2 2 4 7 - & 9 9 5 * . C : * 0 + + s4 s4l 
*d  
*j! "$P c"#c"#8 ( ( 4&,$^BD BJB: BJ?E% E%P'       ) $r,   