
    \h&                        S r SSKJr  SSKJrJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJr  SSKJrJrJr  SS	KJr  SS
KJr  SSKJr  \S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\SS j5       r g)z
This module implements sums and products containing the Kronecker Delta function.

References
==========

.. [1] https://mathworld.wolfram.com/KroneckerDelta.html

   )product)Sum	summation    )AddMulSDummy)cacheit)default_sort_key)KroneckerDelta	Piecewisepiecewise_fold)factor)Interval)solvec                 p   U R                   (       d  U $ Sn[        n[        R                  /nU R                   Hl  nUcR  UR
                  (       aA  [        XQ5      (       a1  SnUR                  nUR                   Vs/ s H
  odS   U-  PM     nnMX  U Vs/ s H  ofU-  PM	     nnMn     U" U6 $ s  snf s  snf )z:
Expand the first Add containing a simple KroneckerDelta.
NTr   )is_Mulr   r	   Oneargsis_Add_has_simple_deltafunc)exprindexdeltar   termshts          L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/concrete/delta.py_expand_deltar!      s    
 ;;EDUUGEYY=QXX*;A*E*EE66D)*0A1XaZE0E"'(%QqS%E(E  < 1(s   <B.B3c                 N   [        X5      (       d  SU 4$ [        U [        5      (       a  U [        R                  4$ U R
                  (       d  [        S5      eSn/ nU R                   H+  nUc  [        XA5      (       a  UnM  UR                  U5        M-     X R                  " U6 4$ )a8  
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
NzIncorrect expr)r   
isinstancer   r	   r   r   
ValueErrorr   _is_simple_deltaappendr   )r   r   r   r   args        r    _extract_deltar(   )   s    D T))d|$''aee};;)**EEyy=-c99ELL	 
 99e$%%    c                    ^ U R                  [        5      (       aS  [        U T5      (       a  gU R                  (       d  U R                  (       a  [        U4S jU R                   5       5      $ g)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``.
Tc              3   <   >#    U  H  n[        UT5      v   M     g 7f)N)r   ).0r'   r   s     r    	<genexpr>$_has_simple_delta.<locals>.<genexpr>g   s     J	(e44	s   F)hasr   r%   r   r   anyr   )r   r   s    `r    r   r   \   sI     xxD%((;;$++J		JJJr)   c                     [        U [        5      (       a^  U R                  U5      (       aH  U R                  S   U R                  S   -
  R	                  U5      nU(       a  UR                  5       S:H  $ g)zi
Returns True if ``delta`` is a KroneckerDelta and is nonzero for a single
value of the index ``index``.
r   r   F)r#   r   r/   r   as_polydegree)r   r   ps      r    r%   r%   k   sZ     %((UYYu-=-=ZZ]UZZ]*33E:88:?"r)   c           	         U R                   (       a0  U R                  " [        [        [        U R
                  5      5      6 $ U R                  (       d  U $ / n/ nU R
                   HY  n[        U[        5      (       a0  UR                  UR
                  S   UR
                  S   -
  5        MH  UR                  U5        M[     U(       d  U $ [        USS9n[        U5      S:X  a  [        R                  $ [        U5      S:X  aR  X$S   R                  5        VVs/ s H  u  pV[        XV5      PM     snn-  nU R                  " U6 nX:w  a  [	        U5      $ U $ s  snnf )z(
Evaluate products of KroneckerDelta's.
r   r   Tdict)r   r   listmap_remove_multiple_deltar   r   r#   r   r&   r   lenr	   Zeroitems)r   eqsnewargsr'   solnskvexpr2s           r    r:   r:   x   s   
 {{yy$s#9499EFGG;;
CGyyc>**JJsxx{SXXa[01NN3	 
 #D!E
5zQvv	Uq1X^^5EF5ETQN1(5EFF		7#=)%00K	 Gs   Ec           
      H   [        U [        5      (       au   [        U R                  S   U R                  S   -
  SS9nU(       aD  [	        U5      S:X  a5  [        US   R                  5        VVs/ s H  u  p#[        X#46 PM     snn6 $ U $ U $ s  snnf ! [         a     U $ f = f)z:
Rewrite a KroneckerDelta's indices in its simplest form.
r   r   Tr6   )r#   r   r   r   r;   r   r=   NotImplementedError)r   slnskeyvalues       r    _simplify_deltarI      s    
 $''	1		!44@DD	Q.21gmmo?.=
 ,c\:.=? @ @ K4K	?" 	K	s$   AB /B
B B 
B! B!c                   ^^^	 TS   TS   -
  S:  S:X  a  [         R                  $ U R                  [        5      (       d  [	        U T5      $ U R
                  (       Ga`  Sm/ n[        U R                  [        S9 H/  nTc  [        UTS   5      (       a  UmM  UR                  U5        M1     U R                  " U6 m	[        SSS9n[        TS   [        5      (       ab  [        TS   [        5      (       aJ  [        T	T5      [!        UUU	4S	 j[#        [        TS   5      [        TS   S-   5      5       5       5      -   nOs[        T	T5      [%        [        T	TS   TS   US-
  45      TR'                  TS   U5      -  [        T	TS   US-   TS   45      -  UTS   TS   4[        T	TS   5      S
9-   n[)        U5      $ [+        U TS   5      u  mnT(       d6  [-        U TS   5      nX:w  a   [/        [        UT5      5      $ [	        U T5      $ [)        U R'                  TS   TS   5      [        TS   TS   5      -  5      [         R                  [3        [        TS   TS   S-
  5      5      -  -   $ ! [0         a    [        UT5      s $ f = f)z
Handle products containing a KroneckerDelta.

See Also
========

deltasummation
sympy.functions.special.tensor_functions.KroneckerDelta
sympy.concrete.products.product
   r   r   TN)rG   kprime)integerc           	   3      >#    U  HL  n[        TTS    TS   US-
  45      TR                  TS    U5      -  [        TTS    US-   TS   45      -  v   MN     g7f)r   r   rK   N)deltaproductsubs)r,   ikr   limitnewexprs     r    r-   deltaproduct.<locals>.<genexpr>   st      8Nu 9EWuUVxY^_`YacehiciNj8k

58R(9)WuQxaq&BC9DNus   AA)no_piecewise)r	   r   r/   r   r   r   sortedr   r   r   r&   r   r
   r#   intrO   sumrangedeltasummationrP   r:   r(   r!   r   AssertionErrorrI   )
frR   r   r'   rA   result_gr   rS   s
    `      @@r    rO   rO      s    
qE!H	!d*uu55  q%  xxx!&&&67C}!23a!A!AS!	 8
 &&%.(D)eAh$$E!Hc)B)B!'51C 8NSTWX]^_X`TacfglmngorsgsctNu8 5 F
 "'51NWuQxq1q5&AB

58Q'(WuQxQa&ABC E!HeAh'.waA5 F &f--aq*HE1!U1X&6.l1e455 q%  !!&&q58"<^ERSHV[\]V^=_"_`	onU1XuQx!|DEEF F	 " .#Au--.s   ,I1 1J
	J
c                    US   US   -
  S:  S:X  a  [         R                  $ U R                  [        5      (       d  [	        X5      $ US   n[        X5      nUR                  (       a=  [        UR                  " UR                   Vs/ s H  n[        XQU5      PM     sn6 5      $ [        XC5      u  pgUb;  UR                  b.  UR                  u  pUS   U-
  S:*  S:X  a  US   U	-
  S:  S:X  a  SnU(       d  [	        X5      $ [        UR                  S   UR                  S   -
  U5      n
[        U
5      S:X  a  [         R                  $ [        U
5      S:w  a  [        X5      $ U
S   nU(       a  UR!                  X;5      $ [#        UR!                  X;5      [%        USS 6 R'                  U5      4[         R                  S45      $ s  snf )a  
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
rK   r   r   T   )r	   r<   r/   r   r   r!   r   r   r   r   rZ   r(   delta_ranger   r;   r   rP   r   r   as_relational)r\   rR   rU   xr_   r   r   r   dinfdsupr@   rH   s               r    rZ   rZ      s   H 
qE!H	!d*vv55  ""aAaAxxFFQVVLV^Al;VLMO 	O !&KE 1 1 =&&
!HtOq T)uQx$!/C.LL""%**Q-%**Q-/3E
5zQvv	Uq1}!HEyy""	1	ha
3AA%HI	
 + Ms   GN)F)!__doc__productsr   
summationsr   r   
sympy.corer   r   r	   r
   sympy.core.cacher   sympy.core.sortingr   sympy.functionsr   r   r   sympy.polys.polytoolsr   sympy.sets.setsr   sympy.solvers.solversr   r!   r(   r   r%   r:   rI   rO   rZ    r)   r    <module>rr      s     & ) ) $ / E E ( $ ' 	 	& 	/& 	/&d 	 	 		 		 	 	8 	 	 	7F 	7Ft 	f 	fr)   