
    wgh9                         d Z ddlmZmZmZmZmZmZ ddlm	Z	 ddl
mZmZ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mZmZ eedd
              Zd Zd Zedd       Zd Zed        Z y	)zEAlgorithms for partial fraction decomposition of rational functions.     )SAddsympifyFunctionLambdaDummy)preorder_traversal)PolyRootSumcancelfactor)PolynomialError)allowed_flagsset_defaults)parallel_poly_from_expr)numbered_symbolstake	xthreadedpublicNc                    t        |g        t        |       } | j                  r| S | j                         \  }}|j	                         }t        |d      }	 t        ||f|fi |\  \  }}}|j.                  r/| j1                         }|| k7  rt        |f||d|S t#        d      |j1                  |      \  }}}|j3                  |d      \  }}|j5                  |      \  }}|j7                         d	k  r||z  }n|st9        ||      }nt;        ||      }t<        j>                  }tA        jB                  |      D ]+  }|jE                  tF              r||z  }|tI        |      z  }- ||jK                         |z   z  S # t        $ r}| j                  rt        |      | j                  r_| j                  d      \  }	}
 | j                  |
 }
|	r2t        | j                  j                  |	      f||d|}	|	|
z  cY d}~S |
cY d}~S | j                  rg }	g }
| j                  D ]_  }|j                  r|	j!                  |       !	 |
j!                  t        |f||d|       A# t"        $ r |
j!                  |       Y ]w xY w t         | j                  |	 f||d| | j                  |
 z   cY d}~S g }t%        |       }t'        |       |D ]B  }	 |j!                  |t        |f||d|f       |j)                          5# t"        $ r Y @w xY w | j+                  t-        |            cY d}~S d}~ww xY w)
a9  
    Compute partial fraction decomposition of a rational function.

    Given a rational function ``f``, computes the partial fraction
    decomposition of ``f``. Two algorithms are available: One is based on the
    undertermined coefficients method, the other is Bronstein's full partial
    fraction decomposition algorithm.

    The undetermined coefficients method (selected by ``full=False``) uses
    polynomial factorization (and therefore accepts the same options as
    factor) for the denominator. Per default it works over the rational
    numbers, therefore decomposition of denominators with non-rational roots
    (e.g. irrational, complex roots) is not supported by default (see options
    of factor).

    Bronstein's algorithm can be selected by using ``full=True`` and allows a
    decomposition of denominators with non-rational roots. A human-readable
    result can be obtained via ``doit()`` (see examples below).

    Examples
    ========

    >>> from sympy.polys.partfrac import apart
    >>> from sympy.abc import x, y

    By default, using the undetermined coefficients method:

    >>> apart(y/(x + 2)/(x + 1), x)
    -y/(x + 2) + y/(x + 1)

    The undetermined coefficients method does not provide a result when the
    denominators roots are not rational:

    >>> apart(y/(x**2 + x + 1), x)
    y/(x**2 + x + 1)

    You can choose Bronstein's algorithm by setting ``full=True``:

    >>> apart(y/(x**2 + x + 1), x, full=True)
    RootSum(_w**2 + _w + 1, Lambda(_a, (-2*_a*y/3 - y/3)/(-_a + x)))

    Calling ``doit()`` yields a human-readable result:

    >>> apart(y/(x**2 + x + 1), x, full=True).doit()
    (-y/3 - 2*y*(-1/2 - sqrt(3)*I/2)/3)/(x + 1/2 + sqrt(3)*I/2) + (-y/3 -
        2*y*(-1/2 + sqrt(3)*I/2)/3)/(x + 1/2 - sqrt(3)*I/2)


    See Also
    ========

    apart_list, assemble_partfrac_list
    T	extensionF)split_1)xfullN+multivariate partial fraction decompositionauto   )&r   r   is_Atomas_numer_denomcopyr   r   r   is_commutativeis_Mulargs_cncfuncapart
_from_argsis_AddargsappendNotImplementedErrorr	   nextskipxreplacedictis_multivariater   divrat_clear_denomsdegreeapart_undetermined_coeffsapart_full_decompositionr   Zeror   	make_argshasr   r   as_expr)fr   r   optionsPQ_optionsoptmsgcncirepspotefccommonpolypartialtermsterms                        Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/partfrac.pyr'   r'      s*   p '2
Ayy!1||~H7d3G$*-q!faC7CAJ 	XXZ78qt8x88!9; 	; 88A;LFAqeeADe!GD!a DAqxxzQA#/15G.q!4GFFEg& "88GTMEVD\!E	" 4<<>E)**C  "*!#&&88JJuJ-EArB!&&++A.K!$K(Kt	XXABVV %##HHQK%		%"FQT"FX"FG. %		!%% @qt@x@61662;NND$Q'CI KKE!$Hqt$Hx$H IJHHJ* 	 ::d4j))E"*s   F 
MA5L<ML<	M=L<I,+L<,J	L<J		/L<8M>L<0LL<	LL<LL<6M<Mc                    t        t              }g g }}|j                         \  }}|D ]n  \  }}|j                         |}
}	t	        d|dz         D ]E  }t        ||	      |
j                  |      }
}|j                  ||
||f       |j                  |       G p  |j                         j                  | }t        d|j                  |      }t        |      D ]C  \  }\  }}
}}t        ||j                  |      }|||f||<   |
j                  |      }
|||
z  z  }E g t        j                   }}|j#                         D ]*  \  \  }}|j                  || j%                  |      z
         , ddlm}  |||      }|D ]>  \  }}}|j+                         j-                  |      }|||j+                         |z  z  z  }@ |S )z;Partial fractions via method of undetermined coefficients. )clsr   r   )domain)solve)r   r   factor_listr4   ranger   quor+   extend
