K i_nddlmZddlmZmZmZddlmZddlm Z m Z ddl m Z ddl mZGdde Zy ) ) ExprBuilder)Function FunctionClassLambda)Dummy)sympify_sympify) MatrixExpr) MatrixBasecjeZdZdZdZedZedZedZdZ dZ dZ d Z d Z d Zy ) ElementwiseApplyFunctionag Apply function to a matrix elementwise without evaluating. Examples ======== It can be created by calling ``.applyfunc()`` on a matrix expression: >>> from sympy import MatrixSymbol >>> from sympy.matrices.expressions.applyfunc import ElementwiseApplyFunction >>> from sympy import exp >>> X = MatrixSymbol("X", 3, 3) >>> X.applyfunc(exp) Lambda(_d, exp(_d)).(X) Otherwise using the class constructor: >>> from sympy import eye >>> expr = ElementwiseApplyFunction(exp, eye(3)) >>> expr Lambda(_d, exp(_d)).(Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]])) >>> expr.doit() Matrix([ [E, 1, 1], [1, E, 1], [1, 1, E]]) Notice the difference with the real mathematical functions: >>> exp(eye(3)) Matrix([ [E, 0, 0], [0, E, 0], [0, 0, E]]) cnt|}|jstdj||jdk(r||}t |t r|St |ttfstd}t|||}t|}t |ttfstdj|d|jvrtdj|t |tstd}t|||}t j|||}|S)Nz{} must be a matrix instance.)rdz4{} should be compatible with SymPy function classes.rz({} should be able to accept 1 arguments.) r is_Matrix ValueErrorformatshape isinstancer rrrrnargs__new__)clsfunctionexprretrobjs j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/expressions/applyfunc.pyrz ElementwiseApplyFunction.__new__2s~~~<CCDIJ J :: 4.C#z* (]F$;<c Aa!-H8$(]F$;<F!# # HNN ":AA(KM M(F+c Aa!-H  h5 c |jdS)Nrargsselfs rrz!ElementwiseApplyFunction.functionSyy|rc |jdS)Nrr r"s rrzElementwiseApplyFunction.exprWr$rc.|jjSN)rrr"s rrzElementwiseApplyFunction.shape[syyrc |jdd}j|rjdi|j}t |t r|j rSt trjjSt tr*tfdjjdi|SS)NdeepTcDjj|Sr')r)xrr#s rz/ElementwiseApplyFunction.doit..ls$-- a(89r) getrdoitrrr is_identityr applyfuncr )r#hintsr)rrs` @rr/zElementwiseApplyFunction.doit_syy&yy 499%u%D== h 'H,@,@K dJ '>>$--0 0 6 7+9 d  Krc \|j|jj||fi|Sr')rr_entry)r#ijkwargss rr4zElementwiseApplyFunction._entryrs*}}-TYY--a=f=>>rctd}|j|}|j|}t|tr t |}|St ||}|S)Nr)rrdiffrrtyper)r#rrfdiffs r_get_function_fdiffz,ElementwiseApplyFunction._get_function_fdiffusT #J==# a  eX &KE 1e$E rcddlm}|jj|}|j }||t ||jS)Nr)hadamard_product)#sympy.matrices.expressions.hadamardr>rr9r<r )r#r+r>dexprr;s r_eval_derivativez)ElementwiseApplyFunction._eval_derivativesCH q!((*  $UDII 6  rc ddlm}ddlm}ddlm}ddlm}|j }|jj|}t||j}d|jvr|jddk(} |D]} | r"| j} ||jd} n!||jd} | j} t|t||| | g| rdndg|j } | g| _| j dj | _d| _| j dj | _d | _|S|D]} | j} | j} || jd}|| jd}t|t|| ||| |gd d g|j } | j dj | _d| _| j dj | _d | _| g| _|S)Nr)Identity)ArrayContraction) ArrayDiagonal)ArrayTensorProductr)r)r) validatorrG)rrGrH)rH)"sympy.matrices.expressions.specialrC0sympy.tensor.array.expressions.array_expressionsrDrErFr<r_eval_derivative_matrix_linesr r first_pointersecond_pointerr _validate_linesr!_first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_index)r#r+rCrDrErFr;lrewdiffiscolumnr5ptr1ptr2subexprnewptr1newptr2s rrOz6ElementwiseApplyFunction._eval_derivative_matrix_liness ?URW((* YY 4 4Q 7)%; <zz!})H ,??D#DJJqM2D#DJJqM2D++D%!#. & $ $#+ ,55$9*1,,q/*>*>')*&+2<<?+?+?(*+'7 ,h - %''"4::a=1"4::a=1%$#.!7FD'B"! /88 +2,,q/*>*>')*&+2<<?+?+?(*+'#9+ %, rcddlm}|j|j||jj S)Nr) Transpose)$sympy.matrices.expressions.transposerafuncrrr/)r#ras r_eval_transposez(ElementwiseApplyFunction._eval_transposes,Byy $))(<(A(A(CDDrN)__name__ __module__ __qualname____doc__rpropertyrrrr/r4r<rArOrdr-rrr r sk&PB&? @DErr N)sympy.core.exprrsympy.core.functionrrrsympy.core.symbolrsympy.core.sympifyrr sympy.matrices.expressionsr sympy.matrices.matrixbaser r r-rrrps*'AA#010CEzCEr