K i&dZddlmZddlmZmZddlmZmZm Z m Z ddl m Z ddl mZddlmZmZmZdd lmZdd lmZdd lmZe d Ze d Ze dZe dZe dZe dZe dZe ddZ y)z This module implements sums and products containing the Kronecker Delta function. References ========== .. [1] https://mathworld.wolfram.com/KroneckerDelta.html )product)Sum summation)AddMulSDummy)cacheit)default_sort_key)KroneckerDelta Piecewisepiecewise_fold)factor)Interval)solvecD|js|Sd}t}tjg}|jD]\}|F|j r:t ||r.d}|j}|jDcgc] }|d|z }}K|Dcgc]}||z }}^||Scc}wcc}w)zB Expand the first Add containing a simple KroneckerDelta. NTr)is_Mulrr Oneargsis_Add_has_simple_deltafunc)exprindexdeltartermshts Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/concrete/delta.py _expand_deltar!s ;; E D UUGE YY) =QXX*;Au*EE66D)*0AU1XaZ0E0"'(QQqS(E( ) <1(s -B Bc$t||sd|fSt|tr|tjfS|j s t dd}g}|jD]$}|t||r|}|j|&||j|fS)a Extract a simple KroneckerDelta from the expression. Explanation =========== Returns the tuple ``(delta, newexpr)`` where: - ``delta`` is a simple KroneckerDelta expression if one was found, or ``None`` if no simple KroneckerDelta expression was found. - ``newexpr`` is a Mul containing the remaining terms; ``expr`` is returned unchanged if no simple KroneckerDelta expression was found. Examples ======== >>> from sympy import KroneckerDelta >>> from sympy.concrete.delta import _extract_delta >>> from sympy.abc import x, y, i, j, k >>> _extract_delta(4*x*y*KroneckerDelta(i, j), i) (KroneckerDelta(i, j), 4*x*y) >>> _extract_delta(4*x*y*KroneckerDelta(i, j), k) (None, 4*x*y*KroneckerDelta(i, j)) See Also ======== sympy.functions.special.tensor_functions.KroneckerDelta deltaproduct deltasummation NzIncorrect expr) r isinstancer r rr ValueErrorr_is_simple_deltaappendr)rrrrargs r _extract_deltar()sD T5 )d|$'aee} ;;)** E Eyy =-c59E LL   9499e$ %%c|jtrCt|ry|js |jrt fd|j DSy)z Returns True if ``expr`` is an expression that contains a KroneckerDelta that is simple in the index ``index``, meaning that this KroneckerDelta is nonzero for a single value of the index ``index``. Tc36K|]}t|yw)N)r).0r'rs r z$_has_simple_delta..gsJ(e4JsF)hasr r%rranyr)rrs `r rr\sC xx D% ( ;;$++J JJ J r)ct|trT|j|rC|jd|jdz j |}|r|j dk(Sy)zu Returns True if ``delta`` is a KroneckerDelta and is nonzero for a single value of the index ``index``. rrF)r#r r.ras_polydegree)rrps r r%r%ksT %(UYYu-= ZZ]UZZ] * 3 3E : 88:? " r)c |jr0|jttt|j S|j s|Sg}g}|j D]R}t|tr/|j|j d|j dz B|j|T|s|St|d}t|dk(rtjSt|dk(rR||djDcgc]\}}t||c}}z }|j|}||k7r t |S|Scc}}w)z0 Evaluate products of KroneckerDelta's. rrTdict)rrlistmap_remove_multiple_deltarrr#r r&rlenr Zeroitems)reqsnewargsr'solnskvexpr2s r r9r9xs  {{tyy$s#9499EFGG ;; CGyy  c> * JJsxx{SXXa[0 1 NN3    #D !E 5zQvv UqU1X^^5EFTQN1a(FF 7# 5=)%0 0 K GsEc 4t|trq t|jd|jdz d}|rBt |dk(r4t |dj Dcgc]\}}t||fc}}S|S|Scc}}w#t$rY|SwxYw)zB Rewrite a KroneckerDelta's indices in its simplest form. rrTr5)r#r rrr:rr<NotImplementedError)rslnskeyvalues r _simplify_deltarHs $' 1 ! 44@DD Q.21gmmo? *U,c5\:?@@ K4K ?"   K s$AB 'B ;B B BBc  ddz dkdk(rtjS|jts t |S|j r_dg}t |jtD]'}t|dr||j|)|j| tdd}tdtrftdtrSt t! fd t#tdtddzDz}t)|St t%t dd|dz fj'd|zt d|dzdfz|ddft d z}t)|St+|d\}s6t-|d}||k7r t/t|St |St)|j'ddtddztjt3tdddz zzS#t0$rt|cYSwxYw) z Handle products containing a KroneckerDelta. See Also ======== deltasummation sympy.functions.special.tensor_functions.KroneckerDelta sympy.concrete.products.product rrTN)rFkprime)integerc 3K|]K}tdd|dz fjd|ztd|dzdfzMyw)rrrJN) deltaproductsubs)r,ikrlimitnewexprs r r-zdeltaproduct..su8HJ9EWuUVxY^_`YacehiciNj8k 58R(9)WuQxaq&BC9D8sAA) no_piecewise)r rr.r rrsortedrr rr&rr r#intrNsumrangedeltasummationrOr9r(r!rAssertionErrorrH) frQrr'r@result_grrRs ` @@r rNrNs qE!H !d*uu 55 q%  xx!&&&67 "C}!23a!A S!  " !&&%. (D ) eAh $E!Hc)B!'51C8NSTWX]^_X`TacfglmngorsgsctNu85F&f--"'51NWuQxq1q5&AB 58Q'(WuQxQa&ABCE!HeAh'.waA 5F&f--aq*HE1  !U1X & 6 .l1e455q%  !!&&q58"<^ERSHV[\]V^=_"_ ` onU1XuQx!|DEE FF " .#Au-- .sI I87I8c p|d|dz dkdk(rtjS|jts t ||S|d}t ||}|j r;t|j|jDcgc]}t|||c}St||\}}|9|j-|j\}} |d|z dkdk(r|d| z dk\dk(rd}|s t ||St|jd|jdz |} t| dk(rtjSt| dk7r t||S| d} |r|j!|| St#|j!|| t%|ddj'| ftjdfScc}w)aw Handle summations containing a KroneckerDelta. Explanation =========== The idea for summation is the following: - If we are dealing with a KroneckerDelta expression, i.e. KroneckerDelta(g(x), j), we try to simplify it. If we could simplify it, then we sum the resulting expression. We already know we can sum a simplified expression, because only simple KroneckerDelta expressions are involved. If we could not simplify it, there are two cases: 1) The expression is a simple expression: we return the summation, taking care if we are dealing with a Derivative or with a proper KroneckerDelta. 2) The expression is not simple (i.e. KroneckerDelta(cos(x))): we can do nothing at all. - If the expr is a multiplication expr having a KroneckerDelta term: First we expand it. If the expansion did work, then we try to sum the expansion. If not, we try to extract a simple KroneckerDelta term, then we have two cases: 1) We have a simple KroneckerDelta term, so we return the summation. 2) We did not have a simple term, but we do have an expression with simplified KroneckerDelta terms, so we sum this expression. Examples ======== >>> from sympy import oo, symbols >>> from sympy.abc import k >>> i, j = symbols('i, j', integer=True, finite=True) >>> from sympy.concrete.delta import deltasummation >>> from sympy import KroneckerDelta >>> deltasummation(KroneckerDelta(i, k), (k, -oo, oo)) 1 >>> deltasummation(KroneckerDelta(i, k), (k, 0, oo)) Piecewise((1, i >= 0), (0, True)) >>> deltasummation(KroneckerDelta(i, k), (k, 1, 3)) Piecewise((1, (i >= 1) & (i <= 3)), (0, True)) >>> deltasummation(k*KroneckerDelta(i, j)*KroneckerDelta(j, k), (k, -oo, oo)) j*KroneckerDelta(i, j) >>> deltasummation(j*KroneckerDelta(i, j), (j, -oo, oo)) i >>> deltasummation(i*KroneckerDelta(i, j), (i, -oo, oo)) j See Also ======== deltaproduct sympy.functions.special.tensor_functions.KroneckerDelta sympy.concrete.sums.summation rJrrT)r r;r.r rr!rrrrrXr( delta_rangerr:rrOrr as_relational) rZrQrSxr]rrrdinfdsupr?rGs r rXrXsH qE!H !d*vv 55 E"" aAaAxx AFFQVVL^Aul;L MO O!A&KE4  1 1 =&& d !HtOq T )uQx$!/C.LL E"" %**Q-%**Q-/ 3E 5zQvv Uq1e} !HEyyE""  1e ha 3AA%HI  +Ms?F3 N)F)!__doc__productsr summationsrr sympy.corerrr r sympy.core.cacher sympy.core.sortingr sympy.functionsr rrsympy.polys.polytoolsrsympy.sets.setsrsympy.solvers.solversrr!r(rr%r9rHrNrXr)r rps&))$/EE($'  & /& /&d          8     7F 7Ft f fr)