U
    ZÊ¹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                   s2   zt |ƒ}ˆ | |ƒW S  tk
r,   ˆ Y S X d S ©N)r   r   )ÚaÚb)ÚfuncÚretval© úa/var/www/html/bwave/python/venv/lib/python3.8/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                      sZ  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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dEdœ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dœ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| f|ž|ŽS r   )Úmapr   r   Ú__new__)ÚclsÚargsÚkwargsr#   r#   r$   r2   Q   s    
zMatrixExpr.__new__ztuple[Expr, Expr])Úreturnc                 C  s   t ‚d S r   ©ÚNotImplementedError©Úselfr#   r#   r$   ÚshapeW   s    zMatrixExpr.shapec                 C  s   t S r   ©ÚMatAddr9   r#   r#   r$   Ú_add_handler[   s    zMatrixExpr._add_handlerc                 C  s   t S r   ©ÚMatMulr9   r#   r#   r$   Ú_mul_handler_   s    zMatrixExpr._mul_handlerc                 C  s   t tj| ƒ ¡ S r   )r@   r   ÚNegativeOneÚdoitr9   r#   r#   r$   Ú__neg__c   s    zMatrixExpr.__neg__c                 C  s   t ‚d S r   r7   r9   r#   r#   r$   Ú__abs__f   s    zMatrixExpr.__abs__ÚotherÚ__radd__c                 C  s   t | |ƒ ¡ S r   ©r=   rC   ©r:   rF   r#   r#   r$   Ú__add__i   s    zMatrixExpr.__add__rJ   c                 C  s   t || ƒ ¡ S r   rH   rI   r#   r#   r$   rG   n   s    zMatrixExpr.__radd__Ú__rsub__c                 C  s   t | | ƒ ¡ S r   rH   rI   r#   r#   r$   Ú__sub__s   s    zMatrixExpr.__sub__rL   c                 C  s   t ||  ƒ ¡ S r   rH   rI   r#   r#   r$   rK   x   s    zMatrixExpr.__rsub__Ú__rmul__c                 C  s   t | |ƒ ¡ S r   ©r@   rC   rI   r#   r#   r$   Ú__mul__}   s    zMatrixExpr.__mul__c                 C  s   t | |ƒ ¡ S r   rN   rI   r#   r#   r$   Ú
__matmul__‚   s    zMatrixExpr.__matmul__rO   c                 C  s   t || ƒ ¡ S r   rN   rI   r#   r#   r$   rM   ‡   s    zMatrixExpr.__rmul__c                 C  s   t || ƒ ¡ S r   rN   rI   r#   r#   r$   Ú__rmatmul__Œ   s    zMatrixExpr.__rmatmul__Ú__rpow__c                 C  s   t | |ƒ ¡ S r   )ÚMatPowrC   rI   r#   r#   r$   Ú__pow__‘   s    zMatrixExpr.__pow__rT   c                 C  s   t dƒ‚d S )NzMatrix Power not definedr7   rI   r#   r#   r$   rR   –   s    zMatrixExpr.__rpow__Ú__rtruediv__c                 C  s   | |t j  S r   )r   rB   rI   r#   r#   r$   Ú__truediv__›   s    zMatrixExpr.__truediv__rV   c                 C  s
   t ƒ ‚d S r   r7   rI   r#   r#   r$   rU       s    zMatrixExpr.__rtruediv__c                 C  s
   | j d S ©Nr   ©r;   r9   r#   r#   r$   Úrows¦   s    zMatrixExpr.rowsc                 C  s
   | j d S ©Né   rX   r9   r#   r#   r$   Úcolsª   s    zMatrixExpr.colszbool | Nonec                 C  s6   | j \}}t|tƒr&t|tƒr&||kS ||kr2dS d S ©NT)r;   Ú
