K i`6ddlmZddlmZmZddlmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZmZdd lmZmZmZmZmZmZdd lmZd ZGd deZdZ dZ!GddeZ"y))Counter)Mulsympify)Add) ExprBuilder)default_sort_key)log) MatrixExpr)validate_matadd_integer) ZeroMatrix OneMatrix)unpackflatten conditionexhaustrm_idsort)sympy_deprecation_warningcn|s tdt|dk(r|dSt|jS)au Return the elementwise (aka Hadamard) product of matrices. Examples ======== >>> from sympy import hadamard_product, MatrixSymbol >>> A = MatrixSymbol('A', 2, 3) >>> B = MatrixSymbol('B', 2, 3) >>> hadamard_product(A) A >>> hadamard_product(A, B) HadamardProduct(A, B) >>> hadamard_product(A, B)[0, 1] A[0, 1]*B[0, 1] z#Empty Hadamard product is undefinedr) TypeErrorlenHadamardProductdoit)matricess i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/expressions/hadamard.pyhadamard_productrs=" =>> 8}{ H % * * ,,c\eZdZdZdZdddfd ZedZdZd Z d Z d Z d Z xZ S) ra( Elementwise product of matrix expressions Examples ======== Hadamard product for matrix symbols: >>> from sympy import hadamard_product, HadamardProduct, MatrixSymbol >>> A = MatrixSymbol('A', 5, 5) >>> B = MatrixSymbol('B', 5, 5) >>> isinstance(hadamard_product(A, B), HadamardProduct) True Notes ===== This is a symbolic object that simply stores its argument without evaluating it. To actually compute the product, use the function ``hadamard_product()`` or ``HadamardProduct.doit`` TFN)evaluatecheckc&ttt|}t|dk(r t dt d|Ds t d|tddd|d urt|t|(|g|}|r|jd }|S) Nrz+HadamardProduct needs at least one argumentc3<K|]}t|tywN) isinstancer ).0args r z*HadamardProduct.__new__..Gs?3:c:.?z Mix of Matrix and Scalar symbolszjPassing check to HadamardProduct is deprecated and the check argument will be removed in a future version.z1.11z,remove-check-argument-from-matrix-operations)deprecated_since_versionactive_deprecations_targetF)deep) listmaprr ValueErrorallrrvalidatesuper__new__r)clsr r!argsobj __class__s rr3zHadamardProduct.__new__AsC&' t9>JK K?$??>? ?   %|)/+Y [   dOgoc)D) (((&C rc4|jdjSNr)r5shapeselfs rr:zHadamardProduct.shapeXsyy|!!!rc pt|jDcgc]}|j||fi|c}Scc}wr$)rr5_entry)r<ijkwargsr's rr>zHadamardProduct._entry\s1499ECZSZZ1//EFFEs3cXddlm}ttt ||j SNr) transpose)$sympy.matrices.expressions.transposerDrr-r.r5r<rDs r_eval_transposezHadamardProduct._eval_transpose_s BSDII%> ?@@rc |jfd|jD}ddlm}ddlm}|jDcgc]}t ||s|}}|rp|jDcgc] }||vs| }}|t|Dcgc]}tj|c}j|j}t|g|z}t|Scc}wcc}wcc}w)Nc3BK|]}|jdiyw)N)r)r&r?hintss rr(z'HadamardProduct.doit..ds>q6166?E?>sr) MatrixBase)ImmutableMatrix)funcr5sympy.matrices.matrixbaserLsympy.matrices.immutablerMr%ziprfromiterreshaper:r canonicalize) r<rKexprrLrMr?explicit remainderexpl_mats ` rrzHadamardProduct.doitcstyy>DII>?8<#yyF!Jq*,EAFF $(IICq(1BCIC),h($% Q(w $H#hZ)%;=DD!!GC(sCC& C0CC cg}t|j}tt|D]=}|d|||j |gz||dzdz}|j t |?tj|SNr) r-r5rangerdiffappendrrrR)r<xtermsr5r?factorss r_eval_derivativez HadamardProduct._eval_derivativess}DIIs4y! 5A2Ah$q',,q/!22T!A#$Z?G LL)73 4 5||E""rcddlm}ddlm}ddlm}t |j Dcgc]\}}|j|s|}}}g}|D]i} |j d| } |j | dzd} |j | j|} t| | z} ddg}t |Dcgc]\}}|j|dk7s|}}}| D]}|j|j}|j|j}t|t|t||g| t||ggg|}|j dj dj |_d|_|j dj dj |_d|_|g|_ |j'|l|Scc}}wcc}}w) Nr ArrayDiagonalArrayTensorProduct _make_matrixr)rri)0sympy.tensor.array.expressions.array_expressionsrdrf"sympy.matrices.expressions.matexprrh enumerater5has_eval_derivative_matrix_linesrr:_lines_first_line_index_second_line_indexr_first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_indexr])r<r^rdrfrhr?r' with_x_indlinesind left_args right_argsdhadamdiagonalr@el1l2subexprs rrqz-HadamardProduct._eval_derivative_matrix_lines{sRWC&/ &:IFAscggajaI I C $3I3q56*J #<*>q*A*F*F')*&+2<<?+?+?+B+G+G(*+'#9 Q-  @ EJQsF?F?0G G)__name__ __module__ __qualname____doc__is_HadamardProductr3propertyr:r>rGrrarq __classcell__r7s@rrr)sI*%*$.""GA" #'rrctdt}t|}||}tdtd}||}d}td|}||}t |t rit |j}g}|jD]7\}}|dk(r|j||jt||9t |}tdtt}||}t|}|S)aCanonicalize the Hadamard product ``x`` with mathematical properties. Examples ======== >>> from sympy import MatrixSymbol, HadamardProduct >>> from sympy import OneMatrix, ZeroMatrix >>> from sympy.matrices.expressions.hadamard import canonicalize >>> from sympy import init_printing >>> init_printing(use_unicode=False) >>> A = MatrixSymbol('A', 2, 2) >>> B = MatrixSymbol('B', 2, 2) >>> C = MatrixSymbol('C', 2, 2) Hadamard product associativity: >>> X = HadamardProduct(A, HadamardProduct(B, C)) >>> X A.*(B.*C) >>> canonicalize(X) A.*B.*C Hadamard product commutativity: >>> X = HadamardProduct(A, B) >>> Y = HadamardProduct(B, A) >>> X A.*B >>> Y B.*A >>> canonicalize(X) A.*B >>> canonicalize(Y) A.*B Hadamard product identity: >>> X = HadamardProduct(A, OneMatrix(2, 2)) >>> X A.*1 >>> canonicalize(X) A Absorbing element of Hadamard product: >>> X = HadamardProduct(A, ZeroMatrix(2, 2)) >>> X A.*0 >>> canonicalize(X) 0 Rewriting to Hadamard Power >>> X = HadamardProduct(A, A, A) >>> X A.*A.*A >>> canonicalize(X) .3 A Notes ===== As the Hadamard product is associative, nested products can be flattened. The Hadamard product is commutative so that factors can be sorted for canonical form. A matrix of only ones is an identity for Hadamard product, so every matrices of only ones can be removed. Any zero matrix will make the whole product a zero matrix. Duplicate elements can be collected and rewritten as HadamardPower References ========== .. [1] https://en.wikipedia.org/wiki/Hadamard_product_(matrices) c"t|tSr$r%rr^s rzcanonicalize.. jO4rc"t|tSr$rrs rrzcanonicalize..rrc"t|tSr$)r%r rs rrzcanonicalize..s Jq)4rcbtd|jDrt|jS|S)Nc3<K|]}t|tywr$)r%r )r&cs rr(z/canonicalize..absorb.. s9Qz!Z(9r))anyr5r r:rs rabsorbzcanonicalize..absorb s( 9!&&9 9qww' 'Hrc"t|tSr$rrs rrzcanonicalize..rrrc"t|tSr$rrs rrzcanonicalize..#rr)rrrrr%rrr5itemsr] HadamardPowerrrr)r^rulefunrtallynew_argbaseexps rrTrTs f  4  D $-C AA  4 4 5 C AA  4  C AA!_% 9ID#axt$}T378  9 W %  4 ! " C AA q A Hrct|}t|}|dk(r|S|js||zS|jr tdt||S)Nrz#cannot raise expression to a matrix)r is_Matrixr/r)rrs rhadamard_powerr-sQ 4=D #,C ax >>Sy }}>?? s ##rcjeZdZdZfdZedZedZedZdZ dZ dZ d Z xZ S) ra Elementwise power of matrix expressions Parameters ========== base : scalar or matrix exp : scalar or matrix Notes ===== There are four definitions for the hadamard power which can be used. Let's consider `A, B` as `(m, n)` matrices, and `a, b` as scalars. Matrix raised to a scalar exponent: .. math:: A^{\circ b} = \begin{bmatrix} A_{0, 0}^b & A_{0, 1}^b & \cdots & A_{0, n-1}^b \\ A_{1, 0}^b & A_{1, 1}^b & \cdots & A_{1, n-1}^b \\ \vdots & \vdots & \ddots & \vdots \\ A_{m-1, 0}^b & A_{m-1, 1}^b & \cdots & A_{m-1, n-1}^b \end{bmatrix} Scalar raised to a matrix exponent: .. math:: a^{\circ B} = \begin{bmatrix} a^{B_{0, 0}} & a^{B_{0, 1}} & \cdots & a^{B_{0, n-1}} \\ a^{B_{1, 0}} & a^{B_{1, 1}} & \cdots & a^{B_{1, n-1}} \\ \vdots & \vdots & \ddots & \vdots \\ a^{B_{m-1, 0}} & a^{B_{m-1, 1}} & \cdots & a^{B_{m-1, n-1}} \end{bmatrix} Matrix raised to a matrix exponent: .. math:: A^{\circ B} = \begin{bmatrix} A_{0, 0}^{B_{0, 0}} & A_{0, 1}^{B_{0, 1}} & \cdots & A_{0, n-1}^{B_{0, n-1}} \\ A_{1, 0}^{B_{1, 0}} & A_{1, 1}^{B_{1, 1}} & \cdots & A_{1, n-1}^{B_{1, n-1}} \\ \vdots & \vdots & \ddots & \vdots \\ A_{m-1, 0}^{B_{m-1, 0}} & A_{m-1, 1}^{B_{m-1, 1}} & \cdots & A_{m-1, n-1}^{B_{m-1, n-1}} \end{bmatrix} Scalar raised to a scalar exponent: .. math:: a^{\circ b} = a^b ct|}t|}|jr|jr||zSt|trt|tr t ||t ||||}|Sr$)r is_scalarr%r r1r2r3)r4rrr6r7s rr3zHadamardPower.__new__rs`t}cl >>cmm3;  dJ 'JsJ,G T3 goc4- rc |jdSr9_argsr;s rrzHadamardPower.basezz!}rc |jdSrZrr;s rrzHadamardPower.exprrc|jjr|jjS|jjSr$)rrr:rr;s rr:zHadamardPower.shapes+ 99  99?? "xx~~rc j|j}|j}|jr|j||fi|}n)|jr|}nt dj ||jr|j||fi|}||zS|jr|}||zSt dj |)Nz)The base {} must be a scalar or a matrix.z-The exponent {} must be a scalar or a matrix.)rrrr>rr/format)r<r?r@rArrabs rr>zHadamardPower._entrysyyhh >> Aq+F+A ^^A;BB4HJ J == 1a*6*AAv ]]A Av ?FFsKM MrcZddlm}t||j|jSrC)rErDrrrrFs rrGzHadamardPower._eval_transposesBYtyy1488<'C'CA $&'A ##$A yAH3 !4 1Vs $F+F)rrrrr3rrrr:r>rGrarqrrs@rrr9s^6p  ,=  rrN)# collectionsr sympy.corerrsympy.core.addrsympy.core.exprrsympy.core.sortingr&sympy.functions.elementary.exponentialr rnr !sympy.matrices.expressions._shaper r1"sympy.matrices.expressions.specialr r sympy.strategiesrrrrrrsympy.utilities.exceptionsrrrrTrrrJrrrs^#'/69QDA-0yjy|C L $WJWr