
    wgJU                     2   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) d Z*dddZ+ G d de      Z, G d de,      Z-y)    )Add)Tuple)Expr)AppliedUndefUndefinedFunction)Mul)Equality
Relational)S)SymbolDummy)sympify)piecewise_fold	Piecewise)BooleanFunction)
MatrixBase)IntervalSet)Range)Idx)flatten)siftis_sequence)sympy_deprecation_warningc                f   t        |      }t        |t              rit        |d|i\  }}|rt	        d |D              st        dddd       |j                  }|j                  }t         | |g|i | | |g|i |      S |t        j                  u rt        j                  S |rWt        |d|i\  }}t        |      D ]:  \  }	}
t        |
      dk(  s|j                  |
d	   |
d
         }t        |
dd
  ||	<   < nC|j                  }t        |      dk7  rt        d|z        |D cg c]  }t        |       c}d}}| t!        |      k(  r3t#        |j$                        |z   }|j&                  }| t!        |      k(  r3i }|D 	ch c]  }	|	d	   	 }}	|j)                  t*              D ]  } |j,                  | rt/               ||<   ! |j1                  |      }t3        |      }|j1                  |j5                         D ci c]  \  }}||
 c}}      }|||fS c c}w c c}	w c c}}w )zReturn either a special return value or the tuple,
    (function, limits, orientation). This code is common to
    both ExprWithLimits and AddWithLimits.discretec              3   8   K   | ]  }t        |      d k(    yw   Nlen).0limits     d/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/concrete/expr_with_limits.py	<genexpr>z_common_new.<locals>.<genexpr>    s     C5s5zQC   ax  
                Creating a indefinite integral with an Eq() argument is
                deprecated.

                This is because indefinite integrals do not preserve equality
                due to the arbitrary constants. If you want an equality of
                indefinite integrals, use Eq(Integral(a, x), Integral(b, x))
                explicitly.
                z1.6z!deprecated-indefinite-integral-eq   )deprecated_since_versionactive_deprecations_target
stacklevel   r   N   zspecify dummy variables for %s)r   
isinstancer	   _process_limitsallr   lhsrhsr   NaN	enumerater!   subsr   free_symbols
ValueErrortypelistlimitsfunctionatomsr   hasr   xreplacer   items)clsr;   r   symbolsassumptionsr:   orientationr1   r2   ilifreesrepssymbols_of_integrationpkvs                     r$   _common_newrM      sH    x H(H% .wJJ3CFCC% */+N llllC9'9[9C9'9[9; 	; 155uu-wJJv& 	,EAr2w!|#==A27!2cr7Oq		, $$t9>08;= =156AuQx6 h
hoo&/$$ h
 D,23qad33^^I& quu,-gDG   &Hh'H  4::<!@41a!Q$!@AHV[((- 7 4 "As   ,H#
H(	H-
N)r   c                 |   g }d}| d}n| rd}nd}|D ]   }t        |t        t        f      rC| rt        |      |j	                  t
              j                         }||j                         f}nt        |t
              st        |dd      rt        |t              rd|j                  |j                  |j                  t        |             nK|j                  t        ||j                  |j                               n|j                  t        |             
t        |      rt        |t              st!        |      dk(  r*t        |d   t              rt#        |      }t        |d   t$              r/| rt        |      |d   j&                  |d   j(                  f|dd nt        |d   t*              r| st        |      |d   j&                  }|d   j(                  }t-        |d   j.                        }	|	dk(  r||g|dd nf|t0        j2                  ur|d	   gd	||z
  |	z  |	|d	   z  |z   gz   }n8|d	   gd	t0        j4                  |	 |d	   z  |z   gz   }nt7        | rd
      d      t9        t;        |            }t        |d	   t
        t        f      st        |d	   dd      rN|d	   }
