K ig9dZddlmZmZmZmZmZmZddlm Z ddl m Z m Z m Z mZddlmZddlmZmZddlmZddlmZmZmZmZeedd Zd Zd Zedd ZdZedZ y )zEAlgorithms for partial fraction decomposition of rational functions. )SAddsympifyFunctionLambdaDummy)preorder_traversal)PolyRootSumcancelfactor)PolynomialError) allowed_flags set_defaults)parallel_poly_from_expr)numbered_symbolstake xthreadedpublicNc t|gt|}|jr|S|j\}}|j }t |d} t ||f|fi|\\}}}|j.r/|j1}||k7rt|f||d|St#d|j1|\}}}|j3|d\}}|j5|\}}|j7d kr||z }n|s t9||}n t;||}t<j>}tAjB|D]+}|jEtFr||z }|tI|z }-||jK|zzS#t$r}|jr t||jr_|jd\} } |j| } | r2t|jj| f||d|} | | zcYd}~S| cYd}~S|jrg} g} |jD]_} | jr| j!| ! | j!t| f||d|A#t"$r| j!| Y]wxYwt|j| f||d||j| zcYd}~Sg} t%|} t'| | D]B} | j!|t|f||d|f| j)5#t"$rY@wxYw|j+t-| cYd}~Sd}~wwxYw) a8 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 undetermined 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)xfullN+multivariate partial fraction decompositionauto)&rris_Atomas_numer_denomcopyrrris_commutativeis_Mulargs_cncfuncapart _from_argsis_AddargsappendNotImplementedErrorr nextskipxreplacedictis_multivariater divrat_clear_denomsdegreeapart_undetermined_coeffsapart_full_decompositionrZeror make_argshasr r as_expr)frroptionsPQ_optionsoptmsgcncirepspotefccommonpolypartialtermsterms Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/partfrac.pyr'r' s*p'2 Ayy!1||~H7d3G$*-q!faC7C AJ  XXZ 78qt8x8 8! 9; ;88A;LFAqeeADe!GD!  a DAqxxzQA#/15G.q!4G FFE g&" 88G  TME VD\ !E " 4<<>E) **C "*  !#& & 88JJuJ-EArB!&&++A.K!$K(Kt  XXABVV %##HHQK% %"FQT"FX"FG.% ! %  %@qt@x@61662;N ND$Q'C I KKE!$Hqt$Hx$H IJHHJ*   ::d4j) )E"*sF M A5L<ML< M=L< I,+L<,J L<J  /L<8M>L<0L  L< LL<LL<6M<Mctt}gg}}|j\}}|D]n\}}|j|} } t d|dzD]E} t || | j |} } |j| | || f|j| Gp|jj|} td|j| }t|D]C\} \} } }}t| |j| }|||f|| <| j| } ||| zz }Egtj }}|j#D]*\\}}|j||j%|z ,ddlm}|||}|D]>\}}}|j+j-|}|||j+|zz z }@|S)z;Partial fractions via method of undetermined coefficients. )clsrr)domain)solve)rr factor_listr4rangerquor+extend get_domaininjectr gen enumerate set_domainrr7rLnth sympy.solversrRr:subs)r=r>XrKsymbols_factorsr;knqrDcoeffsdomFhsystemresultcoeffrRsolutions rNr5r5sU#A2WGJAw#1xxz11q!a% #AQ AEE!HAF NNFAq!, - NN6 " ## !,,.   )C Qc"A )' 2 FAq! s +AY LL  QqS  FFwwy( e eaeeAh&'($VW%H#1a IIK  X &!AIIKN""# McLtt||z |jdS)& 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_listgens)r=r>s rNr6r6s"& "*QqS!&&)"< ==rnc t|gt|}|jr|S|j\}}t |d}t ||f|fi|\\}}}|j r td|j|\}}}|j|d\}}|j|\}}|} | d}|d}t|||} || | fS)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]_ Trrrc3,Kt|} |w)N)r)nameds rNdummieszapart_list..dummiesTsd Asw) rrr r!rrr1r,r r2r3apart_list_full_decomposition) r;rrxr<r=r>r@rIrJpolypart rationalparts rNrrrrsR'2 Ayy!17d3G)1a&!?w?KFQC! 9; ;88A;LFAqeeADe!GD!  a DAqH  #,0Aw?L Hl ++rnc |||jgf\}}}}td|}td}g} |jdD]\} } | j } ||j || dz gz }t ||j| | zz || zz } | gg}}td| D]}||dj ||z gz }td| dzD]#}|||dz | j |||z fgz }%td| D]<}t ||j\}}td|dzD]}|j|||z }|j|d}t||}t||}|j| }| j|}|j|\}}||j|zj|} |j|t|}t!|| j j||}t!|||z }| |z }| j#||||f? | S)rpuaT)allrr)rYrrsqf_list_includer:diffr rUrTr!r^r gcd half_gcdexremr-rr+)r=r>dummygenP_origQ_origrUr~rrKrwrdbriHr^jrDGDBgDwnumerdenomexponents rNrzrz`si&a?FFAq aA c AG""t",$91 IIK qvvaQ !! 6&**QT** +ad 2#r4q! 'A 1R5::a=1$& &A 'q!a% 5A qQx1!124 4D 5q! 9A!A$<..0DAq1a!e_ )AFFDQK( )Q AQ AQ AaAaA<<?DAqQUU1X""1%A4>*B1aiik..q!45E1q1u&EsH NNBuh7 8- 9$9L Nrnc|d}|d}|j}|dD]\}}}}t|trv|j|j} }|j|j} } | j | d|d} t t|| | |zz } |t|| ddz }|D]} ||| || |zz z }||zS)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 rrF)r quadratic) r: isinstancer variablesexprr^rtupler ) partial_listrIr{pfdrnfdfexannuadder&roots rNrqrqsp!_FAH    C&a - 2r2 a \\277B\\277BA1&B%)RBY/D 71d%@ @C -r$x4" ,, - - #:rn)NF)NN)!__doc__ sympy.corerrrrrrsympy.core.traversalr sympy.polysr r r r sympy.polys.polyerrorsrsympy.polys.polyoptionsrrsympy.polys.polytoolsrsympy.utilitiesrrrrr'r5r6rrrzrqrnrNrsK@?3552?9EE D+ D+N$N>,I,I,X@FLLrn