o
    oh<                     @   s  d dl mZmZ d dlmZ d dlmZmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ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  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, ddl-m.Z.m/Z/m0Z0m1Z1 G dd de'eZ2e
3ee2fe2 dd Z4dd Z5dd Z6dd Z7dd Z8d d! Z9d"d# Z:d$d% Z;d&d' Z<e<e5e7e;e9eed(d) e6e8ee:fZ=eee2ee= iZ>d*d+ Z?d,d- Z@e@ed< d.S )/    )askQ)handlers_dict)BasicsympifyS)mulMul)NumberIntegerDummyadjoint)rm_idunpacktypedflattenexhaustdo_onenew)NonInvertibleMatrixError)
MatrixBase)sympy_deprecation_warning)validate_matmul_integer   )Inverse)
MatrixExpr)MatPow	transpose)PermutationMatrix)
ZeroMatrixIdentityGenericIdentity	OneMatrixc                       s   e Zd ZdZdZe ZddddddZedd	 Z	e
d
d Zd$ddZdd Zdd Z fddZdd Zdd Zdd Zdd Zdd Zdd Zd%d d!Zd"d# Z  ZS )&MatMula  
    A product of matrix expressions

    Examples
    ========

    >>> from sympy import MatMul, MatrixSymbol
    >>> A = MatrixSymbol('A', 5, 4)
    >>> B = MatrixSymbol('B', 4, 3)
    >>> C = MatrixSymbol('C', 3, 6)
    >>> MatMul(A, B, C)
    A*B*C
    TFN)evaluatecheck_sympifyc                   s   |s j S tt fdd|}|rttt|}tj g|R  }| \}}|d ur3tdddd |dur;t	|  |s?|S |rF 
|S |S )Nc                    s
    j | kS N)identity)icls u/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/matrices/expressions/matmul.py<lambda>0      
 z MatMul.__new__.<locals>.<lambda>zaPassing check to MatMul is deprecated and the check argument will be removed in a future version.z1.11z,remove-check-argument-from-matrix-operations)deprecated_since_versionactive_deprecations_targetF)r+   listfiltermapr   r   __new__as_coeff_matricesr   validate	_evaluate)r.   r'   r(   r)   argsobjfactormatricesr/   r-   r0   r8   *   s(   
zMatMul.__new__c                 C   s   t |S r*   )canonicalize)r.   exprr/   r/   r0   r;   J   s   zMatMul._evaluatec                 C   s$   dd | j D }|d j|d jfS )Nc                 S      g | ]}|j r|qS r/   	is_Matrix.0argr/   r/   r0   
<listcomp>P       z MatMul.shape.<locals>.<listcomp>r   )r<   rowscols)selfr?   r/   r/   r0   shapeN   s   zMatMul.shapec                    sj  ddl m} ddlm  |  \}}t|dkr"||d ||f  S d gt|d  d gt|d  }|d< |d< dd }	|d|	 tdt|D ]}t|< qNt	|d d D ]\}}
|
j
d d ||< q_fd	d
t	|D }t|}t fdd|D rd}|||gtdd dgt| |R   }tdd |D sd}|r| S |S )Nr   )SumImmutableMatrixr   rJ   c                  s   s     d} 	 t d|  V  | d7 } q)Nr   Tzi_%ir   )counterr/   r/   r0   fb   s   zMatMul._entry.<locals>.fdummy_generatorc                    s,   g | ]\}}|j | |d    dqS )r   )rT   )_entryrF   r,   rG   )rT   indicesr/   r0   rH   o   s   , z!MatMul._entry.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r*   hasrF   vrP   r/   r0   	<genexpr>q       z MatMul._entry.<locals>.<genexpr>Tc                 s   s    | ]
}t |ttfV  qd S r*   )
isinstancer   intrZ   r/   r/   r0   r\   y   s    F)sympy.concrete.summationsrO   sympy.matrices.immutablerQ   r9   lengetrangenext	enumeraterN   r	   fromiteranyzipdoit)rM   r,   jexpandkwargsrO   coeffr?   
ind_rangesrS   rG   expr_in_sumresultr/   )rQ   rT   rW   r0   rU   S   s6   