t!        |      dk(  r)|d   
|d   |dz  }|
g|dd D cg c]  }||	 c}z   }t!        |      }t        |
t              r|dk(  r|dk(  r|j                  t        |        |dk(  rt        |
t              rV|
j                  |
j                  }}	 |t=        |d   |k\        st?        d      	 |t=        |d   |k        st?        d      |j                  t        |        |dk(  s
|dk(  r!|d   |j                  t        |
             |dk(  r |j                  t        |
|d                t?        dtA        |      z         ||fS c c}w # t        $ r Y w xY w# t        $ r Y w xY w)a	  Process the list of symbols and convert them to canonical limits,
    storing them as Tuple(symbol, lower, upper). The orientation of
    the function is also returned when the upper limit is missing
    so (x, 1, None) becomes (x, None, 1) and the orientation is changed.
    In the case that a limit is specified as (symbol, Range), a list of
    length 4 may be returned if a change of variables is needed; the
    expression that should replace the symbol in the expression is
    the fourth element in the list.
    r-   Nzdiscrete must be True or Falsez%use Range, not Interval or Relationalz%use Interval or Relational, not Range	_diff_wrtF   r   zexpecting RangezRelational or single Intervalr   r,   r+   z%Summation will set Idx value too low.z&Summation will set Idx value too high.zInvalid limits given: %s)!r.   r
   r   	TypeErrorr<   r   popas_setgetattrr   lowerupperappendr   r   r   r!   r9   r   infsupr   absstepr   NegativeInfinityInfinityNotImplementedErrorr   r   boolr7   str)r   rA   r:   rC   err_msgVvariablelohidx	newsymbolrD   lenVs                r$   r/   r/   ]   s    FK2	99 SDa*o67((wwv**,H188:&A6"gae&D!S!77?aggoMM%(+MM%177AGG"<=eAh'q>*Q"41v{z!A$4GadH-'00aDHHadhh.AabE!e,#'001B1BQqTYYBQw!#R!"Q%7%77!"1!b2g]BqtGbL)I IA!"1!QZZ"QqTB)G GA .-5): :7: : 
#A!A$.'!A$U2SaD	q6Q;t|!(8#r)"!AB%&IQ1=q&IIA1v!)S1TQYqyeQi0 qy%i5 &/__iooB%#%>$qtrz:J*45\*]$]%#%>$qtrz:J*45]*^$^ eQi0 qyTQY1Q4<eI&67 eIqt&<= 3c'lBCCgSDj ;K 'J& $- % $%
 $- % $%s0   P$PP 8P/ 	P,+P,/	P;:P;c                       e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zed	        Zd
 Zd Zed        Zed        Zy)ExprWithLimits)is_commutativec                 L   ddl m} t        | |g|dt        | |      i|}t	        |t
              r|\  }}}n|S t        d |D              rt        d      t        j                  | fi |}|g}	|	j                  |       t        |	      |_        |j                  |_        |S )Nr   )Productr   c              3   D   K   | ]  }t        |      d k7  xs d|v   ywr   r    r"   ls     r$   r%   z)ExprWithLimits.__new__.<locals>.<genexpr>   s$     8As1v{'dai'8s    z:ExprWithLimits requires values for lower and upper bounds.)sympy.concrete.productsrm   rM   
issubclassr.   tupleanyr7   r   __new__extend_argsrk   )
r@   r;   rA   rB   rm   prer:   _objarglists
             r$   ru   zExprWithLimits.__new__   s    3#x >' >W->1<>c5!"%HfaJ 888YZZll3.+.*v'N	%44
    c                      | j                   d   S )a%  Return the function applied across limits.

        Examples
        ========

        >>> from sympy import Integral
        >>> from sympy.abc import x
        >>> Integral(x**2, (x,)).function
        x**2

        See Also
        ========

        limits, variables, free_symbols
        r   rw   selfs    r$   r;   zExprWithLimits.function   s    " zz!}r|   c                 .    | j                   j                  S N)r;   kindr   s    r$   r   zExprWithLimits.kind   s    }}!!!r|   c                      | j                   dd S )a+  Return the limits of expression.

        Examples
        ========

        >>> from sympy import Integral
        >>> from sympy.abc import x, i
        >>> Integral(x**i, (i, 1, 3)).limits
        ((i, 1, 3),)

        See Also
        ========

        function, variables, free_symbols
        r-   Nr~   r   s    r$   r:   zExprWithLimits.limits   s    " zz!"~r|   c                 F    | j                   D cg c]  }|d   	 c}S c c}w )a  Return a list of the limit variables.

        >>> from sympy import Sum
        >>> from sympy.abc import x, i
        >>> Sum(x**i, (i, 1, 3)).variables
        [i]

        See Also
        ========

        function, limits, free_symbols
        as_dummy : Rename dummy variables
        sympy.integrals.integrals.Integral.transform : Perform mapping on the dummy variable
        r   )r:   r   rp   s     r$   	variableszExprWithLimits.variables
  s       #kk*!***s   c                 d    | j                   D cg c]  }t        |      dk7  s|d    c}S c c}w )a  Return only variables that are dummy variables.

        Examples
        ========

        >>> from sympy import Integral
        >>> from sympy.abc import x, i, j, k
        >>> Integral(x**i, (i, 1, 3), (j, 2), k).bound_symbols
        [i, j]

        See Also
        ========

        function, limits, free_symbols
        as_dummy : Rename dummy variables
        sympy.integrals.integrals.Integral.transform : Perform mapping on the dummy variable
        r-   r   )r:   r!   r   s     r$   bound_symbolszExprWithLimits.bound_symbols  s*    & #kk9SVq[!999s   --c                 d   | j                   | j                  }}| j                  D ci c]+  }|d   |d   j                  |d   hk(  r|d   n	t               - }}|j	                  |      }|j                  }|D ]d  }||d      }t        |      dk(  r|j                  |       +||v r|j                  |       |dd D ]  }|j                  |j                          f |j                         D ci c]  \  }}||
 }}}|D 	ch c]  }	|j                  |	|	       c}	S c c}w c c}}w c c}	w )a4  
        This method returns the symbols in the object, excluding those
        that take on a specific value (i.e. the dummy symbols).

        Examples
        ========

        >>> from sympy import Sum
        >>> from sympy.abc import x, y
        >>> Sum(x, (x, y, 1)).free_symbols
        {y}
        r   r-   N)r;   r:   r6   r   r>   r!   addremoveupdater?   get)
