test_laplace.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. from sympy.integrals.laplace import (
  2. laplace_transform, inverse_laplace_transform,
  3. LaplaceTransform, InverseLaplaceTransform,
  4. _laplace_deep_collect, laplace_correspondence,
  5. laplace_initial_conds)
  6. from sympy.core.function import Function, expand_mul
  7. from sympy.core import EulerGamma, Subs, Derivative, diff
  8. from sympy.core.exprtools import factor_terms
  9. from sympy.core.numbers import I, oo, pi
  10. from sympy.core.relational import Eq
  11. from sympy.core.singleton import S
  12. from sympy.core.symbol import Symbol, symbols
  13. from sympy.simplify.simplify import simplify
  14. from sympy.functions.elementary.complexes import Abs, re
  15. from sympy.functions.elementary.exponential import exp, log, exp_polar
  16. from sympy.functions.elementary.hyperbolic import cosh, sinh, coth, asinh
  17. from sympy.functions.elementary.miscellaneous import sqrt
  18. from sympy.functions.elementary.piecewise import Piecewise
  19. from sympy.functions.elementary.trigonometric import atan, cos, sin
  20. from sympy.logic.boolalg import And
  21. from sympy.functions.special.gamma_functions import (
  22. lowergamma, gamma, uppergamma)
  23. from sympy.functions.special.delta_functions import DiracDelta, Heaviside
  24. from sympy.functions.special.singularity_functions import SingularityFunction
  25. from sympy.functions.special.zeta_functions import lerchphi
  26. from sympy.functions.special.error_functions import (
  27. fresnelc, fresnels, erf, erfc, Ei, Ci, expint, E1)
  28. from sympy.functions.special.bessel import besseli, besselj, besselk, bessely
  29. from sympy.testing.pytest import slow, warns_deprecated_sympy
  30. from sympy.matrices import Matrix, eye
  31. from sympy.abc import s
  32. @slow
  33. def test_laplace_transform():
  34. LT = laplace_transform
  35. ILT = inverse_laplace_transform
  36. a, b, c = symbols('a, b, c', positive=True)
  37. np = symbols('np', integer=True, positive=True)
  38. t, w, x = symbols('t, w, x')
  39. f = Function('f')
  40. F = Function('F')
  41. g = Function('g')
  42. y = Function('y')
  43. Y = Function('Y')
  44. # Test helper functions
  45. assert (
  46. _laplace_deep_collect(exp((t+a)*(t+b)) +
  47. besselj(2, exp((t+a)*(t+b)-t**2)), t) ==
  48. exp(a*b + t**2 + t*(a + b)) + besselj(2, exp(a*b + t*(a + b))))
  49. L = laplace_transform(diff(y(t), t, 3), t, s, noconds=True)
  50. L = laplace_correspondence(L, {y: Y})
  51. L = laplace_initial_conds(L, t, {y: [2, 4, 8, 16, 32]})
  52. assert L == s**3*Y(s) - 2*s**2 - 4*s - 8
  53. # Test whether `noconds=True` in `doit`:
  54. assert (2*LaplaceTransform(exp(t), t, s) - 1).doit() == -1 + 2/(s - 1)
  55. assert (LT(a*t+t**2+t**(S(5)/2), t, s) ==
  56. (a/s**2 + 2/s**3 + 15*sqrt(pi)/(8*s**(S(7)/2)), 0, True))
  57. assert LT(b/(t+a), t, s) == (-b*exp(-a*s)*Ei(-a*s), 0, True)
  58. assert (LT(1/sqrt(t+a), t, s) ==
  59. (sqrt(pi)*sqrt(1/s)*exp(a*s)*erfc(sqrt(a)*sqrt(s)), 0, True))
  60. assert (LT(sqrt(t)/(t+a), t, s) ==
  61. (-pi*sqrt(a)*exp(a*s)*erfc(sqrt(a)*sqrt(s)) + sqrt(pi)*sqrt(1/s),
  62. 0, True))
  63. assert (LT((t+a)**(-S(3)/2), t, s) ==
  64. (-2*sqrt(pi)*sqrt(s)*exp(a*s)*erfc(sqrt(a)*sqrt(s)) + 2/sqrt(a),
  65. 0, True))
  66. assert (LT(t**(S(1)/2)*(t+a)**(-1), t, s) ==
  67. (-pi*sqrt(a)*exp(a*s)*erfc(sqrt(a)*sqrt(s)) + sqrt(pi)*sqrt(1/s),
  68. 0, True))
  69. assert (LT(1/(a*sqrt(t) + t**(3/2)), t, s) ==
  70. (pi*sqrt(a)*exp(a*s)*erfc(sqrt(a)*sqrt(s)), 0, True))
  71. assert (LT((t+a)**b, t, s) ==
  72. (s**(-b - 1)*exp(-a*s)*uppergamma(b + 1, a*s), 0, True))
  73. assert LT(t**5/(t+a), t, s) == (120*a**5*uppergamma(-5, a*s), 0, True)
  74. assert LT(exp(t), t, s) == (1/(s - 1), 1, True)
  75. assert LT(exp(2*t), t, s) == (1/(s - 2), 2, True)
  76. assert LT(exp(a*t), t, s) == (1/(s - a), a, True)
  77. assert LT(exp(a*(t-b)), t, s) == (exp(-a*b)/(-a + s), a, True)
  78. assert LT(t*exp(-a*(t)), t, s) == ((a + s)**(-2), -a, True)
  79. assert LT(t*exp(-a*(t-b)), t, s) == (exp(a*b)/(a + s)**2, -a, True)
  80. assert LT(b*t*exp(-a*t), t, s) == (b/(a + s)**2, -a, True)
  81. assert LT(exp(-a*exp(-t)), t, s) == (lowergamma(s, a)/a**s, 0, True)
  82. assert LT(exp(-a*exp(t)), t, s) == (a**s*uppergamma(-s, a), 0, True)
  83. assert (LT(t**(S(7)/4)*exp(-8*t)/gamma(S(11)/4), t, s) ==
  84. ((s + 8)**(-S(11)/4), -8, True))
  85. assert (LT(t**(S(3)/2)*exp(-8*t), t, s) ==
  86. (3*sqrt(pi)/(4*(s + 8)**(S(5)/2)), -8, True))
  87. assert LT(t**a*exp(-a*t), t, s) == ((a+s)**(-a-1)*gamma(a+1), -a, True)
  88. assert (LT(b*exp(-a*t**2), t, s) ==
  89. (sqrt(pi)*b*exp(s**2/(4*a))*erfc(s/(2*sqrt(a)))/(2*sqrt(a)),
  90. 0, True))
  91. assert (LT(exp(-2*t**2), t, s) ==
  92. (sqrt(2)*sqrt(pi)*exp(s**2/8)*erfc(sqrt(2)*s/4)/4, 0, True))
  93. assert (LT(b*exp(2*t**2), t, s) ==
  94. (b*LaplaceTransform(exp(2*t**2), t, s), -oo, True))
  95. assert (LT(t*exp(-a*t**2), t, s) ==
  96. (1/(2*a) - s*erfc(s/(2*sqrt(a)))/(4*sqrt(pi)*a**(S(3)/2)),
  97. 0, True))
  98. assert (LT(exp(-a/t), t, s) ==
  99. (2*sqrt(a)*sqrt(1/s)*besselk(1, 2*sqrt(a)*sqrt(s)), 0, True))
  100. assert LT(sqrt(t)*exp(-a/t), t, s, simplify=True) == (
  101. sqrt(pi)*(sqrt(a)*sqrt(s) + 1/S(2))*sqrt(s**(-3)) *
  102. exp(-2*sqrt(a)*sqrt(s)), 0, True)
  103. assert (LT(exp(-a/t)/sqrt(t), t, s) ==
  104. (sqrt(pi)*sqrt(1/s)*exp(-2*sqrt(a)*sqrt(s)), 0, True))
  105. assert (LT(exp(-a/t)/(t*sqrt(t)), t, s) ==
  106. (sqrt(pi)*sqrt(1/a)*exp(-2*sqrt(a)*sqrt(s)), 0, True))
  107. # TODO: rules with sqrt(a*t) and sqrt(a/t) have stopped working after
  108. # changes to as_base_exp
  109. # assert (
  110. # LT(exp(-2*sqrt(a*t)), t, s) ==
  111. # (1/s - sqrt(pi)*sqrt(a) * exp(a/s)*erfc(sqrt(a)*sqrt(1/s)) /
  112. # s**(S(3)/2), 0, True))
  113. # assert LT(exp(-2*sqrt(a*t))/sqrt(t), t, s) == (
  114. # exp(a/s)*erfc(sqrt(a) * sqrt(1/s))*(sqrt(pi)*sqrt(1/s)), 0, True)
  115. assert (LT(t**4*exp(-2/t), t, s) ==
  116. (8*sqrt(2)*(1/s)**(S(5)/2)*besselk(5, 2*sqrt(2)*sqrt(s)),
  117. 0, True))
  118. assert LT(sinh(a*t), t, s) == (a/(-a**2 + s**2), a, True)
  119. assert (LT(b*sinh(a*t)**2, t, s) ==
  120. (2*a**2*b/(-4*a**2*s + s**3), 2*a, True))
  121. assert (LT(b*sinh(a*t)**2, t, s, simplify=True) ==
  122. (2*a**2*b/(s*(-4*a**2 + s**2)), 2*a, True))
  123. # The following line confirms that issue #21202 is solved
  124. assert LT(cosh(2*t), t, s) == (s/(-4 + s**2), 2, True)
  125. assert LT(cosh(a*t), t, s) == (s/(-a**2 + s**2), a, True)
  126. assert (LT(cosh(a*t)**2, t, s, simplify=True) ==
  127. ((2*a**2 - s**2)/(s*(4*a**2 - s**2)), 2*a, True))
  128. assert (LT(sinh(x+3), x, s, simplify=True) ==
  129. ((s*sinh(3) + cosh(3))/(s**2 - 1), 1, True))
  130. L, _, _ = LT(42*sin(w*t+x)**2, t, s)
  131. assert (
  132. L -
  133. 21*(s**2 + s*(-s*cos(2*x) + 2*w*sin(2*x)) +
  134. 4*w**2)/(s*(s**2 + 4*w**2))).simplify() == 0
  135. # The following line replaces the old test test_issue_7173()
  136. assert LT(sinh(a*t)*cosh(a*t), t, s, simplify=True) == (a/(-4*a**2 + s**2),
  137. 2*a, True)
  138. assert LT(sinh(a*t)/t, t, s) == (log((a + s)/(-a + s))/2, a, True)
  139. assert (LT(t**(-S(3)/2)*sinh(a*t), t, s) ==
  140. (-sqrt(pi)*(sqrt(-a + s) - sqrt(a + s)), a, True))
  141. # assert (LT(sinh(2*sqrt(a*t)), t, s) ==
  142. # (sqrt(pi)*sqrt(a)*exp(a/s)/s**(S(3)/2), 0, True))
  143. # assert (LT(sqrt(t)*sinh(2*sqrt(a*t)), t, s, simplify=True) ==
  144. # ((-sqrt(a)*s**(S(5)/2) + sqrt(pi)*s**2*(2*a + s)*exp(a/s) *
  145. # erf(sqrt(a)*sqrt(1/s))/2)/s**(S(9)/2), 0, True))
  146. # assert (LT(sinh(2*sqrt(a*t))/sqrt(t), t, s) ==
  147. # (sqrt(pi)*exp(a/s)*erf(sqrt(a)*sqrt(1/s))/sqrt(s), 0, True))
  148. # assert (LT(sinh(sqrt(a*t))**2/sqrt(t), t, s) ==
  149. # (sqrt(pi)*(exp(a/s) - 1)/(2*sqrt(s)), 0, True))
  150. assert (LT(t**(S(3)/7)*cosh(a*t), t, s) ==
  151. (((a + s)**(-S(10)/7) + (-a+s)**(-S(10)/7))*gamma(S(10)/7)/2,
  152. a, True))
  153. # assert (LT(cosh(2*sqrt(a*t)), t, s) ==
  154. # (sqrt(pi)*sqrt(a)*exp(a/s)*erf(sqrt(a)*sqrt(1/s))/s**(S(3)/2) +
  155. # 1/s, 0, True))
  156. # assert (LT(sqrt(t)*cosh(2*sqrt(a*t)), t, s) ==
  157. # (sqrt(pi)*(a + s/2)*exp(a/s)/s**(S(5)/2), 0, True))
  158. # assert (LT(cosh(2*sqrt(a*t))/sqrt(t), t, s) ==
  159. # (sqrt(pi)*exp(a/s)/sqrt(s), 0, True))
  160. # assert (LT(cosh(sqrt(a*t))**2/sqrt(t), t, s) ==
  161. # (sqrt(pi)*(exp(a/s) + 1)/(2*sqrt(s)), 0, True))
  162. assert LT(log(t), t, s, simplify=True) == (
  163. (-log(s) - EulerGamma)/s, 0, True)
  164. assert (LT(-log(t/a), t, s, simplify=True) ==
  165. ((log(a) + log(s) + EulerGamma)/s, 0, True))
  166. assert LT(log(1+a*t), t, s) == (-exp(s/a)*Ei(-s/a)/s, 0, True)
  167. assert (LT(log(t+a), t, s, simplify=True) ==
  168. ((s*log(a) - exp(s/a)*Ei(-s/a))/s**2, 0, True))
  169. assert (LT(log(t)/sqrt(t), t, s, simplify=True) ==
  170. (sqrt(pi)*(-log(s) - log(4) - EulerGamma)/sqrt(s), 0, True))
  171. assert (LT(t**(S(5)/2)*log(t), t, s, simplify=True) ==
  172. (sqrt(pi)*(-15*log(s) - log(1073741824) - 15*EulerGamma + 46) /
  173. (8*s**(S(7)/2)), 0, True))
  174. assert (LT(t**3*log(t), t, s, noconds=True, simplify=True) -
  175. 6*(-log(s) - S.EulerGamma + S(11)/6)/s**4).simplify() == S.Zero
  176. assert (LT(log(t)**2, t, s, simplify=True) ==
  177. (((log(s) + EulerGamma)**2 + pi**2/6)/s, 0, True))
  178. assert (LT(exp(-a*t)*log(t), t, s, simplify=True) ==
  179. ((-log(a + s) - EulerGamma)/(a + s), -a, True))
  180. assert LT(sin(a*t), t, s) == (a/(a**2 + s**2), 0, True)
  181. assert (LT(Abs(sin(a*t)), t, s) ==
  182. (a*coth(pi*s/(2*a))/(a**2 + s**2), 0, True))
  183. assert LT(sin(a*t)/t, t, s) == (atan(a/s), 0, True)
  184. assert LT(sin(a*t)**2/t, t, s) == (log(4*a**2/s**2 + 1)/4, 0, True)
  185. assert (LT(sin(a*t)**2/t**2, t, s) ==
  186. (a*atan(2*a/s) - s*log(4*a**2/s**2 + 1)/4, 0, True))
  187. # assert (LT(sin(2*sqrt(a*t)), t, s) ==
  188. # (sqrt(pi)*sqrt(a)*exp(-a/s)/s**(S(3)/2), 0, True))
  189. # assert LT(sin(2*sqrt(a*t))/t, t, s) == (pi*erf(sqrt(a)*sqrt(1/s)), 0, True)
  190. assert LT(cos(a*t), t, s) == (s/(a**2 + s**2), 0, True)
  191. assert (LT(cos(a*t)**2, t, s) ==
  192. ((2*a**2 + s**2)/(s*(4*a**2 + s**2)), 0, True))
  193. # assert (LT(sqrt(t)*cos(2*sqrt(a*t)), t, s, simplify=True) ==
  194. # (sqrt(pi)*(-a + s/2)*exp(-a/s)/s**(S(5)/2), 0, True))
  195. # assert (LT(cos(2*sqrt(a*t))/sqrt(t), t, s) ==
  196. # (sqrt(pi)*sqrt(1/s)*exp(-a/s), 0, True))
  197. assert (LT(sin(a*t)*sin(b*t), t, s) ==
  198. (2*a*b*s/((s**2 + (a - b)**2)*(s**2 + (a + b)**2)), 0, True))
  199. assert (LT(cos(a*t)*sin(b*t), t, s) ==
  200. (b*(-a**2 + b**2 + s**2)/((s**2 + (a - b)**2)*(s**2 + (a + b)**2)),
  201. 0, True))
  202. assert (LT(cos(a*t)*cos(b*t), t, s) ==
  203. (s*(a**2 + b**2 + s**2)/((s**2 + (a - b)**2)*(s**2 + (a + b)**2)),
  204. 0, True))
  205. assert (LT(-a*t*cos(a*t) + sin(a*t), t, s, simplify=True) ==
  206. (2*a**3/(a**4 + 2*a**2*s**2 + s**4), 0, True))
  207. assert LT(c*exp(-b*t)*sin(a*t), t, s) == (a *
  208. c/(a**2 + (b + s)**2), -b, True)
  209. assert LT(c*exp(-b*t)*cos(a*t), t, s) == (c*(b + s)/(a**2 + (b + s)**2),
  210. -b, True)
  211. L, plane, cond = LT(cos(x + 3), x, s, simplify=True)
  212. assert plane == 0
  213. assert L - (s*cos(3) - sin(3))/(s**2 + 1) == 0
  214. # Error functions (laplace7.pdf)
  215. assert LT(erf(a*t), t, s) == (exp(s**2/(4*a**2))*erfc(s/(2*a))/s, 0, True)
  216. # assert LT(erf(sqrt(a*t)), t, s) == (sqrt(a)/(s*sqrt(a + s)), 0, True)
  217. # assert (LT(exp(a*t)*erf(sqrt(a*t)), t, s, simplify=True) ==
  218. # (-sqrt(a)/(sqrt(s)*(a - s)), a, True))
  219. # assert (LT(erf(sqrt(a/t)/2), t, s, simplify=True) ==
  220. # (1/s - exp(-sqrt(a)*sqrt(s))/s, 0, True))
  221. # assert (LT(erfc(sqrt(a*t)), t, s, simplify=True) ==
  222. # (-sqrt(a)/(s*sqrt(a + s)) + 1/s, -a, True))
  223. # assert (LT(exp(a*t)*erfc(sqrt(a*t)), t, s) ==
  224. # (1/(sqrt(a)*sqrt(s) + s), 0, True))
  225. # assert LT(erfc(sqrt(a/t)/2), t, s) == (exp(-sqrt(a)*sqrt(s))/s, 0, True)
  226. # Bessel functions (laplace8.pdf)
  227. assert LT(besselj(0, a*t), t, s) == (1/sqrt(a**2 + s**2), 0, True)
  228. assert (LT(besselj(1, a*t), t, s, simplify=True) ==
  229. (a/(a**2 + s**2 + s*sqrt(a**2 + s**2)), 0, True))
  230. assert (LT(besselj(2, a*t), t, s, simplify=True) ==
  231. (a**2/(sqrt(a**2 + s**2)*(s + sqrt(a**2 + s**2))**2), 0, True))
  232. assert (LT(t*besselj(0, a*t), t, s) ==
  233. (s/(a**2 + s**2)**(S(3)/2), 0, True))
  234. assert (LT(t*besselj(1, a*t), t, s) ==
  235. (a/(a**2 + s**2)**(S(3)/2), 0, True))
  236. assert (LT(t**2*besselj(2, a*t), t, s) ==
  237. (3*a**2/(a**2 + s**2)**(S(5)/2), 0, True))
  238. # assert LT(besselj(0, 2*sqrt(a*t)), t, s) == (exp(-a/s)/s, 0, True)
  239. # assert (LT(t**(S(3)/2)*besselj(3, 2*sqrt(a*t)), t, s) ==
  240. # (a**(S(3)/2)*exp(-a/s)/s**4, 0, True))
  241. assert (LT(besselj(0, a*sqrt(t**2+b*t)), t, s, simplify=True) ==
  242. (exp(b*(s - sqrt(a**2 + s**2)))/sqrt(a**2 + s**2), 0, True))
  243. assert LT(besseli(0, a*t), t, s) == (1/sqrt(-a**2 + s**2), a, True)
  244. assert (LT(besseli(1, a*t), t, s, simplify=True) ==
  245. (a/(-a**2 + s**2 + s*sqrt(-a**2 + s**2)), a, True))
  246. assert (LT(besseli(2, a*t), t, s, simplify=True) ==
  247. (a**2/(sqrt(-a**2 + s**2)*(s + sqrt(-a**2 + s**2))**2), a, True))
  248. assert LT(t*besseli(0, a*t), t, s) == (s/(-a**2 + s**2)**(S(3)/2), a, True)
  249. assert LT(t*besseli(1, a*t), t, s) == (a/(-a**2 + s**2)**(S(3)/2), a, True)
  250. assert (LT(t**2*besseli(2, a*t), t, s) ==
  251. (3*a**2/(-a**2 + s**2)**(S(5)/2), a, True))
  252. # assert (LT(t**(S(3)/2)*besseli(3, 2*sqrt(a*t)), t, s) ==
  253. # (a**(S(3)/2)*exp(a/s)/s**4, 0, True))
  254. assert (LT(bessely(0, a*t), t, s) ==
  255. (-2*asinh(s/a)/(pi*sqrt(a**2 + s**2)), 0, True))
  256. assert (LT(besselk(0, a*t), t, s) ==
  257. (log((s + sqrt(-a**2 + s**2))/a)/sqrt(-a**2 + s**2), -a, True))
  258. assert (LT(sin(a*t)**4, t, s, simplify=True) ==
  259. (24*a**4/(s*(64*a**4 + 20*a**2*s**2 + s**4)), 0, True))
  260. # Test general rules and unevaluated forms
  261. # These all also test whether issue #7219 is solved.
  262. assert LT(Heaviside(t-1)*cos(t-1), t, s) == (s*exp(-s)/(s**2 + 1), 0, True)
  263. assert LT(a*f(t), t, w) == (a*LaplaceTransform(f(t), t, w), -oo, True)
  264. assert (LT(a*Heaviside(t+1)*f(t+1), t, s) ==
  265. (a*LaplaceTransform(f(t + 1), t, s), -oo, True))
  266. assert (LT(a*Heaviside(t-1)*f(t-1), t, s) ==
  267. (a*LaplaceTransform(f(t), t, s)*exp(-s), -oo, True))
  268. assert (LT(b*f(t/a), t, s) ==
  269. (a*b*LaplaceTransform(f(t), t, a*s), -oo, True))
  270. assert LT(exp(-f(x)*t), t, s) == (1/(s + f(x)), -re(f(x)), True)
  271. assert (LT(exp(-a*t)*f(t), t, s) ==
  272. (LaplaceTransform(f(t), t, a + s), -oo, True))
  273. # assert (LT(exp(-a*t)*erfc(sqrt(b/t)/2), t, s) ==
  274. # (exp(-sqrt(b)*sqrt(a + s))/(a + s), -a, True))
  275. assert (LT(sinh(a*t)*f(t), t, s) ==
  276. (LaplaceTransform(f(t), t, -a + s)/2 -
  277. LaplaceTransform(f(t), t, a + s)/2, -oo, True))
  278. assert (LT(sinh(a*t)*t, t, s, simplify=True) ==
  279. (2*a*s/(a**4 - 2*a**2*s**2 + s**4), a, True))
  280. assert (LT(cosh(a*t)*f(t), t, s) ==
  281. (LaplaceTransform(f(t), t, -a + s)/2 +
  282. LaplaceTransform(f(t), t, a + s)/2, -oo, True))
  283. assert (LT(cosh(a*t)*t, t, s, simplify=True) ==
  284. (1/(2*(a + s)**2) + 1/(2*(a - s)**2), a, True))
  285. assert (LT(sin(a*t)*f(t), t, s, simplify=True) ==
  286. (I*(-LaplaceTransform(f(t), t, -I*a + s) +
  287. LaplaceTransform(f(t), t, I*a + s))/2, -oo, True))
  288. assert (LT(sin(f(t)), t, s) ==
  289. (LaplaceTransform(sin(f(t)), t, s), -oo, True))
  290. assert (LT(sin(a*t)*t, t, s, simplify=True) ==
  291. (2*a*s/(a**4 + 2*a**2*s**2 + s**4), 0, True))
  292. assert (LT(cos(a*t)*f(t), t, s) ==
  293. (LaplaceTransform(f(t), t, -I*a + s)/2 +
  294. LaplaceTransform(f(t), t, I*a + s)/2, -oo, True))
  295. assert (LT(cos(a*t)*t, t, s, simplify=True) ==
  296. ((-a**2 + s**2)/(a**4 + 2*a**2*s**2 + s**4), 0, True))
  297. L, plane, _ = LT(sin(a*t+b)**2*f(t), t, s)
  298. assert plane == -oo
  299. assert (
  300. -L + (
  301. LaplaceTransform(f(t), t, s)/2 -
  302. LaplaceTransform(f(t), t, -2*I*a + s)*exp(2*I*b)/4 -
  303. LaplaceTransform(f(t), t, 2*I*a + s)*exp(-2*I*b)/4)) == 0
  304. L = LT(sin(a*t+b)**2*f(t), t, s, noconds=True)
  305. assert (
  306. laplace_correspondence(L, {f: F}) ==
  307. F(s)/2 - F(-2*I*a + s)*exp(2*I*b)/4 -
  308. F(2*I*a + s)*exp(-2*I*b)/4)
  309. L, plane, _ = LT(sin(a*t)**3*cosh(b*t), t, s)
  310. assert plane == b
  311. assert (
  312. -L - 3*a/(8*(9*a**2 + b**2 + 2*b*s + s**2)) -
  313. 3*a/(8*(9*a**2 + b**2 - 2*b*s + s**2)) +
  314. 3*a/(8*(a**2 + b**2 + 2*b*s + s**2)) +
  315. 3*a/(8*(a**2 + b**2 - 2*b*s + s**2))).simplify() == 0
  316. assert (LT(t**2*exp(-t**2), t, s) ==
  317. (sqrt(pi)*s**2*exp(s**2/4)*erfc(s/2)/8 - s/4 +
  318. sqrt(pi)*exp(s**2/4)*erfc(s/2)/4, 0, True))
  319. assert (LT((a*t**2 + b*t + c)*f(t), t, s) ==
  320. (a*Derivative(LaplaceTransform(f(t), t, s), (s, 2)) -
  321. b*Derivative(LaplaceTransform(f(t), t, s), s) +
  322. c*LaplaceTransform(f(t), t, s), -oo, True))
  323. assert (LT(t**np*g(t), t, s) ==
  324. ((-1)**np*Derivative(LaplaceTransform(g(t), t, s), (s, np)),
  325. -oo, True))
  326. # The following tests check whether _piecewise_to_heaviside works:
  327. x1 = Piecewise((0, t <= 0), (1, t <= 1), (0, True))
  328. X1 = LT(x1, t, s)[0]
  329. assert X1 == 1/s - exp(-s)/s
  330. y1 = ILT(X1, s, t)
  331. assert y1 == Heaviside(t) - Heaviside(t - 1)
  332. x1 = Piecewise((0, t <= 0), (t, t <= 1), (2-t, t <= 2), (0, True))
  333. X1 = LT(x1, t, s)[0].simplify()
  334. assert X1 == (exp(2*s) - 2*exp(s) + 1)*exp(-2*s)/s**2
  335. y1 = ILT(X1, s, t)
  336. assert (
  337. -y1 + t*Heaviside(t) + (t - 2)*Heaviside(t - 2) -
  338. 2*(t - 1)*Heaviside(t - 1)).simplify() == 0
  339. x1 = Piecewise((exp(t), t <= 0), (1, t <= 1), (exp(-(t)), True))
  340. X1 = LT(x1, t, s)[0]
  341. assert X1 == exp(-1)*exp(-s)/(s + 1) + 1/s - exp(-s)/s
  342. y1 = ILT(X1, s, t)
  343. assert y1 == (
  344. exp(-1)*exp(1 - t)*Heaviside(t - 1) + Heaviside(t) - Heaviside(t - 1))
  345. x1 = Piecewise((0, x <= 0), (1, x <= 1), (0, True))
  346. X1 = LT(x1, t, s)[0]
  347. assert X1 == Piecewise((0, x <= 0), (1, x <= 1), (0, True))/s
  348. x1 = [
  349. a*Piecewise((1, And(t > 1, t <= 3)), (2, True)),
  350. a*Piecewise((1, And(t >= 1, t <= 3)), (2, True)),
  351. a*Piecewise((1, And(t >= 1, t < 3)), (2, True)),
  352. a*Piecewise((1, And(t > 1, t < 3)), (2, True))]
  353. for x2 in x1:
  354. assert LT(x2, t, s)[0].expand() == 2*a/s - a*exp(-s)/s + a*exp(-3*s)/s
  355. assert (
  356. LT(Piecewise((1, Eq(t, 1)), (2, True)), t, s)[0] ==
  357. LaplaceTransform(Piecewise((1, Eq(t, 1)), (2, True)), t, s))
  358. # The following lines test whether _laplace_transform successfully
  359. # removes Heaviside(1) before processing espressions. It fails if
  360. # Heaviside(t) remains because then meijerg functions will appear.
  361. X1 = 1/sqrt(a*s**2-b)
  362. x1 = ILT(X1, s, t)
  363. Y1 = LT(x1, t, s)[0]
  364. Z1 = (Y1**2/X1**2).simplify()
  365. assert Z1 == 1
  366. # The following two lines test whether issues #5813 and #7176 are solved.
  367. assert (LT(diff(f(t), (t, 1)), t, s, noconds=True) ==
  368. s*LaplaceTransform(f(t), t, s) - f(0))
  369. assert (LT(diff(f(t), (t, 3)), t, s, noconds=True) ==
  370. s**3*LaplaceTransform(f(t), t, s) - s**2*f(0) -
  371. s*Subs(Derivative(f(t), t), t, 0) -
  372. Subs(Derivative(f(t), (t, 2)), t, 0))
  373. # Issue #7219
  374. assert (LT(diff(f(x, t, w), t, 2), t, s) ==
  375. (s**2*LaplaceTransform(f(x, t, w), t, s) - s*f(x, 0, w) -
  376. Subs(Derivative(f(x, t, w), t), t, 0), -oo, True))
  377. # Issue #23307
  378. assert (LT(10*diff(f(t), (t, 1)), t, s, noconds=True) ==
  379. 10*s*LaplaceTransform(f(t), t, s) - 10*f(0))
  380. assert (LT(a*f(b*t)+g(c*t), t, s, noconds=True) ==
  381. a*LaplaceTransform(f(t), t, s/b)/b +
  382. LaplaceTransform(g(t), t, s/c)/c)
  383. assert inverse_laplace_transform(
  384. f(w), w, t, plane=0) == InverseLaplaceTransform(f(w), w, t, 0)
  385. assert (LT(f(t)*g(t), t, s, noconds=True) ==
  386. LaplaceTransform(f(t)*g(t), t, s))
  387. # Issue #24294
  388. assert (LT(b*f(a*t), t, s, noconds=True) ==
  389. b*LaplaceTransform(f(t), t, s/a)/a)
  390. assert LT(3*exp(t)*Heaviside(t), t, s) == (3/(s - 1), 1, True)
  391. assert (LT(2*sin(t)*Heaviside(t), t, s, simplify=True) ==
  392. (2/(s**2 + 1), 0, True))
  393. # Issue #25293
  394. assert (
  395. LT((1/(t-1))*sin(4*pi*(t-1))*DiracDelta(t-1) *
  396. (Heaviside(t-1/4) - Heaviside(t-2)), t, s)[0] == 4*pi*exp(-s))
  397. # additional basic tests from wikipedia
  398. assert (LT((t - a)**b*exp(-c*(t - a))*Heaviside(t - a), t, s) ==
  399. ((c + s)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True))
  400. assert (
  401. LT((exp(2*t)-1)*exp(-b-t)*Heaviside(t)/2, t, s, noconds=True,
  402. simplify=True) ==
  403. exp(-b)/(s**2 - 1))
  404. # DiracDelta function: standard cases
  405. assert LT(DiracDelta(t), t, s) == (1, -oo, True)
  406. assert LT(DiracDelta(a*t), t, s) == (1/a, -oo, True)
  407. assert LT(DiracDelta(t/42), t, s) == (42, -oo, True)
  408. assert LT(DiracDelta(t+42), t, s) == (0, -oo, True)
  409. assert (LT(DiracDelta(t)+DiracDelta(t-42), t, s) ==
  410. (1 + exp(-42*s), -oo, True))
  411. assert (LT(DiracDelta(t)-a*exp(-a*t), t, s, simplify=True) ==
  412. (s/(a + s), -a, True))
  413. assert (
  414. LT(exp(-t)*(DiracDelta(t)+DiracDelta(t-42)), t, s, simplify=True) ==
  415. (exp(-42*s - 42) + 1, -oo, True))
  416. assert LT(f(t)*DiracDelta(t-42), t, s) == (f(42)*exp(-42*s), -oo, True)
  417. assert LT(f(t)*DiracDelta(b*t-a), t, s) == (f(a/b)*exp(-a*s/b)/b,
  418. -oo, True)
  419. assert LT(f(t)*DiracDelta(b*t+a), t, s) == (0, -oo, True)
  420. # SingularityFunction
  421. assert LT(SingularityFunction(t, a, -1), t, s)[0] == exp(-a*s)
  422. assert LT(SingularityFunction(t, a, 1), t, s)[0] == exp(-a*s)/s**2
  423. assert LT(SingularityFunction(t, a, x), t, s)[0] == (
  424. LaplaceTransform(SingularityFunction(t, a, x), t, s))
  425. # Collection of cases that cannot be fully evaluated and/or would catch
  426. # some common implementation errors
  427. assert (LT(DiracDelta(t**2), t, s, noconds=True) ==
  428. LaplaceTransform(DiracDelta(t**2), t, s))
  429. assert LT(DiracDelta(t**2 - 1), t, s) == (exp(-s)/2, -oo, True)
  430. assert LT(DiracDelta(t*(1 - t)), t, s) == (1 - exp(-s), -oo, True)
  431. assert (LT((DiracDelta(t) + 1)*(DiracDelta(t - 1) + 1), t, s) ==
  432. (LaplaceTransform(DiracDelta(t)*DiracDelta(t - 1), t, s) +
  433. 1 + exp(-s) + 1/s, 0, True))
  434. assert LT(DiracDelta(2*t-2*exp(a)), t, s) == (exp(-s*exp(a))/2, -oo, True)
  435. assert LT(DiracDelta(-2*t+2*exp(a)), t, s) == (exp(-s*exp(a))/2, -oo, True)
  436. # Heaviside tests
  437. assert LT(Heaviside(t), t, s) == (1/s, 0, True)
  438. assert LT(Heaviside(t - a), t, s) == (exp(-a*s)/s, 0, True)
  439. assert LT(Heaviside(t-1), t, s) == (exp(-s)/s, 0, True)
  440. assert LT(Heaviside(2*t-4), t, s) == (exp(-2*s)/s, 0, True)
  441. assert LT(Heaviside(2*t+4), t, s) == (1/s, 0, True)
  442. assert (LT(Heaviside(-2*t+4), t, s, simplify=True) ==
  443. (1/s - exp(-2*s)/s, 0, True))
  444. assert (LT(g(t)*Heaviside(t - w), t, s) ==
  445. (LaplaceTransform(g(t)*Heaviside(t - w), t, s), -oo, True))
  446. assert (
  447. LT(Heaviside(t-a)*g(t), t, s) ==
  448. (LaplaceTransform(g(a + t), t, s)*exp(-a*s), -oo, True))
  449. assert (
  450. LT(Heaviside(t+a)*g(t), t, s) ==
  451. (LaplaceTransform(g(t), t, s), -oo, True))
  452. assert (
  453. LT(Heaviside(-t+a)*g(t), t, s) ==
  454. (LaplaceTransform(g(t), t, s) -
  455. LaplaceTransform(g(a + t), t, s)*exp(-a*s), -oo, True))
  456. assert (
  457. LT(Heaviside(-t-a)*g(t), t, s) == (0, 0, True))
  458. # Fresnel functions
  459. assert (laplace_transform(fresnels(t), t, s, simplify=True) ==
  460. ((-sin(s**2/(2*pi))*fresnels(s/pi) +
  461. sqrt(2)*sin(s**2/(2*pi) + pi/4)/2 -
  462. cos(s**2/(2*pi))*fresnelc(s/pi))/s, 0, True))
  463. assert (laplace_transform(fresnelc(t), t, s, simplify=True) ==
  464. ((sin(s**2/(2*pi))*fresnelc(s/pi) -
  465. cos(s**2/(2*pi))*fresnels(s/pi) +
  466. sqrt(2)*cos(s**2/(2*pi) + pi/4)/2)/s, 0, True))
  467. # Matrix tests
  468. Mt = Matrix([[exp(t), t*exp(-t)], [t*exp(-t), exp(t)]])
  469. Ms = Matrix([[1/(s - 1), (s + 1)**(-2)],
  470. [(s + 1)**(-2), 1/(s - 1)]])
  471. # The default behaviour for Laplace transform of a Matrix returns a Matrix
  472. # of Tuples and is deprecated:
  473. with warns_deprecated_sympy():
  474. Ms_conds = Matrix(
  475. [[(1/(s - 1), 1, True), ((s + 1)**(-2), -1, True)],
  476. [((s + 1)**(-2), -1, True), (1/(s - 1), 1, True)]])
  477. with warns_deprecated_sympy():
  478. assert LT(Mt, t, s) == Ms_conds
  479. # The new behavior is to return a tuple of a Matrix and the convergence
  480. # conditions for the matrix as a whole:
  481. assert LT(Mt, t, s, legacy_matrix=False) == (Ms, 1, True)
  482. # With noconds=True the transformed matrix is returned without conditions
  483. # either way:
  484. assert LT(Mt, t, s, noconds=True) == Ms
  485. assert LT(Mt, t, s, legacy_matrix=False, noconds=True) == Ms
  486. @slow
  487. def test_inverse_laplace_transform():
  488. s = symbols('s')
  489. k, n, t = symbols('k, n, t', real=True)
  490. a, b, c, d = symbols('a, b, c, d', positive=True)
  491. f = Function('f')
  492. F = Function('F')
  493. def ILT(g):
  494. return inverse_laplace_transform(g, s, t)
  495. def ILTS(g):
  496. return inverse_laplace_transform(g, s, t, simplify=True)
  497. def ILTF(g):
  498. return laplace_correspondence(
  499. inverse_laplace_transform(g, s, t), {f: F})
  500. # Tests for the rules in Bateman54.
  501. # Section 4.1: Some of the Laplace transform rules can also be used well
  502. # in the inverse transform.
  503. assert ILTF(exp(-a*s)*F(s)) == f(-a + t)
  504. assert ILTF(k*F(s-a)) == k*f(t)*exp(-a*t)
  505. assert ILTF(diff(F(s), s, 3)) == -t**3*f(t)
  506. assert ILTF(diff(F(s), s, 4)) == t**4*f(t)
  507. # Section 5.1: Most rules are impractical for a computer algebra system.
  508. # Section 5.2: Rational functions
  509. assert ILT(2) == 2*DiracDelta(t)
  510. assert ILT(1/s) == Heaviside(t)
  511. assert ILT(1/s**2) == t*Heaviside(t)
  512. assert ILT(1/s**5) == t**4*Heaviside(t)/24
  513. assert ILT(1/s**n) == t**(n - 1)*Heaviside(t)/gamma(n)
  514. assert ILT(a/(a + s)) == a*exp(-a*t)*Heaviside(t)
  515. assert ILT(s/(a + s)) == -a*exp(-a*t)*Heaviside(t) + DiracDelta(t)
  516. assert (ILT(b*s/(s+a)**2) ==
  517. b*(-a*t*exp(-a*t)*Heaviside(t) + exp(-a*t)*Heaviside(t)))
  518. assert (ILTS(c/((s+a)*(s+b))) ==
  519. c*(exp(a*t) - exp(b*t))*exp(-t*(a + b))*Heaviside(t)/(a - b))
  520. assert (ILTS(c*s/((s+a)*(s+b))) ==
  521. c*(a*exp(b*t) - b*exp(a*t))*exp(-t*(a + b))*Heaviside(t)/(a - b))
  522. assert ILTS(s/(a + s)**3) == t*(-a*t + 2)*exp(-a*t)*Heaviside(t)/2
  523. assert ILTS(1/(s*(a + s)**3)) == (
  524. -a**2*t**2 - 2*a*t + 2*exp(a*t) - 2)*exp(-a*t)*Heaviside(t)/(2*a**3)
  525. assert ILT(1/(s*(a + s)**n)) == (
  526. Heaviside(t)*lowergamma(n, a*t)/(a**n*gamma(n)))
  527. assert ILT((s-a)**(-b)) == t**(b - 1)*exp(a*t)*Heaviside(t)/gamma(b)
  528. assert ILT((a + s)**(-2)) == t*exp(-a*t)*Heaviside(t)
  529. assert ILT((a + s)**(-5)) == t**4*exp(-a*t)*Heaviside(t)/24
  530. assert ILT(s**2/(s**2 + 1)) == -sin(t)*Heaviside(t) + DiracDelta(t)
  531. assert ILT(1 - 1/(s**2 + 1)) == -sin(t)*Heaviside(t) + DiracDelta(t)
  532. assert ILT(a/(a**2 + s**2)) == sin(a*t)*Heaviside(t)
  533. assert ILT(s/(s**2 + a**2)) == cos(a*t)*Heaviside(t)
  534. assert ILT(b/(b**2 + (a + s)**2)) == exp(-a*t)*sin(b*t)*Heaviside(t)
  535. assert (ILT(b*s/(b**2 + (a + s)**2)) ==
  536. b*(-a*exp(-a*t)*sin(b*t)/b + exp(-a*t)*cos(b*t))*Heaviside(t))
  537. assert ILT(1/(s**2*(s**2 + 1))) == t*Heaviside(t) - sin(t)*Heaviside(t)
  538. assert (ILTS(c*s/(d**2*(s+a)**2+b**2)) ==
  539. c*(-a*d*sin(b*t/d) + b*cos(b*t/d))*exp(-a*t)*Heaviside(t)/(b*d**2))
  540. assert ILTS((b*s**2 + d)/(a**2 + s**2)**2) == (
  541. 2*a**2*b*sin(a*t) + (a**2*b - d)*(a*t*cos(a*t) -
  542. sin(a*t)))*Heaviside(t)/(2*a**3)
  543. assert ILTS(b/(s**2-a**2)) == b*sinh(a*t)*Heaviside(t)/a
  544. assert (ILT(b/(s**2-a**2)) ==
  545. b*(exp(a*t)*Heaviside(t)/(2*a) - exp(-a*t)*Heaviside(t)/(2*a)))
  546. assert ILTS(b*s/(s**2-a**2)) == b*cosh(a*t)*Heaviside(t)
  547. assert (ILT(b/(s*(s+a))) ==
  548. b*(Heaviside(t)/a - exp(-a*t)*Heaviside(t)/a))
  549. # Issue #24424
  550. assert (ILTS((s + 8)/((s + 2)*(s**2 + 2*s + 10))) ==
  551. ((8*sin(3*t) - 9*cos(3*t))*exp(t) + 9)*exp(-2*t)*Heaviside(t)/15)
  552. # Issue #8514; this is not important anymore, since this function
  553. # is not solved by integration anymore
  554. assert (ILT(1/(a*s**2+b*s+c)) ==
  555. 2*exp(-b*t/(2*a))*sin(t*sqrt(4*a*c - b**2)/(2*a)) *
  556. Heaviside(t)/sqrt(4*a*c - b**2))
  557. # Section 5.3: Irrational algebraic functions
  558. assert ( # (1)
  559. ILT(1/sqrt(s)/(b*s-a)) ==
  560. exp(a*t/b)*Heaviside(t)*erf(sqrt(a)*sqrt(t)/sqrt(b))/(sqrt(a)*sqrt(b)))
  561. assert ( # (2)
  562. ILT(1/sqrt(k*s)/(c*s-a)/s) ==
  563. (-2*c*sqrt(t)/(sqrt(pi)*a) +
  564. c**(S(3)/2)*exp(a*t/c)*erf(sqrt(a)*sqrt(t)/sqrt(c))/a**(S(3)/2)) *
  565. Heaviside(t)/(c*sqrt(k)))
  566. assert ( # (4)
  567. ILT(1/(sqrt(c*s)+a)) == (-a*exp(a**2*t/c)*erfc(a*sqrt(t)/sqrt(c))/c +
  568. 1/(sqrt(pi)*sqrt(c)*sqrt(t)))*Heaviside(t))
  569. assert ( # (5)
  570. ILT(a/s/(b*sqrt(s)+a)) ==
  571. (-exp(a**2*t/b**2)*erfc(a*sqrt(t)/b) + 1)*Heaviside(t))
  572. assert ( # (6)
  573. ILT((a-b)*sqrt(s)/(sqrt(s)+sqrt(a))/(s-b)) ==
  574. (sqrt(a)*sqrt(b)*exp(b*t)*erfc(sqrt(b)*sqrt(t)) +
  575. a*exp(a*t)*erfc(sqrt(a)*sqrt(t)) - b*exp(b*t))*Heaviside(t))
  576. assert ( # (7)
  577. ILT(1/sqrt(s)/(sqrt(b*s)+a)) ==
  578. exp(a**2*t/b)*Heaviside(t)*erfc(a*sqrt(t)/sqrt(b))/sqrt(b))
  579. assert ( # (8)
  580. ILT(a**2/(sqrt(s)+a)/s**(S(3)/2)) ==
  581. (2*a*sqrt(t)/sqrt(pi) + exp(a**2*t)*erfc(a*sqrt(t)) - 1) *
  582. Heaviside(t))
  583. assert ( # (9)
  584. ILT((a-b)*sqrt(b)/(s-b)/sqrt(s)/(sqrt(s)+sqrt(a))) ==
  585. (sqrt(a)*exp(b*t)*erf(sqrt(b)*sqrt(t)) +
  586. sqrt(b)*exp(a*t)*erfc(sqrt(a)*sqrt(t)) -
  587. sqrt(b)*exp(b*t))*Heaviside(t))
  588. assert ( # (10)
  589. ILT(1/(sqrt(s)+sqrt(a))**2) ==
  590. (-2*sqrt(a)*sqrt(t)/sqrt(pi) +
  591. (-2*a*t + 1)*(erf(sqrt(a)*sqrt(t)) -
  592. 1)*exp(a*t) + 1)*Heaviside(t))
  593. assert ( # (11)
  594. ILT(1/(sqrt(s)+sqrt(a))**2/s) ==
  595. ((2*t - 1/a)*exp(a*t)*erfc(sqrt(a)*sqrt(t)) + 1/a -
  596. 2*sqrt(t)/(sqrt(pi)*sqrt(a)))*Heaviside(t))
  597. assert ( # (12)
  598. ILT(1/(sqrt(s)+a)**2/sqrt(s)) ==
  599. (-2*a*t*exp(a**2*t)*erfc(a*sqrt(t)) +
  600. 2*sqrt(t)/sqrt(pi))*Heaviside(t))
  601. assert ( # (13)
  602. ILT(1/(sqrt(s)+a)**3) ==
  603. (-a*t*(2*a**2*t + 3)*exp(a**2*t)*erfc(a*sqrt(t)) +
  604. 2*sqrt(t)*(a**2*t + 1)/sqrt(pi))*Heaviside(t))
  605. x = (
  606. - ILT(sqrt(s)/(sqrt(s)+a)**3) +
  607. 2*(sqrt(pi)*a**2*t*(-2*sqrt(pi)*erfc(a*sqrt(t)) +
  608. 2*exp(-a**2*t)/(a*sqrt(t))) *
  609. (-a**4*t**2 - 5*a**2*t/2 - S.Half) * exp(a**2*t)/2 +
  610. sqrt(pi)*a*sqrt(t)*(a**2*t + 1)/2) *
  611. Heaviside(t)/(pi*a**2*t)).simplify()
  612. assert ( # (14)
  613. x == 0)
  614. x = (
  615. - ILT(1/sqrt(s)/(sqrt(s)+a)**3) +
  616. Heaviside(t)*(sqrt(t)*((2*a**2*t + 1) *
  617. (sqrt(pi)*a*sqrt(t)*exp(a**2*t) *
  618. erfc(a*sqrt(t)) - 1) + 1) /
  619. (sqrt(pi)*a))).simplify()
  620. assert ( # (15)
  621. x == 0)
  622. assert ( # (16)
  623. factor_terms(ILT(3/(sqrt(s)+a)**4)) ==
  624. 3*(-2*a**3*t**(S(5)/2)*(2*a**2*t + 5)/(3*sqrt(pi)) +
  625. t*(4*a**4*t**2 + 12*a**2*t + 3)*exp(a**2*t) *
  626. erfc(a*sqrt(t))/3)*Heaviside(t))
  627. assert ( # (17)
  628. ILT((sqrt(s)-a)/(s*(sqrt(s)+a))) ==
  629. (2*exp(a**2*t)*erfc(a*sqrt(t))-1)*Heaviside(t))
  630. assert ( # (18)
  631. ILT((sqrt(s)-a)**2/(s*(sqrt(s)+a)**2)) == (
  632. 1 + 8*a**2*t*exp(a**2*t)*erfc(a*sqrt(t)) -
  633. 8/sqrt(pi)*a*sqrt(t))*Heaviside(t))
  634. assert ( # (19)
  635. ILT((sqrt(s)-a)**3/(s*(sqrt(s)+a)**3)) == Heaviside(t)*(
  636. 2*(8*a**4*t**2+8*a**2*t+1)*exp(a**2*t) *
  637. erfc(a*sqrt(t))-8/sqrt(pi)*a*sqrt(t)*(2*a**2*t+1)-1))
  638. assert ( # (22)
  639. ILT(sqrt(s+a)/(s+b)) == Heaviside(t)*(
  640. exp(-a*t)/sqrt(t)/sqrt(pi) +
  641. sqrt(a-b)*exp(-b*t)*erf(sqrt(a-b)*sqrt(t))))
  642. assert ( # (23)
  643. ILT(1/sqrt(s+b)/(s+a)) == Heaviside(t)*(
  644. 1/sqrt(b-a)*exp(-a*t)*erf(sqrt(b-a)*sqrt(t))))
  645. assert ( # (35)
  646. ILT(1/sqrt(s**2+a**2)) == Heaviside(t)*(
  647. besselj(0, a*t)))
  648. assert ( # (44)
  649. ILT(1/sqrt(s**2-a**2)) == Heaviside(t)*(
  650. besseli(0, a*t)))
  651. # Miscellaneous tests
  652. # Can _inverse_laplace_time_shift deal with positive exponents?
  653. assert (
  654. - ILT((s**2*exp(2*s) + 4*exp(s) - 4)*exp(-2*s)/(s*(s**2 + 1))) +
  655. cos(t)*Heaviside(t) + 4*cos(t - 2)*Heaviside(t - 2) -
  656. 4*cos(t - 1)*Heaviside(t - 1) - 4*Heaviside(t - 2) +
  657. 4*Heaviside(t - 1)).simplify() == 0
  658. @slow
  659. def test_inverse_laplace_transform_old():
  660. from sympy.functions.special.delta_functions import DiracDelta
  661. ILT = inverse_laplace_transform
  662. a, b, c, d = symbols('a b c d', positive=True)
  663. n, r = symbols('n, r', real=True)
  664. t, z = symbols('t z')
  665. f = Function('f')
  666. F = Function('F')
  667. def simp_hyp(expr):
  668. return factor_terms(expand_mul(expr)).rewrite(sin)
  669. L = ILT(F(s), s, t)
  670. assert laplace_correspondence(L, {f: F}) == f(t)
  671. assert ILT(exp(-a*s)/s, s, t) == Heaviside(-a + t)
  672. assert ILT(exp(-a*s)/(b + s), s, t) == exp(-b*(-a + t))*Heaviside(-a + t)
  673. assert (ILT((b + s)/(a**2 + (b + s)**2), s, t) ==
  674. exp(-b*t)*cos(a*t)*Heaviside(t))
  675. assert (ILT(exp(-a*s)/s**b, s, t) ==
  676. (-a + t)**(b - 1)*Heaviside(-a + t)/gamma(b))
  677. assert (ILT(exp(-a*s)/sqrt(s**2 + 1), s, t) ==
  678. Heaviside(-a + t)*besselj(0, a - t))
  679. assert ILT(1/(s*sqrt(s + 1)), s, t) == Heaviside(t)*erf(sqrt(t))
  680. # TODO sinh/cosh shifted come out a mess. also delayed trig is a mess
  681. # TODO should this simplify further?
  682. assert (ILT(exp(-a*s)/s**b, s, t) ==
  683. (t - a)**(b - 1)*Heaviside(t - a)/gamma(b))
  684. assert (ILT(exp(-a*s)/sqrt(1 + s**2), s, t) ==
  685. Heaviside(t - a)*besselj(0, a - t)) # note: besselj(0, x) is even
  686. # XXX ILT turns these branch factor into trig functions ...
  687. assert (
  688. simplify(ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2),
  689. s, t).rewrite(exp)) ==
  690. Heaviside(t)*besseli(b, a*t))
  691. assert (
  692. ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2),
  693. s, t, simplify=True).rewrite(exp) ==
  694. Heaviside(t)*besselj(b, a*t))
  695. assert ILT(1/(s*sqrt(s + 1)), s, t) == Heaviside(t)*erf(sqrt(t))
  696. # TODO can we make erf(t) work?
  697. assert (ILT((s * eye(2) - Matrix([[1, 0], [0, 2]])).inv(), s, t) ==
  698. Matrix([[exp(t)*Heaviside(t), 0], [0, exp(2*t)*Heaviside(t)]]))
  699. # Test time_diff rule
  700. assert (ILT(s**42*f(s), s, t) ==
  701. Derivative(InverseLaplaceTransform(f(s), s, t, None), (t, 42)))
  702. assert ILT(cos(s), s, t) == InverseLaplaceTransform(cos(s), s, t, None)
  703. # Rules for testing different DiracDelta cases
  704. assert (
  705. ILT(1 + 2*s + 3*s**2 + 5*s**3, s, t) == DiracDelta(t) +
  706. 2*DiracDelta(t, 1) + 3*DiracDelta(t, 2) + 5*DiracDelta(t, 3))
  707. assert (ILT(2*exp(3*s) - 5*exp(-7*s), s, t) ==
  708. 2*InverseLaplaceTransform(exp(3*s), s, t, None) -
  709. 5*DiracDelta(t - 7))
  710. a = cos(sin(7)/2)
  711. assert ILT(a*exp(-3*s), s, t) == a*DiracDelta(t - 3)
  712. assert ILT(exp(2*s), s, t) == InverseLaplaceTransform(exp(2*s), s, t, None)
  713. r = Symbol('r', real=True)
  714. assert ILT(exp(r*s), s, t) == InverseLaplaceTransform(exp(r*s), s, t, None)
  715. # Rules for testing whether Heaviside(t) is treated properly in diff rule
  716. assert ILT(s**2/(a**2 + s**2), s, t) == (
  717. -a*sin(a*t)*Heaviside(t) + DiracDelta(t))
  718. assert ILT(s**2*(f(s) + 1/(a**2 + s**2)), s, t) == (
  719. -a*sin(a*t)*Heaviside(t) + DiracDelta(t) +
  720. Derivative(InverseLaplaceTransform(f(s), s, t, None), (t, 2)))
  721. # Rules from the previous test_inverse_laplace_transform_delta_cond():
  722. assert (ILT(exp(r*s), s, t, noconds=False) ==
  723. (InverseLaplaceTransform(exp(r*s), s, t, None), True))
  724. # inversion does not exist: verify it doesn't evaluate to DiracDelta
  725. for z in (Symbol('z', extended_real=False),
  726. Symbol('z', imaginary=True, zero=False)):
  727. f = ILT(exp(z*s), s, t, noconds=False)
  728. f = f[0] if isinstance(f, tuple) else f
  729. assert f.func != DiracDelta
  730. @slow
  731. def test_expint():
  732. x = Symbol('x')
  733. a = Symbol('a')
  734. u = Symbol('u', polar=True)
  735. # TODO LT of Si, Shi, Chi is a mess ...
  736. assert laplace_transform(Ci(x), x, s) == (-log(1 + s**2)/2/s, 0, True)
  737. assert (laplace_transform(expint(a, x), x, s, simplify=True) ==
  738. (lerchphi(s*exp_polar(I*pi), 1, a), 0, re(a) > S.Zero))
  739. assert (laplace_transform(expint(1, x), x, s, simplify=True) ==
  740. (log(s + 1)/s, 0, True))
  741. assert (laplace_transform(expint(2, x), x, s, simplify=True) ==
  742. ((s - log(s + 1))/s**2, 0, True))
  743. assert (inverse_laplace_transform(-log(1 + s**2)/2/s, s, u).expand() ==
  744. Heaviside(u)*Ci(u))
  745. assert (
  746. inverse_laplace_transform(log(s + 1)/s, s, x,
  747. simplify=True).rewrite(expint) ==
  748. Heaviside(x)*E1(x))
  749. assert (
  750. inverse_laplace_transform(
  751. (s - log(s + 1))/s**2, s, x,
  752. simplify=True).rewrite(expint).expand() ==
  753. (expint(2, x)*Heaviside(x)).rewrite(Ei).rewrite(expint).expand())