zMatMul._entryc                 C   sB   dd | j D }dd | j D }t| }|jdu rtd||fS )Nc                 S      g | ]}|j s|qS r/   rC   rF   xr/   r/   r0   rH   ~   rI   z,MatMul.as_coeff_matrices.<locals>.<listcomp>c                 S   rB   r/   rC   rs   r/   r/   r0   rH      rI   Fz3noncommutative scalars in MatMul are not supported.)r<   r	   is_commutativeNotImplementedError)rM   scalarsr?   rn   r/   r/   r0   r9   }   s   
zMatMul.as_coeff_matricesc                 C   s   |   \}}|t| fS r*   )r9   r&   rM   rn   r?   r/   r/   r0   as_coeff_mmul   s   zMatMul.as_coeff_mmulc                    s    t t| jdi |}| |S Nr/   )superr&   rl   r;   )rM   rm   expanded	__class__r/   r0   rl      s   
zMatMul.expandc                 C   s4   |   \}}t|gdd |ddd D R   S )a  Transposition of matrix multiplication.

        Notes
        =====

        The following rules are applied.

        Transposition for matrix multiplied with another matrix:
        `\left(A B\right)^{T} = B^{T} A^{T}`

        Transposition for matrix multiplied with scalar:
        `\left(c A\right)^{T} = c A^{T}`

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Transpose
        c                 S      g | ]}t |qS r/   r   rE   r/   r/   r0   rH          z*MatMul._eval_transpose.<locals>.<listcomp>NrJ   )r9   r&   rj   rx   r/   r/   r0   _eval_transpose   s   zMatMul._eval_transposec                 C   s"   t dd | jd d d D   S )Nc                 S   r   r/   r   rE   r/   r/   r0   rH      r   z(MatMul._eval_adjoint.<locals>.<listcomp>rJ   )r&   r<   rj   rM   r/   r/   r0   _eval_adjoint   s   "zMatMul._eval_adjointc                 C   s4   |   \}}|dkrddlm} |||  S d S )Nr   )trace)ry   r   rj   )rM   r>   mmulr   r/   r/   r0   _eval_trace   s
   zMatMul._eval_tracec                 C   s<   ddl m} |  \}}t| }|| j ttt||  S )Nr   )Determinant)&sympy.matrices.expressions.determinantr   r9   only_squaresrK   r	   r5   r7   )rM   r   r>   r?   square_matricesr/   r/   r0   _eval_determinant   s   zMatMul._eval_determinantc                 C   s>   t dd | jD rtdd | jd d d D   S t| S )Nc                 s   s     | ]}t |tr|jV  qd S r*   )r^   r   	is_squarerE   r/   r/   r0   r\          z'MatMul._eval_inverse.<locals>.<genexpr>c                 s   s*    | ]}t |tr| n|d  V  qdS )rJ   N)r^   r   inverserE   r/   r/   r0   r\      s
    
rJ   )allr<   r&   rj   r   r   r/   r/   r0   _eval_inverse   s   zMatMul._eval_inversec                    s@     dd}|rt fdd| jD }n| j}tt| }|S )NdeepTc                 3   s     | ]}|j di  V  qd S rz   )rj   rE   hintsr/   r0   r\      r   zMatMul.doit.<locals>.<genexpr>)rc   tupler<   r@   r&   )rM   r   r   r<   rA   r/   r   r0   rj      s   zMatMul.doitc                    sj   dd  j D }dd  j D }|r1t|}t|}|r1|r1t||kr1td fdd|D  ||gS )Nc                 S   rB   r/   ru   rs   r/   r/   r0   rH      rI   z#MatMul.args_cnc.<locals>.<listcomp>c                 S   rr   r/   r   rs   r/   r/   r0   rH      rI   z"repeated commutative arguments: %sc                    s$   g | ]}t  j|d kr|qS r   )r5   r<   count)rF   cir   r/   r0   rH      s   $ )r<   rb   set
ValueError)rM   csetwarnrm   coeff_ccoeff_ncclenr/   r   r0   args_cnc   s   zMatMul.args_cncc                    s   ddl m  fddt| jD }g }|D ]U}| jd | }| j|d d  }|r0t|}nt| jd }|rHt fddt|D }nt| jd }| j| 	}	|	D ]}
|

