
    wgH                         d dl mZ ddlmZ ddlmZmZmZ d dl	m
Z
 d dl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 dlmZmZ d dlmZ d dlmZmZ  G d de      Z d Z!y)    )Tuple   )ExprWithIntLimits)Sum	summation)_dummy_with_inherited_properties_concrete)Expr)factor_terms)
Derivative)Mul)S)DummySymbol)RisingFactorial)explog)KroneckerDelta)quorootsc                       e Zd ZU dZdZeeeeef      ed<   d Z	d Z
ed        ZeZd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)Producta  
    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
     limitsc                 :    t        j                  | |g|i |}|S N)r   __new__)clsfunctionsymbolsassumptionsobjs        \/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/concrete/products.pyr   zProduct.__new__   s#    ''XOO;O
    c                 f    t        t        t        | j                        g| j                         S r   )r   r   r   r   r   )selfargskwargss      r"   _eval_rewrite_as_SumzProduct._eval_rewrite_as_Sum   s$    3s4==)8DKK899r#   c                      | j                   d   S )Nr   )_argsr%   s    r"   termzProduct.term   s    zz!}r#   c                 p    | j                   ry| j                  j                  }|du ry| j                  r|S y )NFT)has_empty_sequencer,   is_zerohas_finite_limits)r%   zs     r"   _eval_is_zerozProduct._eval_is_zero   s:    ""II9!!H "r#   c                 H    | j                   ry| j                  j                  S NT)r.   r   is_extended_realr+   s    r"   _eval_is_extended_realzProduct._eval_is_extended_real   s    ""}}---r#   c                 f    | j                   ry| j                  j                  r| j                  ryy y r4   )r.   r   is_positiver0   r+   s    r"   _eval_is_positivezProduct._eval_is_positive   s/    ""==$$)?)? *@$r#   c                 f    | j                   ry| j                  j                  r| j                  ryy y r4   )r.   r   is_nonnegativer0   r+   s    r"   _eval_is_nonnegativezProduct._eval_is_nonnegative   s/    ""==''D,B,B -C'r#   c                 L    | j                   ry| j                  j                  ryy r4   )r.   r   is_extended_nonnegativer+   s    r"   _eval_is_extended_nonnegativez%Product._eval_is_extended_nonnegative   s#    ""==00 1r#   c                     | j                   ryy r4   )r.   r+   s    r"   _eval_is_extended_nonpositivez%Product._eval_is_extended_nonpositive   s    "" #r#   c                 L    | j                   r| j                  j                  ryy y r4   )r0   r   	is_finiter+   s    r"   _eval_is_finitezProduct._eval_is_finite   s"    !!dmm&=&= '>!r#   c                 P   i }| j                   D ]  }t        |      }|s|||d   <    |r|j                         D ci c]  \  }}||
 }}} | j                  |      j                  di |}t        |t              r*t        |D 	cg c]  }	|	j                  |       c}	      }|S |j                  |      }|S ddlm}
 | j                  }t        | j                         D ]  \  }}|\  }	}}||z
  }|j                  r|j                  r|dz   |dz
  }}d|z  }| j                  ||	||f      }|d t        j                  fv r( | j                    |
|      g| j                   |d   c S |} |j#                  dd      r |j                  di |S  |
|      S c c}}w c c}	w )Nr   )powsimpr   deepTr   )r   r   itemsxreplacedoit
isinstancetuplesympy.simplify.powsimprF   r   	enumerate
is_integeris_negative_eval_productr   NaNfuncget)r%   hintsrepsxabdkvundodidirF   findexlimitabdifgs                     r"   rJ   zProduct.doit   s    ;; 	!C9#>A SV	! %)ZZ\2TQAqD2D2*$--%**3U3C#u%s;!QZZ-;< J ll4(J2MM%dkk2 	LE5GAq!a%C~~#//1ua!e1E""1q!Qi0AT155M! tyyBdkk%&.ABB	 99VT"166?E?"1:5 3 <s   F	F#c                     | j                   r5 | j                  | j                  j                         g| j                   S y r   )is_commutativerS   r   adjointr   r+   s    r"   _eval_adjointzProduct._eval_adjoint   s5    499T]]224Ct{{CCr#   c                 l     | j                   | j                  j                         g| j                   S r   )rS   r   	conjugater   r+   s    r"   _eval_conjugatezProduct._eval_conjugate%  s)    tyy002AT[[AAr#   c                    |\  }}}||j                   vr*|dz
  j                  rt        j                  S |||z
  dz   z  S ||k(  r|j	                  ||      S ddlm}m} |j                  t              r |||d         r	 |||      S ||z
  }|j                  }	|	r|dk  r| j                  ||      S |j                  |      r|j                  |      }
t        j                  x}x}}t        |
      }d}|j                         D ]0  \  }}||z  }|t!        ||z
  ||z
  dz         |z  z  }|||z
  |z  z  }2 ||
