m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
12 KiB

6 months ago
  1. from sympy.concrete.summations import Sum
  2. from sympy.core.add import Add
  3. from sympy.core.function import (Derivative, Function, diff)
  4. from sympy.core.numbers import (I, Rational, pi)
  5. from sympy.core.relational import Ne
  6. from sympy.core.symbol import (Symbol, symbols)
  7. from sympy.functions.elementary.exponential import (LambertW, exp, log)
  8. from sympy.functions.elementary.hyperbolic import (asinh, cosh, sinh, tanh)
  9. from sympy.functions.elementary.miscellaneous import sqrt
  10. from sympy.functions.elementary.piecewise import Piecewise
  11. from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sin, tan)
  12. from sympy.functions.special.bessel import (besselj, besselk, bessely, jn)
  13. from sympy.functions.special.error_functions import erf
  14. from sympy.integrals.integrals import Integral
  15. from sympy.simplify.ratsimp import ratsimp
  16. from sympy.simplify.simplify import simplify
  17. from sympy.integrals.heurisch import components, heurisch, heurisch_wrapper
  18. from sympy.testing.pytest import XFAIL, skip, slow, ON_TRAVIS
  19. from sympy.integrals.integrals import integrate
  20. x, y, z, nu = symbols('x,y,z,nu')
  21. f = Function('f')
  22. def test_components():
  23. assert components(x*y, x) == {x}
  24. assert components(1/(x + y), x) == {x}
  25. assert components(sin(x), x) == {sin(x), x}
  26. assert components(sin(x)*sqrt(log(x)), x) == \
  27. {log(x), sin(x), sqrt(log(x)), x}
  28. assert components(x*sin(exp(x)*y), x) == \
  29. {sin(y*exp(x)), x, exp(x)}
  30. assert components(x**Rational(17, 54)/sqrt(sin(x)), x) == \
  31. {sin(x), x**Rational(1, 54), sqrt(sin(x)), x}
  32. assert components(f(x), x) == \
  33. {x, f(x)}
  34. assert components(Derivative(f(x), x), x) == \
  35. {x, f(x), Derivative(f(x), x)}
  36. assert components(f(x)*diff(f(x), x), x) == \
  37. {x, f(x), Derivative(f(x), x), Derivative(f(x), x)}
  38. def test_issue_10680():
  39. assert isinstance(integrate(x**log(x**log(x**log(x))),x), Integral)
  40. def test_issue_21166():
  41. assert integrate(sin(x/sqrt(abs(x))), (x, -1, 1)) == 0
  42. def test_heurisch_polynomials():
  43. assert heurisch(1, x) == x
  44. assert heurisch(x, x) == x**2/2
  45. assert heurisch(x**17, x) == x**18/18
  46. # For coverage
  47. assert heurisch_wrapper(y, x) == y*x
  48. def test_heurisch_fractions():
  49. assert heurisch(1/x, x) == log(x)
  50. assert heurisch(1/(2 + x), x) == log(x + 2)
  51. assert heurisch(1/(x + sin(y)), x) == log(x + sin(y))
  52. # Up to a constant, where C = pi*I*Rational(5, 12), Mathematica gives identical
  53. # result in the first case. The difference is because SymPy changes
  54. # signs of expressions without any care.
  55. # XXX ^ ^ ^ is this still correct?
  56. assert heurisch(5*x**5/(
  57. 2*x**6 - 5), x) in [5*log(2*x**6 - 5) / 12, 5*log(-2*x**6 + 5) / 12]
  58. assert heurisch(5*x**5/(2*x**6 + 5), x) == 5*log(2*x**6 + 5) / 12
  59. assert heurisch(1/x**2, x) == -1/x
  60. assert heurisch(-1/x**5, x) == 1/(4*x**4)
  61. def test_heurisch_log():
  62. assert heurisch(log(x), x) == x*log(x) - x
  63. assert heurisch(log(3*x), x) == -x + x*log(3) + x*log(x)
  64. assert heurisch(log(x**2), x) in [x*log(x**2) - 2*x, 2*x*log(x) - 2*x]
  65. def test_heurisch_exp():
  66. assert heurisch(exp(x), x) == exp(x)
  67. assert heurisch(exp(-x), x) == -exp(-x)
  68. assert heurisch(exp(17*x), x) == exp(17*x) / 17
  69. assert heurisch(x*exp(x), x) == x*exp(x) - exp(x)
  70. assert heurisch(x*exp(x**2), x) == exp(x**2) / 2
  71. assert heurisch(exp(-x**2), x) is None
  72. assert heurisch(2**x, x) == 2**x/log(2)
  73. assert heurisch(x*2**x, x) == x*2**x/log(2) - 2**x*log(2)**(-2)
  74. assert heurisch(Integral(x**z*y, (y, 1, 2), (z, 2, 3)).function, x) == (x*x**z*y)/(z+1)
  75. assert heurisch(Sum(x**z, (z, 1, 2)).function, z) == x**z/log(x)
  76. def test_heurisch_trigonometric():
  77. assert heurisch(sin(x), x) == -cos(x)
  78. assert heurisch(pi*sin(x) + 1, x) == x - pi*cos(x)
  79. assert heurisch(cos(x), x) == sin(x)
  80. assert heurisch(tan(x), x) in [
  81. log(1 + tan(x)**2)/2,
  82. log(tan(x) + I) + I*x,
  83. log(tan(x) - I) - I*x,
  84. ]
  85. assert heurisch(sin(x)*sin(y), x) == -cos(x)*sin(y)
  86. assert heurisch(sin(x)*sin(y), y) == -cos(y)*sin(x)
  87. # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test
  88. assert heurisch(sin(x)*cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
  89. assert heurisch(cos(x)/sin(x), x) == log(sin(x))
  90. assert heurisch(x*sin(7*x), x) == sin(7*x) / 49 - x*cos(7*x) / 7
  91. assert heurisch(1/pi/4 * x**2*cos(x), x) == 1/pi/4*(x**2*sin(x) -
  92. 2*sin(x) + 2*x*cos(x))
  93. assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \
  94. + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)
  95. assert heurisch(sin(x)/(cos(x)**2+1), x) == -atan(cos(x)) #fixes issue 13723
  96. assert heurisch(1/(cos(x)+2), x) == 2*sqrt(3)*atan(sqrt(3)*tan(x/2)/3)/3
  97. assert heurisch(2*sin(x)*cos(x)/(sin(x)**4 + 1), x) == atan(sqrt(2)*sin(x)
  98. - 1) - atan(sqrt(2)*sin(x) + 1)
  99. assert heurisch(1/cosh(x), x) == 2*atan(tanh(x/2))
  100. def test_heurisch_hyperbolic():
  101. assert heurisch(sinh(x), x) == cosh(x)
  102. assert heurisch(cosh(x), x) == sinh(x)
  103. assert heurisch(x*sinh(x), x) == x*cosh(x) - sinh(x)
  104. assert heurisch(x*cosh(x), x) == x*sinh(x) - cosh(x)
  105. assert heurisch(
  106. x*asinh(x/2), x) == x**2*asinh(x/2)/2 + asinh(x/2) - x*sqrt(4 + x**2)/4
  107. def test_heurisch_mixed():
  108. assert heurisch(sin(x)*exp(x), x) == exp(x)*sin(x)/2 - exp(x)*cos(x)/2
  109. assert heurisch(sin(x/sqrt(-x)), x) == 2*x*cos(x/sqrt(-x))/sqrt(-x) - 2*sin(x/sqrt(-x))
  110. def test_heurisch_radicals():
  111. assert heurisch(1/sqrt(x), x) == 2*sqrt(x)
  112. assert heurisch(1/sqrt(x)**3, x) == -2/sqrt(x)
  113. assert heurisch(sqrt(x)**3, x) == 2*sqrt(x)**5/5
  114. assert heurisch(sin(x)*sqrt(cos(x)), x) == -2*sqrt(cos(x))**3/3
  115. y = Symbol('y')
  116. assert heurisch(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
  117. 2*sqrt(x)*cos(y*sqrt(x))/y
  118. assert heurisch_wrapper(sin(y*sqrt(x)), x) == Piecewise(
  119. (-2*sqrt(x)*cos(sqrt(x)*y)/y + 2*sin(sqrt(x)*y)/y**2, Ne(y, 0)),
  120. (0, True))
  121. y = Symbol('y', positive=True)
  122. assert heurisch_wrapper(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
  123. 2*sqrt(x)*cos(y*sqrt(x))/y
  124. def test_heurisch_special():
  125. assert heurisch(erf(x), x) == x*erf(x) + exp(-x**2)/sqrt(pi)
  126. assert heurisch(exp(-x**2)*erf(x), x) == sqrt(pi)*erf(x)**2 / 4
  127. def test_heurisch_symbolic_coeffs():
  128. assert heurisch(1/(x + y), x) == log(x + y)
  129. assert heurisch(1/(x + sqrt(2)), x) == log(x + sqrt(2))
  130. assert simplify(diff(heurisch(log(x + y + z), y), y)) == log(x + y + z)
  131. def test_heurisch_symbolic_coeffs_1130():
  132. y = Symbol('y')
  133. assert heurisch_wrapper(1/(x**2 + y), x) == Piecewise(
  134. (log(x - sqrt(-y))/(2*sqrt(-y)) - log(x + sqrt(-y))/(2*sqrt(-y)),
  135. Ne(y, 0)), (-1/x, True))
  136. y = Symbol('y', positive=True)
  137. assert heurisch_wrapper(1/(x**2 + y), x) == (atan(x/sqrt(y))/sqrt(y))
  138. def test_heurisch_hacking():
  139. assert heurisch(sqrt(1 + 7*x**2), x, hints=[]) == \
  140. x*sqrt(1 + 7*x**2)/2 + sqrt(7)*asinh(sqrt(7)*x)/14
  141. assert heurisch(sqrt(1 - 7*x**2), x, hints=[]) == \
  142. x*sqrt(1 - 7*x**2)/2 + sqrt(7)*asin(sqrt(7)*x)/14
  143. assert heurisch(1/sqrt(1 + 7*x**2), x, hints=[]) == \
  144. sqrt(7)*asinh(sqrt(7)*x)/7
  145. assert heurisch(1/sqrt(1 - 7*x**2), x, hints=[]) == \
  146. sqrt(7)*asin(sqrt(7)*x)/7
  147. assert heurisch(exp(-7*x**2), x, hints=[]) == \
  148. sqrt(7*pi)*erf(sqrt(7)*x)/14
  149. assert heurisch(1/sqrt(9 - 4*x**2), x, hints=[]) == \
  150. asin(x*Rational(2, 3))/2
  151. assert heurisch(1/sqrt(9 + 4*x**2), x, hints=[]) == \
  152. asinh(x*Rational(2, 3))/2
  153. def test_heurisch_function():
  154. assert heurisch(f(x), x) is None
  155. @XFAIL
  156. def test_heurisch_function_derivative():
  157. # TODO: it looks like this used to work just by coincindence and
  158. # thanks to sloppy implementation. Investigate why this used to
  159. # work at all and if support for this can be restored.
  160. df = diff(f(x), x)
  161. assert heurisch(f(x)*df, x) == f(x)**2/2
  162. assert heurisch(f(x)**2*df, x) == f(x)**3/3
  163. assert heurisch(df/f(x), x) == log(f(x))
  164. def test_heurisch_wrapper():
  165. f = 1/(y + x)
  166. assert heurisch_wrapper(f, x) == log(x + y)
  167. f = 1/(y - x)
  168. assert heurisch_wrapper(f, x) == -log(x - y)
  169. f = 1/((y - x)*(y + x))
  170. assert heurisch_wrapper(f, x) == Piecewise(
  171. (-log(x - y)/(2*y) + log(x + y)/(2*y), Ne(y, 0)), (1/x, True))
  172. # issue 6926
  173. f = sqrt(x**2/((y - x)*(y + x)))
  174. assert heurisch_wrapper(f, x) == x*sqrt(-x**2/(x**2 - y**2)) \
  175. - y**2*sqrt(-x**2/(x**2 - y**2))/x
  176. def test_issue_3609():
  177. assert heurisch(1/(x * (1 + log(x)**2)), x) == atan(log(x))
  178. ### These are examples from the Poor Man's Integrator
  179. ### http://www-sop.inria.fr/cafe/Manuel.Bronstein/pmint/examples/
  180. def test_pmint_rat():
  181. # TODO: heurisch() is off by a constant: -3/4. Possibly different permutation
  182. # would give the optimal result?
  183. def drop_const(expr, x):
  184. if expr.is_Add:
  185. return Add(*[ arg for arg in expr.args if arg.has(x) ])
  186. else:
  187. return expr
  188. f = (x**7 - 24*x**4 - 4*x**2 + 8*x - 8)/(x**8 + 6*x**6 + 12*x**4 + 8*x**2)
  189. g = (4 + 8*x**2 + 6*x + 3*x**3)/(x**5 + 4*x**3 + 4*x) + log(x)
  190. assert drop_const(ratsimp(heurisch(f, x)), x) == g
  191. def test_pmint_trig():
  192. f = (x - tan(x)) / tan(x)**2 + tan(x)
  193. g = -x**2/2 - x/tan(x) + log(tan(x)**2 + 1)/2
  194. assert heurisch(f, x) == g
  195. @slow # 8 seconds on 3.4 GHz
  196. def test_pmint_logexp():
  197. if ON_TRAVIS:
  198. # See https://github.com/sympy/sympy/pull/12795
  199. skip("Too slow for travis.")
  200. f = (1 + x + x*exp(x))*(x + log(x) + exp(x) - 1)/(x + log(x) + exp(x))**2/x
  201. g = log(x + exp(x) + log(x)) + 1/(x + exp(x) + log(x))
  202. assert ratsimp(heurisch(f, x)) == g
  203. def test_pmint_erf():
  204. f = exp(-x**2)*erf(x)/(erf(x)**3 - erf(x)**2 - erf(x) + 1)
  205. g = sqrt(pi)*log(erf(x) - 1)/8 - sqrt(pi)*log(erf(x) + 1)/8 - sqrt(pi)/(4*erf(x) - 4)
  206. assert ratsimp(heurisch(f, x)) == g
  207. def test_pmint_LambertW():
  208. f = LambertW(x)
  209. g = x*LambertW(x) - x + x/LambertW(x)
  210. assert heurisch(f, x) == g
  211. def test_pmint_besselj():
  212. f = besselj(nu + 1, x)/besselj(nu, x)
  213. g = nu*log(x) - log(besselj(nu, x))
  214. assert heurisch(f, x) == g
  215. f = (nu*besselj(nu, x) - x*besselj(nu + 1, x))/x
  216. g = besselj(nu, x)
  217. assert heurisch(f, x) == g
  218. f = jn(nu + 1, x)/jn(nu, x)
  219. g = nu*log(x) - log(jn(nu, x))
  220. assert heurisch(f, x) == g
  221. @slow
  222. def test_pmint_bessel_products():
  223. # Note: Derivatives of Bessel functions have many forms.
  224. # Recurrence relations are needed for comparisons.
  225. if ON_TRAVIS:
  226. skip("Too slow for travis.")
  227. f = x*besselj(nu, x)*bessely(nu, 2*x)
  228. g = -2*x*besselj(nu, x)*bessely(nu - 1, 2*x)/3 + x*besselj(nu - 1, x)*bessely(nu, 2*x)/3
  229. assert heurisch(f, x) == g
  230. f = x*besselj(nu, x)*besselk(nu, 2*x)
  231. g = -2*x*besselj(nu, x)*besselk(nu - 1, 2*x)/5 - x*besselj(nu - 1, x)*besselk(nu, 2*x)/5
  232. assert heurisch(f, x) == g
  233. @slow # 110 seconds on 3.4 GHz
  234. def test_pmint_WrightOmega():
  235. if ON_TRAVIS:
  236. skip("Too slow for travis.")
  237. def omega(x):
  238. return LambertW(exp(x))
  239. f = (1 + omega(x) * (2 + cos(omega(x)) * (x + omega(x))))/(1 + omega(x))/(x + omega(x))
  240. g = log(x + LambertW(exp(x))) + sin(LambertW(exp(x)))
  241. assert heurisch(f, x) == g
  242. def test_RR():
  243. # Make sure the algorithm does the right thing if the ring is RR. See
  244. # issue 8685.
  245. assert heurisch(sqrt(1 + 0.25*x**2), x, hints=[]) == \
  246. 0.5*x*sqrt(0.25*x**2 + 1) + 1.0*asinh(0.5*x)
  247. # TODO: convert the rest of PMINT tests:
  248. # Airy functions
  249. # f = (x - AiryAi(x)*AiryAi(1, x)) / (x**2 - AiryAi(x)**2)
  250. # g = Rational(1,2)*ln(x + AiryAi(x)) + Rational(1,2)*ln(x - AiryAi(x))
  251. # f = x**2 * AiryAi(x)
  252. # g = -AiryAi(x) + AiryAi(1, x)*x
  253. # Whittaker functions
  254. # f = WhittakerW(mu + 1, nu, x) / (WhittakerW(mu, nu, x) * x)
  255. # g = x/2 - mu*ln(x) - ln(WhittakerW(mu, nu, x))
  256. def test_issue_22527():
  257. t, R = symbols(r't R')
  258. z = Function('z')(t)
  259. def f(x):
  260. return x/sqrt(R**2 - x**2)
  261. Uz = integrate(f(z), z)
  262. Ut = integrate(f(t), t)
  263. assert Ut == Uz.subs(z, t)