| |
| ||
 qYq|S )Nr   	Transposec                    s   g | ]\}}|  r|qS r/   rX   rV   rt   r/   r0   rH          z8MatMul._eval_derivative_matrix_lines.<locals>.<listcomp>c                    s"   g | ]}|j r | n|qS r/   )rD   rj   )rF   r,   r   r/   r0   rH      s   " r   )r    r   rf   r<   r&   rg   r#   rN   reversed_eval_derivative_matrix_linesappend_firstappend_secondappend)rM   rt   
with_x_indlinesind	left_args
right_args	right_matleft_revdr,   r/   )r   rt   r0   r      s&   

z$MatMul._eval_derivative_matrix_lines)T)FT)__name__
__module____qualname____doc__	is_MatMulr$   r+   r8   classmethodr;   propertyrN   rU   r9   ry   rl   r   r   r   r   r   rj   r   r   __classcell__r/   r/   r}   r0   r&      s*     


*		
r&   c                  G   s(   | d dkr| dd  } t tg| R  S )Nr   r   )r   r&   r<   r/   r/   r0   newmul   s   r   c                 C   s>   t dd | jD rdd | jD }t|d j|d jS | S )Nc                 s   s"    | ]}|j p|jo|jV  qd S r*   )is_zerorD   is_ZeroMatrixrE   r/   r/   r0   r\      s    zany_zeros.<locals>.<genexpr>c                 S   rB   r/   rC   rE   r/   r/   r0   rH      rI   zany_zeros.<locals>.<listcomp>r   rJ   )rh   r<   r"   rK   rL   )r   r?   r/   r/   r0   	any_zeros   s   r   c                 C   s   t dd | jD s| S g }| jd }| jdd D ]}t|ttfr/t|ttfr/|| }q|| |}q|| t| S )a   Merge explicit MatrixBase arguments

    >>> from sympy import MatrixSymbol, Matrix, MatMul, pprint
    >>> from sympy.matrices.expressions.matmul import merge_explicit
    >>> A = MatrixSymbol('A', 2, 2)
    >>> B = Matrix([[1, 1], [1, 1]])
    >>> C = Matrix([[1, 2], [3, 4]])
    >>> X = MatMul(A, B, C)
    >>> pprint(X)
      [1  1] [1  2]
    A*[    ]*[    ]
      [1  1] [3  4]
    >>> pprint(merge_explicit(X))
      [4  6]
    A*[    ]
      [4  6]

    >>> X = MatMul(B, A, C)
    >>> pprint(X)
    [1  1]   [1  2]
    [    ]*A*[    ]
    [1  1]   [3  4]
    >>> pprint(merge_explicit(X))
    [1  1]   [1  2]
    [    ]*A*[    ]
    [1  1]   [3  4]
    c                 s   s    | ]}t |tV  qd S r*   )r^   r   rE   r/   r/   r0   r\     r]   z!merge_explicit.<locals>.<genexpr>r   r   N)rh   r<   r^   r   r
   r   r&   )matmulnewargslastrG   r/   r/   r0   merge_explicit   s   



r   c                 C   s:   |   \}}tdd |}||krt|g|jR  S | S )z Remove Identities from a MatMul

    This is a modified version of sympy.strategies.rm_id.
    This is necesssary because MatMul may contain both MatrixExprs and Exprs
    as args.

    See Also
    ========

    sympy.strategies.rm_id
    c                 S   s
   | j du S )NT)is_Identityr   r/   r/   r0   r1   6  r2   zremove_ids.<locals>.<lambda>)ry   r   r   r<   )r   r>   r   rq   r/   r/   r0   
remove_ids'  s
   r   c                 C   s(   |   \}}|dkrt|g|R  S | S Nr   )r9   r   )r   r>   r?   r/   r/   r0   factor_in_front<  s   r   c              	   C   s  |   \}}|d g}tdt|D ]}|d }|| }t|trJt|jtrJ|jj}t|}t||| d krJ|d|  t	|j
