o
    9ôj�k  ã                   @  s  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% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d4dd„Z,G dd„ deƒZ-e)e-eƒdd„ ƒZ.e)e-e-ƒdd„ ƒZ.dd„ Z/e/eƒge/e	ƒgdœej0e-< d5dd„Z1d d!„ Z2G d"d#„ d#eƒZ3G d$d%„ d%e-ƒZ4d&d'„ Z5G d(d)„ d)ƒZ6d*d+„ Z7d,d-l8m9Z9 d,d.l:m;Z; d,d/l<m=Z= d,d0l>m?Z? d,d1l@mAZA d,d2lBmCZCmDZD d,d3lEmFZF dS )6é    )Ú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                   s   ‡ fdd„}|S )Nc                   s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                   s,   z
t |ƒ}ˆ | |ƒW S  ty   ˆ Y S w ©N)r   r   )ÚaÚb)ÚfuncÚretval© ús/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/sympy/matrices/expressions/matexpr.pyÚ__sympifyit_wrapper   s   ÿz5_sympifyit.<locals>.deco.<locals>.__sympifyit_wrapperr   )r!   r%   ©r"   )r!   r$   Údeco   s   z_sympifyit.<locals>.decor#   )Úargr"   r'   r#   r&   r$   Ú
_sympifyit   s   r)   c                      sN  e Zd ZU dZdZded< dZdZdZded	< dZ	ded
< dZ
ded< dZdZdZdZdZdZdZdZdZeƒ Zded< dd„ ZedŒdd„ƒZedd„ ƒZe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d0ƒd1d2„ ƒƒZ&edeƒed0ƒd3d4„ ƒƒZ'edeƒed5ƒd6d7„ ƒƒZ(edeƒed8ƒd9d:„ ƒƒZ)edeƒed;ƒd<d=„ ƒƒZ*edeƒed>ƒd?d@„ ƒƒZ+edAdB„ ƒZ,edCdD„ ƒZ-ed�dFdG„ƒZ.dHdI„ Z/dŽdJdK„Z0dLdM„ Z1dNdO„ Z2dPdQ„ Z3dRdS„ Z4dTdU„ Z5dVdW„ Z6dXdY„ Z7dZd[„ Z8d\d]„ Z9‡ fd^d_„Z:e;d`da„ ƒZ<dbdc„ Z=ddde„ Z>d�dfdg„Z?dhdi„ Z@djdk„ ZAedldm„ ƒZBdndo„ ZCdpdq„ ZDdrds„ ZEedtdu„ ƒZFdvdw„ ZGdxdy„ ZHd�dzd{„ZId|d}„ ZJd~d„ ZKeLdfd€d�„ZMd‚dƒ„ ZNd„d…„ ZOd†d‡„ ZPeQd‘dˆd‰„ƒZRdŠd‹„ ZS‡  ZTS )’Ú
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#   ztuple[str, ...]Ú	__slots__Fg      &@TÚboolÚ	is_MatrixÚis_MatrixExprNr   Úis_Identityr   Úkindc                 O  s"   t t|ƒ}tj| g|¢R i |¤ŽS r   )Úmapr   r   Ú__new__)ÚclsÚargsÚkwargsr#   r#   r$   r2   Q   s   
zMatrixExpr.__new__Úreturnútuple[Expr, Expr]c                 C  ó   t ‚r   ©ÚNotImplementedError©Úselfr#   r#   r$   ÚshapeW   ó   zMatrixExpr.shapec                 C  ó   t S r   ©ÚMatAddr;   r#   r#   r$   Ú_add_handler[   r>   zMatrixExpr._add_handlerc                 C  r?   r   ©ÚMatMulr;   r#   r#   r$   Ú_mul_handler_   r>   zMatrixExpr._mul_handlerc                 C  s   t tj| ƒ ¡ S r   )rD   r   ÚNegativeOneÚdoitr;   r#   r#   r$   Ú__neg__c   ó   zMatrixExpr.__neg__c                 C  r8   r   r9   r;   r#   r#   r$   Ú__abs__f   ó   zMatrixExpr.__abs__ÚotherÚ__radd__c                 C  ó   t | |ƒ ¡ S r   ©rA   rG   ©r<   rL   r#   r#   r$   Ú__add__i   ó   zMatrixExpr.__add__rQ   c                 C  ó   t || ƒ ¡ S r   rO   rP   r#   r#   r$   rM   n   rR   zMatrixExpr.__radd__Ú__rsub__c                 C  s   t | | ƒ ¡ S r   rO   rP   r#   r#   r$   Ú__sub__s   ó   zMatrixExpr.__sub__rU   c                 C  s   t ||  ƒ ¡ S r   rO   rP   r#   r#   r$   rT   x   rV   zMatrixExpr.__rsub__Ú__rmul__c                 C  rN   r   ©rD   rG   rP   r#   r#   r$   Ú__mul__}   rR   zMatrixExpr.__mul__c                 C  rN   r   rX   rP   r#   r#   r$   Ú
__matmul__‚   rR   zMatrixExpr.__matmul__rY   c                 C  rS   r   rX   rP   r#   r#   r$   rW   ‡   rR   zMatrixExpr.__rmul__c                 C  rS   r   rX   rP   r#   r#   r$   Ú__rmatmul__Œ   rR   zMatrixExpr.__rmatmul__Ú__rpow__c                 C  rN   r   )ÚMatPowrG   rP   r#   r#   r$   Ú__pow__‘   rR   zMatrixExpr.__pow__r^   c                 C  s   t dƒ‚)NzMatrix Power not definedr9   rP   r#   r#   r$   r\   –   ó   zMatrixExpr.__rpow__Ú__rtruediv__c                 C  s   | |t j  S r   )r   rF   rP   r#   r#   r$   Ú__truediv__›   rR   zMatrixExpr.__truediv__ra   c                 C  s   t ƒ ‚r   r9   rP   r#   r#   r$   r`       s   zMatrixExpr.__rtruediv__c                 C  ó
   | j d S ©Nr   ©r=   r;   r#   r#   r$   Úrows¦   ó   
