test_integrals.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. import math
  2. from sympy.concrete.summations import (Sum, summation)
  3. from sympy.core.add import Add
  4. from sympy.core.containers import Tuple
  5. from sympy.core.expr import Expr
  6. from sympy.core.function import (Derivative, Function, Lambda, diff)
  7. from sympy.core import EulerGamma
  8. from sympy.core.numbers import (E, I, Rational, nan, oo, pi, zoo, all_close)
  9. from sympy.core.relational import (Eq, Ne)
  10. from sympy.core.singleton import S
  11. from sympy.core.symbol import (Symbol, symbols)
  12. from sympy.core.sympify import sympify
  13. from sympy.functions.elementary.complexes import (Abs, im, polar_lift, re, sign)
  14. from sympy.functions.elementary.exponential import (LambertW, exp, exp_polar, log)
  15. from sympy.functions.elementary.hyperbolic import (acosh, asinh, cosh, coth, csch, sinh, tanh, sech)
  16. from sympy.functions.elementary.miscellaneous import (Max, Min, sqrt)
  17. from sympy.functions.elementary.piecewise import Piecewise
  18. from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sin, sinc, tan, sec)
  19. from sympy.functions.special.delta_functions import DiracDelta, Heaviside
  20. from sympy.functions.special.error_functions import (Ci, Ei, Si, erf, erfc, erfi, fresnelc, li)
  21. from sympy.functions.special.gamma_functions import (gamma, polygamma)
  22. from sympy.functions.special.hyper import (hyper, meijerg)
  23. from sympy.functions.special.singularity_functions import SingularityFunction
  24. from sympy.functions.special.zeta_functions import lerchphi
  25. from sympy.integrals.integrals import integrate
  26. from sympy.logic.boolalg import And
  27. from sympy.matrices.dense import Matrix
  28. from sympy.polys.polytools import (Poly, factor)
  29. from sympy.printing.str import sstr
  30. from sympy.series.order import O
  31. from sympy.sets.sets import Interval
  32. from sympy.simplify.gammasimp import gammasimp
  33. from sympy.simplify.simplify import simplify
  34. from sympy.simplify.trigsimp import trigsimp
  35. from sympy.tensor.indexed import (Idx, IndexedBase)
  36. from sympy.core.expr import unchanged
  37. from sympy.functions.elementary.integers import floor
  38. from sympy.integrals.integrals import Integral
  39. from sympy.integrals.risch import NonElementaryIntegral
  40. from sympy.physics import units
  41. from sympy.testing.pytest import raises, slow, warns_deprecated_sympy, warns
  42. from sympy.utilities.exceptions import SymPyDeprecationWarning
  43. from sympy.core.random import verify_numerically
  44. x, y, z, a, b, c, d, e, s, t, x_1, x_2 = symbols('x y z a b c d e s t x_1 x_2')
  45. n = Symbol('n', integer=True)
  46. f = Function('f')
  47. def NS(e, n=15, **options):
  48. return sstr(sympify(e).evalf(n, **options), full_prec=True)
  49. def test_poly_deprecated():
  50. p = Poly(2*x, x)
  51. assert p.integrate(x) == Poly(x**2, x, domain='QQ')
  52. # The stacklevel is based on Integral(Poly)
  53. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  54. integrate(p, x)
  55. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  56. Integral(p, (x,))
  57. @slow
  58. def test_principal_value():
  59. g = 1 / x
  60. assert Integral(g, (x, -oo, oo)).principal_value() == 0
  61. assert Integral(g, (y, -oo, oo)).principal_value() == oo * sign(1 / x)
  62. raises(ValueError, lambda: Integral(g, (x)).principal_value())
  63. raises(ValueError, lambda: Integral(g).principal_value())
  64. l = 1 / ((x ** 3) - 1)
  65. assert Integral(l, (x, -oo, oo)).principal_value().together() == -sqrt(3)*pi/3
  66. raises(ValueError, lambda: Integral(l, (x, -oo, 1)).principal_value())
  67. d = 1 / (x ** 2 - 1)
  68. assert Integral(d, (x, -oo, oo)).principal_value() == 0
  69. assert Integral(d, (x, -2, 2)).principal_value() == -log(3)
  70. v = x / (x ** 2 - 1)
  71. assert Integral(v, (x, -oo, oo)).principal_value() == 0
  72. assert Integral(v, (x, -2, 2)).principal_value() == 0
  73. s = x ** 2 / (x ** 2 - 1)
  74. assert Integral(s, (x, -oo, oo)).principal_value() is oo
  75. assert Integral(s, (x, -2, 2)).principal_value() == -log(3) + 4
  76. f = 1 / ((x ** 2 - 1) * (1 + x ** 2))
  77. assert Integral(f, (x, -oo, oo)).principal_value() == -pi / 2
  78. assert Integral(f, (x, -2, 2)).principal_value() == -atan(2) - log(3) / 2
  79. def diff_test(i):
  80. """Return the set of symbols, s, which were used in testing that
  81. i.diff(s) agrees with i.doit().diff(s). If there is an error then
  82. the assertion will fail, causing the test to fail."""
  83. syms = i.free_symbols
  84. for s in syms:
  85. assert (i.diff(s).doit() - i.doit().diff(s)).expand() == 0
  86. return syms
  87. def test_improper_integral():
  88. assert integrate(log(x), (x, 0, 1)) == -1
  89. assert integrate(x**(-2), (x, 1, oo)) == 1
  90. assert integrate(1/(1 + exp(x)), (x, 0, oo)) == log(2)
  91. def test_constructor():
  92. # this is shared by Sum, so testing Integral's constructor
  93. # is equivalent to testing Sum's
  94. s1 = Integral(n, n)
  95. assert s1.limits == (Tuple(n),)
  96. s2 = Integral(n, (n,))
  97. assert s2.limits == (Tuple(n),)
  98. s3 = Integral(Sum(x, (x, 1, y)))
  99. assert s3.limits == (Tuple(y),)
  100. s4 = Integral(n, Tuple(n,))
  101. assert s4.limits == (Tuple(n),)
  102. s5 = Integral(n, (n, Interval(1, 2)))
  103. assert s5.limits == (Tuple(n, 1, 2),)
  104. # Testing constructor with inequalities:
  105. s6 = Integral(n, n > 10)
  106. assert s6.limits == (Tuple(n, 10, oo),)
  107. s7 = Integral(n, (n > 2) & (n < 5))
  108. assert s7.limits == (Tuple(n, 2, 5),)
  109. def test_basics():
  110. assert Integral(0, x) != 0
  111. assert Integral(x, (x, 1, 1)) != 0
  112. assert Integral(oo, x) != oo
  113. assert Integral(S.NaN, x) is S.NaN
  114. assert diff(Integral(y, y), x) == 0
  115. assert diff(Integral(x, (x, 0, 1)), x) == 0
  116. assert diff(Integral(x, x), x) == x
  117. assert diff(Integral(t, (t, 0, x)), x) == x
  118. e = (t + 1)**2
  119. assert diff(integrate(e, (t, 0, x)), x) == \
  120. diff(Integral(e, (t, 0, x)), x).doit().expand() == \
  121. ((1 + x)**2).expand()
  122. assert diff(integrate(e, (t, 0, x)), t) == \
  123. diff(Integral(e, (t, 0, x)), t) == 0
  124. assert diff(integrate(e, (t, 0, x)), a) == \
  125. diff(Integral(e, (t, 0, x)), a) == 0
  126. assert diff(integrate(e, t), a) == diff(Integral(e, t), a) == 0
  127. assert integrate(e, (t, a, x)).diff(x) == \
  128. Integral(e, (t, a, x)).diff(x).doit().expand()
  129. assert Integral(e, (t, a, x)).diff(x).doit() == ((1 + x)**2)
  130. assert integrate(e, (t, x, a)).diff(x).doit() == (-(1 + x)**2).expand()
  131. assert integrate(t**2, (t, x, 2*x)).diff(x) == 7*x**2
  132. assert Integral(x, x).atoms() == {x}
  133. assert Integral(f(x), (x, 0, 1)).atoms() == {S.Zero, S.One, x}
  134. assert diff_test(Integral(x, (x, 3*y))) == {y}
  135. assert diff_test(Integral(x, (a, 3*y))) == {x, y}
  136. assert integrate(x, (x, oo, oo)) == 0 #issue 8171
  137. assert integrate(x, (x, -oo, -oo)) == 0
  138. # sum integral of terms
  139. assert integrate(y + x + exp(x), x) == x*y + x**2/2 + exp(x)
  140. assert Integral(x).is_commutative
  141. n = Symbol('n', commutative=False)
  142. assert Integral(n + x, x).is_commutative is False
  143. def test_diff_wrt():
  144. class Test(Expr):
  145. _diff_wrt = True
  146. is_commutative = True
  147. t = Test()
  148. assert integrate(t + 1, t) == t**2/2 + t
  149. assert integrate(t + 1, (t, 0, 1)) == Rational(3, 2)
  150. raises(ValueError, lambda: integrate(x + 1, x + 1))
  151. raises(ValueError, lambda: integrate(x + 1, (x + 1, 0, 1)))
  152. def test_basics_multiple():
  153. assert diff_test(Integral(x, (x, 3*x, 5*y), (y, x, 2*x))) == {x}
  154. assert diff_test(Integral(x, (x, 5*y), (y, x, 2*x))) == {x}
  155. assert diff_test(Integral(x, (x, 5*y), (y, y, 2*x))) == {x, y}
  156. assert diff_test(Integral(y, y, x)) == {x, y}
  157. assert diff_test(Integral(y*x, x, y)) == {x, y}
  158. assert diff_test(Integral(x + y, y, (y, 1, x))) == {x}
  159. assert diff_test(Integral(x + y, (x, x, y), (y, y, x))) == {x, y}
  160. def test_conjugate_transpose():
  161. A, B = symbols("A B", commutative=False)
  162. x = Symbol("x", complex=True)
  163. p = Integral(A*B, (x,))
  164. assert p.adjoint().doit() == p.doit().adjoint()
  165. assert p.conjugate().doit() == p.doit().conjugate()
  166. assert p.transpose().doit() == p.doit().transpose()
  167. x = Symbol("x", real=True)
  168. p = Integral(A*B, (x,))
  169. assert p.adjoint().doit() == p.doit().adjoint()
  170. assert p.conjugate().doit() == p.doit().conjugate()
  171. assert p.transpose().doit() == p.doit().transpose()
  172. def test_integration():
  173. assert integrate(0, (t, 0, x)) == 0
  174. assert integrate(3, (t, 0, x)) == 3*x
  175. assert integrate(t, (t, 0, x)) == x**2/2
  176. assert integrate(3*t, (t, 0, x)) == 3*x**2/2
  177. assert integrate(3*t**2, (t, 0, x)) == x**3
  178. assert integrate(1/t, (t, 1, x)) == log(x)
  179. assert integrate(-1/t**2, (t, 1, x)) == 1/x - 1
  180. assert integrate(t**2 + 5*t - 8, (t, 0, x)) == x**3/3 + 5*x**2/2 - 8*x
  181. assert integrate(x**2, x) == x**3/3
  182. assert integrate((3*t*x)**5, x) == (3*t)**5 * x**6 / 6
  183. b = Symbol("b")
  184. c = Symbol("c")
  185. assert integrate(a*t, (t, 0, x)) == a*x**2/2
  186. assert integrate(a*t**4, (t, 0, x)) == a*x**5/5
  187. assert integrate(a*t**2 + b*t + c, (t, 0, x)) == a*x**3/3 + b*x**2/2 + c*x
  188. def test_multiple_integration():
  189. assert integrate((x**2)*(y**2), (x, 0, 1), (y, -1, 2)) == Rational(1)
  190. assert integrate((y**2)*(x**2), x, y) == Rational(1, 9)*(x**3)*(y**3)
  191. assert integrate(1/(x + 3)/(1 + x)**3, x) == \
  192. log(3 + x)*Rational(-1, 8) + log(1 + x)*Rational(1, 8) + x/(4 + 8*x + 4*x**2)
  193. assert integrate(sin(x*y)*y, (x, 0, 1), (y, 0, 1)) == -sin(1) + 1
  194. def test_issue_3532():
  195. assert integrate(exp(-x), (x, 0, oo)) == 1
  196. def test_issue_3560():
  197. assert integrate(sqrt(x)**3, x) == 2*sqrt(x)**5/5
  198. assert integrate(sqrt(x), x) == 2*sqrt(x)**3/3
  199. assert integrate(1/sqrt(x)**3, x) == -2/sqrt(x)
  200. def test_issue_18038():
  201. raises(AttributeError, lambda: integrate((x, x)))
  202. def test_integrate_poly():
  203. p = Poly(x + x**2*y + y**3, x, y)
  204. # The stacklevel is based on Integral(Poly)
  205. with warns_deprecated_sympy():
  206. qx = Integral(p, x)
  207. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  208. qx = integrate(p, x)
  209. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  210. qy = integrate(p, y)
  211. assert isinstance(qx, Poly) is True
  212. assert isinstance(qy, Poly) is True
  213. assert qx.gens == (x, y)
  214. assert qy.gens == (x, y)
  215. assert qx.as_expr() == x**2/2 + x**3*y/3 + x*y**3
  216. assert qy.as_expr() == x*y + x**2*y**2/2 + y**4/4
  217. def test_integrate_poly_definite():
  218. p = Poly(x + x**2*y + y**3, x, y)
  219. with warns_deprecated_sympy():
  220. Qx = Integral(p, (x, 0, 1))
  221. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  222. Qx = integrate(p, (x, 0, 1))
  223. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  224. Qy = integrate(p, (y, 0, pi))
  225. assert isinstance(Qx, Poly) is True
  226. assert isinstance(Qy, Poly) is True
  227. assert Qx.gens == (y,)
  228. assert Qy.gens == (x,)
  229. assert Qx.as_expr() == S.Half + y/3 + y**3
  230. assert Qy.as_expr() == pi**4/4 + pi*x + pi**2*x**2/2
  231. def test_integrate_omit_var():
  232. y = Symbol('y')
  233. assert integrate(x) == x**2/2
  234. raises(ValueError, lambda: integrate(2))
  235. raises(ValueError, lambda: integrate(x*y))
  236. def test_integrate_poly_accurately():
  237. y = Symbol('y')
  238. assert integrate(x*sin(y), x) == x**2*sin(y)/2
  239. # when passed to risch_norman, this will be a CPU hog, so this really
  240. # checks, that integrated function is recognized as polynomial
  241. assert integrate(x**1000*sin(y), x) == x**1001*sin(y)/1001
  242. def test_issue_3635():
  243. y = Symbol('y')
  244. assert integrate(x**2, y) == x**2*y
  245. assert integrate(x**2, (y, -1, 1)) == 2*x**2
  246. # works in SymPy and py.test but hangs in `setup.py test`
  247. def test_integrate_linearterm_pow():
  248. # check integrate((a*x+b)^c, x) -- issue 3499
  249. y = Symbol('y', positive=True)
  250. # TODO: Remove conds='none' below, let the assumption take care of it.
  251. assert integrate(x**y, x, conds='none') == x**(y + 1)/(y + 1)
  252. assert integrate((exp(y)*x + 1/y)**(1 + sin(y)), x, conds='none') == \
  253. exp(-y)*(exp(y)*x + 1/y)**(2 + sin(y)) / (2 + sin(y))
  254. def test_issue_3618():
  255. assert integrate(pi*sqrt(x), x) == 2*pi*sqrt(x)**3/3
  256. assert integrate(pi*sqrt(x) + E*sqrt(x)**3, x) == \
  257. 2*pi*sqrt(x)**3/3 + 2*E *sqrt(x)**5/5
  258. def test_issue_3623():
  259. assert integrate(cos((n + 1)*x), x) == Piecewise(
  260. (sin(x*(n + 1))/(n + 1), Ne(n + 1, 0)), (x, True))
  261. assert integrate(cos((n - 1)*x), x) == Piecewise(
  262. (sin(x*(n - 1))/(n - 1), Ne(n - 1, 0)), (x, True))
  263. assert integrate(cos((n + 1)*x) + cos((n - 1)*x), x) == \
  264. Piecewise((sin(x*(n - 1))/(n - 1), Ne(n - 1, 0)), (x, True)) + \
  265. Piecewise((sin(x*(n + 1))/(n + 1), Ne(n + 1, 0)), (x, True))
  266. def test_issue_3664():
  267. n = Symbol('n', integer=True, nonzero=True)
  268. assert integrate(-1./2 * x * sin(n * pi * x/2), [x, -2, 0]) == \
  269. 2.0*cos(pi*n)/(pi*n)
  270. assert integrate(x * sin(n * pi * x/2) * Rational(-1, 2), [x, -2, 0]) == \
  271. 2*cos(pi*n)/(pi*n)
  272. def test_issue_3679():
  273. # definite integration of rational functions gives wrong answers
  274. assert NS(Integral(1/(x**2 - 8*x + 17), (x, 2, 4))) == '1.10714871779409'
  275. def test_issue_3686(): # remove this when fresnel integrals are implemented
  276. from sympy.core.function import expand_func
  277. from sympy.functions.special.error_functions import fresnels
  278. assert expand_func(integrate(sin(x**2), x)) == \
  279. sqrt(2)*sqrt(pi)*fresnels(sqrt(2)*x/sqrt(pi))/2
  280. def test_integrate_units():
  281. m = units.m
  282. s = units.s
  283. assert integrate(x * m/s, (x, 1*s, 5*s)) == 12*m*s
  284. def test_transcendental_functions():
  285. assert integrate(LambertW(2*x), x) == \
  286. -x + x*LambertW(2*x) + x/LambertW(2*x)
  287. def test_log_polylog():
  288. assert integrate(log(1 - x)/x, (x, 0, 1)) == -pi**2/6
  289. assert integrate(log(x)*(1 - x)**(-1), (x, 0, 1)) == -pi**2/6
  290. def test_issue_3740():
  291. f = 4*log(x) - 2*log(x)**2
  292. fid = diff(integrate(f, x), x)
  293. assert abs(f.subs(x, 42).evalf() - fid.subs(x, 42).evalf()) < 1e-10
  294. def test_issue_3788():
  295. assert integrate(1/(1 + x**2), x) == atan(x)
  296. def test_issue_3952():
  297. f = sin(x)
  298. assert integrate(f, x) == -cos(x)
  299. raises(ValueError, lambda: integrate(f, 2*x))
  300. def test_issue_4516():
  301. assert integrate(2**x - 2*x, x) == 2**x/log(2) - x**2
  302. def test_issue_7450():
  303. ans = integrate(exp(-(1 + I)*x), (x, 0, oo))
  304. assert re(ans) == S.Half and im(ans) == Rational(-1, 2)
  305. def test_issue_8623():
  306. assert integrate((1 + cos(2*x)) / (3 - 2*cos(2*x)), (x, 0, pi)) == -pi/2 + sqrt(5)*pi/2
  307. assert integrate((1 + cos(2*x))/(3 - 2*cos(2*x))) == -x/2 + sqrt(5)*(atan(sqrt(5)*tan(x)) + \
  308. pi*floor((x - pi/2)/pi))/2
  309. def test_issue_9569():
  310. assert integrate(1 / (2 - cos(x)), (x, 0, pi)) == pi/sqrt(3)
  311. assert integrate(1/(2 - cos(x))) == 2*sqrt(3)*(atan(sqrt(3)*tan(x/2)) + pi*floor((x/2 - pi/2)/pi))/3
  312. def test_issue_13733():
  313. s = Symbol('s', positive=True)
  314. pz = exp(-(z - y)**2/(2*s*s))/sqrt(2*pi*s*s)
  315. pzgx = integrate(pz, (z, x, oo))
  316. assert integrate(pzgx, (x, 0, oo)) == sqrt(2)*s*exp(-y**2/(2*s**2))/(2*sqrt(pi)) + \
  317. y*erf(sqrt(2)*y/(2*s))/2 + y/2
  318. def test_issue_13749():
  319. assert integrate(1 / (2 + cos(x)), (x, 0, pi)) == pi/sqrt(3)
  320. assert integrate(1/(2 + cos(x))) == 2*sqrt(3)*(atan(sqrt(3)*tan(x/2)/3) + pi*floor((x/2 - pi/2)/pi))/3
  321. def test_issue_18133():
  322. assert integrate(exp(x)/(1 + x)**2, x) == NonElementaryIntegral(exp(x)/(x + 1)**2, x)
  323. def test_issue_21741():
  324. a = 4e6
  325. b = 2.5e-7
  326. r = Piecewise((b*I*exp(-a*I*pi*t*y)*exp(-a*I*pi*x*z)/(pi*x), Ne(x, 0)),
  327. (z*exp(-a*I*pi*t*y), True))
  328. fun = E**((-2*I*pi*(z*x+t*y))/(500*10**(-9)))
  329. assert all_close(integrate(fun, z), r)
  330. def test_matrices():
  331. M = Matrix(2, 2, lambda i, j: (i + j + 1)*sin((i + j + 1)*x))
  332. assert integrate(M, x) == Matrix([
  333. [-cos(x), -cos(2*x)],
  334. [-cos(2*x), -cos(3*x)],
  335. ])
  336. def test_integrate_functions():
  337. # issue 4111
  338. assert integrate(f(x), x) == Integral(f(x), x)
  339. assert integrate(f(x), (x, 0, 1)) == Integral(f(x), (x, 0, 1))
  340. assert integrate(f(x)*diff(f(x), x), x) == f(x)**2/2
  341. assert integrate(diff(f(x), x) / f(x), x) == log(f(x))
  342. def test_integrate_derivatives():
  343. assert integrate(Derivative(f(x), x), x) == f(x)
  344. assert integrate(Derivative(f(y), y), x) == x*Derivative(f(y), y)
  345. assert integrate(Derivative(f(x), x)**2, x) == \
  346. Integral(Derivative(f(x), x)**2, x)
  347. def test_transform():
  348. a = Integral(x**2 + 1, (x, -1, 2))
  349. fx = x
  350. fy = 3*y + 1
  351. assert a.doit() == a.transform(fx, fy).doit()
  352. assert a.transform(fx, fy).transform(fy, fx) == a
  353. fx = 3*x + 1
  354. fy = y
  355. assert a.transform(fx, fy).transform(fy, fx) == a
  356. a = Integral(sin(1/x), (x, 0, 1))
  357. assert a.transform(x, 1/y) == Integral(sin(y)/y**2, (y, 1, oo))
  358. assert a.transform(x, 1/y).transform(y, 1/x) == a
  359. a = Integral(exp(-x**2), (x, -oo, oo))
  360. assert a.transform(x, 2*y) == Integral(2*exp(-4*y**2), (y, -oo, oo))
  361. # < 3 arg limit handled properly
  362. assert Integral(x, x).transform(x, a*y).doit() == \
  363. Integral(y*a**2, y).doit()
  364. _3 = S(3)
  365. assert Integral(x, (x, 0, -_3)).transform(x, 1/y).doit() == \
  366. Integral(-1/x**3, (x, -oo, -1/_3)).doit()
  367. assert Integral(x, (x, 0, _3)).transform(x, 1/y) == \
  368. Integral(y**(-3), (y, 1/_3, oo))
  369. # issue 8400
  370. i = Integral(x + y, (x, 1, 2), (y, 1, 2))
  371. assert i.transform(x, (x + 2*y, x)).doit() == \
  372. i.transform(x, (x + 2*z, x)).doit() == 3
  373. i = Integral(x, (x, a, b))
  374. assert i.transform(x, 2*s) == Integral(4*s, (s, a/2, b/2))
  375. raises(ValueError, lambda: i.transform(x, 1))
  376. raises(ValueError, lambda: i.transform(x, s*t))
  377. raises(ValueError, lambda: i.transform(x, -s))
  378. raises(ValueError, lambda: i.transform(x, (s, t)))
  379. raises(ValueError, lambda: i.transform(2*x, 2*s))
  380. i = Integral(x**2, (x, 1, 2))
  381. raises(ValueError, lambda: i.transform(x**2, s))
  382. am = Symbol('a', negative=True)
  383. bp = Symbol('b', positive=True)
  384. i = Integral(x, (x, bp, am))
  385. i.transform(x, 2*s)
  386. assert i.transform(x, 2*s) == Integral(-4*s, (s, am/2, bp/2))
  387. i = Integral(x, (x, a))
  388. assert i.transform(x, 2*s) == Integral(4*s, (s, a/2))
  389. def test_issue_4052():
  390. f = S.Half*asin(x) + x*sqrt(1 - x**2)/2
  391. assert integrate(cos(asin(x)), x) == f
  392. assert integrate(sin(acos(x)), x) == f
  393. @slow
  394. def test_evalf_integrals():
  395. assert NS(Integral(x, (x, 2, 5)), 15) == '10.5000000000000'
  396. gauss = Integral(exp(-x**2), (x, -oo, oo))
  397. assert NS(gauss, 15) == '1.77245385090552'
  398. assert NS(gauss**2 - pi + E*Rational(
  399. 1, 10**20), 15) in ('2.71828182845904e-20', '2.71828182845905e-20')
  400. # A monster of an integral from http://mathworld.wolfram.com/DefiniteIntegral.html
  401. t = Symbol('t')
  402. a = 8*sqrt(3)/(1 + 3*t**2)
  403. b = 16*sqrt(2)*(3*t + 1)*sqrt(4*t**2 + t + 1)**3
  404. c = (3*t**2 + 1)*(11*t**2 + 2*t + 3)**2
  405. d = sqrt(2)*(249*t**2 + 54*t + 65)/(11*t**2 + 2*t + 3)**2
  406. f = a - b/c - d
  407. assert NS(Integral(f, (t, 0, 1)), 50) == \
  408. NS((3*sqrt(2) - 49*pi + 162*atan(sqrt(2)))/12, 50)
  409. # http://mathworld.wolfram.com/VardisIntegral.html
  410. assert NS(Integral(log(log(1/x))/(1 + x + x**2), (x, 0, 1)), 15) == \
  411. NS('pi/sqrt(3) * log(2*pi**(5/6) / gamma(1/6))', 15)
  412. # http://mathworld.wolfram.com/AhmedsIntegral.html
  413. assert NS(Integral(atan(sqrt(x**2 + 2))/(sqrt(x**2 + 2)*(x**2 + 1)), (x,
  414. 0, 1)), 15) == NS(5*pi**2/96, 15)
  415. # http://mathworld.wolfram.com/AbelsIntegral.html
  416. assert NS(Integral(x/((exp(pi*x) - exp(
  417. -pi*x))*(x**2 + 1)), (x, 0, oo)), 15) == NS('log(2)/2-1/4', 15)
  418. # Complex part trimming
  419. # http://mathworld.wolfram.com/VardisIntegral.html
  420. assert NS(Integral(log(log(sin(x)/cos(x))), (x, pi/4, pi/2)), 15, chop=True) == \
  421. NS('pi/4*log(4*pi**3/gamma(1/4)**4)', 15)
  422. #
  423. # Endpoints causing trouble (rounding error in integration points -> complex log)
  424. assert NS(
  425. 2 + Integral(log(2*cos(x/2)), (x, -pi, pi)), 17, chop=True) == NS(2, 17)
  426. assert NS(
  427. 2 + Integral(log(2*cos(x/2)), (x, -pi, pi)), 20, chop=True) == NS(2, 20)
  428. assert NS(
  429. 2 + Integral(log(2*cos(x/2)), (x, -pi, pi)), 22, chop=True) == NS(2, 22)
  430. # Needs zero handling
  431. assert NS(pi - 4*Integral(
  432. 'sqrt(1-x**2)', (x, 0, 1)), 15, maxn=30, chop=True) in ('0.0', '0')
  433. # Oscillatory quadrature
  434. a = Integral(sin(x)/x**2, (x, 1, oo)).evalf(maxn=15)
  435. assert 0.49 < a < 0.51
  436. assert NS(
  437. Integral(sin(x)/x**2, (x, 1, oo)), quad='osc') == '0.504067061906928'
  438. assert NS(Integral(
  439. cos(pi*x + 1)/x, (x, -oo, -1)), quad='osc') == '0.276374705640365'
  440. # indefinite integrals aren't evaluated
  441. assert NS(Integral(x, x)) == 'Integral(x, x)'
  442. assert NS(Integral(x, (x, y))) == 'Integral(x, (x, y))'
  443. def test_evalf_issue_939():
  444. # https://github.com/sympy/sympy/issues/4038
  445. # The output form of an integral may differ by a step function between
  446. # revisions, making this test a bit useless. This can't be said about
  447. # other two tests. For now, all values of this evaluation are used here,
  448. # but in future this should be reconsidered.
  449. assert NS(integrate(1/(x**5 + 1), x).subs(x, 4), chop=True) in \
  450. ['-0.000976138910649103', '0.965906660135753', '1.93278945918216']
  451. assert NS(Integral(1/(x**5 + 1), (x, 2, 4))) == '0.0144361088886740'
  452. assert NS(
  453. integrate(1/(x**5 + 1), (x, 2, 4)), chop=True) == '0.0144361088886740'
  454. def test_double_previously_failing_integrals():
  455. # Double integrals not implemented <- Sure it is!
  456. res = integrate(sqrt(x) + x*y, (x, 1, 2), (y, -1, 1))
  457. # Old numerical test
  458. assert NS(res, 15) == '2.43790283299492'
  459. # Symbolic test
  460. assert res == Rational(-4, 3) + 8*sqrt(2)/3
  461. # double integral + zero detection
  462. assert integrate(sin(x + x*y), (x, -1, 1), (y, -1, 1)) is S.Zero
  463. def test_integrate_SingularityFunction():
  464. in_1 = SingularityFunction(x, a, 3) + SingularityFunction(x, 5, -1)
  465. out_1 = SingularityFunction(x, a, 4)/4 + SingularityFunction(x, 5, 0)
  466. assert integrate(in_1, x) == out_1
  467. in_2 = 10*SingularityFunction(x, 4, 0) - 5*SingularityFunction(x, -6, -2)
  468. out_2 = 10*SingularityFunction(x, 4, 1) - 5*SingularityFunction(x, -6, -1)
  469. assert integrate(in_2, x) == out_2
  470. in_3 = 2*x**2*y -10*SingularityFunction(x, -4, 7) - 2*SingularityFunction(y, 10, -2)
  471. out_3_1 = 2*x**3*y/3 - 2*x*SingularityFunction(y, 10, -2) - 5*SingularityFunction(x, -4, 8)/4
  472. out_3_2 = x**2*y**2 - 10*y*SingularityFunction(x, -4, 7) - 2*SingularityFunction(y, 10, -1)
  473. assert integrate(in_3, x) == out_3_1
  474. assert integrate(in_3, y) == out_3_2
  475. assert unchanged(Integral, in_3, (x,))
  476. assert Integral(in_3, x) == Integral(in_3, (x,))
  477. assert Integral(in_3, x).doit() == out_3_1
  478. in_4 = 10*SingularityFunction(x, -4, 7) - 2*SingularityFunction(x, 10, -2)
  479. out_4 = 5*SingularityFunction(x, -4, 8)/4 - 2*SingularityFunction(x, 10, -1)
  480. assert integrate(in_4, (x, -oo, x)) == out_4
  481. assert integrate(SingularityFunction(x, 5, -1), x) == SingularityFunction(x, 5, 0)
  482. assert integrate(SingularityFunction(x, 0, -1), (x, -oo, oo)) == 1
  483. assert integrate(5*SingularityFunction(x, 5, -1), (x, -oo, oo)) == 5
  484. assert integrate(SingularityFunction(x, 5, -1) * f(x), (x, -oo, oo)) == f(5)
  485. def test_integrate_DiracDelta():
  486. # This is here to check that deltaintegrate is being called, but also
  487. # to test definite integrals. More tests are in test_deltafunctions.py
  488. assert integrate(DiracDelta(x) * f(x), (x, -oo, oo)) == f(0)
  489. assert integrate(DiracDelta(x)**2, (x, -oo, oo)) == DiracDelta(0)
  490. # issue 4522
  491. assert integrate(integrate((4 - 4*x + x*y - 4*y) * \
  492. DiracDelta(x)*DiracDelta(y - 1), (x, 0, 1)), (y, 0, 1)) == 0
  493. # issue 5729
  494. p = exp(-(x**2 + y**2))/pi
  495. assert integrate(p*DiracDelta(x - 10*y), (x, -oo, oo), (y, -oo, oo)) == \
  496. integrate(p*DiracDelta(x - 10*y), (y, -oo, oo), (x, -oo, oo)) == \
  497. integrate(p*DiracDelta(10*x - y), (x, -oo, oo), (y, -oo, oo)) == \
  498. integrate(p*DiracDelta(10*x - y), (y, -oo, oo), (x, -oo, oo)) == \
  499. 1/sqrt(101*pi)
  500. def test_integrate_returns_piecewise():
  501. assert integrate(x**y, x) == Piecewise(
  502. (x**(y + 1)/(y + 1), Ne(y, -1)), (log(x), True))
  503. assert integrate(x**y, y) == Piecewise(
  504. (x**y/log(x), Ne(log(x), 0)), (y, True))
  505. assert integrate(exp(n*x), x) == Piecewise(
  506. (exp(n*x)/n, Ne(n, 0)), (x, True))
  507. assert integrate(x*exp(n*x), x) == Piecewise(
  508. ((n*x - 1)*exp(n*x)/n**2, Ne(n**2, 0)), (x**2/2, True))
  509. assert integrate(x**(n*y), x) == Piecewise(
  510. (x**(n*y + 1)/(n*y + 1), Ne(n*y, -1)), (log(x), True))
  511. assert integrate(x**(n*y), y) == Piecewise(
  512. (x**(n*y)/(n*log(x)), Ne(n*log(x), 0)), (y, True))
  513. assert integrate(cos(n*x), x) == Piecewise(
  514. (sin(n*x)/n, Ne(n, 0)), (x, True))
  515. assert integrate(cos(n*x)**2, x) == Piecewise(
  516. ((n*x/2 + sin(n*x)*cos(n*x)/2)/n, Ne(n, 0)), (x, True))
  517. assert integrate(x*cos(n*x), x) == Piecewise(
  518. (x*sin(n*x)/n + cos(n*x)/n**2, Ne(n, 0)), (x**2/2, True))
  519. assert integrate(sin(n*x), x) == Piecewise(
  520. (-cos(n*x)/n, Ne(n, 0)), (0, True))
  521. assert integrate(sin(n*x)**2, x) == Piecewise(
  522. ((n*x/2 - sin(n*x)*cos(n*x)/2)/n, Ne(n, 0)), (0, True))
  523. assert integrate(x*sin(n*x), x) == Piecewise(
  524. (-x*cos(n*x)/n + sin(n*x)/n**2, Ne(n, 0)), (0, True))
  525. assert integrate(exp(x*y), (x, 0, z)) == Piecewise(
  526. (exp(y*z)/y - 1/y, (y > -oo) & (y < oo) & Ne(y, 0)), (z, True))
  527. # https://github.com/sympy/sympy/issues/23707
  528. assert integrate(exp(t)*exp(-t*sqrt(x - y)), t) == Piecewise(
  529. (-exp(t)/(sqrt(x - y)*exp(t*sqrt(x - y)) - exp(t*sqrt(x - y))),
  530. Ne(x, y + 1)), (t, True))
  531. def test_integrate_max_min():
  532. x = symbols('x', real=True)
  533. assert integrate(Min(x, 2), (x, 0, 3)) == 4
  534. assert integrate(Max(x**2, x**3), (x, 0, 2)) == Rational(49, 12)
  535. assert integrate(Min(exp(x), exp(-x))**2, x) == Piecewise( \
  536. (exp(2*x)/2, x <= 0), (1 - exp(-2*x)/2, True))
  537. # issue 7907
  538. c = symbols('c', extended_real=True)
  539. int1 = integrate(Max(c, x)*exp(-x**2), (x, -oo, oo))
  540. int2 = integrate(c*exp(-x**2), (x, -oo, c))
  541. int3 = integrate(x*exp(-x**2), (x, c, oo))
  542. assert int1 == int2 + int3 == sqrt(pi)*c*erf(c)/2 + \
  543. sqrt(pi)*c/2 + exp(-c**2)/2
  544. def test_integrate_Abs_sign():
  545. assert integrate(Abs(x), (x, -2, 1)) == Rational(5, 2)
  546. assert integrate(Abs(x), (x, 0, 1)) == S.Half
  547. assert integrate(Abs(x + 1), (x, 0, 1)) == Rational(3, 2)
  548. assert integrate(Abs(x**2 - 1), (x, -2, 2)) == 4
  549. assert integrate(Abs(x**2 - 3*x), (x, -15, 15)) == 2259
  550. assert integrate(sign(x), (x, -1, 2)) == 1
  551. assert integrate(sign(x)*sin(x), (x, -pi, pi)) == 4
  552. assert integrate(sign(x - 2) * x**2, (x, 0, 3)) == Rational(11, 3)
  553. t, s = symbols('t s', real=True)
  554. assert integrate(Abs(t), t) == Piecewise(
  555. (-t**2/2, t <= 0), (t**2/2, True))
  556. assert integrate(Abs(2*t - 6), t) == Piecewise(
  557. (-t**2 + 6*t, t <= 3), (t**2 - 6*t + 18, True))
  558. assert (integrate(abs(t - s**2), (t, 0, 2)) ==
  559. 2*s**2*Min(2, s**2) - 2*s**2 - Min(2, s**2)**2 + 2)
  560. assert integrate(exp(-Abs(t)), t) == Piecewise(
  561. (exp(t), t <= 0), (2 - exp(-t), True))
  562. assert integrate(sign(2*t - 6), t) == Piecewise(
  563. (-t, t < 3), (t - 6, True))
  564. assert integrate(2*t*sign(t**2 - 1), t) == Piecewise(
  565. (t**2, t < -1), (-t**2 + 2, t < 1), (t**2, True))
  566. assert integrate(sign(t), (t, s + 1)) == Piecewise(
  567. (s + 1, s + 1 > 0), (-s - 1, s + 1 < 0), (0, True))
  568. def test_subs1():
  569. e = Integral(exp(x - y), x)
  570. assert e.subs(y, 3) == Integral(exp(x - 3), x)
  571. e = Integral(exp(x - y), (x, 0, 1))
  572. assert e.subs(y, 3) == Integral(exp(x - 3), (x, 0, 1))
  573. f = Lambda(x, exp(-x**2))
  574. conv = Integral(f(x - y)*f(y), (y, -oo, oo))
  575. assert conv.subs({x: 0}) == Integral(exp(-2*y**2), (y, -oo, oo))
  576. def test_subs2():
  577. e = Integral(exp(x - y), x, t)
  578. assert e.subs(y, 3) == Integral(exp(x - 3), x, t)
  579. e = Integral(exp(x - y), (x, 0, 1), (t, 0, 1))
  580. assert e.subs(y, 3) == Integral(exp(x - 3), (x, 0, 1), (t, 0, 1))
  581. f = Lambda(x, exp(-x**2))
  582. conv = Integral(f(x - y)*f(y), (y, -oo, oo), (t, 0, 1))
  583. assert conv.subs({x: 0}) == Integral(exp(-2*y**2), (y, -oo, oo), (t, 0, 1))
  584. def test_subs3():
  585. e = Integral(exp(x - y), (x, 0, y), (t, y, 1))
  586. assert e.subs(y, 3) == Integral(exp(x - 3), (x, 0, 3), (t, 3, 1))
  587. f = Lambda(x, exp(-x**2))
  588. conv = Integral(f(x - y)*f(y), (y, -oo, oo), (t, x, 1))
  589. assert conv.subs({x: 0}) == Integral(exp(-2*y**2), (y, -oo, oo), (t, 0, 1))
  590. def test_subs4():
  591. e = Integral(exp(x), (x, 0, y), (t, y, 1))
  592. assert e.subs(y, 3) == Integral(exp(x), (x, 0, 3), (t, 3, 1))
  593. f = Lambda(x, exp(-x**2))
  594. conv = Integral(f(y)*f(y), (y, -oo, oo), (t, x, 1))
  595. assert conv.subs({x: 0}) == Integral(exp(-2*y**2), (y, -oo, oo), (t, 0, 1))
  596. def test_subs5():
  597. e = Integral(exp(-x**2), (x, -oo, oo))
  598. assert e.subs(x, 5) == e
  599. e = Integral(exp(-x**2 + y), x)
  600. assert e.subs(y, 5) == Integral(exp(-x**2 + 5), x)
  601. e = Integral(exp(-x**2 + y), (x, x))
  602. assert e.subs(x, 5) == Integral(exp(y - x**2), (x, 5))
  603. assert e.subs(y, 5) == Integral(exp(-x**2 + 5), x)
  604. e = Integral(exp(-x**2 + y), (y, -oo, oo), (x, -oo, oo))
  605. assert e.subs(x, 5) == e
  606. assert e.subs(y, 5) == e
  607. # Test evaluation of antiderivatives
  608. e = Integral(exp(-x**2), (x, x))
  609. assert e.subs(x, 5) == Integral(exp(-x**2), (x, 5))
  610. e = Integral(exp(x), x)
  611. assert (e.subs(x,1) - e.subs(x,0) - Integral(exp(x), (x, 0, 1))
  612. ).doit().is_zero
  613. def test_subs6():
  614. a, b = symbols('a b')
  615. e = Integral(x*y, (x, f(x), f(y)))
  616. assert e.subs(x, 1) == Integral(x*y, (x, f(1), f(y)))
  617. assert e.subs(y, 1) == Integral(x, (x, f(x), f(1)))
  618. e = Integral(x*y, (x, f(x), f(y)), (y, f(x), f(y)))
  619. assert e.subs(x, 1) == Integral(x*y, (x, f(1), f(y)), (y, f(1), f(y)))
  620. assert e.subs(y, 1) == Integral(x*y, (x, f(x), f(y)), (y, f(x), f(1)))
  621. e = Integral(x*y, (x, f(x), f(a)), (y, f(x), f(a)))
  622. assert e.subs(a, 1) == Integral(x*y, (x, f(x), f(1)), (y, f(x), f(1)))
  623. def test_subs7():
  624. e = Integral(x, (x, 1, y), (y, 1, 2))
  625. assert e.subs({x: 1, y: 2}) == e
  626. e = Integral(sin(x) + sin(y), (x, sin(x), sin(y)),
  627. (y, 1, 2))
  628. assert e.subs(sin(y), 1) == e
  629. assert e.subs(sin(x), 1) == Integral(sin(x) + sin(y), (x, 1, sin(y)),
  630. (y, 1, 2))
  631. def test_expand():
  632. e = Integral(f(x)+f(x**2), (x, 1, y))
  633. assert e.expand() == Integral(f(x), (x, 1, y)) + Integral(f(x**2), (x, 1, y))
  634. e = Integral(f(x)+f(x**2), (x, 1, oo))
  635. assert e.expand() == e
  636. assert e.expand(force=True) == Integral(f(x), (x, 1, oo)) + \
  637. Integral(f(x**2), (x, 1, oo))
  638. def test_integration_variable():
  639. raises(ValueError, lambda: Integral(exp(-x**2), 3))
  640. raises(ValueError, lambda: Integral(exp(-x**2), (3, -oo, oo)))
  641. def test_expand_integral():
  642. assert Integral(cos(x**2)*(sin(x**2) + 1), (x, 0, 1)).expand() == \
  643. Integral(cos(x**2)*sin(x**2), (x, 0, 1)) + \
  644. Integral(cos(x**2), (x, 0, 1))
  645. assert Integral(cos(x**2)*(sin(x**2) + 1), x).expand() == \
  646. Integral(cos(x**2)*sin(x**2), x) + \
  647. Integral(cos(x**2), x)
  648. def test_as_sum_midpoint1():
  649. e = Integral(sqrt(x**3 + 1), (x, 2, 10))
  650. assert e.as_sum(1, method="midpoint") == 8*sqrt(217)
  651. assert e.as_sum(2, method="midpoint") == 4*sqrt(65) + 12*sqrt(57)
  652. assert e.as_sum(3, method="midpoint") == 8*sqrt(217)/3 + \
  653. 8*sqrt(3081)/27 + 8*sqrt(52809)/27
  654. assert e.as_sum(4, method="midpoint") == 2*sqrt(730) + \
  655. 4*sqrt(7) + 4*sqrt(86) + 6*sqrt(14)
  656. assert abs(e.as_sum(4, method="midpoint").n() - e.n()) < 0.5
  657. e = Integral(sqrt(x**3 + y**3), (x, 2, 10), (y, 0, 10))
  658. raises(NotImplementedError, lambda: e.as_sum(4))
  659. def test_as_sum_midpoint2():
  660. e = Integral((x + y)**2, (x, 0, 1))
  661. n = Symbol('n', positive=True, integer=True)
  662. assert e.as_sum(1, method="midpoint").expand() == Rational(1, 4) + y + y**2
  663. assert e.as_sum(2, method="midpoint").expand() == Rational(5, 16) + y + y**2
  664. assert e.as_sum(3, method="midpoint").expand() == Rational(35, 108) + y + y**2
  665. assert e.as_sum(4, method="midpoint").expand() == Rational(21, 64) + y + y**2
  666. assert e.as_sum(n, method="midpoint").expand() == \
  667. y**2 + y + Rational(1, 3) - 1/(12*n**2)
  668. def test_as_sum_left():
  669. e = Integral((x + y)**2, (x, 0, 1))
  670. assert e.as_sum(1, method="left").expand() == y**2
  671. assert e.as_sum(2, method="left").expand() == Rational(1, 8) + y/2 + y**2
  672. assert e.as_sum(3, method="left").expand() == Rational(5, 27) + y*Rational(2, 3) + y**2
  673. assert e.as_sum(4, method="left").expand() == Rational(7, 32) + y*Rational(3, 4) + y**2
  674. assert e.as_sum(n, method="left").expand() == \
  675. y**2 + y + Rational(1, 3) - y/n - 1/(2*n) + 1/(6*n**2)
  676. assert e.as_sum(10, method="left", evaluate=False).has(Sum)
  677. def test_as_sum_right():
  678. e = Integral((x + y)**2, (x, 0, 1))
  679. assert e.as_sum(1, method="right").expand() == 1 + 2*y + y**2
  680. assert e.as_sum(2, method="right").expand() == Rational(5, 8) + y*Rational(3, 2) + y**2
  681. assert e.as_sum(3, method="right").expand() == Rational(14, 27) + y*Rational(4, 3) + y**2
  682. assert e.as_sum(4, method="right").expand() == Rational(15, 32) + y*Rational(5, 4) + y**2
  683. assert e.as_sum(n, method="right").expand() == \
  684. y**2 + y + Rational(1, 3) + y/n + 1/(2*n) + 1/(6*n**2)
  685. def test_as_sum_trapezoid():
  686. e = Integral((x + y)**2, (x, 0, 1))
  687. assert e.as_sum(1, method="trapezoid").expand() == y**2 + y + S.Half
  688. assert e.as_sum(2, method="trapezoid").expand() == y**2 + y + Rational(3, 8)
  689. assert e.as_sum(3, method="trapezoid").expand() == y**2 + y + Rational(19, 54)
  690. assert e.as_sum(4, method="trapezoid").expand() == y**2 + y + Rational(11, 32)
  691. assert e.as_sum(n, method="trapezoid").expand() == \
  692. y**2 + y + Rational(1, 3) + 1/(6*n**2)
  693. assert Integral(sign(x), (x, 0, 1)).as_sum(1, 'trapezoid') == S.Half
  694. def test_as_sum_raises():
  695. e = Integral((x + y)**2, (x, 0, 1))
  696. raises(ValueError, lambda: e.as_sum(-1))
  697. raises(ValueError, lambda: e.as_sum(0))
  698. raises(ValueError, lambda: Integral(x).as_sum(3))
  699. raises(ValueError, lambda: e.as_sum(oo))
  700. raises(ValueError, lambda: e.as_sum(3, method='xxxx2'))
  701. def test_nested_doit():
  702. e = Integral(Integral(x, x), x)
  703. f = Integral(x, x, x)
  704. assert e.doit() == f.doit()
  705. def test_issue_4665():
  706. # Allow only upper or lower limit evaluation
  707. e = Integral(x**2, (x, None, 1))
  708. f = Integral(x**2, (x, 1, None))
  709. assert e.doit() == Rational(1, 3)
  710. assert f.doit() == Rational(-1, 3)
  711. assert Integral(x*y, (x, None, y)).subs(y, t) == Integral(x*t, (x, None, t))
  712. assert Integral(x*y, (x, y, None)).subs(y, t) == Integral(x*t, (x, t, None))
  713. assert integrate(x**2, (x, None, 1)) == Rational(1, 3)
  714. assert integrate(x**2, (x, 1, None)) == Rational(-1, 3)
  715. assert integrate("x**2", ("x", "1", None)) == Rational(-1, 3)
  716. def test_integral_reconstruct():
  717. e = Integral(x**2, (x, -1, 1))
  718. assert e == Integral(*e.args)
  719. def test_doit_integrals():
  720. e = Integral(Integral(2*x), (x, 0, 1))
  721. assert e.doit() == Rational(1, 3)
  722. assert e.doit(deep=False) == Rational(1, 3)
  723. f = Function('f')
  724. # doesn't matter if the integral can't be performed
  725. assert Integral(f(x), (x, 1, 1)).doit() == 0
  726. # doesn't matter if the limits can't be evaluated
  727. assert Integral(0, (x, 1, Integral(f(x), x))).doit() == 0
  728. assert Integral(x, (a, 0)).doit() == 0
  729. limits = ((a, 1, exp(x)), (x, 0))
  730. assert Integral(a, *limits).doit() == Rational(1, 4)
  731. assert Integral(a, *list(reversed(limits))).doit() == 0
  732. def test_issue_4884():
  733. assert integrate(sqrt(x)*(1 + x)) == \
  734. Piecewise(
  735. (2*sqrt(x)*(x + 1)**2/5 - 2*sqrt(x)*(x + 1)/15 - 4*sqrt(x)/15,
  736. Abs(x + 1) > 1),
  737. (2*I*sqrt(-x)*(x + 1)**2/5 - 2*I*sqrt(-x)*(x + 1)/15 -
  738. 4*I*sqrt(-x)/15, True))
  739. assert integrate(x**x*(1 + log(x))) == x**x
  740. def test_issue_18153():
  741. assert integrate(x**n*log(x),x) == \
  742. Piecewise(
  743. (n*x*x**n*log(x)/(n**2 + 2*n + 1) +
  744. x*x**n*log(x)/(n**2 + 2*n + 1) - x*x**n/(n**2 + 2*n + 1)
  745. , Ne(n, -1)), (log(x)**2/2, True)
  746. )
  747. def test_is_number():
  748. from sympy.abc import x, y, z
  749. assert Integral(x).is_number is False
  750. assert Integral(1, x).is_number is False
  751. assert Integral(1, (x, 1)).is_number is True
  752. assert Integral(1, (x, 1, 2)).is_number is True
  753. assert Integral(1, (x, 1, y)).is_number is False
  754. assert Integral(1, (x, y)).is_number is False
  755. assert Integral(x, y).is_number is False
  756. assert Integral(x, (y, 1, x)).is_number is False
  757. assert Integral(x, (y, 1, 2)).is_number is False
  758. assert Integral(x, (x, 1, 2)).is_number is True
  759. # `foo.is_number` should always be equivalent to `not foo.free_symbols`
  760. # in each of these cases, there are pseudo-free symbols
  761. i = Integral(x, (y, 1, 1))
  762. assert i.is_number is False and i.n() == 0
  763. i = Integral(x, (y, z, z))
  764. assert i.is_number is False and i.n() == 0
  765. i = Integral(1, (y, z, z + 2))
  766. assert i.is_number is False and i.n() == 2.0
  767. assert Integral(x*y, (x, 1, 2), (y, 1, 3)).is_number is True
  768. assert Integral(x*y, (x, 1, 2), (y, 1, z)).is_number is False
  769. assert Integral(x, (x, 1)).is_number is True
  770. assert Integral(x, (x, 1, Integral(y, (y, 1, 2)))).is_number is True
  771. assert Integral(Sum(z, (z, 1, 2)), (x, 1, 2)).is_number is True
  772. # it is possible to get a false negative if the integrand is
  773. # actually an unsimplified zero, but this is true of is_number in general.
  774. assert Integral(sin(x)**2 + cos(x)**2 - 1, x).is_number is False
  775. assert Integral(f(x), (x, 0, 1)).is_number is True
  776. def test_free_symbols():
  777. from sympy.abc import x, y, z
  778. assert Integral(0, x).free_symbols == {x}
  779. assert Integral(x).free_symbols == {x}
  780. assert Integral(x, (x, None, y)).free_symbols == {y}
  781. assert Integral(x, (x, y, None)).free_symbols == {y}
  782. assert Integral(x, (x, 1, y)).free_symbols == {y}
  783. assert Integral(x, (x, y, 1)).free_symbols == {y}
  784. assert Integral(x, (x, x, y)).free_symbols == {x, y}
  785. assert Integral(x, x, y).free_symbols == {x, y}
  786. assert Integral(x, (x, 1, 2)).free_symbols == set()
  787. assert Integral(x, (y, 1, 2)).free_symbols == {x}
  788. # pseudo-free in this case
  789. assert Integral(x, (y, z, z)).free_symbols == {x, z}
  790. assert Integral(x, (y, 1, 2), (y, None, None)
  791. ).free_symbols == {x, y}
  792. assert Integral(x, (y, 1, 2), (x, 1, y)
  793. ).free_symbols == {y}
  794. assert Integral(2, (y, 1, 2), (y, 1, x), (x, 1, 2)
  795. ).free_symbols == set()
  796. assert Integral(2, (y, x, 2), (y, 1, x), (x, 1, 2)
  797. ).free_symbols == set()
  798. assert Integral(2, (x, 1, 2), (y, x, 2), (y, 1, 2)
  799. ).free_symbols == {x}
  800. assert Integral(f(x), (f(x), 1, y)).free_symbols == {y}
  801. assert Integral(f(x), (f(x), 1, x)).free_symbols == {x}
  802. def test_is_zero():
  803. from sympy.abc import x, m
  804. assert Integral(0, (x, 1, x)).is_zero
  805. assert Integral(1, (x, 1, 1)).is_zero
  806. assert Integral(1, (x, 1, 2), (y, 2)).is_zero is False
  807. assert Integral(x, (m, 0)).is_zero
  808. assert Integral(x + m, (m, 0)).is_zero is None
  809. i = Integral(m, (m, 1, exp(x)), (x, 0))
  810. assert i.is_zero is None
  811. assert Integral(m, (x, 0), (m, 1, exp(x))).is_zero is True
  812. assert Integral(x, (x, oo, oo)).is_zero # issue 8171
  813. assert Integral(x, (x, -oo, -oo)).is_zero
  814. # this is zero but is beyond the scope of what is_zero
  815. # should be doing
  816. assert Integral(sin(x), (x, 0, 2*pi)).is_zero is None
  817. def test_series():
  818. from sympy.abc import x
  819. i = Integral(cos(x), (x, x))
  820. e = i.lseries(x)
  821. assert i.nseries(x, n=8).removeO() == Add(*[next(e) for j in range(4)])
  822. def test_trig_nonelementary_integrals():
  823. x = Symbol('x')
  824. assert integrate((1 + sin(x))/x, x) == log(x) + Si(x)
  825. # next one comes out as log(x) + log(x**2)/2 + Ci(x)
  826. # so not hardcoding this log ugliness
  827. assert integrate((cos(x) + 2)/x, x).has(Ci)
  828. def test_issue_4403():
  829. x = Symbol('x')
  830. y = Symbol('y')
  831. z = Symbol('z', positive=True)
  832. assert integrate(sqrt(x**2 + z**2), x) == \
  833. z**2*asinh(x/z)/2 + x*sqrt(x**2 + z**2)/2
  834. assert integrate(sqrt(x**2 - z**2), x) == \
  835. x*sqrt(x**2 - z**2)/2 - z**2*log(x + sqrt(x**2 - z**2))/2
  836. x = Symbol('x', real=True)
  837. y = Symbol('y', positive=True)
  838. assert integrate(1/(x**2 + y**2)**S('3/2'), x) == \
  839. x/(y**2*sqrt(x**2 + y**2))
  840. # If y is real and nonzero, we get x*Abs(y)/(y**3*sqrt(x**2 + y**2)),
  841. # which results from sqrt(1 + x**2/y**2) = sqrt(x**2 + y**2)/|y|.
  842. def test_issue_4403_2():
  843. assert integrate(sqrt(-x**2 - 4), x) == \
  844. -2*atan(x/sqrt(-4 - x**2)) + x*sqrt(-4 - x**2)/2
  845. def test_issue_4100():
  846. R = Symbol('R', positive=True)
  847. assert integrate(sqrt(R**2 - x**2), (x, 0, R)) == pi*R**2/4
  848. def test_issue_5167():
  849. from sympy.abc import w, x, y, z
  850. f = Function('f')
  851. assert Integral(Integral(f(x), x), x) == Integral(f(x), x, x)
  852. assert Integral(f(x)).args == (f(x), Tuple(x))
  853. assert Integral(Integral(f(x))).args == (f(x), Tuple(x), Tuple(x))
  854. assert Integral(Integral(f(x)), y).args == (f(x), Tuple(x), Tuple(y))
  855. assert Integral(Integral(f(x), z), y).args == (f(x), Tuple(z), Tuple(y))
  856. assert Integral(Integral(Integral(f(x), x), y), z).args == \
  857. (f(x), Tuple(x), Tuple(y), Tuple(z))
  858. assert integrate(Integral(f(x), x), x) == Integral(f(x), x, x)
  859. assert integrate(Integral(f(x), y), x) == y*Integral(f(x), x)
  860. assert integrate(Integral(f(x), x), y) in [Integral(y*f(x), x), y*Integral(f(x), x)]
  861. assert integrate(Integral(2, x), x) == x**2
  862. assert integrate(Integral(2, x), y) == 2*x*y
  863. # don't re-order given limits
  864. assert Integral(1, x, y).args != Integral(1, y, x).args
  865. # do as many as possible
  866. assert Integral(f(x), y, x, y, x).doit() == y**2*Integral(f(x), x, x)/2
  867. assert Integral(f(x), (x, 1, 2), (w, 1, x), (z, 1, y)).doit() == \
  868. y*(x - 1)*Integral(f(x), (x, 1, 2)) - (x - 1)*Integral(f(x), (x, 1, 2))
  869. def test_issue_4890():
  870. z = Symbol('z', positive=True)
  871. assert integrate(exp(-log(x)**2), x) == \
  872. sqrt(pi)*exp(Rational(1, 4))*erf(log(x) - S.Half)/2
  873. assert integrate(exp(log(x)**2), x) == \
  874. sqrt(pi)*exp(Rational(-1, 4))*erfi(log(x)+S.Half)/2
  875. assert integrate(exp(-z*log(x)**2), x) == \
  876. sqrt(pi)*exp(1/(4*z))*erf(sqrt(z)*log(x) - 1/(2*sqrt(z)))/(2*sqrt(z))
  877. def test_issue_4551():
  878. assert not integrate(1/(x*sqrt(1 - x**2)), x).has(Integral)
  879. def test_issue_4376():
  880. n = Symbol('n', integer=True, positive=True)
  881. assert simplify(integrate(n*(x**(1/n) - 1), (x, 0, S.Half)) -
  882. (n**2 - 2**(1/n)*n**2 - n*2**(1/n))/(2**(1 + 1/n) + n*2**(1 + 1/n))) == 0
  883. def test_issue_4517():
  884. assert integrate((sqrt(x) - x**3)/x**Rational(1, 3), x) == \
  885. 6*x**Rational(7, 6)/7 - 3*x**Rational(11, 3)/11
  886. def test_issue_4527():
  887. k, m = symbols('k m', integer=True)
  888. assert integrate(sin(k*x)*sin(m*x), (x, 0, pi)).simplify() == \
  889. Piecewise((0, Eq(k, 0) | Eq(m, 0)),
  890. (-pi/2, Eq(k, -m) | (Eq(k, 0) & Eq(m, 0))),
  891. (pi/2, Eq(k, m) | (Eq(k, 0) & Eq(m, 0))),
  892. (0, True))
  893. # Should be possible to further simplify to:
  894. # Piecewise(
  895. # (0, Eq(k, 0) | Eq(m, 0)),
  896. # (-pi/2, Eq(k, -m)),
  897. # (pi/2, Eq(k, m)),
  898. # (0, True))
  899. assert integrate(sin(k*x)*sin(m*x), (x,)) == Piecewise(
  900. (0, And(Eq(k, 0), Eq(m, 0))),
  901. (-x*sin(m*x)**2/2 - x*cos(m*x)**2/2 + sin(m*x)*cos(m*x)/(2*m), Eq(k, -m)),
  902. (x*sin(m*x)**2/2 + x*cos(m*x)**2/2 - sin(m*x)*cos(m*x)/(2*m), Eq(k, m)),
  903. (m*sin(k*x)*cos(m*x)/(k**2 - m**2) -
  904. k*sin(m*x)*cos(k*x)/(k**2 - m**2), True))
  905. def test_issue_4199():
  906. ypos = Symbol('y', positive=True)
  907. # TODO: Remove conds='none' below, let the assumption take care of it.
  908. assert integrate(exp(-I*2*pi*ypos*x)*x, (x, -oo, oo), conds='none') == \
  909. Integral(exp(-I*2*pi*ypos*x)*x, (x, -oo, oo))
  910. def test_issue_3940():
  911. a, b, c, d = symbols('a:d', positive=True)
  912. assert integrate(exp(-x**2 + I*c*x), x) == \
  913. -sqrt(pi)*exp(-c**2/4)*erf(I*c/2 - x)/2
  914. assert integrate(exp(a*x**2 + b*x + c), x).equals(
  915. sqrt(pi)*exp(c - b**2/(4*a))*erfi((2*a*x + b)/(2*sqrt(a)))/(2*sqrt(a)))
  916. from sympy.core.function import expand_mul
  917. from sympy.abc import k
  918. assert expand_mul(integrate(exp(-x**2)*exp(I*k*x), (x, -oo, oo))) == \
  919. sqrt(pi)*exp(-k**2/4)
  920. a, d = symbols('a d', positive=True)
  921. assert expand_mul(integrate(exp(-a*x**2 + 2*d*x), (x, -oo, oo))) == \
  922. sqrt(pi)*exp(d**2/a)/sqrt(a)
  923. def test_issue_5413():
  924. # Note that this is not the same as testing ratint() because integrate()
  925. # pulls out the coefficient.
  926. assert integrate(-a/(a**2 + x**2), x) == I*log(-I*a + x)/2 - I*log(I*a + x)/2
  927. def test_issue_4892a():
  928. A, z = symbols('A z')
  929. c = Symbol('c', nonzero=True)
  930. P1 = -A*exp(-z)
  931. P2 = -A/(c*t)*(sin(x)**2 + cos(y)**2)
  932. h1 = -sin(x)**2 - cos(y)**2
  933. h2 = -sin(x)**2 + sin(y)**2 - 1
  934. # there is still some non-deterministic behavior in integrate
  935. # or trigsimp which permits one of the following
  936. assert integrate(c*(P2 - P1), t) in [
  937. c*(-A*(-h1)*log(c*t)/c + A*t*exp(-z)),
  938. c*(-A*(-h2)*log(c*t)/c + A*t*exp(-z)),
  939. c*( A* h1 *log(c*t)/c + A*t*exp(-z)),
  940. c*( A* h2 *log(c*t)/c + A*t*exp(-z)),
  941. (A*c*t - A*(-h1)*log(t)*exp(z))*exp(-z),
  942. (A*c*t - A*(-h2)*log(t)*exp(z))*exp(-z),
  943. ]
  944. def test_issue_4892b():
  945. # Issues relating to issue 4596 are making the actual result of this hard
  946. # to test. The answer should be something like
  947. #
  948. # (-sin(y) + sqrt(-72 + 48*cos(y) - 8*cos(y)**2)/2)*log(x + sqrt(-72 +
  949. # 48*cos(y) - 8*cos(y)**2)/(2*(3 - cos(y)))) + (-sin(y) - sqrt(-72 +
  950. # 48*cos(y) - 8*cos(y)**2)/2)*log(x - sqrt(-72 + 48*cos(y) -
  951. # 8*cos(y)**2)/(2*(3 - cos(y)))) + x**2*sin(y)/2 + 2*x*cos(y)
  952. expr = (sin(y)*x**3 + 2*cos(y)*x**2 + 12)/(x**2 + 2)
  953. assert trigsimp(factor(integrate(expr, x).diff(x) - expr)) == 0
  954. def test_issue_5178():
  955. assert integrate(sin(x)*f(y, z), (x, 0, pi), (y, 0, pi), (z, 0, pi)) == \
  956. 2*Integral(f(y, z), (y, 0, pi), (z, 0, pi))
  957. def test_integrate_series():
  958. f = sin(x).series(x, 0, 10)
  959. g = x**2/2 - x**4/24 + x**6/720 - x**8/40320 + x**10/3628800 + O(x**11)
  960. assert integrate(f, x) == g
  961. assert diff(integrate(f, x), x) == f
  962. assert integrate(O(x**5), x) == O(x**6)
  963. def test_atom_bug():
  964. from sympy.integrals.heurisch import heurisch
  965. assert heurisch(meijerg([], [], [1], [], x), x) is None
  966. def test_limit_bug():
  967. z = Symbol('z', zero=False)
  968. assert integrate(sin(x*y*z), (x, 0, pi), (y, 0, pi)).together() == \
  969. (log(z) - Ci(pi**2*z) + EulerGamma + 2*log(pi))/z
  970. def test_issue_4703():
  971. g = Function('g')
  972. assert integrate(exp(x)*g(x), x).has(Integral)
  973. def test_issue_1888():
  974. f = Function('f')
  975. assert integrate(f(x).diff(x)**2, x).has(Integral)
  976. # The following tests work using meijerint.
  977. def test_issue_3558():
  978. assert integrate(cos(x*y), (x, -pi/2, pi/2), (y, 0, pi)) == 2*Si(pi**2/2)
  979. def test_issue_4422():
  980. assert integrate(1/sqrt(16 + 4*x**2), x) == asinh(x/2) / 2
  981. def test_issue_4493():
  982. assert simplify(integrate(x*sqrt(1 + 2*x), x)) == \
  983. sqrt(2*x + 1)*(6*x**2 + x - 1)/15
  984. def test_issue_4737():
  985. assert integrate(sin(x)/x, (x, -oo, oo)) == pi
  986. assert integrate(sin(x)/x, (x, 0, oo)) == pi/2
  987. assert integrate(sin(x)/x, x) == Si(x)
  988. def test_issue_4992():
  989. # Note: psi in _check_antecedents becomes NaN.
  990. from sympy.core.function import expand_func
  991. a = Symbol('a', positive=True)
  992. assert simplify(expand_func(integrate(exp(-x)*log(x)*x**a, (x, 0, oo)))) == \
  993. (a*polygamma(0, a) + 1)*gamma(a)
  994. def test_issue_4487():
  995. from sympy.functions.special.gamma_functions import lowergamma
  996. assert simplify(integrate(exp(-x)*x**y, x)) == lowergamma(y + 1, x)
  997. def test_issue_4215():
  998. x = Symbol("x")
  999. assert integrate(1/(x**2), (x, -1, 1)) is oo
  1000. def test_issue_4400():
  1001. n = Symbol('n', integer=True, positive=True)
  1002. assert integrate((x**n)*log(x), x) == \
  1003. n*x*x**n*log(x)/(n**2 + 2*n + 1) + x*x**n*log(x)/(n**2 + 2*n + 1) - \
  1004. x*x**n/(n**2 + 2*n + 1)
  1005. def test_issue_6253():
  1006. # Note: this used to raise NotImplementedError
  1007. # Note: psi in _check_antecedents becomes NaN.
  1008. assert integrate((sqrt(1 - x) + sqrt(1 + x))**2/x, x, meijerg=True) == \
  1009. Integral((sqrt(-x + 1) + sqrt(x + 1))**2/x, x)
  1010. def test_issue_4153():
  1011. assert integrate(1/(1 + x + y + z), (x, 0, 1), (y, 0, 1), (z, 0, 1)) in [
  1012. -12*log(3) - 3*log(6)/2 + 3*log(8)/2 + 5*log(2) + 7*log(4),
  1013. 6*log(2) + 8*log(4) - 27*log(3)/2, 22*log(2) - 27*log(3)/2,
  1014. -12*log(3) - 3*log(6)/2 + 47*log(2)/2]
  1015. def test_issue_4326():
  1016. R, b, h = symbols('R b h')
  1017. # It doesn't matter if we can do the integral. Just make sure the result
  1018. # doesn't contain nan. This is really a test against _eval_interval.
  1019. e = integrate(((h*(x - R + b))/b)*sqrt(R**2 - x**2), (x, R - b, R))
  1020. assert not e.has(nan)
  1021. # See that it evaluates
  1022. assert not e.has(Integral)
  1023. def test_powers():
  1024. assert integrate(2**x + 3**x, x) == 2**x/log(2) + 3**x/log(3)
  1025. def test_manual_option():
  1026. raises(ValueError, lambda: integrate(1/x, x, manual=True, meijerg=True))
  1027. # an example of a function that manual integration cannot handle
  1028. assert integrate(log(1+x)/x, (x, 0, 1), manual=True).has(Integral)
  1029. def test_meijerg_option():
  1030. raises(ValueError, lambda: integrate(1/x, x, meijerg=True, risch=True))
  1031. # an example of a function that meijerg integration cannot handle
  1032. assert integrate(tan(x), x, meijerg=True) == Integral(tan(x), x)
  1033. def test_risch_option():
  1034. # risch=True only allowed on indefinite integrals
  1035. raises(ValueError, lambda: integrate(1/log(x), (x, 0, oo), risch=True))
  1036. assert integrate(exp(-x**2), x, risch=True) == NonElementaryIntegral(exp(-x**2), x)
  1037. assert integrate(log(1/x)*y, x, y, risch=True) == y**2*(x*log(1/x)/2 + x/2)
  1038. assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
  1039. # TODO: How to test risch=False?
  1040. @slow
  1041. def test_heurisch_option():
  1042. raises(ValueError, lambda: integrate(1/x, x, risch=True, heurisch=True))
  1043. # an integral that heurisch can handle
  1044. assert integrate(exp(x**2), x, heurisch=True) == sqrt(pi)*erfi(x)/2
  1045. # an integral that heurisch currently cannot handle
  1046. assert integrate(exp(x)/x, x, heurisch=True) == Integral(exp(x)/x, x)
  1047. # an integral where heurisch currently hangs, issue 15471
  1048. assert integrate(log(x)*cos(log(x))/x**Rational(3, 4), x, heurisch=False) == (
  1049. -128*x**Rational(1, 4)*sin(log(x))/289 + 240*x**Rational(1, 4)*cos(log(x))/289 +
  1050. (16*x**Rational(1, 4)*sin(log(x))/17 + 4*x**Rational(1, 4)*cos(log(x))/17)*log(x))
  1051. def test_issue_6828():
  1052. f = 1/(1.08*x**2 - 4.3)
  1053. g = integrate(f, x).diff(x)
  1054. assert verify_numerically(f, g, tol=1e-12)
  1055. def test_issue_4803():
  1056. x_max = Symbol("x_max")
  1057. assert integrate(y/pi*exp(-(x_max - x)/cos(a)), x) == \
  1058. y*exp((x - x_max)/cos(a))*cos(a)/pi
  1059. def test_issue_4234():
  1060. assert integrate(1/sqrt(1 + tan(x)**2)) == tan(x)/sqrt(1 + tan(x)**2)
  1061. def test_issue_4492():
  1062. assert simplify(integrate(x**2 * sqrt(5 - x**2), x)).factor(
  1063. deep=True) == Piecewise(
  1064. (I*(2*x**5 - 15*x**3 + 25*x - 25*sqrt(x**2 - 5)*acosh(sqrt(5)*x/5)) /
  1065. (8*sqrt(x**2 - 5)), (x > sqrt(5)) | (x < -sqrt(5))),
  1066. ((2*x**5 - 15*x**3 + 25*x - 25*sqrt(5 - x**2)*asin(sqrt(5)*x/5)) /
  1067. (-8*sqrt(-x**2 + 5)), True))
  1068. def test_issue_2708():
  1069. # This test needs to use an integration function that can
  1070. # not be evaluated in closed form. Update as needed.
  1071. f = 1/(a + z + log(z))
  1072. integral_f = NonElementaryIntegral(f, (z, 2, 3))
  1073. assert Integral(f, (z, 2, 3)).doit() == integral_f
  1074. assert integrate(f + exp(z), (z, 2, 3)) == integral_f - exp(2) + exp(3)
  1075. assert integrate(2*f + exp(z), (z, 2, 3)) == \
  1076. 2*integral_f - exp(2) + exp(3)
  1077. assert integrate(exp(1.2*n*s*z*(-t + z)/t), (z, 0, x)) == \
  1078. NonElementaryIntegral(exp(-1.2*n*s*z)*exp(1.2*n*s*z**2/t),
  1079. (z, 0, x))
  1080. def test_issue_2884():
  1081. f = (4.000002016020*x + 4.000002016020*y + 4.000006024032)*exp(10.0*x)
  1082. e = integrate(f, (x, 0.1, 0.2))
  1083. assert str(e) == '1.86831064982608*y + 2.16387491480008'
  1084. def test_issue_8368i():
  1085. from sympy.functions.elementary.complexes import arg, Abs
  1086. assert integrate(exp(-s*x)*cosh(x), (x, 0, oo)) == \
  1087. Piecewise(
  1088. ( pi*Piecewise(
  1089. ( -s/(pi*(-s**2 + 1)),
  1090. Abs(s**2) < 1),
  1091. ( 1/(pi*s*(1 - 1/s**2)),
  1092. Abs(s**(-2)) < 1),
  1093. ( meijerg(
  1094. ((S.Half,), (0, 0)),
  1095. ((0, S.Half), (0,)),
  1096. polar_lift(s)**2),
  1097. True)
  1098. ),
  1099. s**2 > 1
  1100. ),
  1101. (
  1102. Integral(exp(-s*x)*cosh(x), (x, 0, oo)),
  1103. True))
  1104. assert integrate(exp(-s*x)*sinh(x), (x, 0, oo)) == \
  1105. Piecewise(
  1106. ( -1/(s + 1)/2 - 1/(-s + 1)/2,
  1107. And(
  1108. Abs(s) > 1,
  1109. Abs(arg(s)) < pi/2,
  1110. Abs(arg(s)) <= pi/2
  1111. )),
  1112. ( Integral(exp(-s*x)*sinh(x), (x, 0, oo)),
  1113. True))
  1114. def test_issue_8901():
  1115. assert integrate(sinh(1.0*x)) == 1.0*cosh(1.0*x)
  1116. assert integrate(tanh(1.0*x)) == 1.0*x - 1.0*log(tanh(1.0*x) + 1)
  1117. assert integrate(tanh(x)) == x - log(tanh(x) + 1)
  1118. @slow
  1119. def test_issue_8945():
  1120. assert integrate(sin(x)**3/x, (x, 0, 1)) == -Si(3)/4 + 3*Si(1)/4
  1121. assert integrate(sin(x)**3/x, (x, 0, oo)) == pi/4
  1122. assert integrate(cos(x)**2/x**2, x) == -Si(2*x) - cos(2*x)/(2*x) - 1/(2*x)
  1123. @slow
  1124. def test_issue_7130():
  1125. i, L, a, b = symbols('i L a b')
  1126. integrand = (cos(pi*i*x/L)**2 / (a + b*x)).rewrite(exp)
  1127. assert x not in integrate(integrand, (x, 0, L)).free_symbols
  1128. def test_issue_10567():
  1129. a, b, c, t = symbols('a b c t')
  1130. vt = Matrix([a*t, b, c])
  1131. assert integrate(vt, t) == Integral(vt, t).doit()
  1132. assert integrate(vt, t) == Matrix([[a*t**2/2], [b*t], [c*t]])
  1133. def test_issue_11742():
  1134. assert integrate(sqrt(-x**2 + 8*x + 48), (x, 4, 12)) == 16*pi
  1135. def test_issue_11856():
  1136. t = symbols('t')
  1137. assert integrate(sinc(pi*t), t) == Si(pi*t)/pi
  1138. @slow
  1139. def test_issue_11876():
  1140. assert integrate(sqrt(log(1/x)), (x, 0, 1)) == sqrt(pi)/2
  1141. def test_issue_4950():
  1142. assert integrate((-60*exp(x) - 19.2*exp(4*x))*exp(4*x), x) ==\
  1143. -2.4*exp(8*x) - 12.0*exp(5*x)
  1144. def test_issue_4968():
  1145. assert integrate(sin(log(x**2))) == x*sin(log(x**2))/5 - 2*x*cos(log(x**2))/5
  1146. def test_singularities():
  1147. assert integrate(1/x**2, (x, -oo, oo)) is oo
  1148. assert integrate(1/x**2, (x, -1, 1)) is oo
  1149. assert integrate(1/(x - 1)**2, (x, -2, 2)) is oo
  1150. assert integrate(1/x**2, (x, 1, -1)) is -oo
  1151. assert integrate(1/(x - 1)**2, (x, 2, -2)) is -oo
  1152. def test_issue_12645():
  1153. x, y = symbols('x y', real=True)
  1154. assert (integrate(sin(x*x*x + y*y),
  1155. (x, -sqrt(pi - y*y), sqrt(pi - y*y)),
  1156. (y, -sqrt(pi), sqrt(pi)))
  1157. == Integral(sin(x**3 + y**2),
  1158. (x, -sqrt(-y**2 + pi), sqrt(-y**2 + pi)),
  1159. (y, -sqrt(pi), sqrt(pi))))
  1160. def test_issue_12677():
  1161. assert integrate(sin(x) / (cos(x)**3), (x, 0, pi/6)) == Rational(1, 6)
  1162. def test_issue_14078():
  1163. assert integrate((cos(3*x)-cos(x))/x, (x, 0, oo)) == -log(3)
  1164. def test_issue_14064():
  1165. assert integrate(1/cosh(x), (x, 0, oo)) == pi/2
  1166. def test_issue_14027():
  1167. assert integrate(1/(1 + exp(x - S.Half)/(1 + exp(x))), x) == \
  1168. x - exp(S.Half)*log(exp(x) + exp(S.Half)/(1 + exp(S.Half)))/(exp(S.Half) + E)
  1169. def test_issue_8170():
  1170. assert integrate(tan(x), (x, 0, pi/2)) is S.Infinity
  1171. def test_issue_8440_14040():
  1172. assert integrate(1/x, (x, -1, 1)) is S.NaN
  1173. assert integrate(1/(x + 1), (x, -2, 3)) is S.NaN
  1174. def test_issue_14096():
  1175. assert integrate(1/(x + y)**2, (x, 0, 1)) == -1/(y + 1) + 1/y
  1176. assert integrate(1/(1 + x + y + z)**2, (x, 0, 1), (y, 0, 1), (z, 0, 1)) == \
  1177. -4*log(4) - 6*log(2) + 9*log(3)
  1178. def test_issue_14144():
  1179. assert Abs(integrate(1/sqrt(1 - x**3), (x, 0, 1)).n() - 1.402182) < 1e-6
  1180. assert Abs(integrate(sqrt(1 - x**3), (x, 0, 1)).n() - 0.841309) < 1e-6
  1181. def test_issue_14375():
  1182. # This raised a TypeError. The antiderivative has exp_polar, which
  1183. # may be possible to unpolarify, so the exact output is not asserted here.
  1184. assert integrate(exp(I*x)*log(x), x).has(Ei)
  1185. def test_issue_14437():
  1186. f = Function('f')(x, y, z)
  1187. assert integrate(f, (x, 0, 1), (y, 0, 2), (z, 0, 3)) == \
  1188. Integral(f, (x, 0, 1), (y, 0, 2), (z, 0, 3))
  1189. def test_issue_14470():
  1190. assert integrate(1/sqrt(exp(x) + 1), x) == log(sqrt(exp(x) + 1) - 1) - log(sqrt(exp(x) + 1) + 1)
  1191. def test_issue_14877():
  1192. f = exp(1 - exp(x**2)*x + 2*x**2)*(2*x**3 + x)/(1 - exp(x**2)*x)**2
  1193. assert integrate(f, x) == \
  1194. -exp(2*x**2 - x*exp(x**2) + 1)/(x*exp(3*x**2) - exp(2*x**2))
  1195. def test_issue_14782():
  1196. f = sqrt(-x**2 + 1)*(-x**2 + x)
  1197. assert integrate(f, [x, -1, 1]) == - pi / 8
  1198. @slow
  1199. def test_issue_14782_slow():
  1200. f = sqrt(-x**2 + 1)*(-x**2 + x)
  1201. assert integrate(f, [x, 0, 1]) == S.One / 3 - pi / 16
  1202. def test_issue_12081():
  1203. f = x**(Rational(-3, 2))*exp(-x)
  1204. assert integrate(f, [x, 0, oo]) is oo
  1205. def test_issue_15285():
  1206. y = 1/x - 1
  1207. f = 4*y*exp(-2*y)/x**2
  1208. assert integrate(f, [x, 0, 1]) == 1
  1209. def test_issue_15432():
  1210. assert integrate(x**n * exp(-x) * log(x), (x, 0, oo)).gammasimp() == Piecewise(
  1211. (gamma(n + 1)*polygamma(0, n) + gamma(n + 1)/n, re(n) + 1 > 0),
  1212. (Integral(x**n*exp(-x)*log(x), (x, 0, oo)), True))
  1213. def test_issue_15124():
  1214. omega = IndexedBase('omega')
  1215. m, p = symbols('m p', cls=Idx)
  1216. assert integrate(exp(x*I*(omega[m] + omega[p])), x, conds='none') == \
  1217. -I*exp(I*x*omega[m])*exp(I*x*omega[p])/(omega[m] + omega[p])
  1218. def test_issue_15218():
  1219. with warns_deprecated_sympy():
  1220. Integral(Eq(x, y))
  1221. with warns_deprecated_sympy():
  1222. assert Integral(Eq(x, y), x) == Eq(Integral(x, x), Integral(y, x))
  1223. with warns_deprecated_sympy():
  1224. assert Integral(Eq(x, y), x).doit() == Eq(x**2/2, x*y)
  1225. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  1226. # The warning is made in the ExprWithLimits superclass. The stacklevel
  1227. # is correct for integrate(Eq) but not Eq.integrate
  1228. assert Eq(x, y).integrate(x) == Eq(x**2/2, x*y)
  1229. # These are not deprecated because they are definite integrals
  1230. assert integrate(Eq(x, y), (x, 0, 1)) == Eq(S.Half, y)
  1231. assert Eq(x, y).integrate((x, 0, 1)) == Eq(S.Half, y)
  1232. def test_issue_15292():
  1233. res = integrate(exp(-x**2*cos(2*t)) * cos(x**2*sin(2*t)), (x, 0, oo))
  1234. assert isinstance(res, Piecewise)
  1235. assert gammasimp((res - sqrt(pi)/2 * cos(t)).subs(t, pi/6)) == 0
  1236. def test_issue_4514():
  1237. assert integrate(sin(2*x)/sin(x), x) == 2*sin(x)
  1238. def test_issue_15457():
  1239. x, a, b = symbols('x a b', real=True)
  1240. definite = integrate(exp(Abs(x-2)), (x, a, b))
  1241. indefinite = integrate(exp(Abs(x-2)), x)
  1242. assert definite.subs({a: 1, b: 3}) == -2 + 2*E
  1243. assert indefinite.subs(x, 3) - indefinite.subs(x, 1) == -2 + 2*E
  1244. assert definite.subs({a: -3, b: -1}) == -exp(3) + exp(5)
  1245. assert indefinite.subs(x, -1) - indefinite.subs(x, -3) == -exp(3) + exp(5)
  1246. def test_issue_15431():
  1247. assert integrate(x*exp(x)*log(x), x) == \
  1248. (x*exp(x) - exp(x))*log(x) - exp(x) + Ei(x)
  1249. def test_issue_15640_log_substitutions():
  1250. f = x/log(x)
  1251. F = Ei(2*log(x))
  1252. assert integrate(f, x) == F and F.diff(x) == f
  1253. f = x**3/log(x)**2
  1254. F = -x**4/log(x) + 4*Ei(4*log(x))
  1255. assert integrate(f, x) == F and F.diff(x) == f
  1256. f = sqrt(log(x))/x**2
  1257. F = -sqrt(pi)*erfc(sqrt(log(x)))/2 - sqrt(log(x))/x
  1258. assert integrate(f, x) == F and F.diff(x) == f
  1259. def test_issue_15509():
  1260. from sympy.vector import CoordSys3D
  1261. N = CoordSys3D('N')
  1262. x = N.x
  1263. assert integrate(cos(a*x + b), (x, x_1, x_2), heurisch=True) == Piecewise(
  1264. (-sin(a*x_1 + b)/a + sin(a*x_2 + b)/a, (a > -oo) & (a < oo) & Ne(a, 0)), \
  1265. (-x_1*cos(b) + x_2*cos(b), True))
  1266. def test_issue_4311_fast():
  1267. x = symbols('x', real=True)
  1268. assert integrate(x*abs(9-x**2), x) == Piecewise(
  1269. (x**4/4 - 9*x**2/2, x <= -3),
  1270. (-x**4/4 + 9*x**2/2 - Rational(81, 2), x <= 3),
  1271. (x**4/4 - 9*x**2/2, True))
  1272. def test_integrate_with_complex_constants():
  1273. K = Symbol('K', positive=True)
  1274. x = Symbol('x', real=True)
  1275. m = Symbol('m', real=True)
  1276. t = Symbol('t', real=True)
  1277. assert integrate(exp(-I*K*x**2+m*x), x) == sqrt(pi)*exp(-I*m**2
  1278. /(4*K))*erfi((-2*I*K*x + m)/(2*sqrt(K)*sqrt(-I)))/(2*sqrt(K)*sqrt(-I))
  1279. assert integrate(1/(1 + I*x**2), x) == (-I*(sqrt(-I)*log(x - I*sqrt(-I))/2
  1280. - sqrt(-I)*log(x + I*sqrt(-I))/2))
  1281. assert integrate(exp(-I*x**2), x) == sqrt(pi)*erf(sqrt(I)*x)/(2*sqrt(I))
  1282. assert integrate((1/(exp(I*t)-2)), t) == -t/2 - I*log(exp(I*t) - 2)/2
  1283. assert integrate((1/(exp(I*t)-2)), (t, 0, 2*pi)) == -pi
  1284. def test_issue_14241():
  1285. x = Symbol('x')
  1286. n = Symbol('n', positive=True, integer=True)
  1287. assert integrate(n * x ** (n - 1) / (x + 1), x) == \
  1288. n**2*x**n*lerchphi(x*exp_polar(I*pi), 1, n)*gamma(n)/gamma(n + 1)
  1289. def test_issue_13112():
  1290. assert integrate(sin(t)**2 / (5 - 4*cos(t)), [t, 0, 2*pi]) == pi / 4
  1291. def test_issue_14709b():
  1292. h = Symbol('h', positive=True)
  1293. i = integrate(x*acos(1 - 2*x/h), (x, 0, h))
  1294. assert i == 5*h**2*pi/16
  1295. def test_issue_8614():
  1296. x = Symbol('x')
  1297. t = Symbol('t')
  1298. assert integrate(exp(t)/t, (t, -oo, x)) == Ei(x)
  1299. assert integrate((exp(-x) - exp(-2*x))/x, (x, 0, oo)) == log(2)
  1300. @slow
  1301. def test_issue_15494():
  1302. s = symbols('s', positive=True)
  1303. integrand = (exp(s/2) - 2*exp(1.6*s) + exp(s))*exp(s)
  1304. solution = integrate(integrand, s)
  1305. assert solution != S.NaN
  1306. # Not sure how to test this properly as it is a symbolic expression with floats
  1307. # assert str(solution) == '0.666666666666667*exp(1.5*s) + 0.5*exp(2.0*s) - 0.769230769230769*exp(2.6*s)'
  1308. # Maybe
  1309. assert abs(solution.subs(s, 1) - (-3.67440080236188)) <= 1e-8
  1310. integrand = (exp(s/2) - 2*exp(S(8)/5*s) + exp(s))*exp(s)
  1311. assert integrate(integrand, s) == -10*exp(13*s/5)/13 + 2*exp(3*s/2)/3 + exp(2*s)/2
  1312. def test_li_integral():
  1313. y = Symbol('y')
  1314. assert Integral(li(y*x**2), x).doit() == Piecewise((x*li(x**2*y) - \
  1315. x*Ei(3*log(x**2*y)/2)/sqrt(x**2*y),
  1316. Ne(y, 0)), (0, True))
  1317. def test_issue_17473():
  1318. x = Symbol('x')
  1319. n = Symbol('n')
  1320. h = S.Half
  1321. ans = x**(n + 1)*gamma(h + h/n)*hyper((h + h/n,),
  1322. (3*h, 3*h + h/n), -x**(2*n)/4)/(2*n*gamma(3*h + h/n))
  1323. got = integrate(sin(x**n), x)
  1324. assert got == ans
  1325. _x = Symbol('x', zero=False)
  1326. reps = {x: _x}
  1327. assert integrate(sin(_x**n), _x) == ans.xreplace(reps).expand()
  1328. def test_issue_17671():
  1329. assert integrate(log(log(x)) / x**2, [x, 1, oo]) == -EulerGamma
  1330. assert integrate(log(log(x)) / x**3, [x, 1, oo]) == -log(2)/2 - EulerGamma/2
  1331. assert integrate(log(log(x)) / x**10, [x, 1, oo]) == -log(9)/9 - EulerGamma/9
  1332. def test_issue_2975():
  1333. w = Symbol('w')
  1334. C = Symbol('C')
  1335. y = Symbol('y')
  1336. assert integrate(1/(y**2+C)**(S(3)/2), (y, -w/2, w/2)) == w/(C**(S(3)/2)*sqrt(1 + w**2/(4*C)))
  1337. def test_issue_7827():
  1338. x, n, M = symbols('x n M')
  1339. N = Symbol('N', integer=True)
  1340. assert integrate(summation(x*n, (n, 1, N)), x) == x**2*(N**2/4 + N/4)
  1341. assert integrate(summation(x*sin(n), (n,1,N)), x) == \
  1342. Sum(x**2*sin(n)/2, (n, 1, N))
  1343. assert integrate(summation(sin(n*x), (n,1,N)), x) == \
  1344. Sum(Piecewise((-cos(n*x)/n, Ne(n, 0)), (0, True)), (n, 1, N))
  1345. assert integrate(integrate(summation(sin(n*x), (n,1,N)), x), x) == \
  1346. Piecewise((Sum(Piecewise((-sin(n*x)/n**2, Ne(n, 0)), (-x/n, True)),
  1347. (n, 1, N)), (n > -oo) & (n < oo) & Ne(n, 0)), (0, True))
  1348. assert integrate(Sum(x, (n, 1, M)), x) == M*x**2/2
  1349. raises(ValueError, lambda: integrate(Sum(x, (x, y, n)), y))
  1350. raises(ValueError, lambda: integrate(Sum(x, (x, 1, n)), n))
  1351. raises(ValueError, lambda: integrate(Sum(x, (x, 1, y)), x))
  1352. def test_issue_4231():
  1353. f = (1 + 2*x + sqrt(x + log(x))*(1 + 3*x) + x**2)/(x*(x + sqrt(x + log(x)))*sqrt(x + log(x)))
  1354. assert integrate(f, x) == 2*sqrt(x + log(x)) + 2*log(x + sqrt(x + log(x)))
  1355. def test_issue_17841():
  1356. f = diff(1/(x**2+x+I), x)
  1357. assert integrate(f, x) == 1/(x**2 + x + I)
  1358. def test_issue_21034():
  1359. x = Symbol('x', real=True, nonzero=True)
  1360. f1 = x*(-x**4/asin(5)**4 - x*sinh(x + log(asin(5))) + 5)
  1361. f2 = (x + cosh(cos(4)))/(x*(x + 1/(12*x)))
  1362. assert integrate(f1, x) == \
  1363. -x**6/(6*asin(5)**4) - x**2*cosh(x + log(asin(5))) + 5*x**2/2 + 2*x*sinh(x + log(asin(5))) - 2*cosh(x + log(asin(5)))
  1364. assert integrate(f2, x) == \
  1365. log(x**2 + S(1)/12)/2 + 2*sqrt(3)*cosh(cos(4))*atan(2*sqrt(3)*x)
  1366. def test_issue_4187():
  1367. assert integrate(log(x)*exp(-x), x) == Ei(-x) - exp(-x)*log(x)
  1368. assert integrate(log(x)*exp(-x), (x, 0, oo)) == -EulerGamma
  1369. def test_issue_5547():
  1370. L = Symbol('L')
  1371. z = Symbol('z')
  1372. r0 = Symbol('r0')
  1373. R0 = Symbol('R0')
  1374. assert integrate(r0**2*cos(z)**2, (z, -L/2, L/2)) == -r0**2*(-L/4 -
  1375. sin(L/2)*cos(L/2)/2) + r0**2*(L/4 + sin(L/2)*cos(L/2)/2)
  1376. assert integrate(r0**2*cos(R0*z)**2, (z, -L/2, L/2)) == Piecewise(
  1377. (-r0**2*(-L*R0/4 - sin(L*R0/2)*cos(L*R0/2)/2)/R0 +
  1378. r0**2*(L*R0/4 + sin(L*R0/2)*cos(L*R0/2)/2)/R0, (R0 > -oo) & (R0 < oo) & Ne(R0, 0)),
  1379. (L*r0**2, True))
  1380. w = 2*pi*z/L
  1381. sol = sqrt(2)*sqrt(L)*r0**2*fresnelc(sqrt(2)*sqrt(L))*gamma(S.One/4)/(16*gamma(S(5)/4)) + L*r0**2/2
  1382. assert integrate(r0**2*cos(w*z)**2, (z, -L/2, L/2)) == sol
  1383. def test_issue_15810():
  1384. assert integrate(1/(2**(2*x/3) + 1), (x, 0, oo)) == Rational(3, 2)
  1385. def test_issue_21024():
  1386. x = Symbol('x', real=True, nonzero=True)
  1387. f = log(x)*log(4*x) + log(3*x + exp(2))
  1388. F = x*log(x)**2 + x*log(3*x + exp(2)) + x*(1 - 2*log(2)) + \
  1389. (-2*x + 2*x*log(2))*log(x) + exp(2)*log(3*x + exp(2))/3
  1390. assert F == integrate(f, x)
  1391. f = (x + exp(3))/x**2
  1392. F = log(x) - exp(3)/x
  1393. assert F == integrate(f, x)
  1394. f = (x**2 + exp(5))/x
  1395. F = x**2/2 + exp(5)*log(x)
  1396. assert F == integrate(f, x)
  1397. f = x/(2*x + tanh(1))
  1398. F = x/2 - log(2*x + tanh(1))*tanh(1)/4
  1399. assert F == integrate(f, x)
  1400. f = x - sinh(4)/x
  1401. F = x**2/2 - log(x)*sinh(4)
  1402. assert F == integrate(f, x)
  1403. f = log(x + exp(5)/x)
  1404. F = x*log(x + exp(5)/x) - x + 2*exp(Rational(5, 2))*atan(x*exp(Rational(-5, 2)))
  1405. assert F == integrate(f, x)
  1406. f = x**5/(x + E)
  1407. F = x**5/5 - E*x**4/4 + x**3*exp(2)/3 - x**2*exp(3)/2 + x*exp(4) - exp(5)*log(x + E)
  1408. assert F == integrate(f, x)
  1409. f = 4*x/(x + sinh(5))
  1410. F = 4*x - 4*log(x + sinh(5))*sinh(5)
  1411. assert F == integrate(f, x)
  1412. f = x**2/(2*x + sinh(2))
  1413. F = x**2/4 - x*sinh(2)/4 + log(2*x + sinh(2))*sinh(2)**2/8
  1414. assert F == integrate(f, x)
  1415. f = -x**2/(x + E)
  1416. F = -x**2/2 + E*x - exp(2)*log(x + E)
  1417. assert F == integrate(f, x)
  1418. f = (2*x + 3)*exp(5)/x
  1419. F = 2*x*exp(5) + 3*exp(5)*log(x)
  1420. assert F == integrate(f, x)
  1421. f = x + 2 + cosh(3)/x
  1422. F = x**2/2 + 2*x + log(x)*cosh(3)
  1423. assert F == integrate(f, x)
  1424. f = x - tanh(1)/x**3
  1425. F = x**2/2 + tanh(1)/(2*x**2)
  1426. assert F == integrate(f, x)
  1427. f = (3*x - exp(6))/x
  1428. F = 3*x - exp(6)*log(x)
  1429. assert F == integrate(f, x)
  1430. f = x**4/(x + exp(5))**2 + x
  1431. F = x**3/3 + x**2*(Rational(1, 2) - exp(5)) + 3*x*exp(10) - 4*exp(15)*log(x + exp(5)) - exp(20)/(x + exp(5))
  1432. assert F == integrate(f, x)
  1433. f = x*(x + exp(10)/x**2) + x
  1434. F = x**3/3 + x**2/2 + exp(10)*log(x)
  1435. assert F == integrate(f, x)
  1436. f = x + x/(5*x + sinh(3))
  1437. F = x**2/2 + x/5 - log(5*x + sinh(3))*sinh(3)/25
  1438. assert F == integrate(f, x)
  1439. f = (x + exp(3))/(2*x**2 + 2*x)
  1440. F = exp(3)*log(x)/2 - exp(3)*log(x + 1)/2 + log(x + 1)/2
  1441. assert F == integrate(f, x).expand()
  1442. f = log(x + 4*sinh(4))
  1443. F = x*log(x + 4*sinh(4)) - x + 4*log(x + 4*sinh(4))*sinh(4)
  1444. assert F == integrate(f, x)
  1445. f = -x + 20*(exp(-5) - atan(4)/x)**3*sin(4)/x
  1446. F = (-x**2*exp(15)/2 + 20*log(x)*sin(4) - (-180*x**2*exp(5)*sin(4)*atan(4) + 90*x*exp(10)*sin(4)*atan(4)**2 - \
  1447. 20*exp(15)*sin(4)*atan(4)**3)/(3*x**3))*exp(-15)
  1448. assert F == integrate(f, x)
  1449. f = 2*x**2*exp(-4) + 6/x
  1450. F_true = (2*x**3/3 + 6*exp(4)*log(x))*exp(-4)
  1451. assert F_true == integrate(f, x)
  1452. def test_issue_21721():
  1453. a = Symbol('a')
  1454. assert integrate(1/(pi*(1+(x-a)**2)),(x,-oo,oo)).expand() == \
  1455. -Heaviside(im(a) - 1, 0) + Heaviside(im(a) + 1, 0)
  1456. def test_issue_21831():
  1457. theta = symbols('theta')
  1458. assert integrate(cos(3*theta)/(5-4*cos(theta)), (theta, 0, 2*pi)) == pi/12
  1459. integrand = cos(2*theta)/(5 - 4*cos(theta))
  1460. assert integrate(integrand, (theta, 0, 2*pi)) == pi/6
  1461. @slow
  1462. def test_issue_22033_integral():
  1463. assert integrate((x**2 - Rational(1, 4))**2 * sqrt(1 - x**2), (x, -1, 1)) == pi/32
  1464. @slow
  1465. def test_issue_21671():
  1466. assert integrate(1,(z,x**2+y**2,2-x**2-y**2),(y,-sqrt(1-x**2),sqrt(1-x**2)),(x,-1,1)) == pi
  1467. assert integrate(-4*(1 - x**2)**(S(3)/2)/3 + 2*sqrt(1 - x**2)*(2 - 2*x**2), (x, -1, 1)) == pi
  1468. def test_issue_18527():
  1469. # The manual integrator can not currently solve this. Assert that it does
  1470. # not give an incorrect result involving Abs when x has real assumptions.
  1471. xr = symbols('xr', real=True)
  1472. expr = (cos(x)/(4+(sin(x))**2))
  1473. res_real = integrate(expr.subs(x, xr), xr, manual=True).subs(xr, x)
  1474. assert integrate(expr, x, manual=True) == res_real == Integral(expr, x)
  1475. def test_issue_23718():
  1476. f = 1/(b*cos(x) + a*sin(x))
  1477. Fpos = (-log(-a/b + tan(x/2) - sqrt(a**2 + b**2)/b)/sqrt(a**2 + b**2)
  1478. +log(-a/b + tan(x/2) + sqrt(a**2 + b**2)/b)/sqrt(a**2 + b**2))
  1479. F = Piecewise(
  1480. # XXX: The zoo case here is for a=b=0 so it should just be zoo or maybe
  1481. # it doesn't really need to be included at all given that the original
  1482. # integrand is really undefined in that case anyway.
  1483. (zoo*(-log(tan(x/2) - 1) + log(tan(x/2) + 1)), Eq(a, 0) & Eq(b, 0)),
  1484. (log(tan(x/2))/a, Eq(b, 0)),
  1485. (-I/(-I*b*sin(x) + b*cos(x)), Eq(a, -I*b)),
  1486. (I/(I*b*sin(x) + b*cos(x)), Eq(a, I*b)),
  1487. (Fpos, True),
  1488. )
  1489. assert integrate(f, x) == F
  1490. ap, bp = symbols('a, b', positive=True)
  1491. rep = {a: ap, b: bp}
  1492. assert integrate(f.subs(rep), x) == Fpos.subs(rep)
  1493. def test_issue_23566():
  1494. i = integrate(1/sqrt(x**2-1), (x, -2, -1))
  1495. assert i == -log(2 - sqrt(3))
  1496. assert math.isclose(i.n(), 1.31695789692482)
  1497. def test_pr_23583():
  1498. # This result from meijerg is wrong. Check whether new result is correct when this test fail.
  1499. assert integrate(1/sqrt((x - I)**2-1)) == Piecewise((acosh(x - I), Abs((x - I)**2) > 1), (-I*asin(x - I), True))
  1500. def test_issue_7264():
  1501. assert integrate(exp(x)*sqrt(1 + exp(2*x))) == sqrt(exp(2*x) + 1)*exp(x)/2 + asinh(exp(x))/2
  1502. def test_issue_11254a():
  1503. assert integrate(sech(x), (x, 0, 1)) == 2*atan(tanh(S.Half))
  1504. def test_issue_11254b():
  1505. assert integrate(csch(x), x) == log(tanh(x/2))
  1506. assert integrate(csch(x), (x, 0, 1)) == oo
  1507. def test_issue_11254d():
  1508. # (sech(x)**2).rewrite(sinh)
  1509. assert integrate(-1/sinh(x + I*pi/2, evaluate=False)**2, x) == -2/(exp(2*x) + 1)
  1510. assert integrate(cosh(x)**(-2), x) == 2*tanh(x/2)/(tanh(x/2)**2 + 1)
  1511. def test_issue_22863():
  1512. i = integrate((3*x**3-x**2+2*x-4)/sqrt(x**2-3*x+2), (x, 0, 1))
  1513. assert i == -101*sqrt(2)/8 - 135*log(3 - 2*sqrt(2))/16
  1514. assert math.isclose(i.n(), -2.98126694400554)
  1515. def test_issue_9723():
  1516. assert integrate(sqrt(x + sqrt(x))) == \
  1517. 2*sqrt(sqrt(x) + x)*(sqrt(x)/12 + x/3 - S(1)/8) + log(2*sqrt(x) + 2*sqrt(sqrt(x) + x) + 1)/8
  1518. assert integrate(sqrt(2*x+3+sqrt(4*x+5))**3) == \
  1519. sqrt(2*x + sqrt(4*x + 5) + 3) * \
  1520. (9*x/10 + 11*(4*x + 5)**(S(3)/2)/40 + sqrt(4*x + 5)/40 + (4*x + 5)**2/10 + S(11)/10)/2
  1521. def test_issue_23704():
  1522. # XXX: This is testing that an exception is not raised in risch Ideally
  1523. # manualintegrate (manual=True) would be able to compute this but
  1524. # manualintegrate is very slow for this example so we don't test that here.
  1525. assert (integrate(log(x)/x**2/(c*x**2+b*x+a),x, risch=True)
  1526. == NonElementaryIntegral(log(x)/(a*x**2 + b*x**3 + c*x**4), x))
  1527. def test_exp_substitution():
  1528. assert integrate(1/sqrt(1-exp(2*x))) == log(sqrt(1 - exp(2*x)) - 1)/2 - log(sqrt(1 - exp(2*x)) + 1)/2
  1529. def test_hyperbolic():
  1530. assert integrate(coth(x)) == x - log(tanh(x) + 1) + log(tanh(x))
  1531. assert integrate(sech(x)) == 2*atan(tanh(x/2))
  1532. assert integrate(csch(x)) == log(tanh(x/2))
  1533. def test_nested_pow():
  1534. assert integrate(sqrt(x**2)) == x*sqrt(x**2)/2
  1535. assert integrate(sqrt(x**(S(5)/3))) == 6*x*sqrt(x**(S(5)/3))/11
  1536. assert integrate(1/sqrt(x**2)) == x*log(x)/sqrt(x**2)
  1537. assert integrate(x*sqrt(x**(-4))) == x**2*sqrt(x**-4)*log(x)
  1538. def test_sqrt_quadratic():
  1539. assert integrate(1/sqrt(3*x**2+4*x+5)) == sqrt(3)*asinh(3*sqrt(11)*(x + S(2)/3)/11)/3
  1540. assert integrate(1/sqrt(-3*x**2+4*x+5)) == sqrt(3)*asin(3*sqrt(19)*(x - S(2)/3)/19)/3
  1541. assert integrate(1/sqrt(3*x**2+4*x-5)) == sqrt(3)*log(6*x + 2*sqrt(3)*sqrt(3*x**2 + 4*x - 5) + 4)/3
  1542. assert integrate(1/sqrt(4*x**2-4*x+1)) == (x - S.Half)*log(x - S.Half)/(2*sqrt((x - S.Half)**2))
  1543. assert integrate(1/sqrt(a+b*x+c*x**2), x) == \
  1544. Piecewise((log(b + 2*sqrt(c)*sqrt(a + b*x + c*x**2) + 2*c*x)/sqrt(c), Ne(c, 0) & Ne(a - b**2/(4*c), 0)),
  1545. ((b/(2*c) + x)*log(b/(2*c) + x)/sqrt(c*(b/(2*c) + x)**2), Ne(c, 0)),
  1546. (2*sqrt(a + b*x)/b, Ne(b, 0)), (x/sqrt(a), True))
  1547. assert integrate((7*x+6)/sqrt(3*x**2+4*x+5)) == \
  1548. 7*sqrt(3*x**2 + 4*x + 5)/3 + 4*sqrt(3)*asinh(3*sqrt(11)*(x + S(2)/3)/11)/9
  1549. assert integrate((7*x+6)/sqrt(-3*x**2+4*x+5)) == \
  1550. -7*sqrt(-3*x**2 + 4*x + 5)/3 + 32*sqrt(3)*asin(3*sqrt(19)*(x - S(2)/3)/19)/9
  1551. assert integrate((7*x+6)/sqrt(3*x**2+4*x-5)) == \
  1552. 7*sqrt(3*x**2 + 4*x - 5)/3 + 4*sqrt(3)*log(6*x + 2*sqrt(3)*sqrt(3*x**2 + 4*x - 5) + 4)/9
  1553. assert integrate((d+e*x)/sqrt(a+b*x+c*x**2), x) == \
  1554. Piecewise(((-b*e/(2*c) + d) *
  1555. Piecewise((log(b + 2*sqrt(c)*sqrt(a + b*x + c*x**2) + 2*c*x)/sqrt(c), Ne(a - b**2/(4*c), 0)),
  1556. ((b/(2*c) + x)*log(b/(2*c) + x)/sqrt(c*(b/(2*c) + x)**2), True)) +
  1557. e*sqrt(a + b*x + c*x**2)/c, Ne(c, 0)),
  1558. ((2*d*sqrt(a + b*x) + 2*e*(-a*sqrt(a + b*x) + (a + b*x)**(S(3)/2)/3)/b)/b, Ne(b, 0)),
  1559. ((d*x + e*x**2/2)/sqrt(a), True))
  1560. assert integrate((3*x**3-x**2+2*x-4)/sqrt(x**2-3*x+2)) == \
  1561. sqrt(x**2 - 3*x + 2)*(x**2 + 13*x/4 + S(101)/8) + 135*log(2*x + 2*sqrt(x**2 - 3*x + 2) - 3)/16
  1562. assert integrate(sqrt(53225*x**2-66732*x+23013)) == \
  1563. (x/2 - S(16683)/53225)*sqrt(53225*x**2 - 66732*x + 23013) + \
  1564. 111576969*sqrt(2129)*asinh(53225*x/10563 - S(11122)/3521)/1133160250
  1565. assert integrate(sqrt(a+b*x+c*x**2), x) == \
  1566. Piecewise(((a/2 - b**2/(8*c)) *
  1567. Piecewise((log(b + 2*sqrt(c)*sqrt(a + b*x + c*x**2) + 2*c*x)/sqrt(c), Ne(a - b**2/(4*c), 0)),
  1568. ((b/(2*c) + x)*log(b/(2*c) + x)/sqrt(c*(b/(2*c) + x)**2), True)) +
  1569. (b/(4*c) + x/2)*sqrt(a + b*x + c*x**2), Ne(c, 0)),
  1570. (2*(a + b*x)**(S(3)/2)/(3*b), Ne(b, 0)),
  1571. (sqrt(a)*x, True))
  1572. assert integrate(x*sqrt(x**2+2*x+4)) == \
  1573. (x**2/3 + x/6 + S(5)/6)*sqrt(x**2 + 2*x + 4) - 3*asinh(sqrt(3)*(x + 1)/3)/2
  1574. def test_mul_pow_derivative():
  1575. assert integrate(x*sec(x)*tan(x)) == x*sec(x) - log(tan(x) + sec(x))
  1576. assert integrate(x*sec(x)**2, x) == x*tan(x) + log(cos(x))
  1577. assert integrate(x**3*Derivative(f(x), (x, 4))) == \
  1578. x**3*Derivative(f(x), (x, 3)) - 3*x**2*Derivative(f(x), (x, 2)) + 6*x*Derivative(f(x), x) - 6*f(x)
  1579. def test_issue_20782():
  1580. fun1 = Piecewise((0, x < 0.0), (1, True))
  1581. fun2 = -Piecewise((0, x < 1.0), (1, True))
  1582. fun_sum = fun1 + fun2
  1583. L = (x, -float('Inf'), 1)
  1584. assert integrate(fun1, L) == 1
  1585. assert integrate(fun2, L) == 0
  1586. assert integrate(-fun1, L) == -1
  1587. assert integrate(-fun2, L) == 0
  1588. assert integrate(fun_sum, L) == 1.
  1589. assert integrate(-fun_sum, L) == -1.
  1590. def test_issue_20781():
  1591. P = lambda a: Piecewise((0, x < a), (1, x >= a))
  1592. f = lambda a: P(int(a)) + P(float(a))
  1593. L = (x, -float('Inf'), x)
  1594. f1 = integrate(f(1), L)
  1595. assert f1 == 2*x - Min(1.0, x) - Min(x, Max(1.0, 1, evaluate=False))
  1596. # XXX is_zero is True for S(0) and Float(0) and this is baked into
  1597. # the code more deeply than the issue of Float(0) != S(0)
  1598. assert integrate(f(0), (x, -float('Inf'), x)
  1599. ) == 2*x - 2*Min(0, x)
  1600. @slow
  1601. def test_issue_19427():
  1602. # <https://github.com/sympy/sympy/issues/19427>
  1603. x = Symbol("x")
  1604. # Have always been okay:
  1605. assert integrate((x ** 4) * sqrt(1 - x ** 2), (x, -1, 1)) == pi / 16
  1606. assert integrate((-2 * x ** 2) * sqrt(1 - x ** 2), (x, -1, 1)) == -pi / 4
  1607. assert integrate((1) * sqrt(1 - x ** 2), (x, -1, 1)) == pi / 2
  1608. # Sum of the above, used to incorrectly return 0 for a while:
  1609. assert integrate((x ** 4 - 2 * x ** 2 + 1) * sqrt(1 - x ** 2), (x, -1, 1)) == 5 * pi / 16
  1610. def test_issue_23942():
  1611. I1 = Integral(1/sqrt(a*(1 + x)**3 + (1 + x)**2), (x, 0, z))
  1612. assert I1.series(a, 1, n=1) == Integral(1/sqrt(x**3 + 4*x**2 + 5*x + 2), (x, 0, z)) + O(a - 1, (a, 1))
  1613. I2 = Integral(1/sqrt(a*(4 - x)**4 + (5 + x)**2), (x, 0, z))
  1614. assert I2.series(a, 2, n=1) == Integral(1/sqrt(2*x**4 - 32*x**3 + 193*x**2 - 502*x + 537), (x, 0, z)) + O(a - 2, (a, 2))
  1615. def test_issue_25886():
  1616. # https://github.com/sympy/sympy/issues/25886
  1617. f = (1-x)*exp(0.937098661j*x)
  1618. F_exp = (1.0*(-1.0671234968289*I*y
  1619. + 1.13875255748434
  1620. + 1.0671234968289*I)*exp(0.937098661*I*y)
  1621. - 1.13875255748434*exp(0.937098661*I))
  1622. F = integrate(f, (x, y, 1.0))
  1623. assert F.is_same(F_exp, math.isclose)
  1624. def test_old_issues():
  1625. # https://github.com/sympy/sympy/issues/5212
  1626. I1 = integrate(cos(log(x**2))/x)
  1627. assert I1 == sin(log(x**2))/2
  1628. # https://github.com/sympy/sympy/issues/5462
  1629. I2 = integrate(1/(x**2+y**2)**(Rational(3,2)),x)
  1630. assert I2 == x/(y**3*sqrt(x**2/y**2 + 1))
  1631. # https://github.com/sympy/sympy/issues/6278
  1632. I3 = integrate(1/(cos(x)+2),(x,0,2*pi))
  1633. assert I3 == 2*sqrt(3)*pi/3
  1634. def test_integral_issue_26566():
  1635. # Define the symbols
  1636. x = symbols('x', real=True)
  1637. a = symbols('a', real=True, positive=True)
  1638. # Define the integral expression
  1639. integral_expr = sin(a * (x + pi))**2
  1640. symbolic_result = integrate(integral_expr, (x, -pi, -pi/2))
  1641. # Known correct result
  1642. correct_result = pi / 4
  1643. # Substitute a specific value for 'a' to evaluate both results
  1644. a_value = 1
  1645. numeric_symbolic_result = symbolic_result.subs(a, a_value).evalf()
  1646. numeric_correct_result = correct_result.evalf()
  1647. # Assert that the symbolic result matches the correct value
  1648. assert simplify(numeric_symbolic_result - numeric_correct_result) == 0
  1649. def test_definite_integral_with_floats_issue_27231():
  1650. # Define the symbol and the integral expression
  1651. x = symbols('x', real=True)
  1652. integral_expr = sqrt(1 - 0.5625 * (x + 0.333333333333333) ** 2)
  1653. # Perform the definite integral with the known limits
  1654. result_symbolic = integrate(integral_expr, (x, -1, 1))
  1655. result_numeric = result_symbolic.evalf()
  1656. # Expected result with higher precision
  1657. expected_result = sqrt(3) / 6 + 4 * pi / 9
  1658. # Verify that the result is approximately equal within a larger tolerance
  1659. assert abs(result_numeric - expected_result.evalf()) < 1e-8
  1660. def test_issue_27374():
  1661. #https://github.com/sympy/sympy/issues/27374
  1662. r = sqrt(x**2 + z**2)
  1663. u = erf(a*r/sqrt(2))/r
  1664. Ec = diff(u, z, z).subs([(x, sqrt(b*b-z*z))])
  1665. expected_result = -2*sqrt(2)*b*a**3*exp(-b**2*a**2/2)/(3*sqrt(pi))
  1666. assert simplify(integrate(Ec, (z, -b, b))) == expected_result