
    shH                        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 SKJr  S S	KJr  S S
KJrJr  S SKJr  S SKJrJr  S SKJr  S SKJrJr   " S S\5      rS r g)    )annotations   )ExprWithIntLimits)Sum	summation)_dummy_with_inherited_properties_concrete)Expr)factor_terms)
Derivative)Mul)S)DummySymbol)RisingFactorial)explog)KroneckerDelta)quorootsc                      \ rS rSr% SrSrS\S'   S rS r\	S 5       r
\
rS	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)Product   a  
Represents unevaluated products.

Explanation
===========

``Product`` represents a finite or infinite product, with the first
argument being the general form of terms in the series, and the second
argument being ``(dummy_variable, start, end)``, with ``dummy_variable``
taking all integer values from ``start`` through ``end``. In accordance
with long-standing mathematical convention, the end term is included in
the product.

Finite products
===============

For finite products (and products with symbolic limits assumed to be finite)
we follow the analogue of the summation convention described by Karr [1],
especially definition 3 of section 1.4. The product:

.. math::

    \prod_{m \leq i < n} f(i)

has *the obvious meaning* for `m < n`, namely:

.. math::

    \prod_{m \leq i < n} f(i) = f(m) f(m+1) \cdot \ldots \cdot f(n-2) f(n-1)

with the upper limit value `f(n)` excluded. The product over an empty set is
one if and only if `m = n`:

.. math::

    \prod_{m \leq i < n} f(i) = 1  \quad \mathrm{for} \quad  m = n

Finally, for all other products over empty sets we assume the following
definition:

.. math::

    \prod_{m \leq i < n} f(i) = \frac{1}{\prod_{n \leq i < m} f(i)}  \quad \mathrm{for} \quad  m > n

It is important to note that above we define all products with the upper
limit being exclusive. This is in contrast to the usual mathematical notation,
but does not affect the product convention. Indeed we have:

.. math::

    \prod_{m \leq i < n} f(i) = \prod_{i = m}^{n - 1} f(i)

where the difference in notation is intentional to emphasize the meaning,
with limits typeset on the top being inclusive.

Examples
========

>>> from sympy.abc import a, b, i, k, m, n, x
>>> from sympy import Product, oo
>>> Product(k, (k, 1, m))
Product(k, (k, 1, m))
>>> Product(k, (k, 1, m)).doit()
factorial(m)
>>> Product(k**2,(k, 1, m))
Product(k**2, (k, 1, m))
>>> Product(k**2,(k, 1, m)).doit()
factorial(m)**2

Wallis' product for pi:

>>> W = Product(2*i/(2*i-1) * 2*i/(2*i+1), (i, 1, oo))
>>> W
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

Direct computation currently fails:

>>> W.doit()
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

But we can approach the infinite product by a limit of finite products:

>>> from sympy import limit
>>> W2 = Product(2*i/(2*i-1)*2*i/(2*i+1), (i, 1, n))
>>> W2
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, n))
>>> W2e = W2.doit()
>>> W2e
4**n*factorial(n)**2/(2**(2*n)*RisingFactorial(1/2, n)*RisingFactorial(3/2, n))
>>> limit(W2e, n, oo)
pi/2

By the same formula we can compute sin(pi/2):

>>> from sympy import combsimp, pi, gamma, simplify
>>> P = pi * x * Product(1 - x**2/k**2, (k, 1, n))
>>> P = P.subs(x, pi/2)
>>> P
pi**2*Product(1 - pi**2/(4*k**2), (k, 1, n))/2
>>> Pe = P.doit()
>>> Pe
pi**2*RisingFactorial(1 - pi/2, n)*RisingFactorial(1 + pi/2, n)/(2*factorial(n)**2)
>>> limit(Pe, n, oo).gammasimp()
sin(pi**2/2)
>>> Pe.rewrite(gamma)
(-1)**n*pi**2*gamma(pi/2)*gamma(n + 1 + pi/2)/(2*gamma(1 + pi/2)*gamma(-n + pi/2)*gamma(n + 1)**2)

Products with the lower limit being larger than the upper one:

>>> Product(1/i, (i, 6, 1)).doit()
120
>>> Product(i, (i, 2, 5)).doit()
120