zMatrixExpr.rowsc                 C  rb   ©Né   rd   r;   r#   r#   r$   Úcolsª   rf   zMatrixExpr.colsúbool | Nonec                 C  s6   | j \}}t|tƒrt|tƒr||kS ||krdS d S ©NT)r=   Ú
isinstancer   )r<   re   ri   r#   r#   r$   Ú	is_square®   s   
zMatrixExpr.is_squarec                 C  s   ddl m} |t| ƒƒS ©Nr   )ÚAdjoint)Ú"sympy.matrices.expressions.adjointro   Ú	Transpose©r<   ro   r#   r#   r$   Ú_eval_conjugate·   s   zMatrixExpr._eval_conjugatec                 K  ó   |   ¡ S r   )Ú_eval_as_real_imag)r<   ÚdeepÚhintsr#   r#   r$   Úas_real_imag»   ó   zMatrixExpr.as_real_imagc                 C  s0   t j| |  ¡   }| |  ¡  dt j  }||fS ©Né   )r   ÚHalfrs   ÚImaginaryUnit)r<   ÚrealÚimr#   r#   r$   ru   ¾   s   zMatrixExpr._eval_as_real_imagc                 C  ó   t | ƒS r   ©ÚInverser;   r#   r#   r$   Ú_eval_inverseÃ   ry   zMatrixExpr._eval_inversec                 C  r€   r   ©ÚDeterminantr;   r#   r#   r$   Ú_eval_determinantÆ   ry   zMatrixExpr._eval_determinantc                 C  r€   r   ©rq   r;   r#   r#   r$   Ú_eval_transposeÉ   ry   zMatrixExpr._eval_transposec                 C  s   d S r   r#   r;   r#   r#   r$   Ú_eval_traceÌ   rK   zMatrixExpr._eval_tracec                 C  s
   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.
        ©r]   )r<   Úexpr#   r#   r$   Ú_eval_powerÏ   s   