isinstancer   )r:   rY   r\   r#   r#   r$   Ú	is_square®   s    
zMatrixExpr.is_squarec                 C  s   ddl m} |t| ƒƒS ©Nr   )ÚAdjoint)Ú"sympy.matrices.expressions.adjointra   Ú	Transpose©r:   ra   r#   r#   r$   Ú_eval_conjugate·   s    zMatrixExpr._eval_conjugatec                 K  s   |   ¡ S r   )Ú_eval_as_real_imag)r:   ÚdeepÚhintsr#   r#   r$   Úas_real_imag»   s    zMatrixExpr.as_real_imagc                 C  s0   t j| |  ¡   }| |  ¡  dt j  }||fS ©Né   )r   ZHalfre   ZImaginaryUnit)r:   ÚrealZimr#   r#   r$   rf   ¾   s    zMatrixExpr._eval_as_real_imagc                 C  s   t | ƒS r   ©ÚInverser9   r#   r#   r$   Ú_eval_inverseÃ   s    zMatrixExpr._eval_inversec                 C  s   t | ƒS r   ©ÚDeterminantr9   r#   r#   r$   Ú_eval_determinantÆ   s    zMatrixExpr._eval_determinantc                 C  s   t | ƒS r   ©rc   r9   r#   r#   r$   Ú_eval_transposeÉ   s    zMatrixExpr._eval_transposec                 C  s   d S r   r#   r9   r#   r#   r$   Ú_eval_traceÌ   s    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.
        ©rS   )r:   Úexpr#   r#   r$   Ú_eval_powerÏ   s    zMatrixExpr._eval_powerc                   s6   | j r
| S ddlm‰ | j‡ ‡fdd„| jD ƒŽ S d S )Nr   )Úsimplifyc                   s   g | ]}ˆ|fˆ Ž‘qS r#   r#   )Ú.0Úx©r5   ry   r#   r$   Ú
<listcomp>Û   s     z-MatrixExpr._eval_simplify.<locals>.<listcomp>)Zis_AtomZsympy.simplifyry   r!   r4   ©r:   r5   r#   r|   r$   Ú_eval_simplifyÖ   s    zMatrixExpr._eval_simplifyc                 C  s   ddl m} || ƒS r`   )rb   ra   rd   r#   r#   r$   Ú_eval_adjointÝ   s    zMatrixExpr._eval_adjointc                 C  s   t  | ||¡S r   )r   Ú_eval_derivative_n_times)r:   r{   Únr#   r#   r$   r�   á   s    z#MatrixExpr._eval_derivative_n_timesc                   s$   |   |¡rtƒ  |¡S t| jŽ S d 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kr,td |¡ƒ‚dS )z2Helper function to check invalid matrix dimensionsT)ÚintegerZnonnegativeFz?The dimension specification {} should be a nonnegative integer.N)Zis_Floatr
   Ú
ValueErrorÚformat)r3   ÚdimÚokr#   r#   r$   Ú
_check_dimì   s    
  ÿÿÿzMatrixExpr._check_dimc                 K  s   t d| jj ƒ‚d S )NzIndexing not implemented for %s)r8   r‰   Ú__name__©r:   ÚiÚjr5   r#   r#   r$   Ú_entry÷   s    
ÿzMatrixExpr._entryc                 C  s   t | ƒS r   )r   r9   r#   r#   r$   r   û   s    zMatrixExpr.adjointc                 C  s
   t j| fS )z1Efficiently extract the coefficient of a product.)r   ÚOne)r:   Zrationalr#   r#   r$   Úas_coeff_Mulþ   s    zMatrixExpr.as_coeff_Mulc                 C  s   t | ƒS r   )r   r9   r#   r#   r$   r     s    zMatrixExpr.conjugatec                 C  s   ddl m} || ƒS )Nr   ©Ú	transpose)Z$sympy.matrices.expressions.transposer˜   )r:   r˜   r#   r#   r$   r˜     s    zMatrixExpr.transposec                 C  s   |   ¡ S )zMatrix transpositionr—   r9   r#   r#   r$   ÚT	  s    zMatrixExpr.Tc                 C  s   | j dkrtdƒ‚|  ¡ S )NFzInverse of non-square matrix)r_   r   ro   r9   r#   r#   r$   Úinverse  s    
