K i(DdZddlmZmZmZmZddlmZdZdZ dZ dZ y) z%Module for differentiation using CSE.)cseMatrix Derivative MatrixBase)iterablec  fd d t|}|Dcgc]\}}| ||f}}}|Dcgc](}|j|Dcgc] } || c}*}}}||fScc}}wcc}wcc}}w)a This function is designed to postprocess the output of a common subexpression elimination (CSE) operation. Specifically, it removes any CSE replacement symbols from the arguments of ``Derivative`` terms in the expression. This is necessary to ensure that the forward Jacobian function correctly handles derivative terms. Parameters ========== replacements : list of (Symbol, expression) pairs Replacement symbols and relative common subexpressions that have been replaced during a CSE operation. reduced_expressions : list of SymPy expressions The reduced expressions with all the replacements from the replacements list above. Returns ======= processed_replacements : list of (Symbol, expression) pairs Processed replacement list, in the same format of the ``replacements`` input list. processed_reduced : list of SymPy expressions Processed reduced list, in the same format of the ``reduced_expressions`` input list. ct|tr ||S|js|S|jDcgc] }|| }}|j|Scc}wN) isinstancerargsfunc)node repl_dictargnew_args replace_alltraverses ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/simplify/_cse_diff.pyrz-_remove_cse_from_derivative..traverse&s[ dJ 'tY/ /yyK8< BHS),BBtyy(##CsAc|} |j}|Dcic] }||vs|||}}|s |S|j|};cc}wr ) free_symbolsxreplace)rrresultrk symbols_dicts rrz0_remove_cse_from_derivative..replace_all.sY!..L5ATQ)^Ay|OTLT __\2F Ts ??)dict __class__) replacementsreduced_expressionsrrep_symsub_expprocessed_replacementsred_expexpprocessed_reducedrrs @@r_remove_cse_from_derivativer%s>$\"I!- GW (7I./ +  wG8C3GH "#4 44 HsA-A8 A3 A83A8c0t|dts td|djddk(s |djddk(s tdt |s tdt|ts t |}|jddk(s|jddk(s tdt ||\}}|rtt t|\}}nt gt g}}t|t|t|d}}}|djj||t|dD cic]3\}} t|D] \} } | j| x} dk7r|| f| "5c} } } }} |sg| ggfSt j||t|dD cgc]} | | jfc} D  cic]:\}\} }t|D]$\} }||vr| j|x} dk7r|| f| &<c}} }} }}t|}|Dcgc]}|j|z}}t jd|t|D cic]#\} } |dj| x} dk7rd| f| %c} } }t!d|D]}t jd|t!|dzD cic]-} || ||vr!||j|| x} dk7rd| f| /c} }t jd|t|D cic]#\} } ||j| x} dk7rd| f| %c} } }|j"j%r7|j'|j)|}t j*||}t j*||}|j'|j)| }|dj|g}|||fScc} } } }wcc} wcc}} }} }wcc}wcc} } wcc} wcc} } w)a| Core function to compute the Jacobian of an input Matrix of expressions through forward accumulation. Takes directly the output of a CSE operation (replacements and reduced_expr), and an iterable of variables (wrt) with respect to which to differentiate the reduced expression and returns the reduced Jacobian matrix and the ``replacements`` list. The function also returns a list of precomputed free symbols for each subexpression, which are useful in the substitution process. Parameters ========== replacements : list of (Symbol, expression) pairs Replacement symbols and relative common subexpressions that have been replaced during a CSE operation. reduced_expr : list of SymPy expressions The reduced expressions with all the replacements from the replacements list above. wrt : iterable Iterable of expressions with respect to which to compute the Jacobian matrix. Returns ======= replacements : list of (Symbol, expression) pairs Replacement symbols and relative common subexpressions that have been replaced during a CSE operation. Compared to the input replacement list, the output one doesn't contain replacement symbols inside ``Derivative``'s arguments. jacobian : list of SymPy expressions The list only contains one element, which is the Jacobian matrix with elements in reduced form (replacement symbols are present). precomputed_fs: list List of sets, which store the free symbols present in each sub-expression. Useful in the substitution process. rz``expr`` must be of matrix typez)``expr`` must be a row or a column matrixz(``wrt`` must be an iterable of variablesz(``wrt`` must be a row or a column matrix)r r TypeErrorshaperrr%mapziplenrfrom_dok enumeratediffrsetrange_repnnzmultiplyaddvstack)r reduced_exprwrtrsub_exprl_subl_wrtl_redirjw diff_valuef1fssf2 rep_sym_setprecomputed_fsc_matrix bi_matrix ai_matrix ci_matrixjacobians r_forward_jacobian_cserMEs@X l1oz 29:: O ! !! $ )\!_-B-B1-E-JCDD C=BCC Z (Sk IIaLA 1!2BCC!<\>   !**8488CI}}Xy9H}}Xy9H:"{{8$((,HQ))(34H > 11_ )W G M %\ %Rs*8O,-O4?O9(P(P2P (PcJt|\}}t|||\}}}|||fS)au Function to compute the Jacobian of an input Matrix of expressions through forward accumulation. Takes a sympy Matrix of expressions (expr) as input and an iterable of variables (wrt) with respect to which to compute the Jacobian matrix. The matrix is returned in reduced form (containing replacement symbols) along with the ``replacements`` list. The function also returns a list of precomputed free symbols for each subexpression, which are useful in the substitution process. Parameters ========== expr : Matrix The vector to be differentiated. wrt : iterable The vector with respect to which to perform the differentiation. Can be a matrix or an iterable of variables. Returns ======= replacements : list of (Symbol, expression) pairs Replacement symbols and relative common subexpressions that have been replaced during a CSE operation. The output replacement list doesn't contain replacement symbols inside ``Derivative``'s arguments. jacobian : list of SymPy expressions The list only contains one element, which is the Jacobian matrix with elements in reduced form (replacement symbols are present). precomputed_fs: list List of sets, which store the free symbols present in each sub-expression. Useful in the substitution process. )rrM)exprr8rr7rLrGs r!_forward_jacobian_norm_in_cse_outrPs7L"%TL,-B 11cjt|\}}|rttt|\}}n tg}t |||\}}}|s|dSt |}t |D]5\} } | D cic]} | ||  } } ||| j| ||| <7|dj|Scc} w)a2 Function to compute the Jacobian of an input Matrix of expressions through forward accumulation. Takes a sympy Matrix of expressions (expr) as input and an iterable of variables (wrt) with respect to which to compute the Jacobian matrix. Explanation =========== Expressions often contain repeated subexpressions. Using a tree structure, these subexpressions are duplicated and differentiated multiple times, leading to inefficiency. Instead, if a data structure called a directed acyclic graph (DAG) is used then each of these repeated subexpressions will only exist a single time. This function uses a combination of representing the expression as a DAG and a forward accumulation algorithm (repeated application of the chain rule symbolically) to more efficiently calculate the Jacobian matrix of a target expression ``expr`` with respect to an expression or set of expressions ``wrt``. Note that this function is intended to improve performance when differentiating large expressions that contain many common subexpressions. For small and simple expressions it is likely less performant than using SymPy's standard differentiation functions and methods. Parameters ========== expr : Matrix The vector to be differentiated. wrt : iterable The vector with respect to which to do the differentiation. Can be a matrix or an iterable of variables. See Also ======== Direct Acyclic Graph : https://en.wikipedia.org/wiki/Directed_acyclic_graph r)rr*rr+rMrr.r) rOr8rr7r_rLrGsub_repr=ikr?sub_dicts r_forward_jacobianrWsV"%TL,l!34 *-B*E2+-.aAwqzM..%gaj1::8D E A;   ((/s. B0N) __doc__sympyrrrrsympy.utilities.iterablesrr%rMrPrWrQrr\s)+55.;5|t2n)2X;)rQ