zMatrixExpr._eval_powerc                   s2   | j r| S ddlm‰ | j‡ ‡fdd„| jD ƒŽ S )Nr   )Úsimplifyc                   s   g | ]
}ˆ|fi ˆ ¤Ž‘qS r#   r#   )Ú.0Úx©r5   r�   r#   r$   Ú
<listcomp>Û   ó    z-MatrixExpr._eval_simplify.<locals>.<listcomp>)Úis_AtomÚsympy.simplifyr�   r!   r4   ©r<   r5   r#   r�   r$   Ú_eval_simplifyÖ   s   zMatrixExpr._eval_simplifyc                 C  ó   ddl m} || ƒS rn   )rp   ro   rr   r#   r#   r$   Ú_eval_adjointÝ   ó   zMatrixExpr._eval_adjointc                 C  s   t  | ||¡S r   )r   Ú_eval_derivative_n_times)r<   r�   Únr#   r#   r$   rš   á   ó   z#MatrixExpr._eval_derivative_n_timesc                   s    |   |¡rtƒ  |¡S t| jŽ S r   )ÚhasÚsuperÚ_eval_derivativeÚ
ZeroMatrixr=   ©r<   r�   ©Ú	__class__r#   r$   rŸ   ä   s   

zMatrixExpr._eval_derivativec                 C  s0   |j  o
t|d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)Úis_Floatr
   Ú
ValueErrorÚformat)r3   ÚdimÚokr#   r#   r$   Ú
_check_dimì   s   
ÿþÿzMatrixExpr._check_dimc                 K  s   t d| jj ƒ‚)NzIndexing not implemented for %s)r:   r£   Ú__name__©r<   ÚiÚjr5   r#   r#   r$   Ú_entry÷   s   
ÿzMatrixExpr._entryc                 C  r€   r   )r   r;   r#   r#   r$   r   û   ry   zMatrixExpr.adjointc                 C  s
   t j| fS )z1Efficiently extract the coefficient of a product.)r   ÚOne)r<   Úrationalr#   r#   r$   Úas_coeff_Mulþ   rf   zMatrixExpr.as_coeff_Mulc                 C  r€   r   )r   r;   r#   r#   r$   r     ry   zMatrixExpr.conjugatec                 C  r—   )Nr   ©Ú	transpose)Z$sympy.matrices.expressions.transposerµ   )r<   rµ   r#   r#   r$   rµ     r™   zMatrixExpr.transposec                 C  rt   )zMatrix transpositionr´   r;   r#   r#   r$   ÚT	  r_   zMatrixExpr.Tc                 C  s   | j du r	tdƒ‚|  ¡ S )NFzInverse of non-square matrix)rm   r   rƒ   r;   r#   r#   r$   Úinverse  s   
zMatrixExpr.inversec                 C  rt   r   ©r·   r;   r#   r#   r$   Úinv  ry   zMatrixExpr.invc                 C  r—   )Nr   )Údet)Z&sympy.matrices.expressions.determinantrº   )r<   rº   r#   r#   r$   rº     r™   zMatrixExpr.detc                 C  rt   r   r¸   r;   r#   r#   r$   ÚI  s   zMatrixExpr.Ic                 C  s^   dd„ }||ƒo.||ƒo.| 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                 S  s   t | ttttfƒS r   )rl   Úintr   r   r   )Úidxr#   r#   r$   Úis_valid  ó   z(MatrixExpr.valid_index.<locals>.is_validF)re   ri   )r<   r®   r¯   r¾   r#   r#   r$   Úvalid_index  s   
þýýzMatrixExpr.valid_indexc                 C  sH  t |tƒst |tƒrddlm} || |dƒS t |tƒrZt|ƒdkrZ|\}}t |tƒs/t |tƒr;ddlm} || ||ƒS t|ƒt|ƒ}}|  ||¡dkrR|  ||¡S t	d||f ƒ‚t |t
tfƒr‘| j\}}t |tƒsqt	tdƒƒ‚t|ƒ}|| }|| }|  ||¡dkr‹|  ||¡S t	d| ƒ‚t |ttfƒržt	td	ƒƒ‚t	d
|  ƒ‚)Nr   )ÚMatrixSlice)r   Nrh   r{   FzInvalid indices (%s, %s)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])rl   ÚtupleÚsliceZ sympy.matrices.expressions.slicerÁ   Úlenr   rÀ   r°   Ú
IndexErrorr   r   r=   r   r   r   )r<   ÚkeyrÁ   r®   r¯   re   ri   r#   r#   r$   Ú__getitem__&  s2   