zMatrixExpr.inversec                 C  s   |   ¡ S r   ©rš   r9   r#   r#   r$   Úinv  s    zMatrixExpr.invc                 C  s   ddl m} || ƒS )Nr   )Údet)Z&sympy.matrices.expressions.determinantr�   )r:   r�   r#   r#   r$   r�     s    zMatrixExpr.detc                 C  s   |   ¡ S r   r›   r9   r#   r#   r$   ÚI  s    zMatrixExpr.Ic                 C  s^   dd„ }||ƒo\||ƒo\| j d ks@|| 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   )r^   Úintr   r   r   )Úidxr#   r#   r$   Úis_valid  s    z(MatrixExpr.valid_index.<locals>.is_validF)rY   r\   )r:   r’   r“   r¡   r#   r#   r$   Úvalid_index  s    
þýýzMatrixExpr.valid_indexc                 C  sV  t |tƒs,t |tƒr,ddlm} || |dƒS t |tƒr¶t|ƒdkr¶|\}}t |tƒs^t |tƒrvddlm} || ||ƒS t|ƒt|ƒ }}|  ||¡dkr¤|  ||¡S t	d||f ƒ‚n�t |t
tfƒ�r*| j\}}t |tƒsæt	tdƒƒ‚t|ƒ}|| }|| }|  ||¡dk�r|  ||¡S t	d| ƒ‚nt |ttfƒ�rFt	td	ƒƒ‚t	d
|  ƒ‚d S )Nr   )ÚMatrixSlice)r   Nr[   rk   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])r^   ÚtupleÚsliceZ sympy.matrices.expressions.slicer£   Úlenr   r¢   r”   Ú
IndexErrorr   r   r;   r   r   r   )r:   Úkeyr£   r’   r“   rY   r\   r#   r#   r$   Ú__getitem__&  s2    

zMatrixExpr.__getitem__c                 C  s$   t | jttfƒ p"t | jttfƒ S r   )r^   rY   r   r   r\   r9   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#   ©rz   r“   )r’   r:   r#   r$   r}   j  s   ÿz5MatrixExpr.as_explicit.<locals>.<listcomp>.<listcomp>)Úranger\   )rz   r9   )r’   r$   r}   j  s   þÿz*MatrixExpr.as_explicit.<locals>.<listcomp>)rª   r‹   Úsympy.matrices.immutabler¬   r®   rY   )r:   r¬   r#   r9   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_mutabler9   r#   r#   r$   r±   n  s    zMatrixExpr.as_mutablec                 C  sf   |d k	r|st dƒ‚ddlm} || jtd�}t| jƒD ](}t| jƒD ]}| ||f |||f< qFq8|S )Nz=Cannot implement copy=False when converting Matrix to ndarrayr   )Úempty)Údtype)Ú	TypeErrorÚnumpyr²   r;   Úobjectr®   rY   r\   )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°   ÚequalsrI   r#   r#   r$   r¹   ‘  s    	zMatrixExpr.equalsc                 C  s   | S r   r#   r9   r#   r#   r$   Úcanonicalizeœ  s    zMatrixExpr.canonicalizec                 C  s   t jt| ƒfS r   )r   r•   r@   r9   r#   r#   r$   Úas_coeff_mmulŸ  s    zMatrixExpr.as_coeff_mmulc                 C  sT   ddl m} ddlm} g }|dk	r.| |¡ |dk	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)Z4sympy.tensor.array.expressions.from_indexed_to_arrayr¼   Ú3sympy.tensor.array.expressions.from_array_to_matrixr¾   Úappend)ÚexprZfirst_indexZ
last_indexÚ
dimensionsr¼   r¾   r¿   Zarrr#   r#   r$   Úfrom_index_summation¢  s    *