The empty product:

>>> Product(i, (i, n, n-1)).doit()
1

An example showing that the symbolic result of a product is still
valid for seemingly nonsensical values of the limits. Then the Karr
convention allows us to give a perfectly valid interpretation to
those products by interchanging the limits according to the above rules:

>>> P = Product(2, (i, 10, n)).doit()
>>> P
2**(n - 9)
>>> P.subs(n, 5)
1/16
>>> Product(2, (i, 10, 5)).doit()
1/16
>>> 1/Product(2, (i, 6, 9)).doit()
1/16

An explicit example of the Karr summation convention applied to products:

>>> P1 = Product(x, (i, a, b)).doit()
>>> P1
x**(-a + b + 1)
>>> P2 = Product(x, (i, b+1, a-1)).doit()
>>> P2
x**(a - b - 1)
>>> simplify(P1 * P2)
1

And another one:

>>> P1 = Product(i, (i, b, a)).doit()
>>> P1
RisingFactorial(b, a - b + 1)
>>> P2 = Product(i, (i, a+1, b-1)).doit()
>>> P2
RisingFactorial(a + 1, -a + b - 1)
>>> P1 * P2
RisingFactorial(b, a - b + 1)*RisingFactorial(a + 1, -a + b - 1)
>>> combsimp(P1 * P2)
1

See Also
========

Sum, summation
product

References
==========

.. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
       Volume 28 Issue 2, April 1981, Pages 305-350
       https://dl.acm.org/doi/10.1145/322248.322255