j#                         k  r>t%        |
|j                  |            }| j'                  ||||f      j)                         }|
j+                         ||z
  dz   z  |z  |z  S |j,                  r/t/        |d      }|j0                  r| j3                  ||||f      S |j0                  r|j5                  |      \  }}t7        |      dk\  rg g }}|D ]<  }| j3                  ||||f      }||j9                  |       ,|j9                  |       > |sy  |j:                  | }t=        | }| j'                  ||||f      j)                         }|||z
  dz   z  |z  |z  S | j3                  |d   |||f      }|&| j'                  |d   |||f      j)                         }|||z
  dz   z  |z  S |j>                  r|j@                  j                  |      s(tC        |jD                  |||f      }|j@                  |z  S |jD                  j                  |      sx| j3                  |j@                  |||f      }|W||jD                  z  S tG        |tH              r8|j)                         }| j3                  ||      }|| j'                  ||      S |S |	r| j                  ||      S y )Nr   )deltaproduct_has_simple_deltar   d   T)fraction   )%free_symbolsr/   r   Onesubsdeltarm   rn   hasr   
is_Integer_eval_product_directis_polynomialas_polyr   rH   r   degreer   rS   rJ   LCis_Addr
   is_MulrQ   as_coeff_mullenappend_new_rawargsr   is_Powbaser   r   rK   r   )r%   r,   r   rY   ra   nrm   rn   rc   definitepolyABQ	all_rootsMrmargfactored	without_kwith_kexcludeincludetps	evaluatedr^   s                                r"   rQ   zProduct._eval_product(  s   	AqD%%%q!!uu!a%!)$$699Q?":88N#(9$q	(Jf--!e>>s,,T6::"<<?DAAdIA!)  1Q_QUAEAI699a!eaZ 
 4;;= $		!-IIcAq!9-224779q1uqy)A-11[[#D48H))(Q1I>>[[ $ 1 1! 4Iv6{a#%r *A**1q!Qi8A}q)q)* +$++W5CWA		#1ay1668A$q1uqy1!3a77 &&vay1a)<9		&)aAY7<<>A 1q519-a// [[99==#dhhAq	2yy!|#XX\\!_&&tyy1a)<=dhh;&g&		I""9f5AyyyF33,,T6:: r#   c                 N    ddl m}  || fi |}|d   r|j                         S |S )Nr   )product_simplifyrJ   )sympy.simplify.simplifyr   rJ   )r%   r'   r   rvs       r"   _eval_simplifyzProduct._eval_simplify  s+    <d-f-"6Nrwwy22r#   c                     | j                   r5 | j                  | j                  j                         g| j                   S y r   )rf   rS   r   	transposer   r+   s    r"   _eval_transposezProduct._eval_transpose  s5    499T]]446EEEr#   c           
          |\  }}}t        t        ||z
  dz         D cg c]  }|j                  |||z          c} S c c}w )Nr   )r   rangert   )r%   r,   r   rY   ra   r   r]   s          r"   rx   zProduct._eval_product_direct  sB    	Aq%A	2BCQTYYq!a%(CDDCs   ?c           	         t        |t              r|| j                  vrt        j                  S | j
                  t        | j                        }}|j                  d      }|r | j                  |g| }|\  }}}||j                  v s||j                  v ry t               }t        t        ||||dz
  f      t        |||dz   |f      z  t        ||d      j                  ||      z  |||f      }	|	S )Nr   T)evaluate)rK   r   rr   r   Zeror   listr   poprS   r   r   r   r   rt   )
r%   xr^   r   r`   r]   ra   rb   hr   s
             r"   _eval_derivativezProduct._eval_derivative  s    a Qd.?.?%?66MMM4#46

2		!%f%A1a!q~~"5G'!aAE]+ga!QUA.GG*UVXYdhJiJnJnoprsJttwxz{}~v  A	r#   c                 6   | j                   }t        |      }| j                  }	 t        |g| j	                         }|S # t
        $ rN t        |dz
  g| j                         t        j                  u rt        j                  cY S t        d|z        w xY w)aU  
        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   z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                 X   t        |      }t        |      D ]*  \  }}t        |t              r| j	                  |      ||<   , d}g }t        | j
                        D ]3  \  }}|}||v r| }|d   |d   dz   |d   dz
  f}|j                  |       5 t        | j                  |z  g| S )ah	  
        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   rq   )	r   rN   rK   intr_   r   r   r   r   )	exprindices	l_indicesr]   indxer   r`   ls	            r"   reverse_orderzProduct.reverse_order  s    J M	 + 	0GAtdC(#zz$/	!	0 !$++. 	HAuAI~B1XuQx!|U1X\:MM!	 t}})3F33r#   N)__name__
__module____qualname____doc__	__slots__tTupler   r	   __annotations__r   r(   propertyr,   r   r2   r6   r9   r<   r?   rA   rD   rJ   rh   rk   rQ   r   r   rx   r   r   r   r   r#   r"   r   r      s    l\ I6&$,-..:  H	.#J
B_;B3

E:xT4r#   r   c                  `    t        | i |}t        |t               r|j                  d      S |S )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)rG   )r   rK   rJ   )r&   r'   prods      r"   productr   8  s4    J D#F#D$ yyey$$r#   N)"typingr   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 "d4 d4N*r#   