zMatrixExpr.from_index_summationc                 C  s   ddl m} ||| ƒS )Nr[   )ÚElementwiseApplyFunction)Ú	applyfuncrÅ   )r:   r!   rÅ   r#   r#   r$   rÆ   Ö  s    zMatrixExpr.applyfunc)T)F)NNN)Ur�   Ú
__module__Ú__qualname__Ú__doc__r+   Ú__annotations__Ú	_iterableZ_op_priorityr-   r.   r/   Z
is_InverseZis_TransposeÚis_ZeroMatrixZ	is_MatAddZ	is_MatMulÚis_commutativeZ	is_numberÚ	is_symbolZ	is_scalarr   r0   r2   Úpropertyr;   r>   rA   rD   rE   r)   ÚNotImplementedr   rJ   rG   rL   rK   rO   rP   rM   rQ   rT   rR   rV   rU   rY   r\   r_   re   ri   rf   ro   rr   rt   ru   rx   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  s   dS )NFr#   ©ÚlhsÚrhsr#   r#   r$   Ú_eval_is_eqÛ  s    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ƒr8| |¡ q| |¡ q|sRˆ  |¡S |r¸ˆ t krœt	t
|ƒƒD ].}|| jsj||  ˆ  |¡¡||< g } q¸qjnˆ  |||Ž jdd�g ¡S |tkrÐ||Ž jdd�S |ˆ  |¡f|žŽ jdd�S )NF)rg   )r   r@   r	   r=   r4   r^   r*   rÁ   Z
_from_argsr®   r¦   r.   rO   rC   )rÂ   Z	mat_classZnonmatricesZ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½   )	r^   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r˜t ‡fdd„|D ƒ¡S || |ƒS )Nr   )ÚArrayDerivativec                 S  s   g | ]}|  ¡ ‘qS r#   )Úbuild©rz   r’   r#   r#   r$   r}   *  s     z4_matrix_derivative_old_algorithm.<locals>.<listcomp>r½   c                   s   g | ]}‡ fd d„|D ƒ‘qS )c                   s   g | ]}ˆ |ƒ‘qS r#   r#   r­   r½   r#   r$   r}   .  s     z?_matrix_derivative_old_algorithm.<locals>.<listcomp>.<listcomp>r#   râ   r½   r#   r$   r}   .  s     c                 S  s   t | tƒr| jS dS )N©r[   r[   ©r^   r*   r;   ©Úelemr#   r#   r$   Ú
_get_shape0  s    
z4_matrix_derivative_old_algorithm.<locals>._get_shapec                   s   t ‡ fdd„| D ƒƒS )Nc                 3  s$   | ]}ˆ |ƒD ]}|d kV  qqdS )©r[   NNr#   )rz   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                   s   g | ]}ˆ |ƒ‘qS r#   r#   râ   )rí   r#   r$   r}   8  s     c                 S  s”   t | ƒdkr| d S | d d… \}}|jr0|j}|tdƒkrB|}n|tdƒkrT|}n|| }t | ƒdkrl|S |jrztdƒ‚|t | dd … ¡ S d S )Nr[   r   rk   Ú )r¦   r-   r™   ÚIdentityr‹   r   Úfromiter)rì   Úp1Zp2Zpbaser#   r#   r$   Úcontract_one_dims;  s    z;_matrix_derivative_old_algorithm.<locals>.contract_one_dimsrk   c                   s   g | ]}ˆ |ƒ‘qS r#   r#   râ   )rò   r#   r$   r}   P  s     )Z$sympy.tensor.array.array_derivativesrà   Ú_eval_derivative_matrix_linesrÀ   r¾   r	   rð   )rÂ   r{   rà   Úlinesrì   Z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  s
   | j d S rW   ©r4   r9   r#   r#   r$   Ú<lambda>V  ó    zMatrixElement.<lambda>c                 C  s
   | j d S rZ   r÷   r9   r#   r#   r$   rø   W  rù   c                 C  s
   | j d S rj   r÷   r9   r#   r#   r$   rø   X  rù   Tc                 C  s    t t||fƒ\}}t|tƒr&t|ƒ}nft|tƒrR|jrH|jrH|||f S t|ƒ}nt|ƒ}t|jtƒsnt	dƒ‚t
|ddd„ ƒ||ƒsŒtdƒ‚t | |||¡}|S )Nz2First argument of MatrixElement should be a matrixr¢   c                 S  s   dS r]   r#   )r‚   Úmr#   r#   r$   rø   j  rù   z'MatrixElement.__new__.<locals>.<lambda>zindices out of range)r1   r   r^   Ústrr   r   Z
is_Integerr0   r   r´   Úgetattrr§   r   r2   ©r3   Únamer‚   rú   Úobjr#   r#   r$   r2   ]  s    



