K indZddlmZddlmZmZmZddlmZddl m Z ddl m Z m Z mZddlmZddlmZdd lmZdd lmZdd lmZdd lmZmZmZmZdd lmZddlm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9d>dZ:dZ;d?dZdZ?dZ@d ZAd!ZBd"ZCd#ZDd$ZEd%ZFd&ZGd'ZHd(ZId)ZJd*ZKd+ZLd,ZMd?d-ZNd.ZO d@d/ZPGd0d1e ZQGd2d3e7ZRGd4d5eRZSGd6d7eSZTGd8d9eSZUGd:d;eSZVdAd=ZWy<)BzFormal Power Series) defaultdict)nanoozoo)Add)Expr) DerivativeFunctionexpand)Mul)Rational)Eq)Interval)S)WildDummysymbolsSymbol)sympify) convolution)binomial factorialrf)bell)floorfracceiling)MinMax) Piecewise)Limit)Order)sequence) SeriesBase)iterablecddlm}m}ddlm}|}g} t |dzD]H} | r|j |}|j|rtjtj} } ||||} | j|r| j} tj| D]}|j\}}|j|s| |z } -t|t r|j#|}|d}||dz}|j%\}}|j'|\}}|s| |z } |dj)|}|| z}|||zz}d|z|zt+||zdz |j-t.z|||zzz }| |z } | j0ry| j|s?| jt2s*| jt4s| jt6ryt | D]<}| ||zdzz } || |} | | j9| z j;|dz } >| j=||| z | | fcS| j?|Ky)a Rational algorithm for computing formula of coefficients of Formal Power Series of a function. Explanation =========== Applicable when f(x) or some derivative of f(x) is a rational function in x. :func:`rational_algorithm` uses :func:`~.apart` function for partial fraction decomposition. :func:`~.apart` by default uses 'undetermined coefficients method'. By setting ``full=True``, 'Bronstein's algorithm' can be used instead. Looks for derivative of a function up to 4'th order (by default). This can be overridden using order option. Parameters ========== x : Symbol order : int, optional Order of the derivative of ``f``, Default is 4. full : bool Returns ======= formula : Expr ind : Expr Independent terms. order : int full : bool Examples ======== >>> from sympy import log, atan >>> from sympy.series.formal import rational_algorithm as ra >>> from sympy.abc import x, k >>> ra(1 / (1 - x), x, k) (1, 0, 0) >>> ra(log(1 + x), x, k) (-1/((-1)**k*k), 0, 1) >>> ra(atan(x), x, k, full=True) ((-I/(2*(-I)**k) + I/(2*I**k))/k, 0, 1) Notes ===== By setting ``full=True``, range of admissible functions to be solved using ``rational_algorithm`` can be increased. This option should be used carefully as it can significantly slow down the computation as ``doit`` is performed on the :class:`~.RootSum` object returned by the :func:`~.apart` function. Use ``full=False`` whenever possible. See Also ======== sympy.polys.partfrac.apart References ========== .. [1] Formal Power Series - Dominik Gruntz, Wolfram Koepf .. [2] Power Series in Computer Algebra - Wolfram Koepf r)RootSumapart integrate)fullN) sympy.polysr'r(sympy.integralsr*rangediffis_rational_functionrZerohasdoitr make_argsas_numer_denom isinstancer as_independent as_base_exp as_coeff_addcoeffrrewriteris_zerorrrpoplimitsubsappend)fxkorderr,r'r(r*r1dsir<septermstnumdenindjaxtermxcaks Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/series/formal.pyrational_algorithmrUsXR+) D B 519 6 99Q>> from sympy import sin, cos >>> from sympy.series.formal import rational_independent >>> from sympy.abc import x >>> rational_independent([cos(x), sin(x)], x) [cos(x), sin(x)] >>> rational_independent([x**2, sin(x), x*sin(x), x**3], x) [x**3 + x**2, x*sin(x) + sin(x)] rr+N)r9 enumeratecancelr2rB) rJrDrNrKnrHtermdqs rTrational_independentr^s  !*C 12Y   Q  " ~ GAt##A&q)AQ A%%a(A!    JJqM  JrVc #> Kddlm}td|z fd}d}td|dzD]}||\}} |j }|j } t | } |st| |k(sKtt d|| d|D} | rd }| j| } | jd} | jjtdd} | jt|fyw) a Generates simple DE. Explanation =========== DE is of the form .. math:: f^k(x) + \sum\limits_{j=0}^{k-1} A_j f^j(x) = 0 where :math:`A_j` should be rational function in x. Generates DE's upto order 4 (default). DE's can also have free parameters. By increasing order, higher order DE's can be found. Yields a tuple of (DE, order). rlinsolveza:%dc \j|ttd|Dcgc]}|j|zc}z}j|ttd|Dcgc] }|j|z"c}z}||fScc}wcc}wNr)r1rr0)rErHeqDErPrCgrDs rT_makeDEzsimpleDE.._makeDEs VVAq\CU1a[!I!A$qvva|"3!IJ J qTYYq!_s5A;$OaQqT!A$))Aq/%9$OP P2v "J$Os B$ 5%B) Fr+c3.K|] }|D]}|ywN.0srHs rT zsimpleDE..sJQJ1qJqJNT)sympy.solvers.solvesetrarr0r as_ordered_termsr^lendictziprAr7factor as_coeff_mulr collect)rCrDrfrFrargfoundrErdrerJrNsolrPs``` @rTsimpleDErzs (0% !A E 1eai 2B YY[##%"5!, CHMs1J(3"1*>JKLCWWS\""$Q'B))*5a8;B**Z!-.1 1 2s A8D?BDcltj}|jtj }d}t j |D]O}|j|\}}t|tr |j} nd} || |kr| }||||| zzz }Q|r|j|||z }|S)aConverts a DE with constant coefficients (explike) into a RE. Explanation =========== Performs the substitution: .. math:: f^j(x) \to r(k + j) Normalises the terms so that lowest order of a term is always r(k). Examples ======== >>> from sympy import Function, Derivative >>> from sympy.series.formal import exp_re >>> from sympy.abc import x, k >>> f, r = Function('f'), Function('r') >>> exp_re(-f(x) + Derivative(f(x)), r, k) -r(k) + r(k + 1) >>> exp_re(Derivative(f(x), x) + Derivative(f(x), (x, 2)), r, k) r(k) + r(k + 1) See Also ======== sympy.series.formal.hyper_re Nr) rr3atomsr r?rr6r9r8r derivative_countrA) rerrERErfminirKr<r\rOs rTexp_rers> B  A D ]]2 ##A&q a $""AA <1t8D eaAh WWQD ! IrVctj}|jtj }|jt j }d}t j|jD]}|j|\}} |j|\} } | j|d} t| tr | j} nd} || t|dz| z | z||| z| z zz }| | | z |ks| | z }|j|||z }t!d}|j#|||zS)a Converts a DE into a RE. Explanation =========== Performs the substitution: .. math:: x^l f^j(x) \to (k + 1 - l)_j . a_{k + j - l} Normalises the terms so that lowest order of a term is always r(k). Examples ======== >>> from sympy import Function, Derivative >>> from sympy.series.formal import hyper_re >>> from sympy.abc import x, k >>> f, r = Function('f'), Function('r') >>> hyper_re(-f(x) + Derivative(f(x)), r, k) (k + 1)*r(k + 1) - r(k) >>> hyper_re(-x*f(x) + Derivative(f(x), (x, 2)), r, k) (k + 2)*(k + 3)*r(k + 3) - r(k) See Also ======== sympy.series.formal.exp_re Nr+rm)rr3r|r r?rrr6r r9as_coeff_exponentr8r r}rrArrw)rer~rErrfrDrrKr<r\cvlrOrs rThyper_rer)s2@ B  A A D ]]299; ' ##A&q##A&1   "1 % a $""AA a"QUQY""Qq1uqy\11 <1q54<q5D  AH B S A ::aAh rVct||| zz}|j|||z}|j|||z}||||fSrirA)rCrDPQrErshifts rT_transformation_arasHeVA q!e)A q!e)A aA:rVc|j|||z}|j|||z }|j|||z }||z}||||fSrir)rCrDrrrErscales rT_transformation_crhsU q!U(A q!e)A q!e)AJA aA:rVc|j|}|j||dz||zdzz}|j||dz|dzz}||||fSNr+)r1rA)rCrDrrrErs rT_transformation_erps[ q A q!a%AEAI&A q!a%AE"A aA:rVcB|Dcgc] \}}|||zfc}}Scc}}wrirj)ryrresconds rT _apply_shiftrw#14 5ICS$,  55 5cB|Dcgc] \}}|||z fc}}Scc}}wrirj)ryrrrs rT _apply_scaler{rrc |Dcgc]5\}}||dz|jdj|zz |dzf7c}}Scc}}wr) as_coeff_Addr<)ryrDrErrs rT_apply_integratersV  "TTAX 1 1 3A 6 < rAleadtermr itemsrrB)rCrDrrrErk_maxrryrHr~ktermrpr]c1c2muls rT_compute_formulars! C 519eai!m ,! Ed?  FF1aL  q! $y| 3 99 !a FF1e  FF1e  VVAqs^ $ $Q ' * VVAqs^ $ $Q ' * b1} s%1+2C2C2EF3RAY^FGG s%1+2C2C2EF3RAY^FGG C< '!* J GFs FFc Nddlm}m}ddlm}||||||} } t | } | j | || jD cgc]$\} } | jr| jd&c} } }t|||||||\}}}}|||} | rt| j}ntj}||z}t|||||||\}}}}||zj!|d}t#|t$s|dk7ry|||} | rt'| j}ntj}tjt( }}t+||zdzD]}|j-||j!|dt/|z } | j0dur|}t|||||||\}}}}t3||||||\}}}}t5||||||\}}}t7|||}t9||}|||}|||z j!|dz }|dz }|||fcS| s|| |||zzzz }t;||z|}||kDs|}|j=||d|z z}t?|||||||}t9||}tA||}|||fScc} } w)a Recursive wrapper to rsolve_hypergeometric. Explanation =========== Returns a Tuple of (formula, series independent terms, maximum power of x in independent terms) if successful otherwise ``None``. See :func:`rsolve_hypergeometric` for details. r)lcmrr)r+NF)!r.rrr/r*rsupdater is_rationalr7rrkeysrr3rr@r8r!rrr0r1r is_finiter_rsolve_hypergeometricrrr rArr)rCrDrrrErrrr*prootsqroots all_rootsr~rKrk_minrrrrNmprHold_frypow_xs rTrrs')1a[%1+FFV I V 9??3D#41aMM!!#A&# $E"1aAq!U;JAq!Q1a[F V[[]# AIE"1aAq!U;JAq!Q 1 AqA a AF 1a[F V[[]#ffrcC 519q= ! FF1aL  q! $y| 3 ;;% E*1aAq!Q?JAq!Q*1aAq!>> from sympy import exp, ln, S >>> from sympy.series.formal import rsolve_hypergeometric as rh >>> from sympy.abc import x, k >>> rh(exp(x), x, -S.One, (k + 1), k, 1) (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1) >>> rh(ln(1 + x), x, k**2, k*(k + 1), k, 1) (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1), Eq(Mod(k, 1), 0)), (0, True)), x, 2) References ========== .. [1] Formal Power Series - Dominik Gruntz, Wolfram Koepf .. [2] Power Series in Computer Algebra - Wolfram Koepf Nc"tjSrirr3rjrVrTz'rsolve_hypergeometric.. s 166rVFTr+rr-)rrrr< is_integerrrrArrrBrr3r rrsumr#)rCrDrrrErresultsol_listrNrsol_dictrrrOmkrryrms rTrsolve_hypergeometricrsb$Aq!Q1 5F ~Hc2>*H  T!!#2 HHQK <<5 1d1g: CaAhhq1q5A+&!a%Q# )1(8 994C; 9C 9JJ~ S/C bSy FF %  BK F 1u s8C!Q$JAr 344 FF a=# :sE6cJtj|}t|dk(rt|j t }t |j|\}}|djd|djdz } | dkr||}}t| } t|||||| Sy)z;See docstring of :func:`rsolve_hypergeometric` for details.r+rN) rr6rrlistr|r mapr<argsabsr) rCrDrrfrErJgsrrrs rT_solve_hyper_RErAs MM" E 5zQ "((8$ %288R 1 qEJJqMBqEJJqM ) q5aqAAA$Q1aA66rVcddlm}tj|D]$}|j |\}}|j s$yt |||} i} tttj| D]@} | r|j|}|j|d| ||j|| <B|| ||| } | r-| t|z tjtjfSy)z%Solves DE with constant coefficients.rrsolveN) sympy.solversrrr6r9 free_symbolsrr0rrr1r@rArrr3) rCrDrerfrErrKr<r\rinitrHrys rT_solve_explike_DErOs$ ]]2 ##A&q     Aq B D 3s}}R() *. q A !1 QqTYYq!_. QqT4 C il"AFFAFF33 rVcddlm}t|||}i}tt t j |D]L}|r|j|}|j|dt|z |||j||<N|||||} | r!| tjtjfSy)z4Converts DE into RE and solves using :func:`rsolve`.rrN) rrrr0rrrr6r1r@rrArr3) rCrDrerfrErrrrHrys rT _solve_simplerfs$ "a B D 3s}}R() *= q A !1 ! <QqTYYq!_= QqT4 C QVVQVV$$ rVc ddlm}g}|jt||||}t |D]j}|jt||||} | |z j j |} |jtj| lg} |D]<} | j|rn+| jts,| j| >| }|rtt|d||t|D} | rZ|j!| }|j#j%tdd}|j t||}|S)zDConverts DE with free parameters into DE with constant coefficients.rr`c3.K|] }|D]}|ywrirjrks rTrnz(_transform_explike_DE..sMA1MaaMaMror+)rprar<r r0r rwextendrr6r4rrBrsrtrrArurv) rerfrDrFsymsrard highest_coeffrHr<temperys rT_transform_explike_DErxs8/ BHHZ!a78M 5\(AaD!Q/0&..088; #--&'( D  558  UU6] KKN   3tM"d4j)AMNO B))*5a8;BJqt,-B IrVc Vddlm}t|||}td|Dcgc]}|j |||z}}t t |d||t|D} | rtd} |j| }|jjdj||| z}|j|dd}t|D]7}|j |||zs|s!|j|||z }|S|Scc}w)z@Converts DE with free parameters into RE of hypergeometric type.rr`r+c3.K|] }|D]}|ywrirjrks rTrnz#_transform_DE_RE..sIqI!!I!Iror)rprarr0r<rsrtrrrArur7rwrv) rerfrErFrrarrHrdryrs rT_transform_DE_RErs / "a B&+Auo 6"((1QU8  6B 6 s4IXb$t*%=IJ KC I WWS\ YY[ ' ' )! , 4 4Qq1uX > __Q  "1 %u Axx!a%!aWWQA& I   I 7s!D&c^d}|jj||h}|rt|||||}n t|||}|jj|hst |||||}|r|S|rt |||||}|jj|hst |||||}|r|Sy)aA Solves the DE. Explanation =========== Tries to solve DE by either converting into a RE containing two terms or converting into a DE having constant coefficients. Returns ======= formula : Expr ind : Expr Independent terms. order : int Examples ======== >>> from sympy import Derivative as D, Function >>> from sympy import exp, ln >>> from sympy.series.formal import solve_de >>> from sympy.abc import x, k >>> f = Function('f') >>> solve_de(exp(x), x, D(f(x), x) - f(x), 1, f, k) (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1) >>> solve_de(ln(1 + x), x, (x + 1)*D(f(x), x, 2) + D(f(x)), 2, f, k) (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1), Eq(Mod(k, 1), 0)), (0, True)), x, 2) N)r differencerrrrr) rCrDrerFrfrEryrrs rTsolve_dersD C ?? % %q!f -D b!Qt 4 b!Q  ?? % %qc *aB1-   "2q!UD 9 ?? % %qc *1b!Q/   rVc td}g}d}t||||D]K\}}|t||||||}|r|cS|jj |hr;|j |M|D]}t |||||}|s|cSy)a Hypergeometric algorithm for computing Formal Power Series. Explanation =========== Steps: * Generates DE * Convert the DE into RE * Solves the RE Examples ======== >>> from sympy import exp, ln >>> from sympy.series.formal import hyper_algorithm >>> from sympy.abc import x, k >>> hyper_algorithm(exp(x), x, k) (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1) >>> hyper_algorithm(ln(1 + x), x, k) (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1), Eq(Mod(k, 1), 0)), (0, True)), x, 2) See Also ======== sympy.series.formal.simpleDE sympy.series.formal.solve_de rfN)r rzrrrrBr) rCrDrErFrfdesryrerHs rThyper_algorithmrsB  A C C!Q5)A >1aQ1-C J))1#. JJrN Aq"a+ JrVc |tjtjfvr|tjurtjntj }|j |d|z }t ||d|||||} | y| d| dj |d|z | dj |d|z fS|s|tj k(r|tj k(r | |z} | } |} n ||z} |} | } |j || }t ||dtj||||} | y| d| dj || | z| dj || | zfS|j |rVtd} tt||| | dtf}t|| z| dtf}|j|d}|||fSt|tr&d} ttjdtf}tjd}}tj|D]}t ||dtj||||}|r| sd} |d}|dj |j kDr|}|j |dj }}n |d}|dj |j }}tt#|d||z |||Dcgc] }|d|dzc}}||dz }||d|zz }||z }| r|||fSy|j$j'|h}t)|j*|\}}d} td} |rt-||| ||} | |rt/||| |} | yddlm}|j4rtj}n||}t| d| | dtf}||| z|z}t|| dtf}|| d|z}|||fScc}w) zPRecursive wrapper to compute fps. See :func:`compute_fps` for details. r+rNrrEFT)powsimp)rInfinityNegativeInfinityOnerA _compute_fps is_polynomialrr#Coeffrr<r8rr3r6startrtrrr r9rUrsympy.simplify.powsimprr>)rCrDx0dirhyperrFrationalr,rrreprep2rep2brErSxkrNrKrseqrmzsaversymbr xk_formulas rTrrs  ajj!,, --QZZ'aeeaeeVvva1~dAq#ueXtL >q 6!9>>!QqS16!9>>!QqS3IJJ squuf} 155&="r'C2DEb&CDCEvva~dAq!%%xN >q 6!9>>!TE\:q q$,/1 1 q #J eAq!nq!Rj 1 adQ2J 'ggam2s{ !S affq"g &&&$Rq! Aq!QueXtLC!FQBq6<<"((*C88SV\\qAa&Cq6<<qAC1q5NBqG0LM1QqT!A$YMNc!f s1v}$q! " r3;  >> $ $aS )D(q (($/IQ F c A#Aq!UD9 ~% Aq%0 ~. ||uut} &)aB/ 0BA%J *q!Rj )B &)d" #C r3;KNsO c Xt|}t|}|j|syt|}|dk(rtj}nP|dk(rtj }n9|tjtj fvr t dt|}t ||||||||S)a Computes the formula for Formal Power Series of a function. Explanation =========== Tries to compute the formula by applying the following techniques (in order): * rational_algorithm * Hypergeometric algorithm Parameters ========== x : Symbol x0 : number, optional Point to perform series expansion about. Default is 0. dir : {1, -1, '+', '-'}, optional If dir is 1 or '+' the series is calculated from the right and for -1 or '-' the series is calculated from the left. For smooth functions this flag will not alter the results. Default is 1. hyper : {True, False}, optional Set hyper to False to skip the hypergeometric algorithm. By default it is set to False. order : int, optional Order of the derivative of ``f``, Default is 4. rational : {True, False}, optional Set rational to False to skip rational algorithm. By default it is set to True. full : {True, False}, optional Set full to True to increase the range of rational algorithm. See :func:`rational_algorithm` for details. By default it is set to False. Returns ======= ak : sequence Sequence of coefficients. xk : sequence Sequence of powers of x. ind : Expr Independent terms. mul : Pow Common terms. See Also ======== sympy.series.formal.rational_algorithm sympy.series.formal.hyper_algorithm N+-zDir must be '+' or '-')rr4rr ValueErrorr)rCrDrrrrFrr,s rT compute_fpsrssn  A A 558 B czee uuf QUUQUUFO #122cl 1b#ueXt DDrVc eZdZdZedZy)rzP Coeff(p, x, n) represents the nth coefficient of the polynomial p in x cd|j|r|jr|j||Syyri)rrr<)clsrrDrZs rTevalz Coeff.evals, ??1 !,,771a= #/ rVN)__name__ __module__ __qualname____doc__ classmethodrrjrVrTrrs!!rVrc`eZdZdZdZdZedZedZedZ edZ edZ ed Z ed Z ed Zed Zed ZedZedZdZd%dZd%dZdZdZdZdZdZd&dZd'dZdZd'dZd'dZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&y)(FormalPowerSeriesa5 Represents Formal Power Series of a function. Explanation =========== No computation is performed. This class should only to be used to represent a series. No checks are performed. For computing a series use :func:`fps`. See Also ======== sympy.series.formal.fps cNtt|}tj|g|Sri)rrr__new__)rrs rTrzFormalPowerSeries.__new__s"7D!||C'$''rVc$|dd}|jd}t|j|dtf|_tt ||dtf|_|j|j z|_td|dtf|_y)Nrr+)r-r+) variablesr#formularak_seqrfact_seqbell_coeff_seqsign_seq)selfrrSrEs rT__init__zFormalPowerSeries.__init__su !WQZ LLOrzzAq":6  11bz: "kkDMM9 1a*5 rVc |jdSrcrrs rTfunctionzFormalPowerSeries.functionyy|rVc |jdSrrrs rTrDzFormalPowerSeries.xrrVc |jdS)Nrrrs rTrzFormalPowerSeries.x0rrVc |jdS)Nrrs rTrzFormalPowerSeries.dirrrVc&|jddS)Nrrrrs rTrSzFormalPowerSeries.akyy|ArVc&|jddS)Nrr+rrs rTrzFormalPowerSeries.xkr#rVc&|jddS)Nrrrrs rTrNzFormalPowerSeries.indr#rVc"tdtSrc)rrrs rTintervalzFormalPowerSeries.intervals2rVc.|jjSri)r'infrs rTrzFormalPowerSeries.start}}   rVc.|jjSri)r'suprs rTstopzFormalPowerSeries.stop r*rVctSri)rrs rTlengthzFormalPowerSeries.length s rVcddlm}|j|j}}|jd}||j |j z||j |jf}|j|zS)z0Returns an infinite representation of the seriesr)Sum) sympy.concreter1rSrrrrr-rN)rr1rSrrEinf_sums rTinfinitezFormalPowerSeries.infinites\ '$''B LLObjj2::-288RWW/EFxx'!!rVc|j|jdj\}}|j|jstj S|S)z!Returns the power of x in a term.r+)r9rDr:r4rr3)rr[rQrs rT _get_pow_xzFormalPowerSeries._get_pow_xsF**466215AAC uyy 66M rVcZg}|j}t|D]\}}|j|}|j|r|j|d}||k\r t|S|j dur||dzk(r t|S|t jusx|j|t|S)z Truncated series as polynomial. Explanation =========== Returns series expansion of ``f`` upto order ``O(x**n)`` as a polynomial(without ``O`` term). rTr+) rrXr6r4r;rrr3rBr)rrZrJsymrHrKxps rT polynomialzFormalPowerSeries.polynomial"sdO DAq#Brvvs|$R__c*1-Qw E{ $&1A:E{!&& Q E{rVc| t|S|j|j}}|jj |}|t j urt j}|j|t|||fzS)z Truncated series. Explanation =========== Returns truncated series expansion of f upto order ``O(x**n)``. If n is ``None``, returns an infinite iterator. ) iterrDrrr<rrrr:r")rrZrDrpt_xks rTtruncatezFormalPowerSeries.truncate;sl 9: 2 a  ## #Bq!E%!R$999rVc$|jdSrc) _eval_termrs rT zero_coeffzFormalPowerSeries.zero_coeffQsq!!rVcX |jj|}|jj|j}||z}|jrt j }|j}tj|jD]X}|j|}|j|r|j|d}|dk(r |dkr||z }E||k\sK||dzksT||z }Z||z }|j|jS#t$rt j }YwxYwNrr+)rr<rSsimplify IndexErrorrr3rNrrr6r6r4r;rwrD) rptr=pt_akr[rNr8rKrs rTr@zFormalPowerSeries._eval_termTs #GGMM"%EGGMM"%..0EEMD 88&&C##C]]488, *599c?.E..4Q7E7uqy1HCb[UR!V^1HC  CKD||DFF##% 66D sAD D)(D)cB|j}|j|r|Syri)rDr4)roldnewrDs rT _eval_subszFormalPowerSeries._eval_subsls FF 771:K rVc>|D]}|tjus|cSyrir)rrDlogxcdirrKs rT_eval_as_leading_termz'FormalPowerSeries._eval_as_leading_termqs! A rVc |jj|}|jj|}|j|jj }|j }|jd}|j j|rg}|j jD]^\}} tj} tj|D]} |j| } | | || zzz } |j| | f`t|}t!|j#||dz||j$dz |j&f}nFt!|j |zj#||dz||j$dz |j&f}|j)||j*|j,|j.||j|fSrC)rr1rNr6rrrSrr4rrr3rr6rBr r#rArr-funcrDrr) rrDrCrNpow_xkrSrEformrrrrKrs rT_eval_derivativez"FormalPowerSeries._eval_derivativevs~ MM  q !hhmmA1 WW LLO ::>>! D  '1vvq)1A OOA.EA%00D1 T1I&  ' d#D$))Aq1u-288a</IJB2::.44QA>bhhlBGG46ByyDFFDGGTXXDGGS7IJJrVNc (ddlm}| |j}nt|r||j|S||j|}||j |}|||z j |dz }|j|jj}|j}|jd}|jj|rg} |jjD]a\} } tj} t!j"| D]!} |j| }| | ||zdzz z } #| j%| | fct'| } t)| j+||dz ||j,dz|j.f}nIt)|j|dzz j+||dz ||j,dz|j.f}|j1||j|j2|j4||j|fS)aK Integrate Formal Power Series. Examples ======== >>> from sympy import fps, sin, integrate >>> from sympy.abc import x >>> f = fps(sin(x)) >>> f.integrate(x).truncate() -1 + x**2/2 - x**4/24 + O(x**6) >>> integrate(f, (x, 0, 1)) 1 - cos(1) rr)r+)r/r*rDr%rrNr@r6rrrSrr4rrr3rr6rBr r#rArr-rQrr)rrDkwargsr*rCrNrRrSrErSrrrrKrs rTr*zFormalPowerSeries.integrates . 9A a[T]]A. . dmmQ '!$ Cq!$$1 WW LLO ::>>! D  '1vvq)5A OOA.EA%!!344D5 T1I&  ' d#D$))Aq1u-288a</IJB2::!4::1a!eDbhhlBGG46ByyDFFDGGTXXDGGS7IJJrVcX| t|St|}t|ts t d|j |j k7r t d|j |j k7r t d|j|jk7r t dt||S)a Multiplies two Formal Power Series, using discrete convolution and return the truncated terms upto specified order. Parameters ========== n : Number, optional Specifies the order of the term up to which the polynomial should be truncated. Examples ======== >>> from sympy import fps, sin, exp >>> from sympy.abc import x >>> f1 = fps(sin(x)) >>> f2 = fps(exp(x)) >>> f1.product(f2, x).truncate(4) x + x**2 + x**3/3 + O(x**4) See Also ======== sympy.discrete.convolutions sympy.series.formal.FormalPowerSeriesProduct =Both series should be an instance of FormalPowerSeries class.9Both series should be calculated from the same direction.6Both series should be calculated about the same point.(Both series should have the same symbol.) r<rr8r rrrrDFormalPowerSeriesProductrotherrDrZs rTproductzFormalPowerSeries.products> 9: %!23'( ( 88uyy 01 1 WW ,- -VVuww GH H'e44rVc td|dzDcgc]+}t||t|jd||z dz-}}t d}t t||dt fScc}w)au self.coeff_bell(n) returns a sequence of Bell polynomials of the second kind. Note that ``n`` should be a integer. The second kind of Bell polynomials (are sometimes called "partial" Bell polynomials or incomplete Bell polynomials) are defined as .. math:: B_{n,k}(x_1, x_2,\dotsc x_{n-k+1}) = \sum_{j_1+j_2+j_2+\dotsb=k \atop j_1+2j_2+3j_2+\dotsb=n} \frac{n!}{j_1!j_2!\dotsb j_{n-k+1}!} \left(\frac{x_1}{1!} \right)^{j_1} \left(\frac{x_2}{2!} \right)^{j_2} \dotsb \left(\frac{x_{n-k+1}}{(n-k+1)!} \right) ^{j_{n-k+1}}. * ``bell(n, k, (x1, x2, ...))`` gives Bell polynomials of the second kind, `B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1})`. See Also ======== sympy.functions.combinatorial.numbers.bell r+NrE)r0rtuplerrr#r)rrZrO inner_coeffsrEs rT coeff_bellzFormalPowerSeries.coeff_bellsr4QVVWYZ[\Y\P]^1Q5)<)>> from sympy import fps, sin, exp >>> from sympy.abc import x >>> f1 = fps(exp(x)) >>> f2 = fps(sin(x)) >>> f1.compose(f2, x).truncate() 1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6) >>> f1.compose(f2, x).truncate(8) 1 + x + x**2/2 - x**4/8 - x**5/15 - x**6/240 + x**7/90 + O(x**8) See Also ======== sympy.functions.combinatorial.numbers.bell sympy.series.formal.FormalPowerSeriesCompose References ========== .. [1] Comtet, Louis: Advanced combinatorics; the art of finite and infinite expansions. Reidel, 1974. rXrYrZr[rz\The formal power series of the inner function should not have any constant coefficient term.) r<rr8r rrrrDr@rvrr3FormalPowerSeriesComposer]s rTcomposezFormalPowerSeries.compose sb 9: %!23'( ( 88uyy 01 1 WW ,- -VVuww GH H   A  + +EGG 4Q 7qvv E-. .(e44rVc~| t|S|jdjr tdt |S)a Returns the truncated terms of the inverse of the formal power series, up to specified ``n``. Explanation =========== If ``f`` and ``g`` are two formal power series of two different functions, then the coefficient sequence ``ak`` of the composed formal power series ``fp`` will be as follows. .. math:: \sum\limits_{k=0}^{n} (-1)^{k} x_0^{-k-1} B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1}) Parameters ========== n : Number, optional Specifies the order of the term up to which the polynomial should be truncated. Examples ======== >>> from sympy import fps, exp, cos >>> from sympy.abc import x >>> f1 = fps(exp(x)) >>> f2 = fps(cos(x)) >>> f1.inverse(x).truncate() 1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + O(x**6) >>> f2.inverse(x).truncate(8) 1 + x**2/2 + 5*x**4/24 + 61*x**6/720 + O(x**8) See Also ======== sympy.functions.combinatorial.numbers.bell sympy.series.formal.FormalPowerSeriesInverse References ========== .. [1] Comtet, Louis: Advanced combinatorics; the art of finite and infinite expansions. Reidel, 1974. rzSConstant coefficient should exist for an inverse of a formal power series to exist.)r<r@r>rFormalPowerSeriesInverse)rrDrZs rTinversezFormalPowerSeries.inverseWsEb 9:  ??1  % %*+ +(--rVc 2t|}t|tr|j|jk7r t d|j |j k7r t d|j |j }}|j|jj||z}|j |jvr|S|j|jz}|jj|jjkDr9|j}|jj|jj}}n8|j}|jj|jj}}tt|d||z |j||D cgc] } | d| dzc} } |j|jz| z} |j!|||j |j||j| fS|j#|j sh|j|z}|j|z} |j!||j |j |j|j|j| fSt||Scc} w)NrYrZrr+)rr8r rrrrDrrArrSrrrtrrNrQr4) rr^rDyrCrSrrmrrrrNs rT__add__zFormalPowerSeries.__add__s e. /xx599$ "455EHH$ "01166577qA  3 3Aq 99AvvQ^^+588#Bww}}uxx~~-hhxx~~tww}}1ggww}}ehhnn1CAq1u! ,MNq1adNOD((UYY&-C99Q477DHHr477C6HI I466" %A((U"C99Q"ggtww46 64OsJc$|j|Srirlrr^s rT__radd__zFormalPowerSeries.__radd__||E""rVc |j|j |j|j|j|j |j |j fSri)rQrrDrrrSrrNrs rT__neg__zFormalPowerSeries.__neg__sGyy$--$((77(DGGdhhY79 9rVc&|j| Srirnros rT__sub__zFormalPowerSeries.__sub__s||UF##rVc&| j|Srirnros rT__rsub__zFormalPowerSeries.__rsub__su%%rVc Xt|}|j|jr t||S|j|z}|j j |}|j|z}|j||j|j|j||j|fSri) rr4rDr rrS coeff_mulrNrQrrr)rr^rCrSrNs rT__mul__zFormalPowerSeries.__mul__s 99TVV tU# # MME ! WW  u %hhyyDFFDGGTXXDGGS7IJJrVc$|j|Sri)rzros rT__rmul__zFormalPowerSeries.__rmul__rqrVri)Nr~)'rrrr rrpropertyrrDrrrSrrNr'rr-r/r4r6r:r>rAr@rKrOrTr*r_rcrfrirlrprsrurwrzr|rjrVrTr r sn (6!!!!""2:,"$0  K.+KZ25h9>H5T8.t$ L#9$& K#rVr ceZdZdZdZedZedZedZedZ edZ dZ d Z d Z dd Zd Zd Zy)FiniteFormalPowerSeriesz3Base Class for Product, Compose and Inverse classescyrirj)rrs rTrz FiniteFormalPowerSeries.__init__s rVc |jdSrcrrs rTffpszFiniteFormalPowerSeries.ffpsrrVc |jdSrrrs rTgfpszFiniteFormalPowerSeries.gfpsrrVc.|jjSri)rrrs rTrCzFiniteFormalPowerSeries.fyy!!!rVc.|jjSri)rrrs rTrfzFiniteFormalPowerSeries.grrVctd)NzCNo infinite version for an object of FiniteFormalPowerSeries class.NotImplementedErrorrs rTr4z FiniteFormalPowerSeries.infinites!#78 8rVctd|z)Nz(%s)._eval_terms()rrrZs rT _eval_termsz#FiniteFormalPowerSeries._eval_termss!"6"=>>rVctd)Nz]By the current logic, one can get termsupto a certain order, instead of getting term by term.r)rrFs rTr@z"FiniteFormalPowerSeries._eval_terms!#\] ]rVc$|j|Sri)rrs rTr:z"FiniteFormalPowerSeries.polynomials""rVc|j}|jj|}|j|j}}|j |t |||fzSri)rrr<rDrr:r")rrZrr=rDrs rTr>z FiniteFormalPowerSeries.truncatesMyy a 2q!E%!R$999rVctrirrrDs rTrTz(FiniteFormalPowerSeries._eval_derivative!!rVctrirrs rTr*z!FiniteFormalPowerSeries.integraterrVNr})rrrr rrrrrCrfr4rr@r:r>rTr*rjrVrTrrs= """"88?]#:""rVrc,eZdZdZdZedZdZy)r\aRepresents the product of two formal power series of two functions. Explanation =========== No computation is performed. Terms are calculated using a term by term logic, instead of a point by point logic. There are two differences between a :obj:`FormalPowerSeries` object and a :obj:`FormalPowerSeriesProduct` object. The first argument contains the two functions involved in the product. Also, the coefficient sequence contains both the coefficient sequence of the formal power series of the involved functions. See Also ======== sympy.series.formal.FormalPowerSeries sympy.series.formal.FiniteFormalPowerSeries cH|j|j}}|jjd}t |jj |dt f|_|jjd}t |jj |dt f|_yrc) rrrSrr#rrcoeff1coeff2)rrrrrEs rTrz!FormalPowerSeriesProduct.__init__ssYY d GG  a twwAr ; GG  a twwAr ; rVc4|j|jzS)z3Function of the product of two formal power series.)rCrfrs rTrz!FormalPowerSeriesProduct.function&svvrVc|j|j}}t|d||d|}g}td|D]<}|j |||j j j|z>t|S)a Returns the first ``n`` terms of the product formal power series. Term by term logic is implemented here. Examples ======== >>> from sympy import fps, sin, exp >>> from sympy.abc import x >>> f1 = fps(sin(x)) >>> f2 = fps(exp(x)) >>> fprod = f1.product(f2, x) >>> fprod._eval_terms(4) x**3/3 + x**2 + x See Also ======== sympy.series.formal.FormalPowerSeries.product Nr) rrrr0rBrrr<r)rrZrraksrJrHs rTrz$FormalPowerSeriesProduct._eval_terms+s{.dkk&!*fRaj1q! 9A LLQ$)),,"4"4Q"77 8 9E{rVN)rrrr rrrrrjrVrTr\r\s%*<rVr\c&eZdZdZedZdZy)rea Represents the composed formal power series of two functions. Explanation =========== No computation is performed. Terms are calculated using a term by term logic, instead of a point by point logic. There are two differences between a :obj:`FormalPowerSeries` object and a :obj:`FormalPowerSeriesCompose` object. The first argument contains the outer function and the inner function involved in the omposition. Also, the coefficient sequence contains the generic sequence which is to be multiplied by a custom ``bell_seq`` finite sequence. The finite terms will then be added up to get the final terms. See Also ======== sympy.series.formal.FormalPowerSeries sympy.series.formal.FiniteFormalPowerSeries c|j|j|jj}}}|j ||S)z.Function for the composed formal power series.)rCrfrrDrA)rrCrfrDs rTrz!FormalPowerSeriesCompose.functionfs1&&$&&$))++a1vva|rVcX|j|j}}|jg}td|D]k}|j |}|j |z}|j t|d||j|dz z |jj|zmt|S)a Returns the first `n` terms of the composed formal power series. Term by term logic is implemented here. Explanation =========== The coefficient sequence of the :obj:`FormalPowerSeriesCompose` object is the generic sequence. It is multiplied by ``bell_seq`` to get a sequence, whose terms are added up to get the final terms for the polynomial. Examples ======== >>> from sympy import fps, sin, exp >>> from sympy.abc import x >>> f1 = fps(exp(x)) >>> f2 = fps(sin(x)) >>> fcomp = f1.compose(f2, x) >>> fcomp._eval_terms(6) -x**5/15 - x**4/8 + x**2/2 + x + 1 >>> fcomp._eval_terms(8) x**7/90 - x**6/240 - x**5/15 - x**4/8 + x**2/2 + x + 1 See Also ======== sympy.series.formal.FormalPowerSeries.compose sympy.series.formal.FormalPowerSeries.coeff_bell r+N) rrrAr0rcrrBrrrr<)rrZrrrJrHbell_seqrs rTrz$FormalPowerSeriesCompose._eval_termslsFYY d"#q! RAq)H&&1C LLs2Aw4==1+== a@PP Q R E{rVN)rrrr rrrrjrVrTrereMs 0 +rVrecLeZdZdZdZedZedZedZdZ y)rha Represents the Inverse of a formal power series. Explanation =========== No computation is performed. Terms are calculated using a term by term logic, instead of a point by point logic. There is a single difference between a :obj:`FormalPowerSeries` object and a :obj:`FormalPowerSeriesInverse` object. The coefficient sequence contains the generic sequence which is to be multiplied by a custom ``bell_seq`` finite sequence. The finite terms will then be added up to get the final terms. See Also ======== sympy.series.formal.FormalPowerSeries sympy.series.formal.FiniteFormalPowerSeries c|j}|jjd}|j}t ||dz z|dt f}|j |jz|z|_yrC) rrrrAr#rrraux_seq)rrrrEinvinv_seqs rTrz!FormalPowerSeriesInverse.__init__sayy GG  a oo3QU8,q!Rj9}}t}}4w> rVc$|j}d|z S)z2Function for the inverse of a formal power series.r+)rC)rrCs rTrz!FormalPowerSeriesInverse.functions FF1u rVctdNzQOnly one function is considered while performinginverse of a formal power series.rrs rTrfzFormalPowerSeriesInverse.g<= =rVctdrrrs rTrzFormalPowerSeriesInverse.gfpsrrVc@|j}|jg}td|D]k}|j|}|j|z}|j t |d||j|dz z |jj|zmt |S)a Returns the first ``n`` terms of the composed formal power series. Term by term logic is implemented here. Explanation =========== The coefficient sequence of the `FormalPowerSeriesInverse` object is the generic sequence. It is multiplied by ``bell_seq`` to get a sequence, whose terms are added up to get the final terms for the polynomial. Examples ======== >>> from sympy import fps, exp, cos >>> from sympy.abc import x >>> f1 = fps(exp(x)) >>> f2 = fps(cos(x)) >>> finv1, finv2 = f1.inverse(), f2.inverse() >>> finv1._eval_terms(6) -x**5/120 + x**4/24 - x**3/6 + x**2/2 - x + 1 >>> finv2._eval_terms(8) 61*x**6/720 + 5*x**4/24 + x**2/2 + 1 See Also ======== sympy.series.formal.FormalPowerSeries.inverse sympy.series.formal.FormalPowerSeries.coeff_bell r+N) rrAr0rcrrBrrrr<)rrZrrJrHrrs rTrz$FormalPowerSeriesInverse._eval_termssDyy"#q! RAq)H<<(*C LLs2Aw4==1+== a@PP Q R E{rVN) rrrr rrrrfrrrjrVrTrhrhsM*? ====*rVrhNc t|}|:|j}t|dk(r|j}n|s|St dt ||||||||} | |St ||||| S)a Generates Formal Power Series of ``f``. Explanation =========== Returns the formal series expansion of ``f`` around ``x = x0`` with respect to ``x`` in the form of a ``FormalPowerSeries`` object. Formal Power Series is represented using an explicit formula computed using different algorithms. See :func:`compute_fps` for the more details regarding the computation of formula. Parameters ========== x : Symbol, optional If x is None and ``f`` is univariate, the univariate symbols will be supplied, otherwise an error will be raised. x0 : number, optional Point to perform series expansion about. Default is 0. dir : {1, -1, '+', '-'}, optional If dir is 1 or '+' the series is calculated from the right and for -1 or '-' the series is calculated from the left. For smooth functions this flag will not alter the results. Default is 1. hyper : {True, False}, optional Set hyper to False to skip the hypergeometric algorithm. By default it is set to False. order : int, optional Order of the derivative of ``f``, Default is 4. rational : {True, False}, optional Set rational to False to skip rational algorithm. By default it is set to True. full : {True, False}, optional Set full to True to increase the range of rational algorithm. See :func:`rational_algorithm` for details. By default it is set to False. Examples ======== >>> from sympy import fps, ln, atan, sin >>> from sympy.abc import x, n Rational Functions >>> fps(ln(1 + x)).truncate() x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6) >>> fps(atan(x), full=True).truncate() x - x**3/3 + x**5/5 + O(x**6) Symbolic Functions >>> fps(x**n*sin(x**2), x).truncate(8) -x**(n + 6)/6 + x**(n + 2) + O(x**(n + 8)) See Also ======== sympy.series.formal.FormalPowerSeries sympy.series.formal.compute_fps r+z multivariate formal power series)rrrrr?rrr ) rCrDrrrrFrr,freers rTfpsrs{D  Ay~~ t9> AH%&HI I Ar3uh EF ~ Q2sF 33rV)rF)r)rr+TrTF)Nrr+TrTF)Xr  collectionsrsympy.core.numbersrrrsympy.core.addrsympy.core.exprrsympy.core.functionr r r sympy.core.mulr r sympy.core.relationalrsympy.sets.setsrsympy.core.singletonrsympy.core.symbolrrrrsympy.core.sympifyrsympy.discrete.convolutionsr(sympy.functions.combinatorial.factorialsrrr%sympy.functions.combinatorial.numbersr#sympy.functions.elementary.integersrrr(sympy.functions.elementary.miscellaneousrr$sympy.functions.elementary.piecewiser sympy.series.limitsr!sympy.series.orderr"sympy.series.sequencesr#sympy.series.series_classr$sympy.utilities.iterablesr%rUr^rzrrrrrrrrrrrrrrrrrrrrrr rr\rerhrrjrVrTrsI#-- <<'$$"::&3LL6DD=:%$+0.GTD*2Z/d5 p66" :DNVr 74.%$6(5p2j\~BFHEV!H!I# I#X0"/0"fC6CLJ6JZX6XvR4rV