K iw0ddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZmZmZddlm Z ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-m.Z.ddl/m0Z0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9m:Z:ddl;mZ>ddl?m@Z@ddlAmBZBddlCmDZDddlEmFZFdd lGmHZHGd!d"eZId#d$d#d#d#d%d&ZJd'ZKeBjeId(ZMdd)lNmOZOdd*lPmQZQmRZRmSZSdd+lTmUZUy#),) annotations) AddWithLimits)Add)Basic)Tuple)Expr) factor_terms)diff) fuzzy_bool)Mul)oopi)Ne)S)DummySymbolWild)sympify) Piecewisesqrtpiecewise_foldtancotatan)log)floor)Abssign)MinMax) Heaviside)ratint) MatrixBase)PolyPolynomialError)FormalPowerSeries)limit)Order)shape)sympy_deprecation_warning) is_sequence) filldedentceZdZUdZdZded<ddZdZefdZ dZ d Z d Z d Z dd Zdd ZddZdZdZddZdZxZS)Integralz Represents unevaluated integral.ztuple[Expr, Tuple]argsct|dr|j|i|St|trt dddt j ||g|i|}|S)aCreate an unevaluated integral. Explanation =========== Arguments are an integrand followed by one or more limits. If no limits are given and there is only one free symbol in the expression, that symbol will be used, otherwise an error will be raised. >>> from sympy import Integral >>> from sympy.abc import x, y >>> Integral(x) Integral(x, x) >>> Integral(y) Integral(y, y) When limits are provided, they are interpreted as follows (using ``x`` as though it were the variable of integration): (x,) or x - indefinite integral (x, a) - "evaluate at" integral is an abstract antiderivative (x, a, b) - definite integral The ``as_dummy`` method can be used to see which symbols cannot be targeted by subs: those with a prepended underscore cannot be changed with ``subs``. (Also, the integration variables themselves -- the first element of a limit -- can never be changed by subs.) >>> i = Integral(x, x) >>> at = Integral(x, (x, x)) >>> i.as_dummy() Integral(x, x) >>> at.as_dummy() Integral(_0, (_0, x)) _eval_Integralz integrate(Poly) and Integral(Poly) are deprecated. Instead, use the Poly.integrate() method, or convert the Poly to an Expr first with the Poly.as_expr() method. z1.6zdeprecated-integrate-poly)deprecated_since_versionactive_deprecations_target)hasattrr3 isinstancer%r+r__new__)clsfunctionsymbols assumptionsobjs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/integrals/integrals.pyr8zIntegral.__new__*skT 8- .*8**GC{C C h % % */+F H##CKGK{K c |jft|jDcgc] }t|c}zScc}wN)r:tuplelimits)selfxabs r>__getnewargs__zIntegral.__getnewargs__ds/ %t{{(Ks(K"LLL(Ks; ct|S)ao This method returns the symbols that will exist when the integral is evaluated. This is useful if one is trying to determine whether an integral depends on a certain symbol or not. Examples ======== >>> from sympy import Integral >>> from sympy.abc import x, y >>> Integral(x, (x, y, 1)).free_symbols {y} See Also ======== sympy.concrete.expr_with_limits.ExprWithLimits.function sympy.concrete.expr_with_limits.ExprWithLimits.limits sympy.concrete.expr_with_limits.ExprWithLimits.variables )super free_symbols)rD __class__s r>rIzIntegral.free_symbolsgs.w##r?c||jjryd}|jD]<}t|dk(s|d|dk(xs|d|dz j}|ry|;d}>|jj}|jD]}t|dk(r|j |d&t|dk(r)|d|vr"|djry|djd}|j |d|ddD]}|j|j|jjdur|duryyy)NTFr"r)r:is_zerorClenrIadddiscardupdate)rDgot_nonelzfreerEis r> _eval_is_zerozIntegral._eval_is_zerosF ==  $A1v{qTQqT\;qtad{&;&;Y#H  $}}));; ,C3x1}Q 3x1}Qt!3q6>>V^^+#H LLQ W , ANN+ , , == E )h%.?/@ )r?ctd|jj|j}t |dkDrt d|jz|r|j n}||jvr|St|}t|tra|j}t |dk(rt tdt |dkDrt td|j }nN|\}}||jvrt tdt|tst td|z|jr|jr|j||iS|js|js t d ||k(r5|j||j|fj|iS||j vrt td dd lm}|jsN|j|g|||z |d }|s t d|D cgc]} | j|} } n|j|g} ddlm} | ||z \} } |j| j+Dcgc] \}}||f c}}}|| |Dcgc]}|j| }}|s t d|D cgc]} | j|c} | D chc]@} |j,j|| | j/zj|B}} t |dkDrt td|j }fdfd}g}|j D]}|d}||k(rt |dk(rG|dd\}}||||||}}t1||z dkDr||}}| }|j3|||fbt |dk(r ||dd}|j3||f|j3||j3||j4|g|Scc} wcc}}wcc}wcc} wcc} w)a Performs a change of variables from `x` to `u` using the relationship given by `x` and `u` which will define the transformations `f` and `F` (which are inverses of each other) as follows: 1) If `x` is a Symbol (which is a variable of integration) then `u` will be interpreted as some function, f(u), with inverse F(u). This, in effect, just makes the substitution of x with f(x). 2) If `u` is a Symbol then `x` will be interpreted as some function, F(x), with inverse f(u). This is commonly referred to as u-substitution. Once f and F have been identified, the transformation is made as follows: .. math:: \int_a^b x \mathrm{d}x \rightarrow \int_{F(a)}^{F(b)} f(x) \frac{\mathrm{d}}{\mathrm{d}x} where `F(x)` is the inverse of `f(x)` and the limits and integrand have been corrected so as to retain the same value after integration. Notes ===== The mappings, F(x) or f(u), must lead to a unique integral. Linear or rational linear expression, ``2*x``, ``1/x`` and ``sqrt(x)``, will always work; quadratic expressions like ``x**2 - 1`` are acceptable as long as the resulting integrand does not depend on the sign of the solutions (see examples). The integral will be returned unchanged if ``x`` is not a variable of integration. ``x`` must be (or contain) only one of of the integration variables. If ``u`` has more than one free symbol then it should be sent as a tuple (``u``, ``uvar``) where ``uvar`` identifies which variable is replacing the integration variable. XXX can it contain another integration variable? Examples ======== >>> from sympy.abc import a, x, u >>> from sympy import Integral, cos, sqrt >>> i = Integral(x*cos(x**2 - 1), (x, 0, 1)) transform can change the variable of integration >>> i.transform(x, u) Integral(u*cos(u**2 - 1), (u, 0, 1)) transform can perform u-substitution as long as a unique integrand is obtained: >>> ui = i.transform(x**2 - 1, u) >>> ui Integral(cos(u)/2, (u, -1, 0)) This attempt fails because x = +/-sqrt(u + 1) and the sign does not cancel out of the integrand: >>> Integral(cos(x**2 - 1), (x, 0, 1)).transform(x**2 - 1, u) Traceback (most recent call last): ... ValueError: The mapping between F(x) and f(u) did not give a unique integrand. transform can do a substitution. Here, the previous result is transformed back into the original expression using "u-substitution": >>> ui.transform(sqrt(u + 1), x) == i True We can accomplish the same with a regular substitution: >>> ui.transform(u, x**2 - 1) == i True If the `x` does not contain a symbol of integration then the integral will be returned unchanged. Integral `i` does not have an integration variable `a` so no change is made: >>> i.transform(a, x) == i True When `u` has more than one free symbol the symbol that is replacing `x` must be identified by passing `u` as a tuple: >>> Integral(x, (x, 0, 1)).transform(x, (u + a, u)) Integral(a + u, (u, -a, 1 - a)) >>> Integral(x, (x, 0, 1)).transform(x, (u + a, a)) Integral(a + u, (a, -u, 1 - u)) See Also ======== sympy.concrete.expr_with_limits.ExprWithLimits.variables : Lists the integration variables as_dummy : Replace integration variables with dummy ones dr"z F(x) can only contain one of: %srz* f(u) cannot be a constantz When f(u) has more than one free symbol, the one replacing x must be identified: pass f(u) as (f(u), u)z Expecting a tuple (expr, symbol) where symbol identified a free symbol in expr, but symbol is not in expr's free symbols.za Expecting a tuple (expr, symbol) but didn't get a symbol; got %szeither x or u must be a symbolz{ u must contain the same variable as in x or a variable that is not already an integration variable)solveF)checkz%no solution for solve(F(x) - f(u), x))posifyz%no solution for solve(F(x) - f(u), u)z[ The mapping between F(x) and f(u) did not give a unique integrand.c|j|}|tjus|jdur%|jrt t ||z|S|S)} replace d with a, using subs if possible, otherwise limit where sign of b is considered F)subsrNaN is_finiter(r)FabwokrZs r> _calc_limit_1z)Integral.transform.._calc_limit_1TsL &&A,Caee|s}}5!++T!WQY1--Jr?c tDchc] }|||c}}t|dkDrttd|dScc}w)r_r"z_ The mapping between F(x) and f(u) did not give a unique limit.r)listrO ValueErrorr-)rdreFiavalsrcrgs r> _calc_limitz'Integral.transform.._calc_limit^sW A>b-Aq1>?E5zA~ -(")**8O ?sA rLNrM)rrI intersection variablesrOrjpoprr7rr-r is_Symbolxreplace transformr`rCsympy.solvers.solversr[sympy.simplify.simplifyr]itemsr:r r appendfunc)rDxuxfreexvarufreeuvarr[solnfifr]pdiffrepskvpuvarsnewfuncsnewfuncrm newlimitsrEsymrdrercrgrZs @@@r>rszIntegral.transforms]N #J++DNN; u:>2T^^CE E#uyy{ t~~ %K AJ a NNE5zQ --".//5zA~ ->"?@@99;DGAt1>>) -"dF+ -$&*-+",-- ;;1;;==!Q( ({{1;;=> > 4<>>!affT1oq%9:CCQIN N 4;; Z)IJK K 0{{a!AQE2D !HII,01bq!1A1a!A 6 Q-KE4II$**,?$!Q1v?@E*/u*=>QAFF4L>D> !HII,01bq!1A345,.]]''b1"''!*<$q$-(55 x=1 Z)#$% %,,.   ;; &Ca&Cd{s8q=qr7DAq&q!,k!Q.?qA!!a%!), !1#*($$dAq\2X]#CFA.A$$dAY/$$T*  %! &$tyy-9--y2 @>25s6QQ" 8Q(#Q-AQ2c 01234jddsSjdd}jdd2jdd0jdd}jd d}jd d}tttd|2||fd kDr t d |rd x2x}}n2rd x}x}}n|rd x}x2}n|rd x}x2}2|||0d}0dvrt d0z|r't dj Dr t djrtjSddl m }tj|rt fdjDr t dt djj dd dDrS}j} | j|j| jg|j j!g| j j!Sj} | j#dd} |r| j d.i} | jrtjSt| t$r| j'fdSt| t(rdtj d kDrt*j d} t| d kDr| j,| fi|S| j,| dfi|Si} j D]} t| dk7r| \4} }| |f}t/d|Dr4j0s t3d}nXt/d|Dr4j4s t3d}n-t/d |Dr4j6s t3d!}nd}|s|| 4<| r| j9Dcic]\}}|| }}}j;| j d.i}t|t<r*t=|Dcgc]}|j;|c}}|S|j;|}|Sg}t?}j D] } t| d k(rt?| dd 3nWt| d"k(r| d j@3n9t| dk(r+| d j@jC| d"j@3| d|vst 3fd#|DrY|jE| |jG3j| g| gz} | jI}t|tJs|} | jMtNtPrt| dkrt/d$| Ds#t| dk(r`t/d%| d dDrKt3d&d!}| j;| d|ijStTj;|| di} n/| jMtVtXr| jStT} | jMtTrt| tTs t[| } t| tTr=t| d k(r| j\| dfi|}nj\| | dfi|}nɈ02fd'}2}2d urt| dk(rw| d j^rh| d"j^rY| j`sM| d jMtbtb s| d"jMtbtb r|| | }||} d }|d ur2durd}n,j\| | dfi|}|2dur|| | }||}  jd(d}|rt|tJs||jetfD]f}|jhd} | jetjD]}!t3d)}"| jm|!|"}#|#jo|"}$|"|$j@vs@|!jhd} |jm|tq|tQ|$trztu| trd"z z trz z}| jetvD]}%t3d)}"| jm|%|"}&|&jo|"}$|"|$j@vs@|%jhd} |jm|tq|tQ|$trztu| trz z}i|W|jE| j| g| gzjI} | jI}t|tJs|}  t| d k(r|} t| dk(r| \4} }nt| d"k(r| \4}d} nt*|rDt| txr| j d.i} t|txr|j d.i}|j`rWt|jz}'|'j}4|j}|j4| |} t| g|'} d*11fd+}(ggg}+}*})tqj|D]},t 14fd,tj|,Dr|)jE|,=t d-tj|,Dr|+jEt[|,}|*jE|,tq|)D,cgc] },|(|,4| |c},}- tq|*j4| |}.t[tq|+j4| |}/|-|.z|/z} | Scc}}wcc}wcc},w#t*$rK|jE| j| g| gz} | jI}t|tJs|} YtwxYw)/a Perform the integration using any hints given. Examples ======== >>> from sympy import Piecewise, S >>> from sympy.abc import x, t >>> p = x**2 + Piecewise((0, x/t < 0), (1, True)) >>> p.integrate((t, S(4)/5, 1), (x, -1, 1)) 1/3 See Also ======== sympy.integrals.trigonometry.trigintegrate sympy.integrals.heurisch.heurisch sympy.integrals.rationaltools.ratint as_sum : Approximate the integral using a sum integralsTdeepmeijergNconds piecewiserischheurischmanualr"z;At most one of manual, meijerg, risch, heurisch can be TrueFrrrrr)separaternonez=conds must be one of "separate", "piecewise", "none", got: %sc38K|]}t|dkDyw)r"N)rO).0rEs r> z Integral.doit..s=#SA=sz4risch=True is only allowed for indefinite integrals.rSumc3TK|]}|jjdv!ywrN)r:rC)rrrDs r>rz Integral.doit..s%HA1 ,,Q//Hs%(z3Limit of the sum cannot be an integration variable.c34K|]}|jywrA) is_infinite)rrTs r>rz Integral.doit..sFQ1==FcPt|txr|jddzdk7S)Nr"rM)r7r!r1rys r>zIntegral.doit..s#jI.C166!9Q;!3Cr?c2t|jdSNr)r!r1rs r>rzIntegral.doit..siq *r?c^j|gjjdiS)Nr0)rxrCdoit)rhintsrDs r>rzIntegral.doit..s,9)$))A4 499BEBr?rLc34K|]}|jywrA)is_nonnegativerrWs r>rz Integral.doit..s/1##/r)positivec34K|]}|jywrA)is_nonpositivers r>rz Integral.doit..s1!Q%%1r)negativec34K|]}|jywrA)is_realrs r>rz Integral.doit..s*1QYY*r)realrMc3,K|] }|dv ywrr0)rrulis r>rz Integral.doit..s#GAAaDCK#Gsc34K|]}|jywrA)is_extended_realrrys r>rz Integral.doit..s%FQa&8&8%Frc3RK|]}|jxr |j !ywrA)rrrs r>rz Integral.doit..s-'()'9'9'O!-->O'O's%'xrcjd}t|dk(r dur|\}}} t||||}|j|\}} dk(r' j ||||f} t ||f| dfdS dk(r2t j dk7rttd ||f}|S|}|S#t$rtdd}YwxYw) NrLF+NotImplementedError from meijerint_definiterT)evaluaterr"z conds=separate not supported in multiple integrals) rOmeijerint_definiteNotImplementedError_debugrxrrCrjr-) r:rEretryrdreresrcondrzrrrDs r> try_meijergz"Integral.doit..try_meijerg8sC3x1})="%1a'"4Xq!Q"GC ?&)GAt$ 3$(IIhAq $B (1!TQI-2(4!4!&*!4#&t{{#3q#8*4ZA>6?+@%@'(gJ'(J- 3'"$:;"&C'sBB21B2finalx1cdt|txrtfd|jDS)Nc3*K|] }|fk( ywrAr0)rrWrys r>rz6Integral.doit..is_indef_int..s'DaaT 'Ds)r7r/anyrC)grys `r> is_indef_intz#Integral.doit..is_indef_ints+$.q($;%E$''D188'D$DFr?cg}tj|D]?}||r#|j|j|||/|j|At|SrA)r make_argsrw_eval_interval)rryrdrer1rrs r> eval_factoredz$Integral.doit..eval_factoreds^$&D%(]]1%53#/1#5$(KK0@0@Aq0I$J$(KKN 3 $':-r?c30K|] }|ywrAr0)rrrrys r>rz Integral.doit..s #>'($01#5#>sc3<K|]}t|tywrA)r7r)rrs r>rz Integral.doit..s!%@)*&09%=%@sr0)DgetrOrifilterrjrrCrNrZerosympy.concrete.summationsrr7r:rorxrreplacer$ applyfuncr'r integrateallrrrrrvrrrBsetrIunionrwrRfactorr/hasrrrewriterrr r_eval_integralris_Polyr atomsrr1rr`r rrrrrgensremoveas_exprrr%rr )5rDrrrrr eval_kwargsr_i_sumr:rErrdrerTrZrrundodidrW undone_limitsuljfactored_functionr antiderivrmeijerg1rr atan_termatan_argtan_partrtan_exp1coeffcot_partcot_exp1rrrothers piecewisesrunevalevalued evalued_pwrrrrrys5`` @@@@@r>rz Integral.doits@ *yyd+Kyy&))It, ';/ '4(99Z.8T* tF4&'5(!CDE F JZ[ [ ). .G .eh (- -F -UX */ /F /Wx ', ,F ,Wu")EVYa  9 9/1678 8 S===ST T <<66M 2 dmmS )HHH !VWWF$--*>*>q*A!"*EFF B==D499WRWWT]]?RYY?DDFUUZZ\ \==## C *, $x}}-u-H   66M h +%%BD D h 1 24;;!#))++a.C3x!|)x))#===)x))#a&@K@@;; C3x1}GAq!AA/Q//8H8H4(1q11!:J:J4(***199t$Q+ , %)ZZ\2TQAqD2D2*$--%**3U3C#u%s;!QZZ-;<Jll4(J e;;R =C3x1}#bq'lSQ!f))SQ!f))//A0C0CD 1v}#G#G G$$S) 3$499zSE'9;$,OO$5!!"3X>0H||C&SA#%F#%F"FSQ3'!"g'$t$/B ( 1 13q62, ? +HHb#a&\,Bc3'#++I6 Y'x3-h7H(I.s8q= 7 7 7A!'%!'I!4 3 3 #a&!9,7!9I<#5(CA #a&*A*Ac!fF]F] ( 0 0QB,A 2s0C%h4C#& $Hu$D $I 3 3 3 #a&!9,7!9I (W_)(C8?'*H$IIgt,EjH=%!*!6?I(~~a0H$,NN3$7D"4[#+==2#> ( b 1U%7%77 ( a 0A(1y#i $U Bua1fb[/A ACC)DID%-NN3$7?"4[#+==2#> ( b 1U%7%77 ( a 0A(1y#i $U BuaV} <C>)?I??, $$S)$499zSE'9;BBD$,OO$5!!"3X>0Hs8q=(H3x1}"%1aSQ"1 11%a/ &A%a/ &A ((#INN3 A$-$5$5$7 #,#;#;Aq!#D#'#84#8F .9;B:6 !$y!91A"#>,/MM!,<#> > ) 0 0 3!$%@.1mmA.>%@"@ * 1 1.2C D & a 01"%09';+,(5Q1a'@';"< =&)6l&A&A!Q&JG)7Z8H)I)X)XYZ\]_`)aJ'-'7*'DHUR =f3<\'; 3=*005'0tyyH:3E'GH080A-#-.?#J+<=s& m5m;n -AnAooc^ jtj}}|jd}t |dk(r|\ }}nt |dk(r|\ }d}n dx}}|d |rj |gt |} fd}tj}| ||||z }| ||||z}t |dk(r k(r |} || z }|Std} |j | jj| } | r|j | ||fz }|S) aEvaluate the derivative of the current Integral object by differentiating under the integral sign [1], using the Fundamental Theorem of Calculus [2] when possible. Explanation =========== Whenever an Integral is encountered that is equivalent to zero or has an integrand that is independent of the variable of integration those integrals are performed. All others are returned as Integral instances which can be resolved with doit() (provided they are integrable). References ========== .. [1] https://en.wikipedia.org/wiki/Differentiation_under_the_integral_sign .. [2] https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus Examples ======== >>> from sympy import Integral >>> from sympy.abc import x, y >>> i = Integral(x + y, y, (y, 1, x)) >>> i.diff(x) Integral(x + y, (y, x)) + Integral(1, y, (y, 1, x)) >>> i.doit().diff(x) == i.diff(x).doit() True >>> i.diff(y) 0 The previous must be true since there is no y in the evaluated integral: >>> i.free_symbols {x} >>> i.doit() 2*x**3/3 - x/2 - 1/6 rLrMNrc4t|}|stjSt|trP|j Dcgc]}t |dk(r |dk(rfn| }}j|jg|}|j||zScc}wNr"r) r rrr7r/rCrOrxr:r`)rabdab_dsymrTrCrDrrys r>_doz&Integral._eval_derivative.._dosB}Hvv !X&#$88-&)Vq[QqTQY1a&QF--DIIajj26266!R=) )-s#Br"rz) r:rirCrprOrxrBrrrr`r ) rDrrrCr(rdrerrvargrzrys `` @r>_eval_derivativezIntegral._eval_derivatives;ZMM4 #46 2 u:?GAq! Z1_DAqALAaA  !,eFm,A *VV = #a)OB = #a)OB u:?saxC #IB c A&&A,##C(--a3CdiiaAY// r?c  ddlm} m} ddlm} |r | |||S|r! | ||} | | j tk7r| S|||||d} t|tr|s|s|s|j|St|tr|j|fi| S|j|s||zS|j!|}|$|s"|s |s|jj#S|durG | ||d| \} }|r3| dk(r| ||j%d S| |j%d zS| Sdd lm}g}t+j,|}|D]}|j/|\}}|t0j2ur|s|j5||zA|j7}|{|j8|j;|fi| }|U|j8|j<|fi| }|5|j |g|j>}|j5|||zzy|j@r6|jBj|s|stEd |g }tEd|g }|jFjI||z|z}||jBdk(rtK|jF}n|dk7r-|jF|jBdzz|jBdzz }netK|jF}|jF|jBdzz|jBdzz }t|tM|jBdf|df}|j5||z||z |jO|r&|s$|s"|s |j5|tQ||zH|s|s|stS|||}||j5||zttU||}||j5||zddl+m,}|||}||j5||z|dur= | ||d| \}}|r||j%d z}|j5||z|dur(ddl-m.}m/}  |dk(r | ||g}n |||g}nd}|dur'|% ta||}||j5||zW||dur | ||} | t| ts| jtro|sm| }!d|!d<d|!d<| j | jdD"cgc]+}"|"jtr|"j$di|!n|"-c}"jgdddd} | jts|j5|| z(|sIti|dk(r;||jgdd}|jjr|j8||fi| cS||j5||zyt+|S#t $rYywxYw#ttf$rYwxYw#t $rYwxYw#t $rd}YwxYw#t$rd}YwxYw#t $rtcdYwxYwcc}"w#ttf$rYwxYw)a Calculate the anti-derivative to the function f(x). Explanation =========== The following algorithms are applied (roughly in this order): 1. Simple heuristics (based on pattern matching and integral table): - most frequently used functions (e.g. polynomials, products of trig functions) 2. Integration of rational functions: - A complete algorithm for integrating rational functions is implemented (the Lazard-Rioboo-Trager algorithm). The algorithm also uses the partial fraction decomposition algorithm implemented in apart() as a preprocessor to make this process faster. Note that the integral of a rational function is always elementary, but in general, it may include a RootSum. 3. Full Risch algorithm: - The Risch algorithm is a complete decision procedure for integrating elementary functions, which means that given any elementary function, it will either compute an elementary antiderivative, or else prove that none exists. Currently, part of transcendental case is implemented, meaning elementary integrals containing exponentials, logarithms, and (soon!) trigonometric functions can be computed. The algebraic case, e.g., functions containing roots, is much more difficult and is not implemented yet. - If the routine fails (because the integrand is not elementary, or because a case is not implemented yet), it continues on to the next algorithms below. If the routine proves that the integrals is nonelementary, it still moves on to the algorithms below, because we might be able to find a closed-form solution in terms of special functions. If risch=True, however, it will stop here. 4. The Meijer G-Function algorithm: - This algorithm works by first rewriting the integrand in terms of very general Meijer G-Function (meijerg in SymPy), integrating it, and then rewriting the result back, if possible. This algorithm is particularly powerful for definite integrals (which is actually part of a different method of Integral), since it can compute closed-form solutions of definite integrals even when no closed-form indefinite integral exists. But it also is capable of computing many indefinite integrals as well. - Another advantage of this method is that it can use some results about the Meijer G-Function to give a result in terms of a Piecewise expression, which allows to express conditionally convergent integrals. - Setting meijerg=True will cause integrate() to use only this method. 5. The "manual integration" algorithm: - This algorithm tries to mimic how a person would find an antiderivative by hand, for example by looking for a substitution or applying integration by parts. This algorithm does not handle as many integrands but can return results in a more familiar form. - Sometimes this algorithm can evaluate parts of an integral; in this case integrate() will try to evaluate the rest of the integrand using the other methods here. - Setting manual=True will cause integrate() to use only this method. 6. The Heuristic Risch algorithm: - This is a heuristic version of the Risch algorithm, meaning that it is not deterministic. This is tried as a last resort because it can be very slow. It is still used because not enough of the full Risch algorithm is implemented, so that there are still some integrals that can only be computed using this method. The goal is to implement enough of the Risch and Meijer G-function methods so that this can be deleted. Setting heurisch=True will cause integrate() to use only this method. Set heurisch=False to not use it. r)risch_integrateNonElementaryIntegral)manualintegrate)rNrFT)separate_integralr)r) sincos_to_sumrd)excludererrr")singularityintegrate)rheurisch_wrapper)rrrr) multinomialr power_exp power_base)mulrr0)6sympy.integrals.rischrrsympy.integrals.manualintegraterrrxr/rjr&r7r%rrpiecewise_integrateras_polyrrsympy.simplify.fur rras_independentrOnerwgetOrremoveOexprrois_Powexprbasematchrris_rational_functionr# trigintegratedeltaintegratesingularityfunctionsr sympy.integrals.heurischrr meijerint_indefiniterr1expandrOis_Add)#rDrryrrrrrrrrrresultrpolyrWr partsr1rr order_termh h_order_expr h_order_termrdreMh1h2r  heurisch_r new_eval_kwargsrs# r>rzIntegral._eval_integral7sx QC  &q!599  (A.%&++*A!M#*EV 52  a 'U;;q> ! a #(1((:k: :uuQxQ3Jyy|  Vw%>>#++- -   "+AqD!   {4Q:??e?LL%U(;;;!M 4}}Qg A''*HE1AEEz' U1W%J%'D'' QF+F=#64#6#6z#Y[#YL#/'6z((A+5+?+?(A  UA ,<%=> xx ! Wqc*qc*FFLL1q)=uu{K+-FFQUUQY/15519= [VVaeeai0AEEAI>%r2aeeR=&9B:FLLQqT!12 %%a(&Gu UVAq\12g!!Qe4=LL+#1a(=LL+F(A.=LL+% !.q!.2% A1  !AFFF$7 7A U1W- 5(L ! K/ 0AR @A )!Qb 9Ae# J,Q2A=LL+yV50,Q2F)*VX2N!::h/ /:O8=OH57 _eval_lserieszIntegral._eval_lseriessx}} AAabEzt MM))$5 0DD/4;;/ / 0s )A.AA.c8|}|jD]}||ddvs |d}n|jj|||jt\}}|D cgc]} | j ||}} t |g|jt||zzScc} w)Nr"r)rynr7)rCr:nseries as_coeff_addr)r`rr) rDryr=r7r8r9rTtermsorderos r> _eval_nserieszIntegral._eval_nseriess AAabEzt }},,ad-$$0L$7 u*/0Qa00--U A ==1sBc|jdj|}|D] }|dk7s ntg|jddS)Nrr")r1r6r)rDryr7r8 series_gen leading_terms r>_eval_as_leading_termzIntegral._eval_as_leading_termsPYYq\))!, & Lq  6 !" 66r?c t|}t|tr4ddlm}|j |j Dcgc] }||fi| c}S|jdi|Scc}w)Nr)simplifyr0)r r7r/rurIrxr1)rDkwargsrrIrWs r>_eval_simplifyzIntegral._eval_simplifys]D! dH % 8499diiHx4V4HI It}}&v&&IsA"c ddlm}|j}t|dkDr t d|d}t|dk7s"|dj dus|dj dur t d|td d d }n t|}|jdus|jdus|j durt d |z|\}}} | |z |z } td d d } |j} |dk(r)| || j||| dz | zz| d|fz} n|dk(r&| || j||| | zz| d|fz} n|dk(r,| || j||| | zz| dz z | d|fz} ne|dk(rR| | j||| j|| zdz || j||| | zz| d|dz fzz} nt d|z|r| jS| S)a= Approximates a definite integral by a sum. Parameters ========== n : The number of subintervals to use, optional. method : One of: 'left', 'right', 'midpoint', 'trapezoid'. evaluate : bool If False, returns an unevaluated Sum expression. The default is True, evaluate the sum. Notes ===== These methods of approximate integration are described in [1]. Examples ======== >>> from sympy import Integral, sin, sqrt >>> from sympy.abc import x, n >>> e = Integral(sin(x), (x, 3, 7)) >>> e Integral(sin(x), (x, 3, 7)) For demonstration purposes, this interval will only be split into 2 regions, bounded by [3, 5] and [5, 7]. The left-hand rule uses function evaluations at the left of each interval: >>> e.as_sum(2, 'left') 2*sin(5) + 2*sin(3) The midpoint rule uses evaluations at the center of each interval: >>> e.as_sum(2, 'midpoint') 2*sin(4) + 2*sin(6) The right-hand rule uses function evaluations at the right of each interval: >>> e.as_sum(2, 'right') 2*sin(5) + 2*sin(7) The trapezoid rule uses function evaluations on both sides of the intervals. This is equivalent to taking the average of the left and right hand rule results: >>> s = e.as_sum(2, 'trapezoid') >>> s 2*sin(5) + sin(3) + sin(7) >>> (e.as_sum(2, 'left') + e.as_sum(2, 'right'))/2 == s True Here, the discontinuity at x = 0 can be avoided by using the midpoint or right-hand method: >>> e = Integral(1/sqrt(x), (x, 0, 1)) >>> e.as_sum(5).n(4) 1.730 >>> e.as_sum(10).n(4) 1.809 >>> e.doit().n(4) # the actual value is 2 2.000 The left- or trapezoid method will encounter the discontinuity and return infinity: >>> e.as_sum(5, 'left') zoo The number of intervals can be symbolic. If omitted, a dummy symbol will be used for it. >>> e = Integral(x**2, (x, 0, 2)) >>> e.as_sum(n, 'right').expand() 8/3 + 4/n + 4/(3*n**2) This shows that the midpoint rule is more accurate, as its error term decays as the square of n: >>> e.as_sum(method='midpoint').expand() 8/3 - 2/(3*_n**2) A symbolic sum is returned with evaluate=False: >>> e.as_sum(n, 'midpoint', evaluate=False) 2*Sum((2*_k/n - 1/n)**2, (_k, 1, n))/n See Also ======== Integral.doit : Perform the integration using any hints References ========== .. [1] https://en.wikipedia.org/wiki/Riemann_sum#Riemann_summation_methods rrr"z2Multidimensional midpoint rule not implemented yetrLFrMz5Expecting a definite integral over a finite interval.r=T)integerrz$n must be a positive integer, got %srleftrightmidpoint trapezoidzUnknown method %s)rrrCrOrrbrjrr is_positive is_integerr:r`r)rDr=methodrrrCr(ryrdredxrrr(s r>as_sumzIntegral.as_sumsR 2 v;?%DF F1IEE a58#5#5#>a""e+ "788 9c4$7A A MMU "alle&; KK5 CaGH H1a!eQY #td 3 MM V AFF1a1Q3(l3aAY??F w AFF1a!B$h/!Q;;F z !AFF1a!B$hAo6Aq BBF { "!&&A,15q8AFF1a!B$h'!QA789F069: : (v{{}4f4r?c t|jdk7s$tt|jddk7r td|jd\}}}|jr|jr||ks td||k(rt j Sddlm}td}|j}|||Dcgc] }|js||cxkr|ksnn|"} }| D]} | ||fvs td| zt||fi|} | jtr|S|t ur.|tur&t| | j!|| z |t} nt| ||d t| ||d z } | D]<}| t| j!|||z | j!|||zz |dd z } >| Scc}w) a Compute the Cauchy Principal Value of the definite integral of a real function in the given interval on the real axis. Explanation =========== In mathematics, the Cauchy principal value, is a method for assigning values to certain improper integrals which would otherwise be undefined. Examples ======== >>> from sympy import Integral, oo >>> from sympy.abc import x >>> Integral(x+1, (x, -oo, oo)).principal_value() oo >>> f = 1 / (x**3) >>> Integral(f, (x, -oo, oo)).principal_value() 0 >>> Integral(f, (x, -10, 10)).principal_value() 0 >>> Integral(f, (x, -10, oo)).principal_value() + Integral(f, (x, -oo, 10)).principal_value() 0 References ========== .. [1] https://en.wikipedia.org/wiki/Cauchy_principal_value .. [2] https://mathworld.wolfram.com/CauchyPrincipalValue.html r"rrLzkYou need to insert a variable, lower_limit, and upper_limit correctly to calculate cauchy's principal valuezThe lower_limit must be smaller than or equal to the upper_limit to calculate cauchy's principal value. Also, a and b need to be comparable.) singularitiesrzRThe principal value is not defined in the given interval due to singularity at %d.-+)rOrCrirj is_comparablerrsympy.calculus.singularitiesrXrr:rrr/r r(r`) rDrJryrdrerXrYrrsingularities_listrWrcIs r>principal_valuezIntegral.principal_value>s@ t{{ q CT[[^(<$=$B89 9++a.1aAOOQ^_ _ 666M> #J MM)6q!)<`ATUYZT_^_T_a``# pAQF{ hlmnpp p a %f % 55?K 8Ra!&&QB-'B/AaAs#eAq!S&99A# KA 1q5)QVVAq1u-==1cJ JA KasF= F=F=)returnr/)NNNNrNr)NrPT)__name__ __module__ __qualname____doc__ __slots____annotations__r8rFpropertyrIrXrsrrrr;rCrGrKrVr` __classcell__)rJs@r>r/r/#s}*I 8tM$$0!FZ.xYv [zEI>BUn 0 >7'K5Z<r?r/Nr)rrrrrc d|||||d}t|i|}t|tr|jdi|S|jD cgc]&} t| tr| jdi|n| (} } |j| Scc} w)a|integrate(f, var, ...) .. deprecated:: 1.6 Using ``integrate()`` with :class:`~.Poly` is deprecated. Use :meth:`.Poly.integrate` instead. See :ref:`deprecated-integrate-poly`. Explanation =========== Compute definite or indefinite integral of one or more variables using Risch-Norman algorithm and table lookup. This procedure is able to handle elementary algebraic and transcendental functions and also a huge class of special functions, including Airy, Bessel, Whittaker and Lambert. var can be: - a symbol -- indefinite integration - a tuple (symbol, a) -- indefinite integration with result given with ``a`` replacing ``symbol`` - a tuple (symbol, a, b) -- definite integration Several variables can be specified, in which case the result is multiple integration. (If var is omitted and the integrand is univariate, the indefinite integral in that variable will be performed.) Indefinite integrals are returned without terms that are independent of the integration variables. (see examples) Definite improper integrals often entail delicate convergence conditions. Pass conds='piecewise', 'separate' or 'none' to have these returned, respectively, as a Piecewise function, as a separate result (i.e. result will be a tuple), or not at all (default is 'piecewise'). **Strategy** SymPy uses various approaches to definite integration. One method is to find an antiderivative for the integrand, and then use the fundamental theorem of calculus. Various functions are implemented to integrate polynomial, rational and trigonometric functions, and integrands containing DiracDelta terms. SymPy also implements the part of the Risch algorithm, which is a decision procedure for integrating elementary functions, i.e., the algorithm can either find an elementary antiderivative, or prove that one does not exist. There is also a (very successful, albeit somewhat slow) general implementation of the heuristic Risch algorithm. This algorithm will eventually be phased out as more of the full Risch algorithm is implemented. See the docstring of Integral._eval_integral() for more details on computing the antiderivative using algebraic methods. The option risch=True can be used to use only the (full) Risch algorithm. This is useful if you want to know if an elementary function has an elementary antiderivative. If the indefinite Integral returned by this function is an instance of NonElementaryIntegral, that means that the Risch algorithm has proven that integral to be non-elementary. Note that by default, additional methods (such as the Meijer G method outlined below) are tried on these integrals, as they may be expressible in terms of special functions, so if you only care about elementary answers, use risch=True. Also note that an unevaluated Integral returned by this function is not necessarily a NonElementaryIntegral, even with risch=True, as it may just be an indication that the particular part of the Risch algorithm needed to integrate that function is not yet implemented. Another family of strategies comes from re-writing the integrand in terms of so-called Meijer G-functions. Indefinite integrals of a single G-function can always be computed, and the definite integral of a product of two G-functions can be computed from zero to infinity. Various strategies are implemented to rewrite integrands as G-functions, and use this information to compute integrals (see the ``meijerint`` module). The option manual=True can be used to use only an algorithm that tries to mimic integration by hand. This algorithm does not handle as many integrands as the other algorithms implemented but may return results in a more familiar form. The ``manualintegrate`` module has functions that return the steps used (see the module docstring for more information). In general, the algebraic methods work best for computing antiderivatives of (possibly complicated) combinations of elementary functions. The G-function methods work best for computing definite integrals from zero to infinity of moderately complicated combinations of special functions, or indefinite integrals of very simple combinations of special functions. The strategy employed by the integration code is as follows: - If computing a definite integral, and both limits are real, and at least one limit is +- oo, try the G-function method of definite integration first. - Try to find an antiderivative, using all available methods, ordered by performance (that is try fastest method first, slowest last; in particular polynomial integration is tried first, Meijer G-functions second to last, and heuristic Risch last). - If still not successful, try G-functions irrespective of the limits. The option meijerg=True, False, None can be used to, respectively: always use G-function methods and no others, never use G-function methods, or use all available methods (in order as described above). It defaults to None. Examples ======== >>> from sympy import integrate, log, exp, oo >>> from sympy.abc import a, x, y >>> integrate(x*y, x) x**2*y/2 >>> integrate(log(x), x) x*log(x) - x >>> integrate(log(x), (x, 1, a)) a*log(a) - a + 1 >>> integrate(x) x**2/2 Terms that are independent of x are dropped by indefinite integration: >>> from sympy import sqrt >>> integrate(sqrt(1 + x), (x, 0, x)) 2*(x + 1)**(3/2)/3 - 2/3 >>> integrate(sqrt(1 + x), x) 2*(x + 1)**(3/2)/3 >>> integrate(x*y) Traceback (most recent call last): ... ValueError: specify integration variables to integrate x*y Note that ``integrate(x)`` syntax is meant only for convenience in interactive sessions and should be avoided in library code. >>> integrate(x**a*exp(-x), (x, 0, oo)) # same as conds='piecewise' Piecewise((gamma(a + 1), re(a) > -1), (Integral(x**a*exp(-x), (x, 0, oo)), True)) >>> integrate(x**a*exp(-x), (x, 0, oo), conds='none') gamma(a + 1) >>> integrate(x**a*exp(-x), (x, 0, oo), conds='separate') (gamma(a + 1), re(a) > -1) See Also ======== Integral, Integral.doit F)rrrrrrr0)r/r7rr1rx) rrrrrr1rJ doit_flagsintegralrdnew_argss r>rr~s|  J((H(H%x}}*z**]]$-7q(,CFAFF(Z(J$$x}}h''$s+Bc:ddlm}t|}|s tdt ||s tdt |s tdt |jt |k7r td|j|vr td|}d}t|D]D\}}|j|} t| |j} || | zz}|j|| }F|t|z}t||jjd } | S) aline_integrate(field, Curve, variables) Compute the line integral. Examples ======== >>> from sympy import Curve, line_integrate, E, ln >>> from sympy.abc import x, y, t >>> C = Curve([E**t + 1, E**t - 1], (t, 0, ln(2))) >>> line_integrate(x + y, C, [x, y]) 3*sqrt(2) See Also ======== sympy.integrals.integrals.integrate, Integral r)Curvez6Expecting function specifying field as first argument.z*Expecting Curve entity as second argument.z)Expecting ordered iterable for variables.z3Field variable size does not match curve dimension.z.Curve parameter clashes with field parameters.F)r)sympy.geometryrorrjr7r,rO functions parameter enumerater r`rr/rCr) fieldcurvevarsrorcFtdldtrWvar_f_dnrls r>line_integrater|-s&%A  DF F eU #EFF t DEE 5??s4y(NOO $IJJ B DD/3 __Q 2u'sSy! WWS"   d4jBELL)..E.:H Or?c,t|jSrA)r*r:)rs r>_r~as  r?)r")rr%r)r!)V __future__rsympy.concrete.expr_with_limitsrsympy.core.addrsympy.core.basicrsympy.core.containersrsympy.core.exprrsympy.core.exprtoolsr sympy.core.functionr sympy.core.logicr sympy.core.mulr sympy.core.numbersr rsympy.core.relationalrsympy.core.singletonrsympy.core.symbolrrrsympy.core.sympifyrsympy.functionsrrrrrr&sympy.functions.elementary.exponentialr#sympy.functions.elementary.integersr$sympy.functions.elementary.complexesrr(sympy.functions.elementary.miscellaneousrr -sympy.functions.special.singularity_functionsr! rationaltoolsr#sympy.matricesr$ sympy.polysr%r&sympy.series.formalr'sympy.series.limitsr(sympy.series.orderr)sympy.tensor.functionsr*sympy.utilities.exceptionsr+sympy.utilities.iterablesr,sympy.utilities.miscr-r/rr|registerr~deltafunctionsr" meijerintrr%r trigonometryr!r0r?r>rs"9"' -$'%$"33&KK65:=C!%-1%$(@1+W}Wv*"D4X\m(^/h  +GG'r?