get_domaininjectr
   gen	enumerate
set_domainr   r7   rL   nthsympy.solversrR   r:   subs)r=   r>   XrK   symbols_factorsr;   knqrD   coeffsdomFhsystemresultcoeffrR   solutions                        rN   r5   r5      s   U#A2WGJAw #1xxz11q!a% 	#AQ
AEE!HAFNNFAq!,-NN6"	##  !,,.


)CQc"A )' 2 FAq!s+AY
LL	QqS	 FFwwy (eeaeeAh&'( $VW%H #1aIIKX&!AIIKN""# M    c                 L    t        t        | |z  | j                  d               S )&  
    Bronstein's full partial fraction decomposition algorithm.

    Given a univariate rational function ``f``, performing only GCD
    operations over the algebraic closure of the initial ground domain
    of definition, compute full partial fraction decomposition with
    fractions having linear denominators.

    Note that no factorization of the initial denominator of ``f`` is
    performed. The final decomposition is formed in terms of a sum of
    :class:`RootSum` instances.

    References
    ==========

    .. [1] [Bronstein93]_

    r   )assemble_partfrac_list
apart_listgens)r=   r>   s     rN   r6   r6      s"    & "*QqS!&&)"<==rn   c                    t        |g        t        |       } | j                  r| S | j                         \  }}t	        |d      }t        ||f|fi |\  \  }}}|j                  rt        d      |j                  |      \  }}}|j                  |d      \  }}|j                  |      \  }}|}	|d } |d      }t        |||      }
||	|
fS )aR  
    Compute partial fraction decomposition of a rational function
    and return the result in structured form.

    Given a rational function ``f`` compute the partial fraction decomposition
    of ``f``. Only Bronstein's full partial fraction decomposition algorithm
    is supported by this method. The return value is highly structured and
    perfectly suited for further algorithmic treatment rather than being
    human-readable. The function returns a tuple holding three elements:

    * The first item is the common coefficient, free of the variable `x` used
      for decomposition. (It is an element of the base field `K`.)

    * The second item is the polynomial part of the decomposition. This can be
      the zero polynomial. (It is an element of `K[x]`.)

    * The third part itself is a list of quadruples. Each quadruple
      has the following elements in this order:

      - The (not necessarily irreducible) polynomial `D` whose roots `w_i` appear
        in the linear denominator of a bunch of related fraction terms. (This item
        can also be a list of explicit roots. However, at the moment ``apart_list``
        never returns a result this way, but the related ``assemble_partfrac_list``
        function accepts this format as input.)

      - The numerator of the fraction, written as a function of the root `w`

      - The linear denominator of the fraction *excluding its power exponent*,
        written as a function of the root `w`.

      - The power to which the denominator has to be raised.

    On can always rebuild a plain expression by using the function ``assemble_partfrac_list``.

    Examples
    ========

    A first example:

    >>> from sympy.polys.partfrac import apart_list, assemble_partfrac_list
    >>> from sympy.abc import x, t

    >>> f = (2*x**3 - 2*x) / (x**2 - 2*x + 1)
    >>> pfd = apart_list(f)
    >>> pfd
    (1,
    Poly(2*x + 4, x, domain='ZZ'),
    [(Poly(_w - 1, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1)])

    >>> assemble_partfrac_list(pfd)
    2*x + 4 + 4/(x - 1)

    Second example:

    >>> f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
    >>> pfd = apart_list(f)
    >>> pfd
    (-1,
    Poly(2/3, x, domain='QQ'),
    [(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])

    >>> assemble_partfrac_list(pfd)
    -2/3 - 2/(x - 2)

    Another example, showing symbolic parameters:

    >>> pfd = apart_list(t/(x**2 + x + t), x)
    >>> pfd
    (1,
    Poly(0, x, domain='ZZ[t]'),
    [(Poly(_w**2 + _w + t, _w, domain='ZZ[t]'),
    Lambda(_a, -2*_a*t/(4*t - 1) - t/(4*t - 1)),
    Lambda(_a, -_a + x),
    1)])

    >>> assemble_partfrac_list(pfd)
    RootSum(_w**2 + _w + t, Lambda(_a, (-2*_a*t/(4*t - 1) - t/(4*t - 1))/(-_a + x)))

    This example is taken from Bronstein's original paper:

    >>> f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
    >>> pfd = apart_list(f)
    >>> pfd
    (1,
    Poly(0, x, domain='ZZ'),
    [(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
    (Poly(_w**2 - 1, _w, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
    (Poly(_w + 1, _w, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])

    >>> assemble_partfrac_list(pfd)
    -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)

    See also
    ========

    apart, assemble_partfrac_list

    References
    ==========

    .. [1] [Bronstein93]_

    Tr   r   r   c              3   ,   K   t        |       }	 | w)N)r   )nameds     rN   dummieszapart_list.<locals>.dummiesT  s     dA s   w)r   r   r    r!   r   r   r1   r,   r   r2   r3   apart_list_full_decomposition)r;   r   rx   r<   r=   r>   r@   rI   rJ   polypartrationalparts              rN   rr   rr      s    R '2