zMatrixExpr.__getitem__c                 C  s$   t | jttfƒ pt | jttfƒ S r   )rl   re   r   r   ri   r;   r#   r#   r$   Ú_is_shape_symbolicI  s   ÿzMatrixExpr._is_shape_symbolicc                   s8   ˆ   ¡ rtdƒ‚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

        z<Matrix with symbolic shape cannot be represented explicitly.r   ©ÚImmutableDenseMatrixc                   s&   g | ]‰ ‡ ‡fd d„t ˆjƒD ƒ‘qS )c                   s   g | ]}ˆˆ |f ‘qS r#   r#   ©rŽ   r¯   )r®   r<   r#   r$   r‘   j  s    ÿz5MatrixExpr.as_explicit.<locals>.<listcomp>.<listcomp>)Úrangeri   )rŽ   r;   )r®   r$   r‘   j  s
    þ
ÿz*MatrixExpr.as_explicit.<locals>.<listcomp>)rÈ   r§   Úsympy.matrices.immutablerÊ   rÌ   re   )r<   rÊ   r#   r;   r$   Úas_explicitM  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_mutabler;   r#   r#   r$   rÏ   n  s   zMatrixExpr.as_mutablec                 C  sf   |d ur
|s
t dƒ‚ddlm} || jtd�}t| jƒD ]}t| jƒD ]}| ||f |||f< q#q|S )Nz=Cannot implement copy=False when converting Matrix to ndarrayr   )Úempty)Údtype)Ú	TypeErrorÚnumpyrÐ   r=   ÚobjectrÌ   re   ri   )r<   rÑ   ÚcopyrÐ   r   r®   r¯   r#   r#   r$   Ú	__array__‡  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Î   ÚequalsrP   r#   r#   r$   r×   ‘  s   	zMatrixExpr.equalsc                 C  ó   | S r   r#   r;   r#   r#   r$   Úcanonicalizeœ  rK   zMatrixExpr.canonicalizec                 C  s   t jt| ƒfS r   )r   r±   rD   r;   r#   r#   r$   Úas_coeff_mmulŸ  rœ   zMatrixExpr.as_coeff_mmulc                 C  sT   ddl m} ddlm} g }|dur| |¡ |dur | |¡ || |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
        >>> 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.from_indexed_to_arrayrÛ   Ú3sympy.tensor.array.expressions.from_array_to_matrixrÝ   Úappend)ÚexprÚfirst_indexÚ
last_indexÚ
dimensionsrÛ   rÝ   rÞ   Úarrr#   r#   r$   Úfrom_index_summation¢  s   *