zMatrixElement.__new__c                 C  s
   | j d S rW   r÷   r9   r#   r#   r$   Úsymbolo  s    zMatrixElement.symbolc                   sD   ˆ   dd¡}|r&‡ fdd„| jD ƒ}n| j}|d |d |d f S )Nrg   Tc                   s   g | ]}|j f ˆ Ž‘qS r#   )rC   )rz   r(   ©rh   r#   r$   r}   v  s     z&MatrixElement.doit.<locals>.<listcomp>r   r[   rk   )Úgetr4   )r:   rh   rg   r4   r#   r  r$   rC   s  s
    zMatrixElement.doitc                 C  s   | j dd … S rZ   r÷   r9   r#   r#   r$   Úindices{  s    zMatrixElement.indicesc                 C  s8  t |tƒs"| j |¡| j| jf S | jd }| jj\}}||jd kr†t| 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 ¡�r2d S tjS )Nr   r[   rk   )ÚSumzz1, z2rÙ   )r^   rö   ÚparentÚdiffr’   r“   r4   r;   r   rn   Zsympy.concrete.summationsr  r   r   rƒ   r   ÚZero)r:   ÚvÚMrú   r‚   r  r’   r“   Úi1Úi2ÚYÚr1Úr2r#   r#   r$   r…     s$    

ÿ

HzMatrixElement._eval_derivativeN)r�   rÇ   rÈ   rÏ   r  r’   r“   Ú	_diff_wrtrÎ   rÍ   r2   r   rC   r  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ƒr8t|ƒ}t | |||¡}|S r   )r   r�   r^   rû   r   r   r2   rý   r#   r#   r$   r2   ¯  s    


zMatrixSymbol.__new__c                 C  s   | j d | j d fS )Nr[   rk   r÷   r9   r#   r#   r$   r;   º  s    zMatrixSymbol.shapec                 C  s   | j d jS rW   )r4   rþ   r9   r#   r#   r$   rþ   ¾  s    zMatrixSymbol.namec                 K  s   t | ||ƒS r   )rö   r‘   r#   r#   r$   r”   Â  s    zMatrixSymbol._entryc                 C  s   | hS r   r#   r9   r#   r#   r$   Úfree_symbolsÅ  s    zMatrixSymbol.free_symbolsc                 K  s   | S r   r#   r~   r#   r#   r$   r   É  s    zMatrixSymbol._eval_simplifyc                 C  s   t | jd | jd ƒS ©Nr   r[   )r†   r;   r‡   r#   r#   r$   r…   Ì  s    zMatrixSymbol._eval_derivativec                 C  sÀ   | |krj| j d dkr,t|j d | j d ƒntj}| j d dkrVt|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 d S r  )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Î   r  r2   rÏ   r;   rþ   r”   r  r   r…   ró   r#   r#   r#   r$   r  š  s   


r  c                 C  s   dd„ | j D ƒS )Nc                 S  s   g | ]}|j r|‘qS r#   )r-   )rz   Úsymr#   r#   r$   r}   à  s      z"matrix_symbols.<locals>.<listcomp>)r  ©rÂ   r#   r#   r$   Úmatrix_symbolsß  s    r  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 )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                 C  s<   t |ƒ| _| j| _d| _d| _| j| _d| _d| _|| _d S r  )	ÚlistÚ_linesÚ_first_pointer_parentÚ_first_pointer_indexÚ_first_line_indexÚ_second_pointer_parentÚ_second_pointer_indexÚ_second_line_indexÚhigher)r:   rô   r!  r#   r#   r$   Ú__init__ñ  s    
z_LeftRightArgs.__init__c                 C  s   | j | j S r   ©r  r  r9   r#   r#   r$   Úfirst_pointerû  s    z_LeftRightArgs.first_pointerc                 C  s   || j | j< d S r   r#  ©r:   Úvaluer#   r#   r$   r$  ÿ  s    c                 C  s   | j | j S r   ©r  r  r9   r#   r#   r$   Úsecond_pointer  s    z_LeftRightArgs.second_pointerc                 C  s   || j | j< d S r   r'  r%  r#   r#   r$   r(    s    c                   s"   ‡ fdd„ˆ j D ƒ}d|ˆ jf S )Nc                   s   g | ]}ˆ   |¡‘qS r#   ©Ú_buildrâ   r9   r#   r$   r}     s     z+_LeftRightArgs.__repr__.<locals>.<listcomp>z#_LeftRightArgs(lines=%s, higher=%s))r  r!  )r:   Zbuiltr#   r9   r$   Ú__repr__  s
    þz_LeftRightArgs.__repr__c                 C  s:   | j | j | _| _ | j| j | _| _| j| j | _| _| S r   )r  r  r  r  r   r  r9   r#   r#   r$   r˜     s    z_LeftRightArgs.transposec                 C  sT   t | tƒr|  ¡ S t | tƒrLt| ƒdkr0| d S | d dd„ | d D ƒŽ S n| S d S )Nr[   r   c                 S  s   g | ]}t  |¡‘qS r#   )r  r*  râ   r#   r#   r$   r}      s     z)_LeftRightArgs._build.<locals>.<listcomp>)r^   r   rá   r  r¦   r  r#   r#   r$   r*    s    