.. [2] https://en.wikipedia.org/wiki/Multiplication#Capital_Pi_notation
.. [3] https://en.wikipedia.org/wiki/Empty_product
 z tuple[tuple[Symbol, Expr, Expr]]limitsc                :    [         R                  " X/UQ70 UD6nU$ N)r   __new__)clsfunctionsymbolsassumptionsobjs        k/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/sympy/concrete/products.pyr   Product.__new__   s!    ''OO;O
    c                f    [        [        [        U R                  5      /U R                  Q76 5      $ r   )r   r   r   r   r   )selfargskwargss      r#   _eval_rewrite_as_SumProduct._eval_rewrite_as_Sum   s$    3s4==)8DKK899r%   c                     U R                   S   $ )Nr   )_argsr'   s    r#   termProduct.term   s    zz!}r%   c                    U R                   (       a  gU R                  R                  nUSL a  gU R                  (       a  U$ g )NFT)has_empty_sequencer/   is_zerohas_finite_limits)r'   zs     r#   _eval_is_zeroProduct._eval_is_zero   s:    ""II9!!H "r%   c                R    U R                   (       a  gU R                  R                  $ NT)r2   r   is_extended_realr.   s    r#   _eval_is_extended_realProduct._eval_is_extended_real   s    ""}}---r%   c                    U R                   (       a  gU R                  R                  (       a  U R                  (       a  gg g r9   )r2   r   is_positiver4   r.   s    r#   _eval_is_positiveProduct._eval_is_positive   s/    ""==$$)?)? *@$r%   c                    U R                   (       a  gU R                  R                  (       a  U R                  (       a  gg g r9   )r2   r   is_nonnegativer4   r.   s    r#   _eval_is_nonnegativeProduct._eval_is_nonnegative   s/    ""==''D,B,B -C'r%   c                `    U R                   (       a  gU R                  R                  (       a  gg r9   )r2   r   is_extended_nonnegativer.   s    r#   _eval_is_extended_nonnegative%Product._eval_is_extended_nonnegative   s#    ""==00 1r%   c                (    U R                   (       a  gg r9   )r2   r.   s    r#   _eval_is_extended_nonpositive%Product._eval_is_extended_nonpositive   s    "" #r%   c                `    U R                   (       a  U R                  R                  (       a  gg g r9   )r4   r   	is_finiter.   s    r#   _eval_is_finiteProduct._eval_is_finite   s"    !!dmm&=&= '>!r%   c                   0 nU R                    H  n[        U5      nU(       d  M  XBUS   '   M      U(       a  UR                  5        VVs0 s H  u  pVXe_M	     nnnU R                  U5      R                  " S0 UD6n[        U[        5      (       a,  [        U V	s/ s H  oR                  U5      PM     sn	5      nU$ UR                  U5      nU$ SSKJn
  U R                  n[        U R                   5       H  u  pUu  pnX-
  nUR                  (       a  UR                  (       a  US-   US-
  pSU-  nU R                  XX45      nUS [        R                  4;   a(  U R                   " U
" U5      /U R                   US  Q76 s  $ UnM     UR#                  SS5      (       a  UR                  " S0 UD6$ U
" U5      $ s  snnf s  sn	f )Nr   )powsimpr   deepTr   )r   r   itemsxreplacedoit
isinstancetuplesympy.simplify.powsimprQ   r   	enumerate
is_integeris_negative_eval_productr   NaNfuncget)r'   hintsrepsxabdkvundodidirQ   findexlimitabdifgs                     r#   rU   Product.doit   s    ;;C9#>Aq SV  %)ZZ\2\TQAD\D2--%**3U3C#u%%s;s!ZZ-s;< J ll4(J2MM%dkk2LEGA!%C~~#//1ua!e1E""1!i0AT155M!yyBdkk%&.ABB 3 99VT""66?E?"1:5 3 <s   F>Gc                l    U R                   " U R                  R                  5       /U R                  Q76 $ r   )r^   r   	conjugater   r.   s    r#   _eval_conjugateProduct._eval_conjugate   s'    yy002AT[[AAr%   c                H   Uu  p4nX1R                   ;  a.  US-
  R                  (       a  [        R                  $ XU-
  S-   -  $ XE:X  a  UR	                  X45      $ SSKJnJn  UR                  [        5      (       a  U" XS   5      (       a  U" X5      $ XT-
  nUR                  n	U	(       a  US:  a  U R                  X5      $ UR                  U5      (       a  UR                  U5      n
[        R                  =n=p[        U
5      nSnUR                  5        H/  u  nnUU-  nU[!        UU-
  XT-
  S-   5      U-  -  nXU-
  U-  -  nM1     XR#                  5       :  a<  [%        XR                  U5      5      nU R'                  UX4U45      R)                  5       nU
R+                  5       XT-
  S-   -  U-  U-  $ UR,                  (       a1  [/        USS9nUR0                  (       a  U R3                  UX4U45      $ GOUR0                  (       Ga  UR5                  U5      u  nn[7        U5      S:  a  / / nnU H>  nU R3                  UX4U45      nUb  UR9                  U5        M-  UR9                  U5        M@     U(       d  g UR:                  " U6 n[=        U6 nU R'                  UX4U45      R)                  5       nUXT-
  S-   -  U-  U-  $ U R3                  US   X4U45      nUc%  U R'                  US   X4U45      R)                  5       nUXT-
  S-   -  U-  $ UR>                  (       a  UR@                  R                  U5      (       d'  [C        URD                  X4U45      nUR@                  U-  $ URD                  R                  U5      (       d0  U R3                  UR@                  X4U45      nUb  UURD                  -  $ ON[G        U[H        5      (       a9  UR)                  5       nU R3                  UU5      nUc  U R'                  UU5      $ U$ U	(       a  U R                  X5      $ g )Nr   )deltaproduct_has_simple_deltar   d   T)fraction   )%free_symbolsr3   r   Onesubsdeltarv   rw   hasr   
is_Integer_eval_product_directis_polynomialas_polyr   rS   r   degreer   r^   rU   LCis_Addr
   is_Mulr\   as_coeff_mullenappend_new_rawargsr   is_Powbaser   r   rV   r   )r'   r/   r   rd   rl   nrv   rw   rn   definitepolyABQ	all_rootsMrmargfactored	without_kwith_kexcludeincludetps	evaluatedri   s                                r#   r\   Product._eval_product#  s   	q%%%q!!uua%!)$$699Q?":88N##(9$q	(J(J--e>>s,,T::""<<?DAdIA!)1Q_QUAEAI699!eaZ *
 ;;= $		!-IIcA!9-224779quqy)A-11[[#D48H))(Q1I>>  [[[ $ 1 1! 4Iv6{a#%rA**1qQi8A}q)q)   ++W5CWA		#ay1668A$quqy1!3a77 &&vay1)<9		&)aAY7<<>A 1519-a// [[99==##dhhq	2yy!|#XX\\!__&&tyy1)<=dhh;&g&&		I""9f5AyyyF33,,T:: r%   c                X    SSK Jn  U" U 40 UD6nUS   (       a  UR                  5       $ U$ )Nr   )product_simplifyrU   )sympy.simplify.simplifyr   rU   )r'   r)   r   rvs       r#   _eval_simplifyProduct._eval_simplify  s+    <d-f-"6Nrwwy22r%   c                    U R                   (       a5  U R                  " U R                  R                  5       /U R                  Q76 $ g r   )is_commutativer^   r   	transposer   r.   s    r#   _eval_transposeProduct._eval_transpose  s3    99T]]446EEEr%   c           
         Uu  p4n[        [        XT-
  S-   5       Vs/ s H  oaR                  X4U-   5      PM     sn6 $ s  snf )Nr   )r   ranger}   )r'   r/   r   rd   rl   r   rh   s          r#   r   Product._eval_product_direct  s@    	q%	2BC2BQYYqa%(2BCDDCs   >c           	        [        U[        5      (       a  XR                  ;  a  [        R                  $ U R
                  [        U R                  5      p2UR                  S5      nU(       a  U R                  " U/UQ76 nUu  pVnXR                  ;   d  XR                  ;   a  g [        5       n[        [        X%XhS-
  45      [        X%US-   U45      -  [        X!SS9R                  XX5      -  XU45      n	U	$ )Nr   T)evaluate)rV   r   r{   r   Zeror   listr   popr^   r   r   r   r   r}   )
r'   xri   r   rk   rh   rl   rm   hr   s
             r#   _eval_derivativeProduct._eval_derivative  s    a  Q.?.?%?66MMM4#46

2		!%f%Aa!~~"5G'!E]+gaQUA.GG*UVdhJiJnJnopJttwx}~v  A	r%   c                :   U R                   n[        U5      nU R                  n [        U/UQ76 R	                  5       nU$ ! [
         aO    [        US-
  /UQ76 R                  5       [        R                  L a  [        R                  s $ [        SU-  5      ef = f)a]  
See docs of :obj:`.Sum.is_convergent()` for explanation of convergence
in SymPy.

Explanation
===========

The infinite product:

.. math::

    \prod_{1 \leq i < \infty} f(i)

is defined by the sequence of partial products:

.. math::

    \prod_{i=1}^{n} f(i) = f(1) f(2) \cdots f(n)

as n increases without bound. The product converges to a non-zero
value if and only if the sum:

.. math::

    \sum_{1 \leq i < \infty} \log{f(n)}

converges.

Examples
========

>>> from sympy import Product, Symbol, cos, pi, exp, oo
>>> n = Symbol('n', integer=True)
>>> Product(n/(n + 1), (n, 1, oo)).is_convergent()
False
>>> Product(1/n**2, (n, 1, oo)).is_convergent()
False
>>> Product(cos(pi/n), (n, 1, oo)).is_convergent()
True
>>> Product(exp(-n**2), (n, 1, oo)).is_convergent()
False

References
==========

.. [1] https://en.wikipedia.org/wiki/Infinite_product
r   zJThe algorithm to find the product convergence of %s is not yet implemented)	r   r   r   r   is_convergentNotImplementedErroris_absolutely_convergentr   true)r'   sequence_termlog_sumlimis_convs        r#   r   Product.is_convergent  s    ` m$kk	T'(C(668G  # 	T=1$+s+DDF!&&Pvv% 'ADQ'S T T	Ts   A ABBc                h   [        U5      n[        U5       H/  u  p4[        U[        5      (       a  M  U R	                  U5      X#'   M1     Sn/ n[        U R
                  5       H4  u  p7UnX2;   a  U* nUS   US   S-   US   S-
  4nUR                  U5        M6     [        U R                  U-  /UQ76 $ )a  
Reverse the order of a limit in a Product.

Explanation
===========

``reverse_order(expr, *indices)`` reverses some limits in the expression
``expr`` which can be either a ``Sum`` or a ``Product``. The selectors in
the argument ``indices`` specify some indices whose limits get reversed.
These selectors are either variable names or numerical indices counted
starting from the inner-most limit tuple.

Examples
========

>>> from sympy import gamma, Product, simplify, Sum
>>> from sympy.abc import x, y, a, b, c, d
>>> P = Product(x, (x, a, b))
>>> Pr = P.reverse_order(x)
>>> Pr
Product(1/x, (x, b + 1, a - 1))
>>> Pr = Pr.doit()
>>> Pr
1/RisingFactorial(b + 1, a - b - 1)
>>> simplify(Pr.rewrite(gamma))
Piecewise((gamma(b + 1)/gamma(a), b > -1), ((-1)**(-a + b + 1)*gamma(1 - a)/gamma(-b), True))
>>> P = P.doit()
>>> P
RisingFactorial(a, -a + b + 1)
>>> simplify(P.rewrite(gamma))
Piecewise((gamma(b + 1)/gamma(a), a > 0), ((-1)**(-a + b + 1)*gamma(1 - a)/gamma(-b), True))

While one should prefer variable names when specifying which limits
to reverse, the index counting notation comes in handy in case there
are several symbols with the same name.

>>> S = Sum(x*y, (x, a, b), (y, c, d))
>>> S
Sum(x*y, (x, a, b), (y, c, d))
>>> S0 = S.reverse_order(0)
>>> S0
Sum(-x*y, (x, b + 1, a - 1), (y, c, d))
>>> S1 = S0.reverse_order(1)
>>> S1
Sum(x*y, (x, b + 1, a - 1), (y, d + 1, c - 1))

Of course we can mix both notations:

>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))

See Also
========

sympy.concrete.expr_with_intlimits.ExprWithIntLimits.index,
reorder_limit,
sympy.concrete.expr_with_intlimits.ExprWithIntLimits.reorder

References
==========

.. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
       Volume 28 Issue 2, April 1981, Pages 305-350
       https://dl.acm.org/doi/10.1145/322248.322255

r   r   rz   )	r   rY   rV   intrj   r   r   r   r   )	exprindices	l_indicesrh   indxer   rk   ls	            r#   reverse_orderProduct.reverse_order  s    J M	 +GAdC((#zz$/	 , !$++.HAA~B1XuQx!|U1X\:MM! / t}})3F33r%   N)__name__
__module____qualname____firstlineno____doc__	__slots____annotations__r   r*   propertyr/   r   r6   r;   r?   rC   rG   rJ   rN   rU   rs   r\   r   r   r   r   r   r   __static_attributes__r   r%   r#   r   r      s    l\ I,,:  H	.#JB_;B3

E:xT4r%   r   c                 d    [        U 0 UD6n[        U[         5      (       a  UR                  SS9$ U$ )a(  
Compute the product.

Explanation
===========

The notation for symbols is similar to the notation used in Sum or
Integral. product(f, (i, a, b)) computes the product of f with
respect to i from a to b, i.e.,

::

                                 b
                               _____
    product(f(n), (i, a, b)) = |   | f(n)
                               |   |
                               i = a

If it cannot compute the product, it returns an unevaluated Product object.
Repeated products can be computed by introducing additional symbols tuples::

Examples
========

>>> from sympy import product, symbols
>>> i, n, m, k = symbols('i n m k', integer=True)

>>> product(i, (i, 1, k))
factorial(k)
>>> product(m, (i, 1, k))
m**k
>>> product(i, (i, 1, k), (k, 1, n))
Product(factorial(k), (k, 1, n))

F)rR   )r   rV   rU   )r(   r)   prods      r#   productr   3  s7    J D#F#D$  yyey$$r%   N)!
__future__r   expr_with_intlimitsr   
summationsr   r   r   sympy.core.exprr	   sympy.core.exprtoolsr
   sympy.core.functionr   sympy.core.mulr   sympy.core.singletonr   sympy.core.symbolr   r   (sympy.functions.combinatorial.factorialsr   &sympy.functions.elementary.exponentialr   r   (sympy.functions.special.tensor_functionsr   sympy.polysr   r   r   r   r   r%   r#   <module>r      sD    " 2 Q Q   - *  " + D ; C "_4 _4D*r%   