zMatrixExpr.from_index_summationc                 C  s   ddl m} ||| ƒS )Nrh   )ÚElementwiseApplyFunction)Ú	applyfuncrè   )r<   r!   rè   r#   r#   r$   ré   Ö  s   
zMatrixExpr.applyfunc)r6   r7   )r6   rj   )T©F)r6   r,   )NNN)Ur¬   Ú
__module__Ú__qualname__Ú__doc__r+   Ú__annotations__Ú	_iterableÚ_op_priorityr-   r.   r/   Z
is_InverseZis_TransposeÚis_ZeroMatrixÚ	is_MatAddÚ	is_MatMulÚis_commutativeÚ	is_numberÚ	is_symbolÚ	is_scalarr   r0   r2   Úpropertyr=   rB   rE   rH   rJ   r)   ÚNotImplementedr   rQ   rM   rU   rT   rY   rZ   rW   r[   r^   r\   ra   r`   re   ri   rm   rs   rx   ru   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Ö   r×   rÙ   rÚ   Ústaticmethodrç   ré   Ú__classcell__r#   r#   r¢   r$   r*   %   sÖ   
 










#!
3r*   c                 C  ó   dS )NFr#   ©ÚlhsÚrhsr#   r#   r$   Ú_eval_is_eqÛ  r>   r  c                 C  s"   | j |j krdS | | jrdS d S )NFT)r=   rñ   rþ   r#   r#   r$   r  ß  s
   
ÿc                   s   ‡ fdd„}|S )Nc                   sî   t tttiˆ  }g }g }| jD ]}t|tƒr| |¡ q| |¡ q|s)ˆ  |¡S |r\ˆ t krNt	t
|ƒƒD ]}|| jsL||  ˆ  |¡¡||< g } nq5nˆ  |||Ž jdd�g ¡S |tkrh||Ž jdd�S |ˆ  |¡g|¢R Ž jdd�S )NF)rv   )r   rD   r	   rA   r4   rl   r*   rá   Ú
_from_argsrÌ   rÄ   r.   rY   rG   )râ   Z	mat_classZnonmatricesÚmatricesÚtermr®   ©r3   r#   r$   Ú_postprocessorç  s,   



ú€z)get_postprocessor.<locals>._postprocessorr#   )r3   r  r#   r  r$   Úget_postprocessoræ  s   #r  )r   r	   Fc           	      C  sh   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Ü   )	rl   r   Ú _matrix_derivative_old_algorithmZ3sympy.tensor.array.expressions.from_matrix_to_arrayr  Z4sympy.tensor.array.expressions.arrayexpr_derivativesr	  rà   rÝ   )	râ   r�   Zold_algorithmr  r	  rÝ   Z
array_exprZdiff_array_exprZdiff_matrix_exprr#   r#   r$   Ú_matrix_derivative  s   

r  c                   s¢   ddl m} |  |¡}dd„ |D ƒ}ddlm‰ ‡fdd„|D ƒ}dd„ ‰ ‡ fd	d
„‰‡fdd„|D ƒ}|d }dd„ ‰|dkrLt ‡fdd„|D ƒ¡S || |ƒS )Nr   )ÚArrayDerivativec                 S  s   g | ]}|  ¡ ‘qS r#   )Úbuild©rŽ   r®   r#   r#   r$   r‘   *  ó    z4_matrix_derivative_old_algorithm.<locals>.<listcomp>rÜ   c                   s   g | ]}‡ fd d„|D ƒ‘qS )c                   ó   g | ]}ˆ |ƒ‘qS r#   r#   rË   rÜ   r#   r$   r‘   .  r  z?_matrix_derivative_old_algorithm.<locals>.<listcomp>.<listcomp>r#   r  rÜ   r#   r$   r‘   .  s    c                 S  ó   t | tƒr| jS dS )N©rh   rh   ©rl   r*   r=   ©Úelemr#   r#   r$   Ú
_get_shape0  ó   
z4_matrix_derivative_old_algorithm.<locals>._get_shapec                   s   t ‡ fdd„| D ƒƒS )Nc                 3  s&   � | ]}ˆ |ƒD ]}|d vV  qqdS )©rh   NNr#   )rŽ   r®   r¯   ©r  r#   r$   Ú	<genexpr>6  s   €$ zE_matrix_derivative_old_algorithm.<locals>.get_rank.<locals>.<genexpr>)Úsum)Úpartsr  r#   r$   Úget_rank5  s   z2_matrix_derivative_old_algorithm.<locals>.get_rankc                   r  r#   r#   r  )r  r#   r$   r‘   8  r  c                 S  s�   t | ƒdkr
| d S | d d… \}}|jr|j}|tdƒkr!|}n|tdƒkr*|}n|| }t | ƒdkr6|S |jr=tdƒ‚|t | dd … ¡ S )Nrh   r   r{   Ú )rÄ   r-   r¶   ÚIdentityr§   r   Úfromiter)r  Úp1Úp2Zpbaser#   r#   r$   Úcontract_one_dims;  s   z;_matrix_derivative_old_algorithm.<locals>.contract_one_dimsr{   c                   r  r#   r#   r  )r#  r#   r$   r‘   P  r  )Ú$sympy.tensor.array.array_derivativesr  Ú_eval_derivative_matrix_linesrà   rÝ   r	   r   )râ   r�   r  Úlinesr  ÚranksÚrankr#   )r  r#  rÝ   r  r$   r
  &  s   

r
  c                   @  sl   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
edd	„ ƒZd
d„ Zedd„ ƒZdd„ ZdS )ÚMatrixElementc                 C  rb   rc   ©r4   r;   r#   r#   r$   Ú<lambda>V  ó   
 zMatrixElement.<lambda>c                 C  rb   rg   r*  r;   r#   r#   r$   r+  W  r,  c                 C  rb   rz   r*  r;   r#   r#   r$   r+  X  r,  Tc                 C  s    t t||fƒ\}}t|tƒrt|ƒ}n3t|tƒr)|jr$|jr$|||f S t|ƒ}nt|ƒ}t|jtƒs7t	dƒ‚t
|ddd„ ƒ||ƒsFtdƒ‚t | |||¡}|S )Nz2First argument of MatrixElement should be a matrixrÀ   c                 S  rý   rk   r#   )r›   Úmr#   r#   r$   r+  j  s    z'MatrixElement.__new__.<locals>.<lambda>zindices out of range)r1   r   rl   Ústrr   r   Ú
is_Integerr0   r   rÒ   ÚgetattrrÅ   r   r2   ©r3   Únamer›   r-  Úobjr#   r#   r$   r2   ]  s   



zMatrixElement.__new__c                 C  rb   rc   r*  r;   r#   r#   r$   Úsymbolo  rf   zMatrixElement.symbolc                   sD   ˆ   dd¡}|r‡ fdd„| jD ƒ}n| j}|d |d |d f S )Nrv   Tc                   s   g | ]
}|j d i ˆ ¤Ž‘qS )r#   )rG   )rŽ   r(   ©rw   r#   r$   r‘   v  r’   z&MatrixElement.doit.<locals>.<listcomp>r   rh   r{   )Úgetr4   )r<   rw   rv   r4   r#   r5  r$   rG   s  s
   zMatrixElement.doitc                 C  s   | j dd … S rg   r*  r;   r#   r#   r$   Úindices{  s   zMatrixElement.indicesc                 C  s4  t |tƒs| j |¡| j| jf S | jd }| jj\}}||jd krCt| jd |jd d|d fƒt| jd |jd d|d fƒ S t |t	ƒr�ddl
m} | jdd … \}}tdtd�\}}	|jd }
|
j\}}||||f |
||	f  |¡ ||	|f  |d|d f|	d|d fƒ S |  |jd ¡r—d S tjS )Nr   rh   r{   )ÚSumzz1, z2r  )rl   r)  ÚparentÚdiffr®   r¯   r4   r=   r   r‚   Úsympy.concrete.summationsr8  r   r   r�   r   ÚZero)r<   ÚvÚMr-  r›   r8  r®   r¯   Úi1Úi2ÚYÚr1Úr2r#   r#   r$   rŸ     s$   

ÿ


HzMatrixElement._eval_derivativeN)r¬   rë   rì   rø   r9  r®   r¯   Ú	_diff_wrtrö   rô   r2   r4  rG   r7  rŸ   r#   r#   r#   r$   r)  U  s    

r)  c                   @  sh   e Zd ZdZdZdZdZdd„ Zedd„ ƒZ	edd	„ ƒZ
d
d„ Zedd„ ƒZ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

    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                 C  sL   t |ƒt |ƒ}}|  |¡ |  |¡ t|tƒrt|ƒ}t | |||¡}|S r   )r   r«   rl   r.  r   r   r2   r1  r#   r#   r$   r2   ¯  s   


zMatrixSymbol.__new__c                 C  s   | j d | j d fS )Nrh   r{   r*  r;   r#   r#   r$   r=   º  s   zMatrixSymbol.shapec                 C  s   | j d jS rc   )r4   r2  r;   r#   r#   r$   r2  ¾  ó   zMatrixSymbol.namec                 K  s   t | ||ƒS r   )r)  r­   r#   r#   r$   r°   Â  s   zMatrixSymbol._entryc                 C  s   | hS r   r#   r;   r#   r#   r$   Úfree_symbolsÅ  s   zMatrixSymbol.free_symbolsc                 K  rØ   r   r#   r•   r#   r#   r$   r–   É  rK   zMatrixSymbol._eval_simplifyc                 C  s   t | jd | jd ƒS ©Nr   rh   )r    r=   r¡   r#   r#   r$   rŸ   Ì  s   zMatrixSymbol._eval_derivativec                 C  s¼   | |kr5| 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rCt| j d ƒntj}| j d dkrTt| j d ƒntj}t||gƒgS rH  )r=   r    r   r<  Ú_LeftRightArgsr  r±   )r<   r�   ÚfirstÚsecondr#   r#   r$   r%  Ð  s   **ÿ""ÿz*MatrixSymbol._eval_derivative_matrix_linesN)r¬   rë   rì   rí   rô   rö   rD  r2   rø   r=   r2  r°   rG  r–   rŸ   r%  r#   r#   r#   r$   rE  š  s     


rE  c                 C  s   dd„ | j D ƒS )Nc                 S  s   g | ]}|j r|‘qS r#   )r-   )rŽ   Úsymr#   r#   r$   r‘   à  ó    z"matrix_symbols.<locals>.<listcomp>)rG  ©râ   r#   r#   r$   Úmatrix_symbolsß  rI   rO  c                   @  sž   e Zd ZdZejfdd„Zedd„ ƒZej	dd„ ƒZedd„ ƒZ
e
j	d	d„ ƒZ
d
d„ Z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dS )rI  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                 C  s<   t |ƒ| _| j| _d| _d| _| j| _d| _d| _|| _d S rH  )	ÚlistÚ_linesÚ_first_pointer_parentÚ_first_pointer_indexÚ_first_line_indexÚ_second_pointer_parentÚ_second_pointer_indexÚ_second_line_indexÚhigher)r<   r&  rX  r#   r#   r$   Ú__init__ñ  s   

z_LeftRightArgs.__init__c                 C  ó   | j | j S r   ©rR  rS  r;   r#   r#   r$   Úfirst_pointerû  rF  z_LeftRightArgs.first_pointerc                 C  ó   || j | j< d S r   r[  ©r<   Úvaluer#   r#   r$   r\  ÿ  ó   c                 C  rZ  r   ©rU  rV  r;   r#   r#   r$   Úsecond_pointer  rF  z_LeftRightArgs.second_pointerc                 C  r]  r   ra  r^  r#   r#   r$   rb    r`  c                   s"   ‡ fdd„ˆ j D ƒ}d|ˆ jf S )Nc                   ó   g | ]}ˆ   |¡‘qS r#   ©Ú_buildr  r;   r#   r$   r‘     rM  z+_LeftRightArgs.__repr__.<locals>.<listcomp>z#_LeftRightArgs(lines=%s, higher=%s))rQ  rX  )r<   Zbuiltr#   r;   r$   Ú__repr__  s
   þz_LeftRightArgs.__repr__c                 C  s:   | j | j| _| _ | j| j| _| _| j| j| _| _| S r   )rU  rR  rV  rS  rW  rT  r;   r#   r#   r$   rµ     s   z_LeftRightArgs.transposec                 C  sN   t | tƒr	|  ¡ S t | tƒr%t| ƒdkr| d S | d dd„ | d D ƒŽ S | S )Nrh   r   c                 S  s   g | ]}t  |¡‘qS r#   )rI  re  r  r#   r#   r$   r‘      rM  z)_LeftRightArgs._build.<locals>.<listcomp>)rl   r   r  rP  rÄ   rN  r#   r#   r$   re    s   