r   r;   r:   rD   rH   isymsxabrL   rK   ry   s
             r$   r6   zExprWithLimits.free_symbols1  s2   "  ==$++& [[" !ad//AaD69aduwF " "$$T*%% 
	-CSVA3x1}		!EzQW -Q^^,-
	- "&.A1..(-.1A..!" /.s   0D"2D'D-c                     | j                    S )z7Return True if the Sum has no free symbols, else False.)r6   r   s    r$   	is_numberzExprWithLimits.is_numberW  s     $$$$r|   c                     | j                   D cg c]  }|d   |k7  r|n|||f }}| j                  } | j                  |g| S c c}w )Nr   )r:   r;   func)r   xabrD   r:   	integrands          r$   _eval_intervalzExprWithLimits._eval_interval\  sS    ;?;;Ga!	11ay0GGMM	tyy,V,, Hs   Ac                 N   | j                   t        | j                        }}|j                          t	        |t
              r&|j                  j                  | j                        rjd}t        |      D ]  \  }}dt        |      k(  r||d   k(  r|j                  r|f}n||f}t        |d   g|dd D cg c]  }|j                  ||       c} ||<   t        |d   j                  j                  |j                              dk7  sd} n t	        |t        t        f      rt        | j                         j                  t        |j#                  t
                          }	t        | j                         j                  t        |j$                              }
|	j'                  |
      st)        d      d}|rr|j+                  ||      }n_t        |      D ]Q  \  }}t        |      dk(  st        |d   g|dd D cg c]  }|j                  ||       c} ||<   ||d   k(  sQ n t        |      D ];  \  }}t        |      dk(  s|d   |d   z
  j,                  s+t        |d         ||<   = |j                           | j.                  |g| S c c}w c c}w )	a  
        Perform substitutions over non-dummy variables
        of an expression with limits.  Also, can be used
        to specify point-evaluation of an abstract antiderivative.

        Examples
        ========

        >>> from sympy import Sum, oo
        >>> from sympy.abc import s, n
        >>> Sum(1/n**s, (n, 1, oo)).subs(s, 2)
        Sum(n**(-2), (n, 1, oo))

        >>> from sympy import Integral
        >>> from sympy.abc import x, a
        >>> Integral(a*x**2, x).subs(x, 4)
        Integral(a*x**2, (x, 4))

        See Also
        ========

        variables : Lists the integration variables
        transform : Perform mapping on the dummy variable for integrals
        change_index : Perform mapping on the sum and product dummy variables

        Tr-   r   NFz-substitution cannot create dummy dependenciesr   rP   )r;   r9   r:   reverser.   r   r6   intersectionr4   r!   rO   r   _subsr   r   setr   r<   argsissubsetr7   r5   is_zeror   )r   oldnewr   r:   sub_into_funcrD   r   rp   sy2sy1s              r$   
