test_integrals.py 75 KB

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