z_LeftRightArgs._buildc                   s<   ‡ fdd„ˆ j D ƒ}ˆ jdkr|ˆ  ˆ j¡g7 }t|ƒ}|S )Nc                   rc  r#   rd  r  r;   r#   r$   r‘   %  rM  z(_LeftRightArgs.build.<locals>.<listcomp>rh   )rQ  rX  re  rP  )r<   Údatar#   r;   r$   r  $  s
   
z_LeftRightArgs.buildc                 C  s¤   | j dkr| jdkrtdƒ‚dd„ }|| j ƒd || jƒd krC|| jƒdkr/| j | jd  S || j ƒdkr?| j d | jj S tdƒ‚| j dkrO| j | jj S | jS )Nrh   z.higher dimensional array cannot be representedc                 S  r  )N)NNr  r  r#   r#   r$   r  /  r  z._LeftRightArgs.matrix_form.<locals>._get_shaper  )r   r   zincompatible shapes)rJ  rX  r§   rK  r¶   )r<   r  r#   r#   r$   Úmatrix_form+  s   
z_LeftRightArgs.matrix_formc                 C  sb   d}| j dkr|tdd„ | j jD ƒƒ7 }| jdkr&|tdd„ | jjD ƒƒ7 }| jdkr/|d7 }|S )zl
        Number of dimensions different from trivial (warning: not related to
        matrix rank).
        r   rh   c                 s  ó   � | ]}|d kV  qdS r  r#   r  r#   r#   r$   r  H  ó   € z&_LeftRightArgs.rank.<locals>.<genexpr>c                 s  ri  r  r#   r  r#   r#   r$   r  J  rj  r{   )rJ  r  r=   rK  rX  )r<   r(  r#   r#   r$   r(  A  s   