Ayy!17d3G)1a&!?w?KFQC!9; 	; 88A;LFAqeeADe!GD!a DAqH	
 #,0Aw?LHl++rn   c           	         | || j                   g f\  }}}} t        d      |      }t        d      }g }	|j                  d      D ]  \  }
}|
j	                         }||j                  ||dz
        gz  }t        ||j                  |
|z        z        ||z  z  }|gg }}t        d|      D ]  }||d   j                  |      |z  gz  } t        d|dz         D ]#  }|||dz
     |j                  ||      |z  fgz  }% t        d|      D ]<  }t        ||         j                         \  } }t        d|dz         D ]  } | j                  |||z
      }   |j                  |d    }t        | |      } t        ||      }| j                  |
      }|
j                  |      }|j                  |      \  }}| |j                  |      z  j                  |      }|j                  |t        |            }t!        ||j	                         j                  ||            }t!        |||z
        }||z
  }|	j#                  ||||f       ? 
 |	S )rp   uaT)allr   r   )rY   r   r   sqf_list_includer:   diffr   rU   rT   r!   r^   r
   gcd
half_gcdexremr-   r   r+   )r=   r>   dummygenP_origQ_origr   Ur~   r   rK   rw   rd   bri   Hr^   jrD   GDBgDwnumerdenomexponents                             rN   rz   rz   `  si   & a?FFAqaAc
AG""t", $91IIK	qvvaQ!!6&**QT**+ad2#r4q! 	'A1R5::a=1$&&A	' q!a% 	5AqQx1!1244D	5 q! 	9A!A$<..0DAq1a!e_ )AFFDQK() Q AQ
AQ
AaAaA<<?DAqQUU1X""1%A4>*B1aiik..q!45E1q1u&EsHNNBuh78-	9$9L Nrn   c                    | d   }| d   }|j                         }| d   D ]  \  }}}}t        |t              rv|j                  |j                  }	}|j                  |j                  }}
|j                  |
d   |d         }t        t        |      |	||z  z        }|t        ||dd      z  }|D ]  }| ||       ||      |z  z  z  }  ||z  S )a(  Reassemble a full partial fraction decomposition
    from a structured result obtained by the function ``apart_list``.

    Examples
    ========

    This example is taken from Bronstein's original paper:

    >>> from sympy.polys.partfrac import apart_list, assemble_partfrac_list
    >>> from sympy.abc import x

    >>> f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
    >>> pfd = apart_list(f)
    >>> pfd
    (1,
    Poly(0, x, domain='ZZ'),
    [(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
    (Poly(_w**2 - 1, _w, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
    (Poly(_w + 1, _w, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])

    >>> assemble_partfrac_list(pfd)
    -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)

    If we happen to know some roots we can provide them easily inside the structure:

    >>> pfd = apart_list(2/(x**2-2))
    >>> pfd
    (1,
    Poly(0, x, domain='ZZ'),
    [(Poly(_w**2 - 2, _w, domain='ZZ'),
    Lambda(_a, _a/2),
    Lambda(_a, -_a + x),
    1)])

    >>> pfda = assemble_partfrac_list(pfd)
    >>> pfda
    RootSum(_w**2 - 2, Lambda(_a, _a/(-_a + x)))/2

    >>> pfda.doit()
    -sqrt(2)/(2*(x + sqrt(2))) + sqrt(2)/(2*(x - sqrt(2)))

    >>> from sympy import Dummy, Poly, Lambda, sqrt
    >>> a = Dummy("a")
    >>> pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])

    >>> assemble_partfrac_list(pfd)
    -sqrt(2)/(2*(x + sqrt(2))) + sqrt(2)/(2*(x - sqrt(2)))

    See Also
    ========

    apart, apart_list
    r   r      F)r   	quadratic)	r:   
isinstancer
   	variablesexprr^   r   tupler   )partial_listrI   r{   pfdrnfdfexannuadder&   roots                 rN   rq   rq     s    p !_F AH



C &a -2r2a\\277B\\277BA1&B%)RBY/D71d%@@C  -r$x4",,-- #:rn   )NF)NN)!__doc__
sympy.corer   r   r   r   r   r   sympy.core.traversalr	   sympy.polysr
   r   r   r   sympy.polys.polyerrorsr   sympy.polys.polyoptionsr   r   sympy.polys.polytoolsr   sympy.utilitiesr   r   r   r   r'   r5   r6   rr   rz   rq    rn   rN   <module>r      s    K @ ? 3 5 5 2 ? 9 E E D+  D+N$N>, I, I,X@F L Lrn   