d g }qt|trt|jtr|jj}	t|	}t|	||||  krt	|j
d }
|
|d< t||| D ]}|
||< q{q|jdks|jdkr|| qt|tr|j\}}n|tj}}t|tr|j\}}n|tj}}||kr|| }t||jdd|d< qt|tsz| }W n ty   d}Y nw |dur||kr|| }t||jdd|d< q|| qt|g|R  S )a  Combine consecutive powers with the same base into one, e.g.
    $$A \times A^2 \Rightarrow A^3$$

    This also cancels out the possible matrix inverses using the
    knowledgebase of :class:`~.Inverse`, e.g.,
    $$ Y \times X \times X^{-1} \Rightarrow Y $$
    r   r   rJ   NF)r   )r9   rd   rb   r^   r   rG   r&   r<   r5   r#   rN   r   r   r   r   Onerj   r   r   r   r   )r   r>   r<   new_argsr,   ABBargslAargsr+   rk   A_baseA_expB_baseB_expnew_exp
B_base_invr/   r/   r0   combine_powersB  sZ   





r   c           	      C   s   | j }t|}|dk r| S |d g}td|D ],}|d }|| }t|tr>t|tr>|j d }|j d }t|| |d< q|| qt| S )zGRefine products of permutation matrices as the products of cycles.
       r   r   rJ   )r<   rb   rd   r^   r!   r   r&   )	r   r<   r   rq   r,   r   r   cycle_1cycle_2r/   r/   r0   combine_permutations  s    



r   c                 C   s   |   \}}|d g}|dd D ]/}|d }t|tr!t|ts'|| q|  |t|jd |jd  ||jd 9 }qt|g|R  S )zj
    Combine products of OneMatrix

    e.g. OneMatrix(2, 3) * OneMatrix(3, 4) -> 3 * OneMatrix(2, 4)
    r   r   NrJ   )r9   r^   r%   r   poprN   r   )r   r>   r<   r   r   r   r/   r/   r0   combine_one_matrices  s   

r   c                    s   | j  t dkr?ddlm}  d jr' d jr'| fdd d j D  S  d jr? d jr?| fdd d j D  S | S )zr
    Simplify MatMul expressions but distributing
    rational term to MatMul.

    e.g. 2*(A+B) -> 2*A + 2*B
    r   r   )MatAddr   c                    s   g | ]}t | d   qS r   r&   rj   rF   matr   r/   r0   rH     r   z$distribute_monom.<locals>.<listcomp>c                    s   g | ]}t  d  | qS )r   r   r   r   r/   r0   rH     r   )r<   rb   mataddr   	is_MatAddis_Rational)r   r   r/   r   r0   distribute_monom  s   r   c                 C   s   | dkS r   r/   r   r/   r/   r0   r1     s    r1   c                  G   sp   | d j | d jkrtdg }d}t| D ]\}}|j| | j kr5|t| ||d     |d }q|S )z'factor matrices only if they are squarer   rJ   z!Invalid matrices being multipliedr   )rK   rL   RuntimeErrorrf   r   r&   rj   )r?   outstartr,   Mr/   r/   r0   r     s   r   c                 C   s   g }g }| j D ]}|jr|| q|| q|d }|dd D ]4}||jkr9tt||r9t|jd }q"||	 krOtt
||rOt|jd }q"|| |}q"|| t| S )z
    >>> from sympy import MatrixSymbol, Q, assuming, refine
    >>> X = MatrixSymbol('X', 2, 2)
    >>> expr = X * X.T
    >>> print(expr)
    X*X.T
    >>> with assuming(Q.orthogonal(X)):
    ...     print(refine(expr))
    I
    r   r   N)r<   rD   r   Tr   r   
orthogonalr#   rN   	conjugateunitaryr&   )rA   assumptionsr   exprargsr<   r   rG   r/   r/   r0   refine_MatMul  s    


r   N)Asympy.assumptions.askr   r   sympy.assumptions.refiner   
sympy.corer   r   r   sympy.core.mulr   r	   sympy.core.numbersr
   r   sympy.core.symbolr   sympy.functionsr   sympy.strategiesr   r   r   r   r   r   r   sympy.matrices.exceptionsr   sympy.matrices.matrixbaser   sympy.utilities.exceptionsr   !sympy.matrices.expressions._shaper   r:   r   r   matexprr   matpowr   r    permutationr!   specialr"   r#   r$   r%   r&   register_handlerclassr   r   r   r   r   r   r   r   r   rulesr@   r   r   r/   r/   r/   r0   <module>   sJ    $ V*?"