z_LeftRightArgs.rankc                 C  s:   ddl m} ddl m} t|t|||gƒdg|jd�}|S )Né   )ÚArrayTensorProduct)ÚArrayContraction)rh   r{   )Ú	validator)Z*tensor.array.expressions.array_expressionsrl  rm  r   Ú	_validate)r<   ÚpointerrL   rl  rm  Zsubexprr#   r#   r$   Ú_multiply_pointerO  s   þþø
ôz _LeftRightArgs._multiply_pointerc                 C  ó   |  j |9  _ d S r   )r\  rP   r#   r#   r$   Úappend_firstd  r¿   z_LeftRightArgs.append_firstc                 C  rr  r   )rb  rP   r#   r#   r$   Úappend_secondg  r¿   z_LeftRightArgs.append_secondN)r¬   rë   rì   rí   r   r±   rY  rø   r\  Úsetterrb  rf  rµ   rû   re  r  rh  r(  rq  rs  rt  r#   r#   r#   r$   rI  ã  s*    





rI  c                 C  s&   ddl m} t| tƒr| S || ggƒS )Nr   rÉ   )rÍ   rÊ   rl   r*   )r�   rÊ   r#   r#   r$   Ú_make_matrixk  s   
rv  rh   rC   r@   rŠ   r‡   r�   )r    r  r„   r   rê   )GÚ
__future__r   Ú	functoolsr   Ú
sympy.corer   r   r   r   r	   Zsympy.core.assumptionsr
   Zsympy.core.decoratorsr   Úsympy.core.exprr   r   Zsympy.core.logicr   Úsympy.core.symbolr   r   r   r   Úsympy.core.sympifyr   r   Úsympy.external.gmpyr   Úsympy.functionsr   r   Ú(sympy.functions.special.tensor_functionsr   Zsympy.matrices.exceptionsr   Zsympy.matrices.kindr   Úsympy.matrices.matrixbaser   Úsympy.multipledispatchr   Úsympy.utilities.miscr   r)   r*   r  r  Ú"_constructor_postprocessor_mappingr  r
  r)  rE  rO  rI  rv  ÚmatmulrD   ÚmataddrA   Úmatpowr]   rµ   rq   r·   r‚   Úspecialr    r  Údeterminantr…   r#   r#   r#   r$   Ú<module>   sZ    
   9

(þ
/EE 	