z_LeftRightArgs._buildc                   s<   ‡ fdd„ˆ j D ƒ}ˆ jdkr0|ˆ  ˆ j¡g7 }t|ƒ}|S )Nc                   s   g | ]}ˆ   |¡‘qS r#   r)  râ   r9   r#   r$   r}   %  s     z(_LeftRightArgs.build.<locals>.<listcomp>r[   )r  r!  r*  r  )r:   Údatar#   r9   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r†|| jƒdkr^| j | jd  S || j ƒdkr~| j d | jj S tdƒ‚| j dkrž| j | jj S | jS d S )Nr[   z.higher dimensional array cannot be representedc                 S  s   t | tƒr| jS dS )N)NNrä   rå   r#   r#   r$   rç   /  s    
z._LeftRightArgs.matrix_form.<locals>._get_shaperã   )r   r   zincompatible shapes)r  r!  r‹   r  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rL|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   r[   c                 s  s   | ]}|d kV  qdS rè   r#   râ   r#   r#   r$   rê   H  s     z&_LeftRightArgs.rank.<locals>.<genexpr>c                 s  s   | ]}|d kV  qdS rè   r#   râ   r#   r#   r$   rê   J  s     rk   )r  rë   r;   r  r!  )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)r[   rk   )Ú	validator)Z*tensor.array.expressions.array_expressionsr/  r0  r   Ú	_validate)r:   ZpointerrF   r/  r0  Zsubexprr#   r#   r$   Ú_multiply_pointerO  s    þþø
ôz _LeftRightArgs._multiply_pointerc                 C  s   |  j |9  _ d S r   )r$  rI   r#   r#   r$   Úappend_firstd  s    z_LeftRightArgs.append_firstc                 C  s   |  j |9  _ d S r   )r(  rI   r#   r#   r$   Úappend_secondg  s    z_LeftRightArgs.append_secondN)r�   rÇ   rÈ   rÉ   r   r•   r"  rÏ   r$  Úsetterr(  r+  r˜   rÒ   r*  rá   r-  rõ   r3  r4  r5  r#   r#   r#   r$   r  ã  s(   





r  c                 C  s&   ddl m} t| tƒr| S || ggƒS )Nr   r«   )r¯   r¬   r^   r*   )r{   r¬   r#   r#   r$   Ú_make_matrixk  s    
r7  r[   r?   r<   rv   rs   rm   )r†   rï   rp   )N)F)GÚ
__future__r   Ú	functoolsr   Z
sympy.corer   r   r   r   r	   Zsympy.core.assumptionsr
   Zsympy.core.decoratorsr   Zsympy.core.exprr   r   Zsympy.core.logicr   Zsympy.core.symbolr   r   r   r   Zsympy.core.sympifyr   r   Zsympy.external.gmpyr   Zsympy.functionsr   r   Z(sympy.functions.special.tensor_functionsr   Zsympy.matrices.exceptionsr   Zsympy.matrices.kindr   Zsympy.matrices.matrixbaser   Zsympy.multipledispatchr   Zsympy.utilities.miscr   r)   r*   r×   rÛ   Z"_constructor_postprocessor_mappingrß   rÞ   rö   r  r  r  r7  Úmatmulr@   Zmataddr=   ZmatpowrS   r˜   rc   rš   rn   Úspecialr†   rï   Zdeterminantrq   r#   r#   r#   r$   Ú<module>   sX   
   9

(þ
/EE 	