_eval_subszExprWithLimits._eval_subsa  sd   6 }}d4;;&7f 	#v&  --d.?.?@ M#F+ 	3C=SCF]}}"f"Cj!#a&Ps12w+O!AGGC,=+OPq	s1v**778H8HIJaO$)M	 #.?@A$..)66s399V;L7MN$..)66s388}E||C($GI I $yyc* $F+ 3s8q= %c!f T3qr7/SaS0A/S TF1Ic!f}	  ' 	,FAs3x1}#a&3q6/!:!:!#a&Oq		,
 	tyy'''9 ,P" 0Ts   6JJ"c                     d}| j                   D ]b  }t        |      dk(  r@t        d |dd D              r yt        d |dd D              s>|d   j                  Nd}Q|d   j                  ad}d |ryy)	a#  
        Returns True if the limits are known to be finite, either by the
        explicit bounds, assumptions on the bounds, or assumptions on the
        variables.  False if known to be infinite, based on the bounds.
        None if not enough information is available to determine.

        Examples
        ========

        >>> from sympy import Sum, Integral, Product, oo, Symbol
        >>> x = Symbol('x')
        >>> Sum(x, (x, 1, 8)).has_finite_limits
        True

        >>> Integral(x, (x, 1, oo)).has_finite_limits
        False

        >>> M = Symbol('M')
        >>> Sum(x, (x, 1, M)).has_finite_limits

        >>> N = Symbol('N', integer=True)
        >>> Product(x, (x, 1, N)).has_finite_limits
        True

        See Also
        ========

        has_reversed_limits

        Fr   c              3   4   K   | ]  }|j                     y wr   is_infinitero   s     r$   r%   z3ExprWithLimits.has_finite_limits.<locals>.<genexpr>  s     6q}}6   r-   Nc              3   8   K   | ]  }|j                   d u   y wr   r   ro   s     r$   r%   z3ExprWithLimits.has_finite_limits.<locals>.<genexpr>  s     @1$.@r&   r   T)r:   r!   rt   r   )r   ret_Nonelims      r$   has_finite_limitsz ExprWithLimits.has_finite_limits  s    B ;; 	$C3x1}6c!"g66 @AB@@1v))1#'q6%%-#H	$ r|   c                     d}| j                   D ]:  }t        |      dk(  r)|\  }}}||z
  }|j                  r y|j                  r7d}: y |ryy)a  
        Returns True if the limits are known to be in reversed order, either
        by the explicit bounds, assumptions on the bounds, or assumptions on the
        variables.  False if known to be in normal order, based on the bounds.
        None if not enough information is available to determine.

        Examples
        ========

        >>> from sympy import Sum, Integral, Product, oo, Symbol
        >>> x = Symbol('x')
        >>> Sum(x, (x, 8, 1)).has_reversed_limits
        True

        >>> Sum(x, (x, 1, oo)).has_reversed_limits
        False

        >>> M = Symbol('M')
        >>> Integral(x, (x, 1, M)).has_reversed_limits

        >>> N = Symbol('N', integer=True, positive=True)
        >>> Sum(x, (x, 1, N)).has_reversed_limits
        False

        >>> Product(x, (x, 2, N)).has_reversed_limits

        >>> Product(x, (x, 2, N)).subs(N, N + 2).has_reversed_limits
        False

        See Also
        ========

        sympy.concrete.expr_with_intlimits.ExprWithIntLimits.has_empty_sequence

        Fr   TN)r:   r!   is_extended_negativeis_extended_nonnegative)r   r   r   varr   r   difs          r$   has_reversed_limitsz"ExprWithLimits.has_reversed_limits  sg    J ;; 	C3x1}	Q!e++00#H	 r|   N)__name__
__module____qualname__	__slots__ru   propertyr;   r   r:   r   r   r6   r   r   r   r   r    r|   r$   rj   rj      s    #I,  $ " "  $ + +" : :( #/ #/J % %-
M(^ 0 0d 3 3r|   rj   c                   8    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
y	)
AddWithLimitszZRepresents unevaluated oriented additions.
        Parent class for Integral and Sum.
    r   c                    ddl m} t        | |g|dt        | |      i|}t	        |t
              r|\  }}}n|S t        j                  | fi |}||z  g}	|	j                  |       t        |	      |_	        |j                  |_
        |S )Nr   )Sumr   )sympy.concrete.summationsr   rM   rr   r.   rs   r   ru   rv   rw   rk   )
r@   r;   rA   rB   r   rx   r:   rC   rz   r{   s
             r$   ru   zAddWithLimits.__new__!  s    1#x :' :S):-8:c5!,/)HfkJll3.+.x'(v'N	%44
r|   c                     t        d t        | j                        D              r5 | j                  | j                  j                         g| j                   S y )Nc              3   4   K   | ]  }|j                     y wr   is_realr"   r   s     r$   r%   z.AddWithLimits._eval_adjoint.<locals>.<genexpr>3       7Qqyy7r   )r0   r   r:   r   r;   adjointr   s    r$   _eval_adjointzAddWithLimits._eval_adjoint2  sC    7'$++"677499T]]224Ct{{CCr|   c                     t        d t        | j                        D              r5 | j                  | j                  j                         g| j                   S y )Nc              3   4   K   | ]  }|j                     y wr   r   r   s     r$   r%   z0AddWithLimits._eval_conjugate.<locals>.<genexpr>8  r   r   )r0   r   r:   r   r;   	conjugater   s    r$   _eval_conjugatezAddWithLimits._eval_conjugate7  C    7'$++"677499T]]446EEEr|   c                     t        d t        | j                        D              r5 | j                  | j                  j                         g| j                   S y )Nc              3   4   K   | ]  }|j                     y wr   r   r   s     r$   r%   z0AddWithLimits._eval_transpose.<locals>.<genexpr>=  r   r   )r0   r   r:   r   r;   	transposer   s    r$   _eval_transposezAddWithLimits._eval_transpose<  r   r|   c                     dt         j                        k(  ru  j                  j                  di |}|j                  rKt        |j                   fd      }t        |d      j                  t        |d    g j                   z  S  S   j                   j                  g j                  dd  j                         }|j                   j                  d         s1 j                  d j                  d   g      j                         |z  S t        |t              r- j                  | j                  d         j                         S  S )Nr-   c                 f    | j                   xr# t        j                        | j                  z   S r   )rk   r   r   r6   )wr   s    r$   <lambda>z,AddWithLimits._eval_factor.<locals>.<lambda>E  s.    13C3C 4A/!..@@ r|   TFr   r,   r   )r!   r:   r;   factoris_Mulr   r   r   r   r=   r   doitr.   )r   hintssummandouts   `   r$   _eval_factorzAddWithLimits._eval_factorA  s*   DKK  *dmm**3U3G~~7<< *A BCIytyyc%j1A ("[[(" " "   diiBAb0ABIIKG;;t~~b12yyT[[_$56;;=gEEGS)yy$++b/:AACCr|   c           	            j                   j                  di |}|j                  dd      }|j                  rW|s|j                  rI j
                  dur;t        |j                  D cg c]  }  j                  |g j                   ! c} S t        |t              r|j                   fd      S | j                   k7  r  j                  |g j                   S  S c c}w )NforceFc                 >     j                   | gj                   S r   )r   r:   )r   r   s    r$   r   z2AddWithLimits._eval_expand_basic.<locals>.<lambda>X  s    ytyy/IT[[/I r|   r   )r;   expandr   is_Addrk   r   r   r   r   r:   r.   r   	applyfunc)r   r   r   r   rD   s   `    r$   _eval_expand_basicz AddWithLimits._eval_expand_basicQ  s    &$--&&//		'5)NN)?)?''u4W\\J13t{{3JKK,$$%IJJ%499W3t{{33 Ks   +$C%N)r   r   r   __doc__r   ru   r   r   r   r   r   r   r|   r$   r   r     s,     I"


 
r|   r   ).sympy.core.addr   sympy.core.containersr   sympy.core.exprr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.relationalr	   r
   sympy.core.singletonr   sympy.core.symbolr   r   sympy.core.sympifyr   $sympy.functions.elementary.piecewiser   r   sympy.logic.boolalgr   sympy.matrices.matrixbaser   sympy.sets.setsr   r   sympy.sets.fancysetsr   sympy.tensor.indexedr   sympy.utilitiesr   sympy.utilities.iterablesr   r   sympy.utilities.exceptionsr   rM   r/   rj   r   r   r|   r$   <module>r      sm     '   ?  6 " + & / 0 ) & $ # 7 @D)N (, gTPT Pf
AN Ar|   