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.

3099 lines
92 KiB

6 months ago
  1. """ Tests from Michael Wester's 1999 paper "Review of CAS mathematical
  2. capabilities".
  3. http://www.math.unm.edu/~wester/cas/book/Wester.pdf
  4. See also http://math.unm.edu/~wester/cas_review.html for detailed output of
  5. each tested system.
  6. """
  7. from sympy.assumptions.ask import Q, ask
  8. from sympy.assumptions.refine import refine
  9. from sympy.concrete.products import product
  10. from sympy.core import EulerGamma
  11. from sympy.core.evalf import N
  12. from sympy.core.function import (Derivative, Function, Lambda, Subs,
  13. diff, expand, expand_func)
  14. from sympy.core.mul import Mul
  15. from sympy.core.numbers import (AlgebraicNumber, E, I, Rational, igcd,
  16. nan, oo, pi, zoo)
  17. from sympy.core.relational import Eq, Lt
  18. from sympy.core.singleton import S
  19. from sympy.core.symbol import Dummy, Symbol, symbols
  20. from sympy.functions.combinatorial.factorials import (rf, binomial,
  21. factorial, factorial2)
  22. from sympy.functions.combinatorial.numbers import bernoulli, fibonacci
  23. from sympy.functions.elementary.complexes import (conjugate, im, re,
  24. sign)
  25. from sympy.functions.elementary.exponential import LambertW, exp, log
  26. from sympy.functions.elementary.hyperbolic import (asinh, cosh, sinh,
  27. tanh)
  28. from sympy.functions.elementary.integers import ceiling, floor
  29. from sympy.functions.elementary.miscellaneous import Max, Min, sqrt
  30. from sympy.functions.elementary.piecewise import Piecewise
  31. from sympy.functions.elementary.trigonometric import (acos, acot, asin,
  32. atan, cos, cot, csc, sec, sin, tan)
  33. from sympy.functions.special.bessel import besselj
  34. from sympy.functions.special.delta_functions import DiracDelta
  35. from sympy.functions.special.elliptic_integrals import (elliptic_e,
  36. elliptic_f)
  37. from sympy.functions.special.gamma_functions import gamma, polygamma
  38. from sympy.functions.special.hyper import hyper
  39. from sympy.functions.special.polynomials import (assoc_legendre,
  40. chebyshevt)
  41. from sympy.functions.special.zeta_functions import polylog
  42. from sympy.geometry.util import idiff
  43. from sympy.logic.boolalg import And
  44. from sympy.matrices.dense import hessian, wronskian
  45. from sympy.matrices.expressions.matmul import MatMul
  46. from sympy.ntheory.continued_fraction import (
  47. continued_fraction_convergents as cf_c,
  48. continued_fraction_iterator as cf_i, continued_fraction_periodic as
  49. cf_p, continued_fraction_reduce as cf_r)
  50. from sympy.ntheory.factor_ import factorint, totient
  51. from sympy.ntheory.generate import primerange
  52. from sympy.ntheory.partitions_ import npartitions
  53. from sympy.polys.domains.integerring import ZZ
  54. from sympy.polys.orthopolys import legendre_poly
  55. from sympy.polys.partfrac import apart
  56. from sympy.polys.polytools import Poly, factor, gcd, resultant
  57. from sympy.series.limits import limit
  58. from sympy.series.order import O
  59. from sympy.series.residues import residue
  60. from sympy.series.series import series
  61. from sympy.sets.fancysets import ImageSet
  62. from sympy.sets.sets import FiniteSet, Intersection, Interval, Union
  63. from sympy.simplify.combsimp import combsimp
  64. from sympy.simplify.hyperexpand import hyperexpand
  65. from sympy.simplify.powsimp import powdenest, powsimp
  66. from sympy.simplify.radsimp import radsimp
  67. from sympy.simplify.simplify import logcombine, simplify
  68. from sympy.simplify.sqrtdenest import sqrtdenest
  69. from sympy.simplify.trigsimp import trigsimp
  70. from sympy.solvers.solvers import solve
  71. import mpmath
  72. from sympy.functions.combinatorial.numbers import stirling
  73. from sympy.functions.special.delta_functions import Heaviside
  74. from sympy.functions.special.error_functions import Ci, Si, erf
  75. from sympy.functions.special.zeta_functions import zeta
  76. from sympy.testing.pytest import (XFAIL, slow, SKIP, skip, ON_TRAVIS,
  77. raises)
  78. from sympy.utilities.iterables import partitions
  79. from mpmath import mpi, mpc
  80. from sympy.matrices import Matrix, GramSchmidt, eye
  81. from sympy.matrices.expressions.blockmatrix import BlockMatrix, block_collapse
  82. from sympy.matrices.expressions import MatrixSymbol, ZeroMatrix
  83. from sympy.physics.quantum import Commutator
  84. from sympy.polys.rings import PolyRing
  85. from sympy.polys.fields import FracField
  86. from sympy.polys.solvers import solve_lin_sys
  87. from sympy.concrete import Sum
  88. from sympy.concrete.products import Product
  89. from sympy.integrals import integrate
  90. from sympy.integrals.transforms import laplace_transform,\
  91. inverse_laplace_transform, LaplaceTransform, fourier_transform,\
  92. mellin_transform
  93. from sympy.solvers.recurr import rsolve
  94. from sympy.solvers.solveset import solveset, solveset_real, linsolve
  95. from sympy.solvers.ode import dsolve
  96. from sympy.core.relational import Equality
  97. from itertools import islice, takewhile
  98. from sympy.series.formal import fps
  99. from sympy.series.fourier import fourier_series
  100. from sympy.calculus.util import minimum
  101. EmptySet = S.EmptySet
  102. R = Rational
  103. x, y, z = symbols('x y z')
  104. i, j, k, l, m, n = symbols('i j k l m n', integer=True)
  105. f = Function('f')
  106. g = Function('g')
  107. # A. Boolean Logic and Quantifier Elimination
  108. # Not implemented.
  109. # B. Set Theory
  110. def test_B1():
  111. assert (FiniteSet(i, j, j, k, k, k) | FiniteSet(l, k, j) |
  112. FiniteSet(j, m, j)) == FiniteSet(i, j, k, l, m)
  113. def test_B2():
  114. assert (FiniteSet(i, j, j, k, k, k) & FiniteSet(l, k, j) &
  115. FiniteSet(j, m, j)) == Intersection({j, m}, {i, j, k}, {j, k, l})
  116. # Previous output below. Not sure why that should be the expected output.
  117. # There should probably be a way to rewrite Intersections that way but I
  118. # don't see why an Intersection should evaluate like that:
  119. #
  120. # == Union({j}, Intersection({m}, Union({j, k}, Intersection({i}, {l}))))
  121. def test_B3():
  122. assert (FiniteSet(i, j, k, l, m) - FiniteSet(j) ==
  123. FiniteSet(i, k, l, m))
  124. def test_B4():
  125. assert (FiniteSet(*(FiniteSet(i, j)*FiniteSet(k, l))) ==
  126. FiniteSet((i, k), (i, l), (j, k), (j, l)))
  127. # C. Numbers
  128. def test_C1():
  129. assert (factorial(50) ==
  130. 30414093201713378043612608166064768844377641568960512000000000000)
  131. def test_C2():
  132. assert (factorint(factorial(50)) == {2: 47, 3: 22, 5: 12, 7: 8,
  133. 11: 4, 13: 3, 17: 2, 19: 2, 23: 2, 29: 1, 31: 1, 37: 1,
  134. 41: 1, 43: 1, 47: 1})
  135. def test_C3():
  136. assert (factorial2(10), factorial2(9)) == (3840, 945)
  137. # Base conversions; not really implemented by SymPy
  138. # Whatever. Take credit!
  139. def test_C4():
  140. assert 0xABC == 2748
  141. def test_C5():
  142. assert 123 == int('234', 7)
  143. def test_C6():
  144. assert int('677', 8) == int('1BF', 16) == 447
  145. def test_C7():
  146. assert log(32768, 8) == 5
  147. def test_C8():
  148. # Modular multiplicative inverse. Would be nice if divmod could do this.
  149. assert ZZ.invert(5, 7) == 3
  150. assert ZZ.invert(5, 6) == 5
  151. def test_C9():
  152. assert igcd(igcd(1776, 1554), 5698) == 74
  153. def test_C10():
  154. x = 0
  155. for n in range(2, 11):
  156. x += R(1, n)
  157. assert x == R(4861, 2520)
  158. def test_C11():
  159. assert R(1, 7) == S('0.[142857]')
  160. def test_C12():
  161. assert R(7, 11) * R(22, 7) == 2
  162. def test_C13():
  163. test = R(10, 7) * (1 + R(29, 1000)) ** R(1, 3)
  164. good = 3 ** R(1, 3)
  165. assert test == good
  166. def test_C14():
  167. assert sqrtdenest(sqrt(2*sqrt(3) + 4)) == 1 + sqrt(3)
  168. def test_C15():
  169. test = sqrtdenest(sqrt(14 + 3*sqrt(3 + 2*sqrt(5 - 12*sqrt(3 - 2*sqrt(2))))))
  170. good = sqrt(2) + 3
  171. assert test == good
  172. def test_C16():
  173. test = sqrtdenest(sqrt(10 + 2*sqrt(6) + 2*sqrt(10) + 2*sqrt(15)))
  174. good = sqrt(2) + sqrt(3) + sqrt(5)
  175. assert test == good
  176. def test_C17():
  177. test = radsimp((sqrt(3) + sqrt(2)) / (sqrt(3) - sqrt(2)))
  178. good = 5 + 2*sqrt(6)
  179. assert test == good
  180. def test_C18():
  181. assert simplify((sqrt(-2 + sqrt(-5)) * sqrt(-2 - sqrt(-5))).expand(complex=True)) == 3
  182. @XFAIL
  183. def test_C19():
  184. assert radsimp(simplify((90 + 34*sqrt(7)) ** R(1, 3))) == 3 + sqrt(7)
  185. def test_C20():
  186. inside = (135 + 78*sqrt(3))
  187. test = AlgebraicNumber((inside**R(2, 3) + 3) * sqrt(3) / inside**R(1, 3))
  188. assert simplify(test) == AlgebraicNumber(12)
  189. def test_C21():
  190. assert simplify(AlgebraicNumber((41 + 29*sqrt(2)) ** R(1, 5))) == \
  191. AlgebraicNumber(1 + sqrt(2))
  192. @XFAIL
  193. def test_C22():
  194. test = simplify(((6 - 4*sqrt(2))*log(3 - 2*sqrt(2)) + (3 - 2*sqrt(2))*log(17
  195. - 12*sqrt(2)) + 32 - 24*sqrt(2)) / (48*sqrt(2) - 72))
  196. good = sqrt(2)/3 - log(sqrt(2) - 1)/3
  197. assert test == good
  198. def test_C23():
  199. assert 2 * oo - 3 is oo
  200. @XFAIL
  201. def test_C24():
  202. raise NotImplementedError("2**aleph_null == aleph_1")
  203. # D. Numerical Analysis
  204. def test_D1():
  205. assert 0.0 / sqrt(2) == 0.0
  206. def test_D2():
  207. assert str(exp(-1000000).evalf()) == '3.29683147808856e-434295'
  208. def test_D3():
  209. assert exp(pi*sqrt(163)).evalf(50).num.ae(262537412640768744)
  210. def test_D4():
  211. assert floor(R(-5, 3)) == -2
  212. assert ceiling(R(-5, 3)) == -1
  213. @XFAIL
  214. def test_D5():
  215. raise NotImplementedError("cubic_spline([1, 2, 4, 5], [1, 4, 2, 3], x)(3) == 27/8")
  216. @XFAIL
  217. def test_D6():
  218. raise NotImplementedError("translate sum(a[i]*x**i, (i,1,n)) to FORTRAN")
  219. @XFAIL
  220. def test_D7():
  221. raise NotImplementedError("translate sum(a[i]*x**i, (i,1,n)) to C")
  222. @XFAIL
  223. def test_D8():
  224. # One way is to cheat by converting the sum to a string,
  225. # and replacing the '[' and ']' with ''.
  226. # E.g., horner(S(str(_).replace('[','').replace(']','')))
  227. raise NotImplementedError("apply Horner's rule to sum(a[i]*x**i, (i,1,5))")
  228. @XFAIL
  229. def test_D9():
  230. raise NotImplementedError("translate D8 to FORTRAN")
  231. @XFAIL
  232. def test_D10():
  233. raise NotImplementedError("translate D8 to C")
  234. @XFAIL
  235. def test_D11():
  236. #Is there a way to use count_ops?
  237. raise NotImplementedError("flops(sum(product(f[i][k], (i,1,k)), (k,1,n)))")
  238. @XFAIL
  239. def test_D12():
  240. assert (mpi(-4, 2) * x + mpi(1, 3)) ** 2 == mpi(-8, 16)*x**2 + mpi(-24, 12)*x + mpi(1, 9)
  241. @XFAIL
  242. def test_D13():
  243. raise NotImplementedError("discretize a PDE: diff(f(x,t),t) == diff(diff(f(x,t),x),x)")
  244. # E. Statistics
  245. # See scipy; all of this is numerical.
  246. # F. Combinatorial Theory.
  247. def test_F1():
  248. assert rf(x, 3) == x*(1 + x)*(2 + x)
  249. def test_F2():
  250. assert expand_func(binomial(n, 3)) == n*(n - 1)*(n - 2)/6
  251. @XFAIL
  252. def test_F3():
  253. assert combsimp(2**n * factorial(n) * factorial2(2*n - 1)) == factorial(2*n)
  254. @XFAIL
  255. def test_F4():
  256. assert combsimp(2**n * factorial(n) * product(2*k - 1, (k, 1, n))) == factorial(2*n)
  257. @XFAIL
  258. def test_F5():
  259. assert gamma(n + R(1, 2)) / sqrt(pi) / factorial(n) == factorial(2*n)/2**(2*n)/factorial(n)**2
  260. def test_F6():
  261. partTest = [p.copy() for p in partitions(4)]
  262. partDesired = [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2:1}, {1: 4}]
  263. assert partTest == partDesired
  264. def test_F7():
  265. assert npartitions(4) == 5
  266. def test_F8():
  267. assert stirling(5, 2, signed=True) == -50 # if signed, then kind=1
  268. def test_F9():
  269. assert totient(1776) == 576
  270. # G. Number Theory
  271. def test_G1():
  272. assert list(primerange(999983, 1000004)) == [999983, 1000003]
  273. @XFAIL
  274. def test_G2():
  275. raise NotImplementedError("find the primitive root of 191 == 19")
  276. @XFAIL
  277. def test_G3():
  278. raise NotImplementedError("(a+b)**p mod p == a**p + b**p mod p; p prime")
  279. # ... G14 Modular equations are not implemented.
  280. def test_G15():
  281. assert Rational(sqrt(3).evalf()).limit_denominator(15) == R(26, 15)
  282. assert list(takewhile(lambda x: x.q <= 15, cf_c(cf_i(sqrt(3)))))[-1] == \
  283. R(26, 15)
  284. def test_G16():
  285. assert list(islice(cf_i(pi),10)) == [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
  286. def test_G17():
  287. assert cf_p(0, 1, 23) == [4, [1, 3, 1, 8]]
  288. def test_G18():
  289. assert cf_p(1, 2, 5) == [[1]]
  290. assert cf_r([[1]]).expand() == S.Half + sqrt(5)/2
  291. @XFAIL
  292. def test_G19():
  293. s = symbols('s', integer=True, positive=True)
  294. it = cf_i((exp(1/s) - 1)/(exp(1/s) + 1))
  295. assert list(islice(it, 5)) == [0, 2*s, 6*s, 10*s, 14*s]
  296. def test_G20():
  297. s = symbols('s', integer=True, positive=True)
  298. # Wester erroneously has this as -s + sqrt(s**2 + 1)
  299. assert cf_r([[2*s]]) == s + sqrt(s**2 + 1)
  300. @XFAIL
  301. def test_G20b():
  302. s = symbols('s', integer=True, positive=True)
  303. assert cf_p(s, 1, s**2 + 1) == [[2*s]]
  304. # H. Algebra
  305. def test_H1():
  306. assert simplify(2*2**n) == simplify(2**(n + 1))
  307. assert powdenest(2*2**n) == simplify(2**(n + 1))
  308. def test_H2():
  309. assert powsimp(4 * 2**n) == 2**(n + 2)
  310. def test_H3():
  311. assert (-1)**(n*(n + 1)) == 1
  312. def test_H4():
  313. expr = factor(6*x - 10)
  314. assert type(expr) is Mul
  315. assert expr.args[0] == 2
  316. assert expr.args[1] == 3*x - 5
  317. p1 = 64*x**34 - 21*x**47 - 126*x**8 - 46*x**5 - 16*x**60 - 81
  318. p2 = 72*x**60 - 25*x**25 - 19*x**23 - 22*x**39 - 83*x**52 + 54*x**10 + 81
  319. q = 34*x**19 - 25*x**16 + 70*x**7 + 20*x**3 - 91*x - 86
  320. def test_H5():
  321. assert gcd(p1, p2, x) == 1
  322. def test_H6():
  323. assert gcd(expand(p1 * q), expand(p2 * q)) == q
  324. def test_H7():
  325. p1 = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  326. p2 = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  327. assert gcd(p1, p2, x, y, z) == 1
  328. def test_H8():
  329. p1 = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  330. p2 = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  331. q = 11*x**12*y**7*z**13 - 23*x**2*y**8*z**10 + 47*x**17*y**5*z**8
  332. assert gcd(p1 * q, p2 * q, x, y, z) == q
  333. def test_H9():
  334. p1 = 2*x**(n + 4) - x**(n + 2)
  335. p2 = 4*x**(n + 1) + 3*x**n
  336. assert gcd(p1, p2) == x**n
  337. def test_H10():
  338. p1 = 3*x**4 + 3*x**3 + x**2 - x - 2
  339. p2 = x**3 - 3*x**2 + x + 5
  340. assert resultant(p1, p2, x) == 0
  341. def test_H11():
  342. assert resultant(p1 * q, p2 * q, x) == 0
  343. def test_H12():
  344. num = x**2 - 4
  345. den = x**2 + 4*x + 4
  346. assert simplify(num/den) == (x - 2)/(x + 2)
  347. @XFAIL
  348. def test_H13():
  349. assert simplify((exp(x) - 1) / (exp(x/2) + 1)) == exp(x/2) - 1
  350. def test_H14():
  351. p = (x + 1) ** 20
  352. ep = expand(p)
  353. assert ep == (1 + 20*x + 190*x**2 + 1140*x**3 + 4845*x**4 + 15504*x**5
  354. + 38760*x**6 + 77520*x**7 + 125970*x**8 + 167960*x**9 + 184756*x**10
  355. + 167960*x**11 + 125970*x**12 + 77520*x**13 + 38760*x**14 + 15504*x**15
  356. + 4845*x**16 + 1140*x**17 + 190*x**18 + 20*x**19 + x**20)
  357. dep = diff(ep, x)
  358. assert dep == (20 + 380*x + 3420*x**2 + 19380*x**3 + 77520*x**4
  359. + 232560*x**5 + 542640*x**6 + 1007760*x**7 + 1511640*x**8 + 1847560*x**9
  360. + 1847560*x**10 + 1511640*x**11 + 1007760*x**12 + 542640*x**13
  361. + 232560*x**14 + 77520*x**15 + 19380*x**16 + 3420*x**17 + 380*x**18
  362. + 20*x**19)
  363. assert factor(dep) == 20*(1 + x)**19
  364. def test_H15():
  365. assert simplify(Mul(*[x - r for r in solveset(x**3 + x**2 - 7)])) == x**3 + x**2 - 7
  366. def test_H16():
  367. assert factor(x**100 - 1) == ((x - 1)*(x + 1)*(x**2 + 1)*(x**4 - x**3
  368. + x**2 - x + 1)*(x**4 + x**3 + x**2 + x + 1)*(x**8 - x**6 + x**4
  369. - x**2 + 1)*(x**20 - x**15 + x**10 - x**5 + 1)*(x**20 + x**15 + x**10
  370. + x**5 + 1)*(x**40 - x**30 + x**20 - x**10 + 1))
  371. def test_H17():
  372. assert simplify(factor(expand(p1 * p2)) - p1*p2) == 0
  373. @XFAIL
  374. def test_H18():
  375. # Factor over complex rationals.
  376. test = factor(4*x**4 + 8*x**3 + 77*x**2 + 18*x + 153)
  377. good = (2*x + 3*I)*(2*x - 3*I)*(x + 1 - 4*I)*(x + 1 + 4*I)
  378. assert test == good
  379. def test_H19():
  380. a = symbols('a')
  381. # The idea is to let a**2 == 2, then solve 1/(a-1). Answer is a+1")
  382. assert Poly(a - 1).invert(Poly(a**2 - 2)) == a + 1
  383. @XFAIL
  384. def test_H20():
  385. raise NotImplementedError("let a**2==2; (x**3 + (a-2)*x**2 - "
  386. + "(2*a+3)*x - 3*a) / (x**2-2) = (x**2 - 2*x - 3) / (x-a)")
  387. @XFAIL
  388. def test_H21():
  389. raise NotImplementedError("evaluate (b+c)**4 assuming b**3==2, c**2==3. \
  390. Answer is 2*b + 8*c + 18*b**2 + 12*b*c + 9")
  391. def test_H22():
  392. assert factor(x**4 - 3*x**2 + 1, modulus=5) == (x - 2)**2 * (x + 2)**2
  393. def test_H23():
  394. f = x**11 + x + 1
  395. g = (x**2 + x + 1) * (x**9 - x**8 + x**6 - x**5 + x**3 - x**2 + 1)
  396. assert factor(f, modulus=65537) == g
  397. def test_H24():
  398. phi = AlgebraicNumber(S.GoldenRatio.expand(func=True), alias='phi')
  399. assert factor(x**4 - 3*x**2 + 1, extension=phi) == \
  400. (x - phi)*(x + 1 - phi)*(x - 1 + phi)*(x + phi)
  401. def test_H25():
  402. e = (x - 2*y**2 + 3*z**3) ** 20
  403. assert factor(expand(e)) == e
  404. def test_H26():
  405. g = expand((sin(x) - 2*cos(y)**2 + 3*tan(z)**3)**20)
  406. assert factor(g, expand=False) == (-sin(x) + 2*cos(y)**2 - 3*tan(z)**3)**20
  407. def test_H27():
  408. f = 24*x*y**19*z**8 - 47*x**17*y**5*z**8 + 6*x**15*y**9*z**2 - 3*x**22 + 5
  409. g = 34*x**5*y**8*z**13 + 20*x**7*y**7*z**7 + 12*x**9*y**16*z**4 + 80*y**14*z
  410. h = -2*z*y**7 \
  411. *(6*x**9*y**9*z**3 + 10*x**7*z**6 + 17*y*x**5*z**12 + 40*y**7) \
  412. *(3*x**22 + 47*x**17*y**5*z**8 - 6*x**15*y**9*z**2 - 24*x*y**19*z**8 - 5)
  413. assert factor(expand(f*g)) == h
  414. @XFAIL
  415. def test_H28():
  416. raise NotImplementedError("expand ((1 - c**2)**5 * (1 - s**2)**5 * "
  417. + "(c**2 + s**2)**10) with c**2 + s**2 = 1. Answer is c**10*s**10.")
  418. @XFAIL
  419. def test_H29():
  420. assert factor(4*x**2 - 21*x*y + 20*y**2, modulus=3) == (x + y)*(x - y)
  421. def test_H30():
  422. test = factor(x**3 + y**3, extension=sqrt(-3))
  423. answer = (x + y)*(x + y*(-R(1, 2) - sqrt(3)/2*I))*(x + y*(-R(1, 2) + sqrt(3)/2*I))
  424. assert answer == test
  425. def test_H31():
  426. f = (x**2 + 2*x + 3)/(x**3 + 4*x**2 + 5*x + 2)
  427. g = 2 / (x + 1)**2 - 2 / (x + 1) + 3 / (x + 2)
  428. assert apart(f) == g
  429. @XFAIL
  430. def test_H32(): # issue 6558
  431. raise NotImplementedError("[A*B*C - (A*B*C)**(-1)]*A*C*B (product \
  432. of a non-commuting product and its inverse)")
  433. def test_H33():
  434. A, B, C = symbols('A, B, C', commutative=False)
  435. assert (Commutator(A, Commutator(B, C))
  436. + Commutator(B, Commutator(C, A))
  437. + Commutator(C, Commutator(A, B))).doit().expand() == 0
  438. # I. Trigonometry
  439. def test_I1():
  440. assert tan(pi*R(7, 10)) == -sqrt(1 + 2/sqrt(5))
  441. @XFAIL
  442. def test_I2():
  443. assert sqrt((1 + cos(6))/2) == -cos(3)
  444. def test_I3():
  445. assert cos(n*pi) + sin((4*n - 1)*pi/2) == (-1)**n - 1
  446. def test_I4():
  447. assert refine(cos(pi*cos(n*pi)) + sin(pi/2*cos(n*pi)), Q.integer(n)) == (-1)**n - 1
  448. @XFAIL
  449. def test_I5():
  450. assert sin((n**5/5 + n**4/2 + n**3/3 - n/30) * pi) == 0
  451. @XFAIL
  452. def test_I6():
  453. raise NotImplementedError("assuming -3*pi<x<-5*pi/2, abs(cos(x)) == -cos(x), abs(sin(x)) == -sin(x)")
  454. @XFAIL
  455. def test_I7():
  456. assert cos(3*x)/cos(x) == cos(x)**2 - 3*sin(x)**2
  457. @XFAIL
  458. def test_I8():
  459. assert cos(3*x)/cos(x) == 2*cos(2*x) - 1
  460. @XFAIL
  461. def test_I9():
  462. # Supposed to do this with rewrite rules.
  463. assert cos(3*x)/cos(x) == cos(x)**2 - 3*sin(x)**2
  464. def test_I10():
  465. assert trigsimp((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1)) is nan
  466. @SKIP("hangs")
  467. @XFAIL
  468. def test_I11():
  469. assert limit((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1), x, 0) != 0
  470. @XFAIL
  471. def test_I12():
  472. # This should fail or return nan or something.
  473. res = diff((tan(x)**2 + 1 - cos(x)**-2) / (sin(x)**2 + cos(x)**2 - 1), x)
  474. assert res is nan # trigsimp(res) gives nan
  475. # J. Special functions.
  476. def test_J1():
  477. assert bernoulli(16) == R(-3617, 510)
  478. def test_J2():
  479. assert diff(elliptic_e(x, y**2), y) == (elliptic_e(x, y**2) - elliptic_f(x, y**2))/y
  480. @XFAIL
  481. def test_J3():
  482. raise NotImplementedError("Jacobi elliptic functions: diff(dn(u,k), u) == -k**2*sn(u,k)*cn(u,k)")
  483. def test_J4():
  484. assert gamma(R(-1, 2)) == -2*sqrt(pi)
  485. def test_J5():
  486. assert polygamma(0, R(1, 3)) == -log(3) - sqrt(3)*pi/6 - EulerGamma - log(sqrt(3))
  487. def test_J6():
  488. assert mpmath.besselj(2, 1 + 1j).ae(mpc('0.04157988694396212', '0.24739764151330632'))
  489. def test_J7():
  490. assert simplify(besselj(R(-5,2), pi/2)) == 12/(pi**2)
  491. def test_J8():
  492. p = besselj(R(3,2), z)
  493. q = (sin(z)/z - cos(z))/sqrt(pi*z/2)
  494. assert simplify(expand_func(p) -q) == 0
  495. def test_J9():
  496. assert besselj(0, z).diff(z) == - besselj(1, z)
  497. def test_J10():
  498. mu, nu = symbols('mu, nu', integer=True)
  499. assert assoc_legendre(nu, mu, 0) == 2**mu*sqrt(pi)/gamma((nu - mu)/2 + 1)/gamma((-nu - mu + 1)/2)
  500. def test_J11():
  501. assert simplify(assoc_legendre(3, 1, x)) == simplify(-R(3, 2)*sqrt(1 - x**2)*(5*x**2 - 1))
  502. @slow
  503. def test_J12():
  504. assert simplify(chebyshevt(1008, x) - 2*x*chebyshevt(1007, x) + chebyshevt(1006, x)) == 0
  505. def test_J13():
  506. a = symbols('a', integer=True, negative=False)
  507. assert chebyshevt(a, -1) == (-1)**a
  508. def test_J14():
  509. p = hyper([S.Half, S.Half], [R(3, 2)], z**2)
  510. assert hyperexpand(p) == asin(z)/z
  511. @XFAIL
  512. def test_J15():
  513. raise NotImplementedError("F((n+2)/2,-(n-2)/2,R(3,2),sin(z)**2) == sin(n*z)/(n*sin(z)*cos(z)); F(.) is hypergeometric function")
  514. @XFAIL
  515. def test_J16():
  516. raise NotImplementedError("diff(zeta(x), x) @ x=0 == -log(2*pi)/2")
  517. def test_J17():
  518. assert integrate(f((x + 2)/5)*DiracDelta((x - 2)/3) - g(x)*diff(DiracDelta(x - 1), x), (x, 0, 3)) == 3*f(R(4, 5)) + Subs(Derivative(g(x), x), x, 1)
  519. @XFAIL
  520. def test_J18():
  521. raise NotImplementedError("define an antisymmetric function")
  522. # K. The Complex Domain
  523. def test_K1():
  524. z1, z2 = symbols('z1, z2', complex=True)
  525. assert re(z1 + I*z2) == -im(z2) + re(z1)
  526. assert im(z1 + I*z2) == im(z1) + re(z2)
  527. def test_K2():
  528. assert abs(3 - sqrt(7) + I*sqrt(6*sqrt(7) - 15)) == 1
  529. @XFAIL
  530. def test_K3():
  531. a, b = symbols('a, b', real=True)
  532. assert simplify(abs(1/(a + I/a + I*b))) == 1/sqrt(a**2 + (I/a + b)**2)
  533. def test_K4():
  534. assert log(3 + 4*I).expand(complex=True) == log(5) + I*atan(R(4, 3))
  535. def test_K5():
  536. x, y = symbols('x, y', real=True)
  537. assert tan(x + I*y).expand(complex=True) == (sin(2*x)/(cos(2*x) +
  538. cosh(2*y)) + I*sinh(2*y)/(cos(2*x) + cosh(2*y)))
  539. def test_K6():
  540. assert sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z)) == sqrt(x*y)/sqrt(x)
  541. assert sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z)) != sqrt(y)
  542. def test_K7():
  543. y = symbols('y', real=True, negative=False)
  544. expr = sqrt(x*y*abs(z)**2)/(sqrt(x)*abs(z))
  545. sexpr = simplify(expr)
  546. assert sexpr == sqrt(y)
  547. def test_K8():
  548. z = symbols('z', complex=True)
  549. assert simplify(sqrt(1/z) - 1/sqrt(z)) != 0 # Passes
  550. z = symbols('z', complex=True, negative=False)
  551. assert simplify(sqrt(1/z) - 1/sqrt(z)) == 0 # Fails
  552. def test_K9():
  553. z = symbols('z', positive=True)
  554. assert simplify(sqrt(1/z) - 1/sqrt(z)) == 0
  555. def test_K10():
  556. z = symbols('z', negative=True)
  557. assert simplify(sqrt(1/z) + 1/sqrt(z)) == 0
  558. # This goes up to K25
  559. # L. Determining Zero Equivalence
  560. def test_L1():
  561. assert sqrt(997) - (997**3)**R(1, 6) == 0
  562. def test_L2():
  563. assert sqrt(999983) - (999983**3)**R(1, 6) == 0
  564. def test_L3():
  565. assert simplify((2**R(1, 3) + 4**R(1, 3))**3 - 6*(2**R(1, 3) + 4**R(1, 3)) - 6) == 0
  566. def test_L4():
  567. assert trigsimp(cos(x)**3 + cos(x)*sin(x)**2 - cos(x)) == 0
  568. @XFAIL
  569. def test_L5():
  570. assert log(tan(R(1, 2)*x + pi/4)) - asinh(tan(x)) == 0
  571. def test_L6():
  572. assert (log(tan(x/2 + pi/4)) - asinh(tan(x))).diff(x).subs({x: 0}) == 0
  573. @XFAIL
  574. def test_L7():
  575. assert simplify(log((2*sqrt(x) + 1)/(sqrt(4*x + 4*sqrt(x) + 1)))) == 0
  576. @XFAIL
  577. def test_L8():
  578. assert simplify((4*x + 4*sqrt(x) + 1)**(sqrt(x)/(2*sqrt(x) + 1)) \
  579. *(2*sqrt(x) + 1)**(1/(2*sqrt(x) + 1)) - 2*sqrt(x) - 1) == 0
  580. @XFAIL
  581. def test_L9():
  582. z = symbols('z', complex=True)
  583. assert simplify(2**(1 - z)*gamma(z)*zeta(z)*cos(z*pi/2) - pi**2*zeta(1 - z)) == 0
  584. # M. Equations
  585. @XFAIL
  586. def test_M1():
  587. assert Equality(x, 2)/2 + Equality(1, 1) == Equality(x/2 + 1, 2)
  588. def test_M2():
  589. # The roots of this equation should all be real. Note that this
  590. # doesn't test that they are correct.
  591. sol = solveset(3*x**3 - 18*x**2 + 33*x - 19, x)
  592. assert all(s.expand(complex=True).is_real for s in sol)
  593. @XFAIL
  594. def test_M5():
  595. assert solveset(x**6 - 9*x**4 - 4*x**3 + 27*x**2 - 36*x - 23, x) == FiniteSet(2**(1/3) + sqrt(3), 2**(1/3) - sqrt(3), +sqrt(3) - 1/2**(2/3) + I*sqrt(3)/2**(2/3), +sqrt(3) - 1/2**(2/3) - I*sqrt(3)/2**(2/3), -sqrt(3) - 1/2**(2/3) + I*sqrt(3)/2**(2/3), -sqrt(3) - 1/2**(2/3) - I*sqrt(3)/2**(2/3))
  596. def test_M6():
  597. assert set(solveset(x**7 - 1, x)) == \
  598. {cos(n*pi*R(2, 7)) + I*sin(n*pi*R(2, 7)) for n in range(0, 7)}
  599. # The paper asks for exp terms, but sin's and cos's may be acceptable;
  600. # if the results are simplified, exp terms appear for all but
  601. # -sin(pi/14) - I*cos(pi/14) and -sin(pi/14) + I*cos(pi/14) which
  602. # will simplify if you apply the transformation foo.rewrite(exp).expand()
  603. def test_M7():
  604. # TODO: Replace solve with solveset, as of now test fails for solveset
  605. assert set(solve(x**8 - 8*x**7 + 34*x**6 - 92*x**5 + 175*x**4 - 236*x**3 +
  606. 226*x**2 - 140*x + 46, x)) == set([
  607. 1 - sqrt(2)*I*sqrt(-sqrt(-3 + 4*sqrt(3)) + 3)/2,
  608. 1 - sqrt(2)*sqrt(-3 + I*sqrt(3 + 4*sqrt(3)))/2,
  609. 1 - sqrt(2)*I*sqrt(sqrt(-3 + 4*sqrt(3)) + 3)/2,
  610. 1 - sqrt(2)*sqrt(-3 - I*sqrt(3 + 4*sqrt(3)))/2,
  611. 1 + sqrt(2)*I*sqrt(sqrt(-3 + 4*sqrt(3)) + 3)/2,
  612. 1 + sqrt(2)*sqrt(-3 - I*sqrt(3 + 4*sqrt(3)))/2,
  613. 1 + sqrt(2)*sqrt(-3 + I*sqrt(3 + 4*sqrt(3)))/2,
  614. 1 + sqrt(2)*I*sqrt(-sqrt(-3 + 4*sqrt(3)) + 3)/2,
  615. ])
  616. @XFAIL # There are an infinite number of solutions.
  617. def test_M8():
  618. x = Symbol('x')
  619. z = symbols('z', complex=True)
  620. assert solveset(exp(2*x) + 2*exp(x) + 1 - z, x, S.Reals) == \
  621. FiniteSet(log(1 + z - 2*sqrt(z))/2, log(1 + z + 2*sqrt(z))/2)
  622. # This one could be simplified better (the 1/2 could be pulled into the log
  623. # as a sqrt, and the function inside the log can be factored as a square,
  624. # giving [log(sqrt(z) - 1), log(sqrt(z) + 1)]). Also, there should be an
  625. # infinite number of solutions.
  626. # x = {log(sqrt(z) - 1), log(sqrt(z) + 1) + i pi} [+ n 2 pi i, + n 2 pi i]
  627. # where n is an arbitrary integer. See url of detailed output above.
  628. @XFAIL
  629. def test_M9():
  630. # x = symbols('x')
  631. raise NotImplementedError("solveset(exp(2-x**2)-exp(-x),x) has complex solutions.")
  632. def test_M10():
  633. # TODO: Replace solve with solveset, as of now test fails for solveset
  634. assert solve(exp(x) - x, x) == [-LambertW(-1)]
  635. @XFAIL
  636. def test_M11():
  637. assert solveset(x**x - x, x) == FiniteSet(-1, 1)
  638. def test_M12():
  639. # TODO: x = [-1, 2*(+/-asinh(1)*I + n*pi}, 3*(pi/6 + n*pi/3)]
  640. # TODO: Replace solve with solveset, as of now test fails for solveset
  641. assert solve((x + 1)*(sin(x)**2 + 1)**2*cos(3*x)**3, x) == [
  642. -1, pi/6, pi/2,
  643. - I*log(1 + sqrt(2)), I*log(1 + sqrt(2)),
  644. pi - I*log(1 + sqrt(2)), pi + I*log(1 + sqrt(2)),
  645. ]
  646. @XFAIL
  647. def test_M13():
  648. n = Dummy('n')
  649. assert solveset_real(sin(x) - cos(x), x) == ImageSet(Lambda(n, n*pi - pi*R(7, 4)), S.Integers)
  650. @XFAIL
  651. def test_M14():
  652. n = Dummy('n')
  653. assert solveset_real(tan(x) - 1, x) == ImageSet(Lambda(n, n*pi + pi/4), S.Integers)
  654. def test_M15():
  655. n = Dummy('n')
  656. got = solveset(sin(x) - S.Half)
  657. assert any(got.dummy_eq(i) for i in (
  658. Union(ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers),
  659. ImageSet(Lambda(n, 2*n*pi + pi*R(5, 6)), S.Integers)),
  660. Union(ImageSet(Lambda(n, 2*n*pi + pi*R(5, 6)), S.Integers),
  661. ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers))))
  662. @XFAIL
  663. def test_M16():
  664. n = Dummy('n')
  665. assert solveset(sin(x) - tan(x), x) == ImageSet(Lambda(n, n*pi), S.Integers)
  666. @XFAIL
  667. def test_M17():
  668. assert solveset_real(asin(x) - atan(x), x) == FiniteSet(0)
  669. @XFAIL
  670. def test_M18():
  671. assert solveset_real(acos(x) - atan(x), x) == FiniteSet(sqrt((sqrt(5) - 1)/2))
  672. def test_M19():
  673. # TODO: Replace solve with solveset, as of now test fails for solveset
  674. assert solve((x - 2)/x**R(1, 3), x) == [2]
  675. def test_M20():
  676. assert solveset(sqrt(x**2 + 1) - x + 2, x) == EmptySet
  677. def test_M21():
  678. assert solveset(x + sqrt(x) - 2) == FiniteSet(1)
  679. def test_M22():
  680. assert solveset(2*sqrt(x) + 3*x**R(1, 4) - 2) == FiniteSet(R(1, 16))
  681. def test_M23():
  682. x = symbols('x', complex=True)
  683. # TODO: Replace solve with solveset, as of now test fails for solveset
  684. assert solve(x - 1/sqrt(1 + x**2)) == [
  685. -I*sqrt(S.Half + sqrt(5)/2), sqrt(Rational(-1, 2) + sqrt(5)/2)]
  686. def test_M24():
  687. # TODO: Replace solve with solveset, as of now test fails for solveset
  688. solution = solve(1 - binomial(m, 2)*2**k, k)
  689. answer = log(2/(m*(m - 1)), 2)
  690. assert solution[0].expand() == answer.expand()
  691. def test_M25():
  692. a, b, c, d = symbols(':d', positive=True)
  693. x = symbols('x')
  694. # TODO: Replace solve with solveset, as of now test fails for solveset
  695. assert solve(a*b**x - c*d**x, x)[0].expand() == (log(c/a)/log(b/d)).expand()
  696. def test_M26():
  697. # TODO: Replace solve with solveset, as of now test fails for solveset
  698. assert solve(sqrt(log(x)) - log(sqrt(x))) == [1, exp(4)]
  699. def test_M27():
  700. x = symbols('x', real=True)
  701. b = symbols('b', real=True)
  702. # TODO: Replace solve with solveset which gives both [+/- current answer]
  703. # note that there is a typo in this test in the wester.pdf; there is no
  704. # real solution for the equation as it appears in wester.pdf
  705. assert solve(log(acos(asin(x**R(2, 3) - b)) - 1) + 2, x
  706. ) == [(b + sin(cos(exp(-2) + 1)))**R(3, 2)]
  707. @XFAIL
  708. def test_M28():
  709. assert solveset_real(5*x + exp((x - 5)/2) - 8*x**3, x, assume=Q.real(x)) == [-0.784966, -0.016291, 0.802557]
  710. def test_M29():
  711. x = symbols('x')
  712. assert solveset(abs(x - 1) - 2, domain=S.Reals) == FiniteSet(-1, 3)
  713. def test_M30():
  714. # TODO: Replace solve with solveset, as of now
  715. # solveset doesn't supports assumptions
  716. # assert solve(abs(2*x + 5) - abs(x - 2),x, assume=Q.real(x)) == [-1, -7]
  717. assert solveset_real(abs(2*x + 5) - abs(x - 2), x) == FiniteSet(-1, -7)
  718. def test_M31():
  719. # TODO: Replace solve with solveset, as of now
  720. # solveset doesn't supports assumptions
  721. # assert solve(1 - abs(x) - max(-x - 2, x - 2),x, assume=Q.real(x)) == [-3/2, 3/2]
  722. assert solveset_real(1 - abs(x) - Max(-x - 2, x - 2), x) == FiniteSet(R(-3, 2), R(3, 2))
  723. @XFAIL
  724. def test_M32():
  725. # TODO: Replace solve with solveset, as of now
  726. # solveset doesn't supports assumptions
  727. assert solveset_real(Max(2 - x**2, x)- Max(-x, (x**3)/9), x) == FiniteSet(-1, 3)
  728. @XFAIL
  729. def test_M33():
  730. # TODO: Replace solve with solveset, as of now
  731. # solveset doesn't supports assumptions
  732. # Second answer can be written in another form. The second answer is the root of x**3 + 9*x**2 - 18 = 0 in the interval (-2, -1).
  733. assert solveset_real(Max(2 - x**2, x) - x**3/9, x) == FiniteSet(-3, -1.554894, 3)
  734. @XFAIL
  735. def test_M34():
  736. z = symbols('z', complex=True)
  737. assert solveset((1 + I) * z + (2 - I) * conjugate(z) + 3*I, z) == FiniteSet(2 + 3*I)
  738. def test_M35():
  739. x, y = symbols('x y', real=True)
  740. assert linsolve((3*x - 2*y - I*y + 3*I).as_real_imag(), y, x) == FiniteSet((3, 2))
  741. def test_M36():
  742. # TODO: Replace solve with solveset, as of now
  743. # solveset doesn't supports solving for function
  744. # assert solve(f**2 + f - 2, x) == [Eq(f(x), 1), Eq(f(x), -2)]
  745. assert solveset(f(x)**2 + f(x) - 2, f(x)) == FiniteSet(-2, 1)
  746. def test_M37():
  747. assert linsolve([x + y + z - 6, 2*x + y + 2*z - 10, x + 3*y + z - 10 ], x, y, z) == \
  748. FiniteSet((-z + 4, 2, z))
  749. def test_M38():
  750. a, b, c = symbols('a, b, c')
  751. domain = FracField([a, b, c], ZZ).to_domain()
  752. ring = PolyRing('k1:50', domain)
  753. (k1, k2, k3, k4, k5, k6, k7, k8, k9, k10,
  754. k11, k12, k13, k14, k15, k16, k17, k18, k19, k20,
  755. k21, k22, k23, k24, k25, k26, k27, k28, k29, k30,
  756. k31, k32, k33, k34, k35, k36, k37, k38, k39, k40,
  757. k41, k42, k43, k44, k45, k46, k47, k48, k49) = ring.gens
  758. system = [
  759. -b*k8/a + c*k8/a, -b*k11/a + c*k11/a, -b*k10/a + c*k10/a + k2, -k3 - b*k9/a + c*k9/a,
  760. -b*k14/a + c*k14/a, -b*k15/a + c*k15/a, -b*k18/a + c*k18/a - k2, -b*k17/a + c*k17/a,
  761. -b*k16/a + c*k16/a + k4, -b*k13/a + c*k13/a - b*k21/a + c*k21/a + b*k5/a - c*k5/a,
  762. b*k44/a - c*k44/a, -b*k45/a + c*k45/a, -b*k20/a + c*k20/a, -b*k44/a + c*k44/a,
  763. b*k46/a - c*k46/a, b**2*k47/a**2 - 2*b*c*k47/a**2 + c**2*k47/a**2, k3, -k4,
  764. -b*k12/a + c*k12/a - a*k6/b + c*k6/b, -b*k19/a + c*k19/a + a*k7/c - b*k7/c,
  765. b*k45/a - c*k45/a, -b*k46/a + c*k46/a, -k48 + c*k48/a + c*k48/b - c**2*k48/(a*b),
  766. -k49 + b*k49/a + b*k49/c - b**2*k49/(a*c), a*k1/b - c*k1/b, a*k4/b - c*k4/b,
  767. a*k3/b - c*k3/b + k9, -k10 + a*k2/b - c*k2/b, a*k7/b - c*k7/b, -k9, k11,
  768. b*k12/a - c*k12/a + a*k6/b - c*k6/b, a*k15/b - c*k15/b, k10 + a*k18/b - c*k18/b,
  769. -k11 + a*k17/b - c*k17/b, a*k16/b - c*k16/b, -a*k13/b + c*k13/b + a*k21/b - c*k21/b + a*k5/b - c*k5/b,
  770. -a*k44/b + c*k44/b, a*k45/b - c*k45/b, a*k14/c - b*k14/c + a*k20/b - c*k20/b,
  771. a*k44/b - c*k44/b, -a*k46/b + c*k46/b, -k47 + c*k47/a + c*k47/b - c**2*k47/(a*b),
  772. a*k19/b - c*k19/b, -a*k45/b + c*k45/b, a*k46/b - c*k46/b, a**2*k48/b**2 - 2*a*c*k48/b**2 + c**2*k48/b**2,
  773. -k49 + a*k49/b + a*k49/c - a**2*k49/(b*c), k16, -k17, -a*k1/c + b*k1/c,
  774. -k16 - a*k4/c + b*k4/c, -a*k3/c + b*k3/c, k18 - a*k2/c + b*k2/c, b*k19/a - c*k19/a - a*k7/c + b*k7/c,
  775. -a*k6/c + b*k6/c, -a*k8/c + b*k8/c, -a*k11/c + b*k11/c + k17, -a*k10/c + b*k10/c - k18,
  776. -a*k9/c + b*k9/c, -a*k14/c + b*k14/c - a*k20/b + c*k20/b, -a*k13/c + b*k13/c + a*k21/c - b*k21/c - a*k5/c + b*k5/c,
  777. a*k44/c - b*k44/c, -a*k45/c + b*k45/c, -a*k44/c + b*k44/c, a*k46/c - b*k46/c,
  778. -k47 + b*k47/a + b*k47/c - b**2*k47/(a*c), -a*k12/c + b*k12/c, a*k45/c - b*k45/c,
  779. -a*k46/c + b*k46/c, -k48 + a*k48/b + a*k48/c - a**2*k48/(b*c),
  780. a**2*k49/c**2 - 2*a*b*k49/c**2 + b**2*k49/c**2, k8, k11, -k15, k10 - k18,
  781. -k17, k9, -k16, -k29, k14 - k32, -k21 + k23 - k31, -k24 - k30, -k35, k44,
  782. -k45, k36, k13 - k23 + k39, -k20 + k38, k25 + k37, b*k26/a - c*k26/a - k34 + k42,
  783. -2*k44, k45, k46, b*k47/a - c*k47/a, k41, k44, -k46, -b*k47/a + c*k47/a,
  784. k12 + k24, -k19 - k25, -a*k27/b + c*k27/b - k33, k45, -k46, -a*k48/b + c*k48/b,
  785. a*k28/c - b*k28/c + k40, -k45, k46, a*k48/b - c*k48/b, a*k49/c - b*k49/c,
  786. -a*k49/c + b*k49/c, -k1, -k4, -k3, k15, k18 - k2, k17, k16, k22, k25 - k7,
  787. k24 + k30, k21 + k23 - k31, k28, -k44, k45, -k30 - k6, k20 + k32, k27 + b*k33/a - c*k33/a,
  788. k44, -k46, -b*k47/a + c*k47/a, -k36, k31 - k39 - k5, -k32 - k38, k19 - k37,
  789. k26 - a*k34/b + c*k34/b - k42, k44, -2*k45, k46, a*k48/b - c*k48/b,
  790. a*k35/c - b*k35/c - k41, -k44, k46, b*k47/a - c*k47/a, -a*k49/c + b*k49/c,
  791. -k40, k45, -k46, -a*k48/b + c*k48/b, a*k49/c - b*k49/c, k1, k4, k3, -k8,
  792. -k11, -k10 + k2, -k9, k37 + k7, -k14 - k38, -k22, -k25 - k37, -k24 + k6,
  793. -k13 - k23 + k39, -k28 + b*k40/a - c*k40/a, k44, -k45, -k27, -k44, k46,
  794. b*k47/a - c*k47/a, k29, k32 + k38, k31 - k39 + k5, -k12 + k30, k35 - a*k41/b + c*k41/b,
  795. -k44, k45, -k26 + k34 + a*k42/c - b*k42/c, k44, k45, -2*k46, -b*k47/a + c*k47/a,
  796. -a*k48/b + c*k48/b, a*k49/c - b*k49/c, k33, -k45, k46, a*k48/b - c*k48/b,
  797. -a*k49/c + b*k49/c
  798. ]
  799. solution = {
  800. k49: 0, k48: 0, k47: 0, k46: 0, k45: 0, k44: 0, k41: 0, k40: 0,
  801. k38: 0, k37: 0, k36: 0, k35: 0, k33: 0, k32: 0, k30: 0, k29: 0,
  802. k28: 0, k27: 0, k25: 0, k24: 0, k22: 0, k21: 0, k20: 0, k19: 0,
  803. k18: 0, k17: 0, k16: 0, k15: 0, k14: 0, k13: 0, k12: 0, k11: 0,
  804. k10: 0, k9: 0, k8: 0, k7: 0, k6: 0, k5: 0, k4: 0, k3: 0,
  805. k2: 0, k1: 0,
  806. k34: b/c*k42, k31: k39, k26: a/c*k42, k23: k39
  807. }
  808. assert solve_lin_sys(system, ring) == solution
  809. def test_M39():
  810. x, y, z = symbols('x y z', complex=True)
  811. # TODO: Replace solve with solveset, as of now
  812. # solveset doesn't supports non-linear multivariate
  813. assert solve([x**2*y + 3*y*z - 4, -3*x**2*z + 2*y**2 + 1, 2*y*z**2 - z**2 - 1 ]) ==\
  814. [{y: 1, z: 1, x: -1}, {y: 1, z: 1, x: 1},\
  815. {y: sqrt(2)*I, z: R(1,3) - sqrt(2)*I/3, x: -sqrt(-1 - sqrt(2)*I)},\
  816. {y: sqrt(2)*I, z: R(1,3) - sqrt(2)*I/3, x: sqrt(-1 - sqrt(2)*I)},\
  817. {y: -sqrt(2)*I, z: R(1,3) + sqrt(2)*I/3, x: -sqrt(-1 + sqrt(2)*I)},\
  818. {y: -sqrt(2)*I, z: R(1,3) + sqrt(2)*I/3, x: sqrt(-1 + sqrt(2)*I)}]
  819. # N. Inequalities
  820. def test_N1():
  821. assert ask(E**pi > pi**E)
  822. @XFAIL
  823. def test_N2():
  824. x = symbols('x', real=True)
  825. assert ask(x**4 - x + 1 > 0) is True
  826. assert ask(x**4 - x + 1 > 1) is False
  827. @XFAIL
  828. def test_N3():
  829. x = symbols('x', real=True)
  830. assert ask(And(Lt(-1, x), Lt(x, 1)), abs(x) < 1 )
  831. @XFAIL
  832. def test_N4():
  833. x, y = symbols('x y', real=True)
  834. assert ask(2*x**2 > 2*y**2, (x > y) & (y > 0)) is True
  835. @XFAIL
  836. def test_N5():
  837. x, y, k = symbols('x y k', real=True)
  838. assert ask(k*x**2 > k*y**2, (x > y) & (y > 0) & (k > 0)) is True
  839. @slow
  840. @XFAIL
  841. def test_N6():
  842. x, y, k, n = symbols('x y k n', real=True)
  843. assert ask(k*x**n > k*y**n, (x > y) & (y > 0) & (k > 0) & (n > 0)) is True
  844. @XFAIL
  845. def test_N7():
  846. x, y = symbols('x y', real=True)
  847. assert ask(y > 0, (x > 1) & (y >= x - 1)) is True
  848. @XFAIL
  849. @slow
  850. def test_N8():
  851. x, y, z = symbols('x y z', real=True)
  852. assert ask(Eq(x, y) & Eq(y, z),
  853. (x >= y) & (y >= z) & (z >= x))
  854. def test_N9():
  855. x = Symbol('x')
  856. assert solveset(abs(x - 1) > 2, domain=S.Reals) == Union(Interval(-oo, -1, False, True),
  857. Interval(3, oo, True))
  858. def test_N10():
  859. x = Symbol('x')
  860. p = (x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5)
  861. assert solveset(expand(p) < 0, domain=S.Reals) == Union(Interval(-oo, 1, True, True),
  862. Interval(2, 3, True, True),
  863. Interval(4, 5, True, True))
  864. def test_N11():
  865. x = Symbol('x')
  866. assert solveset(6/(x - 3) <= 3, domain=S.Reals) == Union(Interval(-oo, 3, True, True), Interval(5, oo))
  867. def test_N12():
  868. x = Symbol('x')
  869. assert solveset(sqrt(x) < 2, domain=S.Reals) == Interval(0, 4, False, True)
  870. def test_N13():
  871. x = Symbol('x')
  872. assert solveset(sin(x) < 2, domain=S.Reals) == S.Reals
  873. @XFAIL
  874. def test_N14():
  875. x = Symbol('x')
  876. # Gives 'Union(Interval(Integer(0), Mul(Rational(1, 2), pi), false, true),
  877. # Interval(Mul(Rational(1, 2), pi), Mul(Integer(2), pi), true, false))'
  878. # which is not the correct answer, but the provided also seems wrong.
  879. assert solveset(sin(x) < 1, x, domain=S.Reals) == Union(Interval(-oo, pi/2, True, True),
  880. Interval(pi/2, oo, True, True))
  881. def test_N15():
  882. r, t = symbols('r t')
  883. # raises NotImplementedError: only univariate inequalities are supported
  884. solveset(abs(2*r*(cos(t) - 1) + 1) <= 1, r, S.Reals)
  885. def test_N16():
  886. r, t = symbols('r t')
  887. solveset((r**2)*((cos(t) - 4)**2)*sin(t)**2 < 9, r, S.Reals)
  888. @XFAIL
  889. def test_N17():
  890. # currently only univariate inequalities are supported
  891. assert solveset((x + y > 0, x - y < 0), (x, y)) == (abs(x) < y)
  892. def test_O1():
  893. M = Matrix((1 + I, -2, 3*I))
  894. assert sqrt(expand(M.dot(M.H))) == sqrt(15)
  895. def test_O2():
  896. assert Matrix((2, 2, -3)).cross(Matrix((1, 3, 1))) == Matrix([[11],
  897. [-5],
  898. [4]])
  899. # The vector module has no way of representing vectors symbolically (without
  900. # respect to a basis)
  901. @XFAIL
  902. def test_O3():
  903. # assert (va ^ vb) | (vc ^ vd) == -(va | vc)*(vb | vd) + (va | vd)*(vb | vc)
  904. raise NotImplementedError("""The vector module has no way of representing
  905. vectors symbolically (without respect to a basis)""")
  906. def test_O4():
  907. from sympy.vector import CoordSys3D, Del
  908. N = CoordSys3D("N")
  909. delop = Del()
  910. i, j, k = N.base_vectors()
  911. x, y, z = N.base_scalars()
  912. F = i*(x*y*z) + j*((x*y*z)**2) + k*((y**2)*(z**3))
  913. assert delop.cross(F).doit() == (-2*x**2*y**2*z + 2*y*z**3)*i + x*y*j + (2*x*y**2*z**2 - x*z)*k
  914. @XFAIL
  915. def test_O5():
  916. #assert grad|(f^g)-g|(grad^f)+f|(grad^g) == 0
  917. raise NotImplementedError("""The vector module has no way of representing
  918. vectors symbolically (without respect to a basis)""")
  919. #testO8-O9 MISSING!!
  920. def test_O10():
  921. L = [Matrix([2, 3, 5]), Matrix([3, 6, 2]), Matrix([8, 3, 6])]
  922. assert GramSchmidt(L) == [Matrix([
  923. [2],
  924. [3],
  925. [5]]),
  926. Matrix([
  927. [R(23, 19)],
  928. [R(63, 19)],
  929. [R(-47, 19)]]),
  930. Matrix([
  931. [R(1692, 353)],
  932. [R(-1551, 706)],
  933. [R(-423, 706)]])]
  934. def test_P1():
  935. assert Matrix(3, 3, lambda i, j: j - i).diagonal(-1) == Matrix(
  936. 1, 2, [-1, -1])
  937. def test_P2():
  938. M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  939. M.row_del(1)
  940. M.col_del(2)
  941. assert M == Matrix([[1, 2],
  942. [7, 8]])
  943. def test_P3():
  944. A = Matrix([
  945. [11, 12, 13, 14],
  946. [21, 22, 23, 24],
  947. [31, 32, 33, 34],
  948. [41, 42, 43, 44]])
  949. A11 = A[0:3, 1:4]
  950. A12 = A[(0, 1, 3), (2, 0, 3)]
  951. A21 = A
  952. A221 = -A[0:2, 2:4]
  953. A222 = -A[(3, 0), (2, 1)]
  954. A22 = BlockMatrix([[A221, A222]]).T
  955. rows = [[-A11, A12], [A21, A22]]
  956. raises(ValueError, lambda: BlockMatrix(rows))
  957. B = Matrix(rows)
  958. assert B == Matrix([
  959. [-12, -13, -14, 13, 11, 14],
  960. [-22, -23, -24, 23, 21, 24],
  961. [-32, -33, -34, 43, 41, 44],
  962. [11, 12, 13, 14, -13, -23],
  963. [21, 22, 23, 24, -14, -24],
  964. [31, 32, 33, 34, -43, -13],
  965. [41, 42, 43, 44, -42, -12]])
  966. @XFAIL
  967. def test_P4():
  968. raise NotImplementedError("Block matrix diagonalization not supported")
  969. def test_P5():
  970. M = Matrix([[7, 11],
  971. [3, 8]])
  972. assert M % 2 == Matrix([[1, 1],
  973. [1, 0]])
  974. def test_P6():
  975. M = Matrix([[cos(x), sin(x)],
  976. [-sin(x), cos(x)]])
  977. assert M.diff(x, 2) == Matrix([[-cos(x), -sin(x)],
  978. [sin(x), -cos(x)]])
  979. def test_P7():
  980. M = Matrix([[x, y]])*(
  981. z*Matrix([[1, 3, 5],
  982. [2, 4, 6]]) + Matrix([[7, -9, 11],
  983. [-8, 10, -12]]))
  984. assert M == Matrix([[x*(z + 7) + y*(2*z - 8), x*(3*z - 9) + y*(4*z + 10),
  985. x*(5*z + 11) + y*(6*z - 12)]])
  986. def test_P8():
  987. M = Matrix([[1, -2*I],
  988. [-3*I, 4]])
  989. assert M.norm(ord=S.Infinity) == 7
  990. def test_P9():
  991. a, b, c = symbols('a b c', nonzero=True)
  992. M = Matrix([[a/(b*c), 1/c, 1/b],
  993. [1/c, b/(a*c), 1/a],
  994. [1/b, 1/a, c/(a*b)]])
  995. assert factor(M.norm('fro')) == (a**2 + b**2 + c**2)/(abs(a)*abs(b)*abs(c))
  996. @XFAIL
  997. def test_P10():
  998. M = Matrix([[1, 2 + 3*I],
  999. [f(4 - 5*I), 6]])
  1000. # conjugate(f(4 - 5*i)) is not simplified to f(4+5*I)
  1001. assert M.H == Matrix([[1, f(4 + 5*I)],
  1002. [2 + 3*I, 6]])
  1003. @XFAIL
  1004. def test_P11():
  1005. # raises NotImplementedError("Matrix([[x,y],[1,x*y]]).inv()
  1006. # not simplifying to extract common factor")
  1007. assert Matrix([[x, y],
  1008. [1, x*y]]).inv() == (1/(x**2 - 1))*Matrix([[x, -1],
  1009. [-1/y, x/y]])
  1010. def test_P11_workaround():
  1011. # This test was changed to inverse method ADJ because it depended on the
  1012. # specific form of inverse returned from the 'GE' method which has changed.
  1013. M = Matrix([[x, y], [1, x*y]]).inv('ADJ')
  1014. c = gcd(tuple(M))
  1015. assert MatMul(c, M/c, evaluate=False) == MatMul(c, Matrix([
  1016. [x*y, -y],
  1017. [ -1, x]]), evaluate=False)
  1018. def test_P12():
  1019. A11 = MatrixSymbol('A11', n, n)
  1020. A12 = MatrixSymbol('A12', n, n)
  1021. A22 = MatrixSymbol('A22', n, n)
  1022. B = BlockMatrix([[A11, A12],
  1023. [ZeroMatrix(n, n), A22]])
  1024. assert block_collapse(B.I) == BlockMatrix([[A11.I, (-1)*A11.I*A12*A22.I],
  1025. [ZeroMatrix(n, n), A22.I]])
  1026. def test_P13():
  1027. M = Matrix([[1, x - 2, x - 3],
  1028. [x - 1, x**2 - 3*x + 6, x**2 - 3*x - 2],
  1029. [x - 2, x**2 - 8, 2*(x**2) - 12*x + 14]])
  1030. L, U, _ = M.LUdecomposition()
  1031. assert simplify(L) == Matrix([[1, 0, 0],
  1032. [x - 1, 1, 0],
  1033. [x - 2, x - 3, 1]])
  1034. assert simplify(U) == Matrix([[1, x - 2, x - 3],
  1035. [0, 4, x - 5],
  1036. [0, 0, x - 7]])
  1037. def test_P14():
  1038. M = Matrix([[1, 2, 3, 1, 3],
  1039. [3, 2, 1, 1, 7],
  1040. [0, 2, 4, 1, 1],
  1041. [1, 1, 1, 1, 4]])
  1042. R, _ = M.rref()
  1043. assert R == Matrix([[1, 0, -1, 0, 2],
  1044. [0, 1, 2, 0, -1],
  1045. [0, 0, 0, 1, 3],
  1046. [0, 0, 0, 0, 0]])
  1047. def test_P15():
  1048. M = Matrix([[-1, 3, 7, -5],
  1049. [4, -2, 1, 3],
  1050. [2, 4, 15, -7]])
  1051. assert M.rank() == 2
  1052. def test_P16():
  1053. M = Matrix([[2*sqrt(2), 8],
  1054. [6*sqrt(6), 24*sqrt(3)]])
  1055. assert M.rank() == 1
  1056. def test_P17():
  1057. t = symbols('t', real=True)
  1058. M=Matrix([
  1059. [sin(2*t), cos(2*t)],
  1060. [2*(1 - (cos(t)**2))*cos(t), (1 - 2*(sin(t)**2))*sin(t)]])
  1061. assert M.rank() == 1
  1062. def test_P18():
  1063. M = Matrix([[1, 0, -2, 0],
  1064. [-2, 1, 0, 3],
  1065. [-1, 2, -6, 6]])
  1066. assert M.nullspace() == [Matrix([[2],
  1067. [4],
  1068. [1],
  1069. [0]]),
  1070. Matrix([[0],
  1071. [-3],
  1072. [0],
  1073. [1]])]
  1074. def test_P19():
  1075. w = symbols('w')
  1076. M = Matrix([[1, 1, 1, 1],
  1077. [w, x, y, z],
  1078. [w**2, x**2, y**2, z**2],
  1079. [w**3, x**3, y**3, z**3]])
  1080. assert M.det() == (w**3*x**2*y - w**3*x**2*z - w**3*x*y**2 + w**3*x*z**2
  1081. + w**3*y**2*z - w**3*y*z**2 - w**2*x**3*y + w**2*x**3*z
  1082. + w**2*x*y**3 - w**2*x*z**3 - w**2*y**3*z + w**2*y*z**3
  1083. + w*x**3*y**2 - w*x**3*z**2 - w*x**2*y**3 + w*x**2*z**3
  1084. + w*y**3*z**2 - w*y**2*z**3 - x**3*y**2*z + x**3*y*z**2
  1085. + x**2*y**3*z - x**2*y*z**3 - x*y**3*z**2 + x*y**2*z**3
  1086. )
  1087. @XFAIL
  1088. def test_P20():
  1089. raise NotImplementedError("Matrix minimal polynomial not supported")
  1090. def test_P21():
  1091. M = Matrix([[5, -3, -7],
  1092. [-2, 1, 2],
  1093. [2, -3, -4]])
  1094. assert M.charpoly(x).as_expr() == x**3 - 2*x**2 - 5*x + 6
  1095. def test_P22():
  1096. d = 100
  1097. M = (2 - x)*eye(d)
  1098. assert M.eigenvals() == {-x + 2: d}
  1099. def test_P23():
  1100. M = Matrix([
  1101. [2, 1, 0, 0, 0],
  1102. [1, 2, 1, 0, 0],
  1103. [0, 1, 2, 1, 0],
  1104. [0, 0, 1, 2, 1],
  1105. [0, 0, 0, 1, 2]])
  1106. assert M.eigenvals() == {
  1107. S('1'): 1,
  1108. S('2'): 1,
  1109. S('3'): 1,
  1110. S('sqrt(3) + 2'): 1,
  1111. S('-sqrt(3) + 2'): 1}
  1112. def test_P24():
  1113. M = Matrix([[611, 196, -192, 407, -8, -52, -49, 29],
  1114. [196, 899, 113, -192, -71, -43, -8, -44],
  1115. [-192, 113, 899, 196, 61, 49, 8, 52],
  1116. [ 407, -192, 196, 611, 8, 44, 59, -23],
  1117. [ -8, -71, 61, 8, 411, -599, 208, 208],
  1118. [ -52, -43, 49, 44, -599, 411, 208, 208],
  1119. [ -49, -8, 8, 59, 208, 208, 99, -911],
  1120. [ 29, -44, 52, -23, 208, 208, -911, 99]])
  1121. assert M.eigenvals() == {
  1122. S('0'): 1,
  1123. S('10*sqrt(10405)'): 1,
  1124. S('100*sqrt(26) + 510'): 1,
  1125. S('1000'): 2,
  1126. S('-100*sqrt(26) + 510'): 1,
  1127. S('-10*sqrt(10405)'): 1,
  1128. S('1020'): 1}
  1129. def test_P25():
  1130. MF = N(Matrix([[ 611, 196, -192, 407, -8, -52, -49, 29],
  1131. [ 196, 899, 113, -192, -71, -43, -8, -44],
  1132. [-192, 113, 899, 196, 61, 49, 8, 52],
  1133. [ 407, -192, 196, 611, 8, 44, 59, -23],
  1134. [ -8, -71, 61, 8, 411, -599, 208, 208],
  1135. [ -52, -43, 49, 44, -599, 411, 208, 208],
  1136. [ -49, -8, 8, 59, 208, 208, 99, -911],
  1137. [ 29, -44, 52, -23, 208, 208, -911, 99]]))
  1138. ev_1 = sorted(MF.eigenvals(multiple=True))
  1139. ev_2 = sorted(
  1140. [-1020.0490184299969, 0.0, 0.09804864072151699, 1000.0, 1000.0,
  1141. 1019.9019513592784, 1020.0, 1020.0490184299969])
  1142. for x, y in zip(ev_1, ev_2):
  1143. assert abs(x - y) < 1e-12
  1144. def test_P26():
  1145. a0, a1, a2, a3, a4 = symbols('a0 a1 a2 a3 a4')
  1146. M = Matrix([[-a4, -a3, -a2, -a1, -a0, 0, 0, 0, 0],
  1147. [ 1, 0, 0, 0, 0, 0, 0, 0, 0],
  1148. [ 0, 1, 0, 0, 0, 0, 0, 0, 0],
  1149. [ 0, 0, 1, 0, 0, 0, 0, 0, 0],
  1150. [ 0, 0, 0, 1, 0, 0, 0, 0, 0],
  1151. [ 0, 0, 0, 0, 0, -1, -1, 0, 0],
  1152. [ 0, 0, 0, 0, 0, 1, 0, 0, 0],
  1153. [ 0, 0, 0, 0, 0, 0, 1, -1, -1],
  1154. [ 0, 0, 0, 0, 0, 0, 0, 1, 0]])
  1155. assert M.eigenvals(error_when_incomplete=False) == {
  1156. S('-1/2 - sqrt(3)*I/2'): 2,
  1157. S('-1/2 + sqrt(3)*I/2'): 2}
  1158. def test_P27():
  1159. a = symbols('a')
  1160. M = Matrix([[a, 0, 0, 0, 0],
  1161. [0, 0, 0, 0, 1],
  1162. [0, 0, a, 0, 0],
  1163. [0, 0, 0, a, 0],
  1164. [0, -2, 0, 0, 2]])
  1165. assert M.eigenvects() == [
  1166. (a, 3, [
  1167. Matrix([1, 0, 0, 0, 0]),
  1168. Matrix([0, 0, 1, 0, 0]),
  1169. Matrix([0, 0, 0, 1, 0])
  1170. ]),
  1171. (1 - I, 1, [
  1172. Matrix([0, (1 + I)/2, 0, 0, 1])
  1173. ]),
  1174. (1 + I, 1, [
  1175. Matrix([0, (1 - I)/2, 0, 0, 1])
  1176. ]),
  1177. ]
  1178. @XFAIL
  1179. def test_P28():
  1180. raise NotImplementedError("Generalized eigenvectors not supported \
  1181. https://github.com/sympy/sympy/issues/5293")
  1182. @XFAIL
  1183. def test_P29():
  1184. raise NotImplementedError("Generalized eigenvectors not supported \
  1185. https://github.com/sympy/sympy/issues/5293")
  1186. def test_P30():
  1187. M = Matrix([[1, 0, 0, 1, -1],
  1188. [0, 1, -2, 3, -3],
  1189. [0, 0, -1, 2, -2],
  1190. [1, -1, 1, 0, 1],
  1191. [1, -1, 1, -1, 2]])
  1192. _, J = M.jordan_form()
  1193. assert J == Matrix([[-1, 0, 0, 0, 0],
  1194. [0, 1, 1, 0, 0],
  1195. [0, 0, 1, 0, 0],
  1196. [0, 0, 0, 1, 1],
  1197. [0, 0, 0, 0, 1]])
  1198. @XFAIL
  1199. def test_P31():
  1200. raise NotImplementedError("Smith normal form not implemented")
  1201. def test_P32():
  1202. M = Matrix([[1, -2],
  1203. [2, 1]])
  1204. assert exp(M).rewrite(cos).simplify() == Matrix([[E*cos(2), -E*sin(2)],
  1205. [E*sin(2), E*cos(2)]])
  1206. def test_P33():
  1207. w, t = symbols('w t')
  1208. M = Matrix([[0, 1, 0, 0],
  1209. [0, 0, 0, 2*w],
  1210. [0, 0, 0, 1],
  1211. [0, -2*w, 3*w**2, 0]])
  1212. assert exp(M*t).rewrite(cos).expand() == Matrix([
  1213. [1, -3*t + 4*sin(t*w)/w, 6*t*w - 6*sin(t*w), -2*cos(t*w)/w + 2/w],
  1214. [0, 4*cos(t*w) - 3, -6*w*cos(t*w) + 6*w, 2*sin(t*w)],
  1215. [0, 2*cos(t*w)/w - 2/w, -3*cos(t*w) + 4, sin(t*w)/w],
  1216. [0, -2*sin(t*w), 3*w*sin(t*w), cos(t*w)]])
  1217. @XFAIL
  1218. def test_P34():
  1219. a, b, c = symbols('a b c', real=True)
  1220. M = Matrix([[a, 1, 0, 0, 0, 0],
  1221. [0, a, 0, 0, 0, 0],
  1222. [0, 0, b, 0, 0, 0],
  1223. [0, 0, 0, c, 1, 0],
  1224. [0, 0, 0, 0, c, 1],
  1225. [0, 0, 0, 0, 0, c]])
  1226. # raises exception, sin(M) not supported. exp(M*I) also not supported
  1227. # https://github.com/sympy/sympy/issues/6218
  1228. assert sin(M) == Matrix([[sin(a), cos(a), 0, 0, 0, 0],
  1229. [0, sin(a), 0, 0, 0, 0],
  1230. [0, 0, sin(b), 0, 0, 0],
  1231. [0, 0, 0, sin(c), cos(c), -sin(c)/2],
  1232. [0, 0, 0, 0, sin(c), cos(c)],
  1233. [0, 0, 0, 0, 0, sin(c)]])
  1234. @XFAIL
  1235. def test_P35():
  1236. M = pi/2*Matrix([[2, 1, 1],
  1237. [2, 3, 2],
  1238. [1, 1, 2]])
  1239. # raises exception, sin(M) not supported. exp(M*I) also not supported
  1240. # https://github.com/sympy/sympy/issues/6218
  1241. assert sin(M) == eye(3)
  1242. @XFAIL
  1243. def test_P36():
  1244. M = Matrix([[10, 7],
  1245. [7, 17]])
  1246. assert sqrt(M) == Matrix([[3, 1],
  1247. [1, 4]])
  1248. def test_P37():
  1249. M = Matrix([[1, 1, 0],
  1250. [0, 1, 0],
  1251. [0, 0, 1]])
  1252. assert M**S.Half == Matrix([[1, R(1, 2), 0],
  1253. [0, 1, 0],
  1254. [0, 0, 1]])
  1255. @XFAIL
  1256. def test_P38():
  1257. M=Matrix([[0, 1, 0],
  1258. [0, 0, 0],
  1259. [0, 0, 0]])
  1260. with raises(AssertionError):
  1261. # raises ValueError: Matrix det == 0; not invertible
  1262. M**S.Half
  1263. # if it doesn't raise then this assertion will be
  1264. # raised and the test will be flagged as not XFAILing
  1265. assert None
  1266. @XFAIL
  1267. def test_P39():
  1268. """
  1269. M=Matrix([
  1270. [1, 1],
  1271. [2, 2],
  1272. [3, 3]])
  1273. M.SVD()
  1274. """
  1275. raise NotImplementedError("Singular value decomposition not implemented")
  1276. def test_P40():
  1277. r, t = symbols('r t', real=True)
  1278. M = Matrix([r*cos(t), r*sin(t)])
  1279. assert M.jacobian(Matrix([r, t])) == Matrix([[cos(t), -r*sin(t)],
  1280. [sin(t), r*cos(t)]])
  1281. def test_P41():
  1282. r, t = symbols('r t', real=True)
  1283. assert hessian(r**2*sin(t),(r,t)) == Matrix([[ 2*sin(t), 2*r*cos(t)],
  1284. [2*r*cos(t), -r**2*sin(t)]])
  1285. def test_P42():
  1286. assert wronskian([cos(x), sin(x)], x).simplify() == 1
  1287. def test_P43():
  1288. def __my_jacobian(M, Y):
  1289. return Matrix([M.diff(v).T for v in Y]).T
  1290. r, t = symbols('r t', real=True)
  1291. M = Matrix([r*cos(t), r*sin(t)])
  1292. assert __my_jacobian(M,[r,t]) == Matrix([[cos(t), -r*sin(t)],
  1293. [sin(t), r*cos(t)]])
  1294. def test_P44():
  1295. def __my_hessian(f, Y):
  1296. V = Matrix([diff(f, v) for v in Y])
  1297. return Matrix([V.T.diff(v) for v in Y])
  1298. r, t = symbols('r t', real=True)
  1299. assert __my_hessian(r**2*sin(t), (r, t)) == Matrix([
  1300. [ 2*sin(t), 2*r*cos(t)],
  1301. [2*r*cos(t), -r**2*sin(t)]])
  1302. def test_P45():
  1303. def __my_wronskian(Y, v):
  1304. M = Matrix([Matrix(Y).T.diff(x, n) for n in range(0, len(Y))])
  1305. return M.det()
  1306. assert __my_wronskian([cos(x), sin(x)], x).simplify() == 1
  1307. # Q1-Q6 Tensor tests missing
  1308. @XFAIL
  1309. def test_R1():
  1310. i, j, n = symbols('i j n', integer=True, positive=True)
  1311. xn = MatrixSymbol('xn', n, 1)
  1312. Sm = Sum((xn[i, 0] - Sum(xn[j, 0], (j, 0, n - 1))/n)**2, (i, 0, n - 1))
  1313. # sum does not calculate
  1314. # Unknown result
  1315. Sm.doit()
  1316. raise NotImplementedError('Unknown result')
  1317. @XFAIL
  1318. def test_R2():
  1319. m, b = symbols('m b')
  1320. i, n = symbols('i n', integer=True, positive=True)
  1321. xn = MatrixSymbol('xn', n, 1)
  1322. yn = MatrixSymbol('yn', n, 1)
  1323. f = Sum((yn[i, 0] - m*xn[i, 0] - b)**2, (i, 0, n - 1))
  1324. f1 = diff(f, m)
  1325. f2 = diff(f, b)
  1326. # raises TypeError: solveset() takes at most 2 arguments (3 given)
  1327. solveset((f1, f2), (m, b), domain=S.Reals)
  1328. @XFAIL
  1329. def test_R3():
  1330. n, k = symbols('n k', integer=True, positive=True)
  1331. sk = ((-1)**k) * (binomial(2*n, k))**2
  1332. Sm = Sum(sk, (k, 1, oo))
  1333. T = Sm.doit()
  1334. T2 = T.combsimp()
  1335. # returns -((-1)**n*factorial(2*n)
  1336. # - (factorial(n))**2)*exp_polar(-I*pi)/(factorial(n))**2
  1337. assert T2 == (-1)**n*binomial(2*n, n)
  1338. @XFAIL
  1339. def test_R4():
  1340. # Macsyma indefinite sum test case:
  1341. #(c15) /* Check whether the full Gosper algorithm is implemented
  1342. # => 1/2^(n + 1) binomial(n, k - 1) */
  1343. #closedform(indefsum(binomial(n, k)/2^n - binomial(n + 1, k)/2^(n + 1), k));
  1344. #Time= 2690 msecs
  1345. # (- n + k - 1) binomial(n + 1, k)
  1346. #(d15) - --------------------------------
  1347. # n
  1348. # 2 2 (n + 1)
  1349. #
  1350. #(c16) factcomb(makefact(%));
  1351. #Time= 220 msecs
  1352. # n!
  1353. #(d16) ----------------
  1354. # n
  1355. # 2 k! 2 (n - k)!
  1356. # Might be possible after fixing https://github.com/sympy/sympy/pull/1879
  1357. raise NotImplementedError("Indefinite sum not supported")
  1358. @XFAIL
  1359. def test_R5():
  1360. a, b, c, n, k = symbols('a b c n k', integer=True, positive=True)
  1361. sk = ((-1)**k)*(binomial(a + b, a + k)
  1362. *binomial(b + c, b + k)*binomial(c + a, c + k))
  1363. Sm = Sum(sk, (k, 1, oo))
  1364. T = Sm.doit() # hypergeometric series not calculated
  1365. assert T == factorial(a+b+c)/(factorial(a)*factorial(b)*factorial(c))
  1366. def test_R6():
  1367. n, k = symbols('n k', integer=True, positive=True)
  1368. gn = MatrixSymbol('gn', n + 2, 1)
  1369. Sm = Sum(gn[k, 0] - gn[k - 1, 0], (k, 1, n + 1))
  1370. assert Sm.doit() == -gn[0, 0] + gn[n + 1, 0]
  1371. def test_R7():
  1372. n, k = symbols('n k', integer=True, positive=True)
  1373. T = Sum(k**3,(k,1,n)).doit()
  1374. assert T.factor() == n**2*(n + 1)**2/4
  1375. @XFAIL
  1376. def test_R8():
  1377. n, k = symbols('n k', integer=True, positive=True)
  1378. Sm = Sum(k**2*binomial(n, k), (k, 1, n))
  1379. T = Sm.doit() #returns Piecewise function
  1380. assert T.combsimp() == n*(n + 1)*2**(n - 2)
  1381. def test_R9():
  1382. n, k = symbols('n k', integer=True, positive=True)
  1383. Sm = Sum(binomial(n, k - 1)/k, (k, 1, n + 1))
  1384. assert Sm.doit().simplify() == (2**(n + 1) - 1)/(n + 1)
  1385. @XFAIL
  1386. def test_R10():
  1387. n, m, r, k = symbols('n m r k', integer=True, positive=True)
  1388. Sm = Sum(binomial(n, k)*binomial(m, r - k), (k, 0, r))
  1389. T = Sm.doit()
  1390. T2 = T.combsimp().rewrite(factorial)
  1391. assert T2 == factorial(m + n)/(factorial(r)*factorial(m + n - r))
  1392. assert T2 == binomial(m + n, r).rewrite(factorial)
  1393. # rewrite(binomial) is not working.
  1394. # https://github.com/sympy/sympy/issues/7135
  1395. T3 = T2.rewrite(binomial)
  1396. assert T3 == binomial(m + n, r)
  1397. @XFAIL
  1398. def test_R11():
  1399. n, k = symbols('n k', integer=True, positive=True)
  1400. sk = binomial(n, k)*fibonacci(k)
  1401. Sm = Sum(sk, (k, 0, n))
  1402. T = Sm.doit()
  1403. # Fibonacci simplification not implemented
  1404. # https://github.com/sympy/sympy/issues/7134
  1405. assert T == fibonacci(2*n)
  1406. @XFAIL
  1407. def test_R12():
  1408. n, k = symbols('n k', integer=True, positive=True)
  1409. Sm = Sum(fibonacci(k)**2, (k, 0, n))
  1410. T = Sm.doit()
  1411. assert T == fibonacci(n)*fibonacci(n + 1)
  1412. @XFAIL
  1413. def test_R13():
  1414. n, k = symbols('n k', integer=True, positive=True)
  1415. Sm = Sum(sin(k*x), (k, 1, n))
  1416. T = Sm.doit() # Sum is not calculated
  1417. assert T.simplify() == cot(x/2)/2 - cos(x*(2*n + 1)/2)/(2*sin(x/2))
  1418. @XFAIL
  1419. def test_R14():
  1420. n, k = symbols('n k', integer=True, positive=True)
  1421. Sm = Sum(sin((2*k - 1)*x), (k, 1, n))
  1422. T = Sm.doit() # Sum is not calculated
  1423. assert T.simplify() == sin(n*x)**2/sin(x)
  1424. @XFAIL
  1425. def test_R15():
  1426. n, k = symbols('n k', integer=True, positive=True)
  1427. Sm = Sum(binomial(n - k, k), (k, 0, floor(n/2)))
  1428. T = Sm.doit() # Sum is not calculated
  1429. assert T.simplify() == fibonacci(n + 1)
  1430. def test_R16():
  1431. k = symbols('k', integer=True, positive=True)
  1432. Sm = Sum(1/k**2 + 1/k**3, (k, 1, oo))
  1433. assert Sm.doit() == zeta(3) + pi**2/6
  1434. def test_R17():
  1435. k = symbols('k', integer=True, positive=True)
  1436. assert abs(float(Sum(1/k**2 + 1/k**3, (k, 1, oo)))
  1437. - 2.8469909700078206) < 1e-15
  1438. def test_R18():
  1439. k = symbols('k', integer=True, positive=True)
  1440. Sm = Sum(1/(2**k*k**2), (k, 1, oo))
  1441. T = Sm.doit()
  1442. assert T.simplify() == -log(2)**2/2 + pi**2/12
  1443. @slow
  1444. @XFAIL
  1445. def test_R19():
  1446. k = symbols('k', integer=True, positive=True)
  1447. Sm = Sum(1/((3*k + 1)*(3*k + 2)*(3*k + 3)), (k, 0, oo))
  1448. T = Sm.doit()
  1449. # assert fails, T not simplified
  1450. assert T.simplify() == -log(3)/4 + sqrt(3)*pi/12
  1451. @XFAIL
  1452. def test_R20():
  1453. n, k = symbols('n k', integer=True, positive=True)
  1454. Sm = Sum(binomial(n, 4*k), (k, 0, oo))
  1455. T = Sm.doit()
  1456. # assert fails, T not simplified
  1457. assert T.simplify() == 2**(n/2)*cos(pi*n/4)/2 + 2**(n - 1)/2
  1458. @XFAIL
  1459. def test_R21():
  1460. k = symbols('k', integer=True, positive=True)
  1461. Sm = Sum(1/(sqrt(k*(k + 1)) * (sqrt(k) + sqrt(k + 1))), (k, 1, oo))
  1462. T = Sm.doit() # Sum not calculated
  1463. assert T.simplify() == 1
  1464. # test_R22 answer not available in Wester samples
  1465. # Sum(Sum(binomial(n, k)*binomial(n - k, n - 2*k)*x**n*y**(n - 2*k),
  1466. # (k, 0, floor(n/2))), (n, 0, oo)) with abs(x*y)<1?
  1467. @XFAIL
  1468. def test_R23():
  1469. n, k = symbols('n k', integer=True, positive=True)
  1470. Sm = Sum(Sum((factorial(n)/(factorial(k)**2*factorial(n - 2*k)))*
  1471. (x/y)**k*(x*y)**(n - k), (n, 2*k, oo)), (k, 0, oo))
  1472. # Missing how to express constraint abs(x*y)<1?
  1473. T = Sm.doit() # Sum not calculated
  1474. assert T == -1/sqrt(x**2*y**2 - 4*x**2 - 2*x*y + 1)
  1475. def test_R24():
  1476. m, k = symbols('m k', integer=True, positive=True)
  1477. Sm = Sum(Product(k/(2*k - 1), (k, 1, m)), (m, 2, oo))
  1478. assert Sm.doit() == pi/2
  1479. def test_S1():
  1480. k = symbols('k', integer=True, positive=True)
  1481. Pr = Product(gamma(k/3), (k, 1, 8))
  1482. assert Pr.doit().simplify() == 640*sqrt(3)*pi**3/6561
  1483. def test_S2():
  1484. n, k = symbols('n k', integer=True, positive=True)
  1485. assert Product(k, (k, 1, n)).doit() == factorial(n)
  1486. def test_S3():
  1487. n, k = symbols('n k', integer=True, positive=True)
  1488. assert Product(x**k, (k, 1, n)).doit().simplify() == x**(n*(n + 1)/2)
  1489. def test_S4():
  1490. n, k = symbols('n k', integer=True, positive=True)
  1491. assert Product(1 + 1/k, (k, 1, n -1)).doit().simplify() == n
  1492. def test_S5():
  1493. n, k = symbols('n k', integer=True, positive=True)
  1494. assert (Product((2*k - 1)/(2*k), (k, 1, n)).doit().gammasimp() ==
  1495. gamma(n + S.Half)/(sqrt(pi)*gamma(n + 1)))
  1496. @XFAIL
  1497. def test_S6():
  1498. n, k = symbols('n k', integer=True, positive=True)
  1499. # Product does not evaluate
  1500. assert (Product(x**2 -2*x*cos(k*pi/n) + 1, (k, 1, n - 1)).doit().simplify()
  1501. == (x**(2*n) - 1)/(x**2 - 1))
  1502. @XFAIL
  1503. def test_S7():
  1504. k = symbols('k', integer=True, positive=True)
  1505. Pr = Product((k**3 - 1)/(k**3 + 1), (k, 2, oo))
  1506. T = Pr.doit() # Product does not evaluate
  1507. assert T.simplify() == R(2, 3)
  1508. @XFAIL
  1509. def test_S8():
  1510. k = symbols('k', integer=True, positive=True)
  1511. Pr = Product(1 - 1/(2*k)**2, (k, 1, oo))
  1512. T = Pr.doit()
  1513. # Product does not evaluate
  1514. assert T.simplify() == 2/pi
  1515. @XFAIL
  1516. def test_S9():
  1517. k = symbols('k', integer=True, positive=True)
  1518. Pr = Product(1 + (-1)**(k + 1)/(2*k - 1), (k, 1, oo))
  1519. T = Pr.doit()
  1520. # Product produces 0
  1521. # https://github.com/sympy/sympy/issues/7133
  1522. assert T.simplify() == sqrt(2)
  1523. @XFAIL
  1524. def test_S10():
  1525. k = symbols('k', integer=True, positive=True)
  1526. Pr = Product((k*(k + 1) + 1 + I)/(k*(k + 1) + 1 - I), (k, 0, oo))
  1527. T = Pr.doit()
  1528. # Product does not evaluate
  1529. assert T.simplify() == -1
  1530. def test_T1():
  1531. assert limit((1 + 1/n)**n, n, oo) == E
  1532. assert limit((1 - cos(x))/x**2, x, 0) == S.Half
  1533. def test_T2():
  1534. assert limit((3**x + 5**x)**(1/x), x, oo) == 5
  1535. def test_T3():
  1536. assert limit(log(x)/(log(x) + sin(x)), x, oo) == 1
  1537. def test_T4():
  1538. assert limit((exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1))))
  1539. - exp(x))/x, x, oo) == -exp(2)
  1540. def test_T5():
  1541. assert limit(x*log(x)*log(x*exp(x) - x**2)**2/log(log(x**2
  1542. + 2*exp(exp(3*x**3*log(x))))), x, oo) == R(1, 3)
  1543. def test_T6():
  1544. assert limit(1/n * factorial(n)**(1/n), n, oo) == exp(-1)
  1545. def test_T7():
  1546. limit(1/n * gamma(n + 1)**(1/n), n, oo)
  1547. def test_T8():
  1548. a, z = symbols('a z', positive=True)
  1549. assert limit(gamma(z + a)/gamma(z)*exp(-a*log(z)), z, oo) == 1
  1550. @XFAIL
  1551. def test_T9():
  1552. z, k = symbols('z k', positive=True)
  1553. # raises NotImplementedError:
  1554. # Don't know how to calculate the mrv of '(1, k)'
  1555. assert limit(hyper((1, k), (1,), z/k), k, oo) == exp(z)
  1556. @XFAIL
  1557. def test_T10():
  1558. # No longer raises PoleError, but should return euler-mascheroni constant
  1559. assert limit(zeta(x) - 1/(x - 1), x, 1) == integrate(-1/x + 1/floor(x), (x, 1, oo))
  1560. @XFAIL
  1561. def test_T11():
  1562. n, k = symbols('n k', integer=True, positive=True)
  1563. # evaluates to 0
  1564. assert limit(n**x/(x*product((1 + x/k), (k, 1, n))), n, oo) == gamma(x)
  1565. def test_T12():
  1566. x, t = symbols('x t', real=True)
  1567. # Does not evaluate the limit but returns an expression with erf
  1568. assert limit(x * integrate(exp(-t**2), (t, 0, x))/(1 - exp(-x**2)),
  1569. x, 0) == 1
  1570. def test_T13():
  1571. x = symbols('x', real=True)
  1572. assert [limit(x/abs(x), x, 0, dir='-'),
  1573. limit(x/abs(x), x, 0, dir='+')] == [-1, 1]
  1574. def test_T14():
  1575. x = symbols('x', real=True)
  1576. assert limit(atan(-log(x)), x, 0, dir='+') == pi/2
  1577. def test_U1():
  1578. x = symbols('x', real=True)
  1579. assert diff(abs(x), x) == sign(x)
  1580. def test_U2():
  1581. f = Lambda(x, Piecewise((-x, x < 0), (x, x >= 0)))
  1582. assert diff(f(x), x) == Piecewise((-1, x < 0), (1, x >= 0))
  1583. def test_U3():
  1584. f = Lambda(x, Piecewise((x**2 - 1, x == 1), (x**3, x != 1)))
  1585. f1 = Lambda(x, diff(f(x), x))
  1586. assert f1(x) == 3*x**2
  1587. assert f1(1) == 3
  1588. @XFAIL
  1589. def test_U4():
  1590. n = symbols('n', integer=True, positive=True)
  1591. x = symbols('x', real=True)
  1592. d = diff(x**n, x, n)
  1593. assert d.rewrite(factorial) == factorial(n)
  1594. def test_U5():
  1595. # issue 6681
  1596. t = symbols('t')
  1597. ans = (
  1598. Derivative(f(g(t)), g(t))*Derivative(g(t), (t, 2)) +
  1599. Derivative(f(g(t)), (g(t), 2))*Derivative(g(t), t)**2)
  1600. assert f(g(t)).diff(t, 2) == ans
  1601. assert ans.doit() == ans
  1602. def test_U6():
  1603. h = Function('h')
  1604. T = integrate(f(y), (y, h(x), g(x)))
  1605. assert T.diff(x) == (
  1606. f(g(x))*Derivative(g(x), x) - f(h(x))*Derivative(h(x), x))
  1607. @XFAIL
  1608. def test_U7():
  1609. p, t = symbols('p t', real=True)
  1610. # Exact differential => d(V(P, T)) => dV/dP DP + dV/dT DT
  1611. # raises ValueError: Since there is more than one variable in the
  1612. # expression, the variable(s) of differentiation must be supplied to
  1613. # differentiate f(p,t)
  1614. diff(f(p, t))
  1615. def test_U8():
  1616. x, y = symbols('x y', real=True)
  1617. eq = cos(x*y) + x
  1618. # If SymPy had implicit_diff() function this hack could be avoided
  1619. # TODO: Replace solve with solveset, current test fails for solveset
  1620. assert idiff(y - eq, y, x) == (-y*sin(x*y) + 1)/(x*sin(x*y) + 1)
  1621. def test_U9():
  1622. # Wester sample case for Maple:
  1623. # O29 := diff(f(x, y), x) + diff(f(x, y), y);
  1624. # /d \ /d \
  1625. # |-- f(x, y)| + |-- f(x, y)|
  1626. # \dx / \dy /
  1627. #
  1628. # O30 := factor(subs(f(x, y) = g(x^2 + y^2), %));
  1629. # 2 2
  1630. # 2 D(g)(x + y ) (x + y)
  1631. x, y = symbols('x y', real=True)
  1632. su = diff(f(x, y), x) + diff(f(x, y), y)
  1633. s2 = su.subs(f(x, y), g(x**2 + y**2))
  1634. s3 = s2.doit().factor()
  1635. # Subs not performed, s3 = 2*(x + y)*Subs(Derivative(
  1636. # g(_xi_1), _xi_1), _xi_1, x**2 + y**2)
  1637. # Derivative(g(x*2 + y**2), x**2 + y**2) is not valid in SymPy,
  1638. # and probably will remain that way. You can take derivatives with respect
  1639. # to other expressions only if they are atomic, like a symbol or a
  1640. # function.
  1641. # D operator should be added to SymPy
  1642. # See https://github.com/sympy/sympy/issues/4719.
  1643. assert s3 == (x + y)*Subs(Derivative(g(x), x), x, x**2 + y**2)*2
  1644. def test_U10():
  1645. # see issue 2519:
  1646. assert residue((z**3 + 5)/((z**4 - 1)*(z + 1)), z, -1) == R(-9, 4)
  1647. @XFAIL
  1648. def test_U11():
  1649. # assert (2*dx + dz) ^ (3*dx + dy + dz) ^ (dx + dy + 4*dz) == 8*dx ^ dy ^dz
  1650. raise NotImplementedError
  1651. @XFAIL
  1652. def test_U12():
  1653. # Wester sample case:
  1654. # (c41) /* d(3 x^5 dy /\ dz + 5 x y^2 dz /\ dx + 8 z dx /\ dy)
  1655. # => (15 x^4 + 10 x y + 8) dx /\ dy /\ dz */
  1656. # factor(ext_diff(3*x^5 * dy ~ dz + 5*x*y^2 * dz ~ dx + 8*z * dx ~ dy));
  1657. # 4
  1658. # (d41) (10 x y + 15 x + 8) dx dy dz
  1659. raise NotImplementedError(
  1660. "External diff of differential form not supported")
  1661. def test_U13():
  1662. assert minimum(x**4 - x + 1, x) == -3*2**R(1,3)/8 + 1
  1663. @XFAIL
  1664. def test_U14():
  1665. #f = 1/(x**2 + y**2 + 1)
  1666. #assert [minimize(f), maximize(f)] == [0,1]
  1667. raise NotImplementedError("minimize(), maximize() not supported")
  1668. @XFAIL
  1669. def test_U15():
  1670. raise NotImplementedError("minimize() not supported and also solve does \
  1671. not support multivariate inequalities")
  1672. @XFAIL
  1673. def test_U16():
  1674. raise NotImplementedError("minimize() not supported in SymPy and also \
  1675. solve does not support multivariate inequalities")
  1676. @XFAIL
  1677. def test_U17():
  1678. raise NotImplementedError("Linear programming, symbolic simplex not \
  1679. supported in SymPy")
  1680. def test_V1():
  1681. x = symbols('x', real=True)
  1682. assert integrate(abs(x), x) == Piecewise((-x**2/2, x <= 0), (x**2/2, True))
  1683. def test_V2():
  1684. assert integrate(Piecewise((-x, x < 0), (x, x >= 0)), x
  1685. ) == Piecewise((-x**2/2, x < 0), (x**2/2, True))
  1686. def test_V3():
  1687. assert integrate(1/(x**3 + 2),x).diff().simplify() == 1/(x**3 + 2)
  1688. def test_V4():
  1689. assert integrate(2**x/sqrt(1 + 4**x), x) == asinh(2**x)/log(2)
  1690. @XFAIL
  1691. def test_V5():
  1692. # Returns (-45*x**2 + 80*x - 41)/(5*sqrt(2*x - 1)*(4*x**2 - 4*x + 1))
  1693. assert (integrate((3*x - 5)**2/(2*x - 1)**R(7, 2), x).simplify() ==
  1694. (-41 + 80*x - 45*x**2)/(5*(2*x - 1)**R(5, 2)))
  1695. @XFAIL
  1696. def test_V6():
  1697. # returns RootSum(40*_z**2 - 1, Lambda(_i, _i*log(-4*_i + exp(-m*x))))/m
  1698. assert (integrate(1/(2*exp(m*x) - 5*exp(-m*x)), x) == sqrt(10)*(
  1699. log(2*exp(m*x) - sqrt(10)) - log(2*exp(m*x) + sqrt(10)))/(20*m))
  1700. def test_V7():
  1701. r1 = integrate(sinh(x)**4/cosh(x)**2)
  1702. assert r1.simplify() == x*R(-3, 2) + sinh(x)**3/(2*cosh(x)) + 3*tanh(x)/2
  1703. @XFAIL
  1704. def test_V8_V9():
  1705. #Macsyma test case:
  1706. #(c27) /* This example involves several symbolic parameters
  1707. # => 1/sqrt(b^2 - a^2) log([sqrt(b^2 - a^2) tan(x/2) + a + b]/
  1708. # [sqrt(b^2 - a^2) tan(x/2) - a - b]) (a^2 < b^2)
  1709. # [Gradshteyn and Ryzhik 2.553(3)] */
  1710. #assume(b^2 > a^2)$
  1711. #(c28) integrate(1/(a + b*cos(x)), x);
  1712. #(c29) trigsimp(ratsimp(diff(%, x)));
  1713. # 1
  1714. #(d29) ------------
  1715. # b cos(x) + a
  1716. raise NotImplementedError(
  1717. "Integrate with assumption not supported")
  1718. def test_V10():
  1719. assert integrate(1/(3 + 3*cos(x) + 4*sin(x)), x) == log(4*tan(x/2) + 3)/4
  1720. def test_V11():
  1721. r1 = integrate(1/(4 + 3*cos(x) + 4*sin(x)), x)
  1722. r2 = factor(r1)
  1723. assert (logcombine(r2, force=True) ==
  1724. log(((tan(x/2) + 1)/(tan(x/2) + 7))**R(1, 3)))
  1725. def test_V12():
  1726. r1 = integrate(1/(5 + 3*cos(x) + 4*sin(x)), x)
  1727. assert r1 == -1/(tan(x/2) + 2)
  1728. @XFAIL
  1729. def test_V13():
  1730. r1 = integrate(1/(6 + 3*cos(x) + 4*sin(x)), x)
  1731. # expression not simplified, returns: -sqrt(11)*I*log(tan(x/2) + 4/3
  1732. # - sqrt(11)*I/3)/11 + sqrt(11)*I*log(tan(x/2) + 4/3 + sqrt(11)*I/3)/11
  1733. assert r1.simplify() == 2*sqrt(11)*atan(sqrt(11)*(3*tan(x/2) + 4)/11)/11
  1734. @slow
  1735. @XFAIL
  1736. def test_V14():
  1737. r1 = integrate(log(abs(x**2 - y**2)), x)
  1738. # Piecewise result does not simplify to the desired result.
  1739. assert (r1.simplify() == x*log(abs(x**2 - y**2))
  1740. + y*log(x + y) - y*log(x - y) - 2*x)
  1741. def test_V15():
  1742. r1 = integrate(x*acot(x/y), x)
  1743. assert simplify(r1 - (x*y + (x**2 + y**2)*acot(x/y))/2) == 0
  1744. @XFAIL
  1745. def test_V16():
  1746. # Integral not calculated
  1747. assert integrate(cos(5*x)*Ci(2*x), x) == Ci(2*x)*sin(5*x)/5 - (Si(3*x) + Si(7*x))/10
  1748. @XFAIL
  1749. def test_V17():
  1750. r1 = integrate((diff(f(x), x)*g(x)
  1751. - f(x)*diff(g(x), x))/(f(x)**2 - g(x)**2), x)
  1752. # integral not calculated
  1753. assert simplify(r1 - (f(x) - g(x))/(f(x) + g(x))/2) == 0
  1754. @XFAIL
  1755. def test_W1():
  1756. # The function has a pole at y.
  1757. # The integral has a Cauchy principal value of zero but SymPy returns -I*pi
  1758. # https://github.com/sympy/sympy/issues/7159
  1759. assert integrate(1/(x - y), (x, y - 1, y + 1)) == 0
  1760. @XFAIL
  1761. def test_W2():
  1762. # The function has a pole at y.
  1763. # The integral is divergent but SymPy returns -2
  1764. # https://github.com/sympy/sympy/issues/7160
  1765. # Test case in Macsyma:
  1766. # (c6) errcatch(integrate(1/(x - a)^2, x, a - 1, a + 1));
  1767. # Integral is divergent
  1768. assert integrate(1/(x - y)**2, (x, y - 1, y + 1)) is zoo
  1769. @XFAIL
  1770. @slow
  1771. def test_W3():
  1772. # integral is not calculated
  1773. # https://github.com/sympy/sympy/issues/7161
  1774. assert integrate(sqrt(x + 1/x - 2), (x, 0, 1)) == R(4, 3)
  1775. @XFAIL
  1776. @slow
  1777. def test_W4():
  1778. # integral is not calculated
  1779. assert integrate(sqrt(x + 1/x - 2), (x, 1, 2)) == -2*sqrt(2)/3 + R(4, 3)
  1780. @XFAIL
  1781. @slow
  1782. def test_W5():
  1783. # integral is not calculated
  1784. assert integrate(sqrt(x + 1/x - 2), (x, 0, 2)) == -2*sqrt(2)/3 + R(8, 3)
  1785. @XFAIL
  1786. @slow
  1787. def test_W6():
  1788. # integral is not calculated
  1789. assert integrate(sqrt(2 - 2*cos(2*x))/2, (x, pi*R(-3, 4), -pi/4)) == sqrt(2)
  1790. def test_W7():
  1791. a = symbols('a', positive=True)
  1792. r1 = integrate(cos(x)/(x**2 + a**2), (x, -oo, oo))
  1793. assert r1.simplify() == pi*exp(-a)/a
  1794. @XFAIL
  1795. def test_W8():
  1796. # Test case in Mathematica:
  1797. # In[19]:= Integrate[t^(a - 1)/(1 + t), {t, 0, Infinity},
  1798. # Assumptions -> 0 < a < 1]
  1799. # Out[19]= Pi Csc[a Pi]
  1800. raise NotImplementedError(
  1801. "Integrate with assumption 0 < a < 1 not supported")
  1802. @XFAIL
  1803. @slow
  1804. def test_W9():
  1805. # Integrand with a residue at infinity => -2 pi [sin(pi/5) + sin(2pi/5)]
  1806. # (principal value) [Levinson and Redheffer, p. 234] *)
  1807. r1 = integrate(5*x**3/(1 + x + x**2 + x**3 + x**4), (x, -oo, oo))
  1808. r2 = r1.doit()
  1809. assert r2 == -2*pi*(sqrt(-sqrt(5)/8 + 5/8) + sqrt(sqrt(5)/8 + 5/8))
  1810. @XFAIL
  1811. def test_W10():
  1812. # integrate(1/[1 + x + x^2 + ... + x^(2 n)], x = -infinity..infinity) =
  1813. # 2 pi/(2 n + 1) [1 + cos(pi/[2 n + 1])] csc(2 pi/[2 n + 1])
  1814. # [Levinson and Redheffer, p. 255] => 2 pi/5 [1 + cos(pi/5)] csc(2 pi/5) */
  1815. r1 = integrate(x/(1 + x + x**2 + x**4), (x, -oo, oo))
  1816. r2 = r1.doit()
  1817. assert r2 == 2*pi*(sqrt(5)/4 + 5/4)*csc(pi*R(2, 5))/5
  1818. @XFAIL
  1819. def test_W11():
  1820. # integral not calculated
  1821. assert (integrate(sqrt(1 - x**2)/(1 + x**2), (x, -1, 1)) ==
  1822. pi*(-1 + sqrt(2)))
  1823. def test_W12():
  1824. p = symbols('p', positive=True)
  1825. q = symbols('q', real=True)
  1826. r1 = integrate(x*exp(-p*x**2 + 2*q*x), (x, -oo, oo))
  1827. assert r1.simplify() == sqrt(pi)*q*exp(q**2/p)/p**R(3, 2)
  1828. @XFAIL
  1829. def test_W13():
  1830. # Integral not calculated. Expected result is 2*(Euler_mascheroni_constant)
  1831. r1 = integrate(1/log(x) + 1/(1 - x) - log(log(1/x)), (x, 0, 1))
  1832. assert r1 == 2*EulerGamma
  1833. def test_W14():
  1834. assert integrate(sin(x)/x*exp(2*I*x), (x, -oo, oo)) == 0
  1835. @XFAIL
  1836. def test_W15():
  1837. # integral not calculated
  1838. assert integrate(log(gamma(x))*cos(6*pi*x), (x, 0, 1)) == R(1, 12)
  1839. def test_W16():
  1840. assert integrate((1 + x)**3*legendre_poly(1, x)*legendre_poly(2, x),
  1841. (x, -1, 1)) == R(36, 35)
  1842. def test_W17():
  1843. a, b = symbols('a b', positive=True)
  1844. assert integrate(exp(-a*x)*besselj(0, b*x),
  1845. (x, 0, oo)) == 1/(b*sqrt(a**2/b**2 + 1))
  1846. def test_W18():
  1847. assert integrate((besselj(1, x)/x)**2, (x, 0, oo)) == 4/(3*pi)
  1848. @XFAIL
  1849. def test_W19():
  1850. # Integral not calculated
  1851. # Expected result is (cos 7 - 1)/7 [Gradshteyn and Ryzhik 6.782(3)]
  1852. assert integrate(Ci(x)*besselj(0, 2*sqrt(7*x)), (x, 0, oo)) == (cos(7) - 1)/7
  1853. @XFAIL
  1854. def test_W20():
  1855. # integral not calculated
  1856. assert (integrate(x**2*polylog(3, 1/(x + 1)), (x, 0, 1)) ==
  1857. -pi**2/36 - R(17, 108) + zeta(3)/4 +
  1858. (-pi**2/2 - 4*log(2) + log(2)**2 + 35/3)*log(2)/9)
  1859. def test_W21():
  1860. assert abs(N(integrate(x**2*polylog(3, 1/(x + 1)), (x, 0, 1)))
  1861. - 0.210882859565594) < 1e-15
  1862. def test_W22():
  1863. t, u = symbols('t u', real=True)
  1864. s = Lambda(x, Piecewise((1, And(x >= 1, x <= 2)), (0, True)))
  1865. assert integrate(s(t)*cos(t), (t, 0, u)) == Piecewise(
  1866. (0, u < 0),
  1867. (-sin(Min(1, u)) + sin(Min(2, u)), True))
  1868. @slow
  1869. def test_W23():
  1870. a, b = symbols('a b', positive=True)
  1871. r1 = integrate(integrate(x/(x**2 + y**2), (x, a, b)), (y, -oo, oo))
  1872. assert r1.collect(pi).cancel() == -pi*a + pi*b
  1873. def test_W23b():
  1874. # like W23 but limits are reversed
  1875. a, b = symbols('a b', positive=True)
  1876. r2 = integrate(integrate(x/(x**2 + y**2), (y, -oo, oo)), (x, a, b))
  1877. assert r2.collect(pi) == pi*(-a + b)
  1878. @XFAIL
  1879. @slow
  1880. def test_W24():
  1881. if ON_TRAVIS:
  1882. skip("Too slow for travis.")
  1883. # Not that slow, but does not fully evaluate so simplify is slow.
  1884. # Maybe also require doit()
  1885. x, y = symbols('x y', real=True)
  1886. r1 = integrate(integrate(sqrt(x**2 + y**2), (x, 0, 1)), (y, 0, 1))
  1887. assert (r1 - (sqrt(2) + asinh(1))/3).simplify() == 0
  1888. @XFAIL
  1889. @slow
  1890. def test_W25():
  1891. if ON_TRAVIS:
  1892. skip("Too slow for travis.")
  1893. a, x, y = symbols('a x y', real=True)
  1894. i1 = integrate(
  1895. sin(a)*sin(y)/sqrt(1 - sin(a)**2*sin(x)**2*sin(y)**2),
  1896. (x, 0, pi/2))
  1897. i2 = integrate(i1, (y, 0, pi/2))
  1898. assert (i2 - pi*a/2).simplify() == 0
  1899. def test_W26():
  1900. x, y = symbols('x y', real=True)
  1901. assert integrate(integrate(abs(y - x**2), (y, 0, 2)),
  1902. (x, -1, 1)) == R(46, 15)
  1903. def test_W27():
  1904. a, b, c = symbols('a b c')
  1905. assert integrate(integrate(integrate(1, (z, 0, c*(1 - x/a - y/b))),
  1906. (y, 0, b*(1 - x/a))),
  1907. (x, 0, a)) == a*b*c/6
  1908. def test_X1():
  1909. v, c = symbols('v c', real=True)
  1910. assert (series(1/sqrt(1 - (v/c)**2), v, x0=0, n=8) ==
  1911. 5*v**6/(16*c**6) + 3*v**4/(8*c**4) + v**2/(2*c**2) + 1 + O(v**8))
  1912. def test_X2():
  1913. v, c = symbols('v c', real=True)
  1914. s1 = series(1/sqrt(1 - (v/c)**2), v, x0=0, n=8)
  1915. assert (1/s1**2).series(v, x0=0, n=8) == -v**2/c**2 + 1 + O(v**8)
  1916. def test_X3():
  1917. s1 = (sin(x).series()/cos(x).series()).series()
  1918. s2 = tan(x).series()
  1919. assert s2 == x + x**3/3 + 2*x**5/15 + O(x**6)
  1920. assert s1 == s2
  1921. def test_X4():
  1922. s1 = log(sin(x)/x).series()
  1923. assert s1 == -x**2/6 - x**4/180 + O(x**6)
  1924. assert log(series(sin(x)/x)).series() == s1
  1925. @XFAIL
  1926. def test_X5():
  1927. # test case in Mathematica syntax:
  1928. # In[21]:= (* => [a f'(a d) + g(b d) + integrate(h(c y), y = 0..d)]
  1929. # + [a^2 f''(a d) + b g'(b d) + h(c d)] (x - d) *)
  1930. # In[22]:= D[f[a*x], x] + g[b*x] + Integrate[h[c*y], {y, 0, x}]
  1931. # Out[22]= g[b x] + Integrate[h[c y], {y, 0, x}] + a f'[a x]
  1932. # In[23]:= Series[%, {x, d, 1}]
  1933. # Out[23]= (g[b d] + Integrate[h[c y], {y, 0, d}] + a f'[a d]) +
  1934. # 2 2
  1935. # (h[c d] + b g'[b d] + a f''[a d]) (-d + x) + O[-d + x]
  1936. h = Function('h')
  1937. a, b, c, d = symbols('a b c d', real=True)
  1938. # series() raises NotImplementedError:
  1939. # The _eval_nseries method should be added to <class
  1940. # 'sympy.core.function.Subs'> to give terms up to O(x**n) at x=0
  1941. series(diff(f(a*x), x) + g(b*x) + integrate(h(c*y), (y, 0, x)),
  1942. x, x0=d, n=2)
  1943. # assert missing, until exception is removed
  1944. def test_X6():
  1945. # Taylor series of nonscalar objects (noncommutative multiplication)
  1946. # expected result => (B A - A B) t^2/2 + O(t^3) [Stanly Steinberg]
  1947. a, b = symbols('a b', commutative=False, scalar=False)
  1948. assert (series(exp((a + b)*x) - exp(a*x) * exp(b*x), x, x0=0, n=3) ==
  1949. x**2*(-a*b/2 + b*a/2) + O(x**3))
  1950. def test_X7():
  1951. # => sum( Bernoulli[k]/k! x^(k - 2), k = 1..infinity )
  1952. # = 1/x^2 - 1/(2 x) + 1/12 - x^2/720 + x^4/30240 + O(x^6)
  1953. # [Levinson and Redheffer, p. 173]
  1954. assert (series(1/(x*(exp(x) - 1)), x, 0, 7) == x**(-2) - 1/(2*x) +
  1955. R(1, 12) - x**2/720 + x**4/30240 - x**6/1209600 + O(x**7))
  1956. def test_X8():
  1957. # Puiseux series (terms with fractional degree):
  1958. # => 1/sqrt(x - 3/2 pi) + (x - 3/2 pi)^(3/2) / 12 + O([x - 3/2 pi]^(7/2))
  1959. # see issue 7167:
  1960. x = symbols('x', real=True)
  1961. assert (series(sqrt(sec(x)), x, x0=pi*3/2, n=4) ==
  1962. 1/sqrt(x - pi*R(3, 2)) + (x - pi*R(3, 2))**R(3, 2)/12 +
  1963. (x - pi*R(3, 2))**R(7, 2)/160 + O((x - pi*R(3, 2))**4, (x, pi*R(3, 2))))
  1964. def test_X9():
  1965. assert (series(x**x, x, x0=0, n=4) == 1 + x*log(x) + x**2*log(x)**2/2 +
  1966. x**3*log(x)**3/6 + O(x**4*log(x)**4))
  1967. def test_X10():
  1968. z, w = symbols('z w')
  1969. assert (series(log(sinh(z)) + log(cosh(z + w)), z, x0=0, n=2) ==
  1970. log(cosh(w)) + log(z) + z*sinh(w)/cosh(w) + O(z**2))
  1971. def test_X11():
  1972. z, w = symbols('z w')
  1973. assert (series(log(sinh(z) * cosh(z + w)), z, x0=0, n=2) ==
  1974. log(cosh(w)) + log(z) + z*sinh(w)/cosh(w) + O(z**2))
  1975. @XFAIL
  1976. def test_X12():
  1977. # Look at the generalized Taylor series around x = 1
  1978. # Result => (x - 1)^a/e^b [1 - (a + 2 b) (x - 1) / 2 + O((x - 1)^2)]
  1979. a, b, x = symbols('a b x', real=True)
  1980. # series returns O(log(x-1)**2)
  1981. # https://github.com/sympy/sympy/issues/7168
  1982. assert (series(log(x)**a*exp(-b*x), x, x0=1, n=2) ==
  1983. (x - 1)**a/exp(b)*(1 - (a + 2*b)*(x - 1)/2 + O((x - 1)**2)))
  1984. def test_X13():
  1985. assert series(sqrt(2*x**2 + 1), x, x0=oo, n=1) == sqrt(2)*x + O(1/x, (x, oo))
  1986. @XFAIL
  1987. def test_X14():
  1988. # Wallis' product => 1/sqrt(pi n) + ... [Knopp, p. 385]
  1989. assert series(1/2**(2*n)*binomial(2*n, n),
  1990. n, x==oo, n=1) == 1/(sqrt(pi)*sqrt(n)) + O(1/x, (x, oo))
  1991. @SKIP("https://github.com/sympy/sympy/issues/7164")
  1992. def test_X15():
  1993. # => 0!/x - 1!/x^2 + 2!/x^3 - 3!/x^4 + O(1/x^5) [Knopp, p. 544]
  1994. x, t = symbols('x t', real=True)
  1995. # raises RuntimeError: maximum recursion depth exceeded
  1996. # https://github.com/sympy/sympy/issues/7164
  1997. # 2019-02-17: Raises
  1998. # PoleError:
  1999. # Asymptotic expansion of Ei around [-oo] is not implemented.
  2000. e1 = integrate(exp(-t)/t, (t, x, oo))
  2001. assert (series(e1, x, x0=oo, n=5) ==
  2002. 6/x**4 + 2/x**3 - 1/x**2 + 1/x + O(x**(-5), (x, oo)))
  2003. def test_X16():
  2004. # Multivariate Taylor series expansion => 1 - (x^2 + 2 x y + y^2)/2 + O(x^4)
  2005. assert (series(cos(x + y), x + y, x0=0, n=4) == 1 - (x + y)**2/2 +
  2006. O(x**4 + x**3*y + x**2*y**2 + x*y**3 + y**4, x, y))
  2007. @XFAIL
  2008. def test_X17():
  2009. # Power series (compute the general formula)
  2010. # (c41) powerseries(log(sin(x)/x), x, 0);
  2011. # /aquarius/data2/opt/local/macsyma_422/library1/trgred.so being loaded.
  2012. # inf
  2013. # ==== i1 2 i1 2 i1
  2014. # \ (- 1) 2 bern(2 i1) x
  2015. # (d41) > ------------------------------
  2016. # / 2 i1 (2 i1)!
  2017. # ====
  2018. # i1 = 1
  2019. # fps does not calculate
  2020. assert fps(log(sin(x)/x)) == \
  2021. Sum((-1)**k*2**(2*k - 1)*bernoulli(2*k)*x**(2*k)/(k*factorial(2*k)), (k, 1, oo))
  2022. @XFAIL
  2023. def test_X18():
  2024. # Power series (compute the general formula). Maple FPS:
  2025. # > FormalPowerSeries(exp(-x)*sin(x), x = 0);
  2026. # infinity
  2027. # ----- (1/2 k) k
  2028. # \ 2 sin(3/4 k Pi) x
  2029. # ) -------------------------
  2030. # / k!
  2031. # -----
  2032. #
  2033. # Now, SymPy returns
  2034. # oo
  2035. # _____
  2036. # \ `
  2037. # \ / k k\
  2038. # \ k |I*(-1 - I) I*(-1 + I) |
  2039. # \ x *|----------- - -----------|
  2040. # / \ 2 2 /
  2041. # / ------------------------------
  2042. # / k!
  2043. # /____,
  2044. # k = 0
  2045. k = Dummy('k')
  2046. assert fps(exp(-x)*sin(x)) == \
  2047. Sum(2**(S.Half*k)*sin(R(3, 4)*k*pi)*x**k/factorial(k), (k, 0, oo))
  2048. @XFAIL
  2049. def test_X19():
  2050. # (c45) /* Derive an explicit Taylor series solution of y as a function of
  2051. # x from the following implicit relation:
  2052. # y = x - 1 + (x - 1)^2/2 + 2/3 (x - 1)^3 + (x - 1)^4 +
  2053. # 17/10 (x - 1)^5 + ...
  2054. # */
  2055. # x = sin(y) + cos(y);
  2056. # Time= 0 msecs
  2057. # (d45) x = sin(y) + cos(y)
  2058. #
  2059. # (c46) taylor_revert(%, y, 7);
  2060. raise NotImplementedError("Solve using series not supported. \
  2061. Inverse Taylor series expansion also not supported")
  2062. @XFAIL
  2063. def test_X20():
  2064. # Pade (rational function) approximation => (2 - x)/(2 + x)
  2065. # > numapprox[pade](exp(-x), x = 0, [1, 1]);
  2066. # bytes used=9019816, alloc=3669344, time=13.12
  2067. # 1 - 1/2 x
  2068. # ---------
  2069. # 1 + 1/2 x
  2070. # mpmath support numeric Pade approximant but there is
  2071. # no symbolic implementation in SymPy
  2072. # https://en.wikipedia.org/wiki/Pad%C3%A9_approximant
  2073. raise NotImplementedError("Symbolic Pade approximant not supported")
  2074. def test_X21():
  2075. """
  2076. Test whether `fourier_series` of x periodical on the [-p, p] interval equals
  2077. `- (2 p / pi) sum( (-1)^n / n sin(n pi x / p), n = 1..infinity )`.
  2078. """
  2079. p = symbols('p', positive=True)
  2080. n = symbols('n', positive=True, integer=True)
  2081. s = fourier_series(x, (x, -p, p))
  2082. # All cosine coefficients are equal to 0
  2083. assert s.an.formula == 0
  2084. # Check for sine coefficients
  2085. assert s.bn.formula.subs(s.bn.variables[0], 0) == 0
  2086. assert s.bn.formula.subs(s.bn.variables[0], n) == \
  2087. -2*p/pi * (-1)**n / n * sin(n*pi*x/p)
  2088. @XFAIL
  2089. def test_X22():
  2090. # (c52) /* => p / 2
  2091. # - (2 p / pi^2) sum( [1 - (-1)^n] cos(n pi x / p) / n^2,
  2092. # n = 1..infinity ) */
  2093. # fourier_series(abs(x), x, p);
  2094. # p
  2095. # (e52) a = -
  2096. # 0 2
  2097. #
  2098. # %nn
  2099. # (2 (- 1) - 2) p
  2100. # (e53) a = ------------------
  2101. # %nn 2 2
  2102. # %pi %nn
  2103. #
  2104. # (e54) b = 0
  2105. # %nn
  2106. #
  2107. # Time= 5290 msecs
  2108. # inf %nn %pi %nn x
  2109. # ==== (2 (- 1) - 2) cos(---------)
  2110. # \ p
  2111. # p > -------------------------------
  2112. # / 2
  2113. # ==== %nn
  2114. # %nn = 1 p
  2115. # (d54) ----------------------------------------- + -
  2116. # 2 2
  2117. # %pi
  2118. raise NotImplementedError("Fourier series not supported")
  2119. def test_Y1():
  2120. t = symbols('t', positive=True)
  2121. w = symbols('w', real=True)
  2122. s = symbols('s')
  2123. F, _, _ = laplace_transform(cos((w - 1)*t), t, s)
  2124. assert F == s/(s**2 + (w - 1)**2)
  2125. def test_Y2():
  2126. t = symbols('t', positive=True)
  2127. w = symbols('w', real=True)
  2128. s = symbols('s')
  2129. f = inverse_laplace_transform(s/(s**2 + (w - 1)**2), s, t)
  2130. assert f == cos(t*(w - 1))
  2131. def test_Y3():
  2132. t = symbols('t', positive=True)
  2133. w = symbols('w', real=True)
  2134. s = symbols('s')
  2135. F, _, _ = laplace_transform(sinh(w*t)*cosh(w*t), t, s)
  2136. assert F == w/(s**2 - 4*w**2)
  2137. def test_Y4():
  2138. t = symbols('t', positive=True)
  2139. s = symbols('s')
  2140. F, _, _ = laplace_transform(erf(3/sqrt(t)), t, s)
  2141. assert F == (1 - exp(-6*sqrt(s)))/s
  2142. def test_Y5_Y6():
  2143. # Solve y'' + y = 4 [H(t - 1) - H(t - 2)], y(0) = 1, y'(0) = 0 where H is the
  2144. # Heaviside (unit step) function (the RHS describes a pulse of magnitude 4 and
  2145. # duration 1). See David A. Sanchez, Richard C. Allen, Jr. and Walter T.
  2146. # Kyner, _Differential Equations: An Introduction_, Addison-Wesley Publishing
  2147. # Company, 1983, p. 211. First, take the Laplace transform of the ODE
  2148. # => s^2 Y(s) - s + Y(s) = 4/s [e^(-s) - e^(-2 s)]
  2149. # where Y(s) is the Laplace transform of y(t)
  2150. t = symbols('t', positive=True)
  2151. s = symbols('s')
  2152. y = Function('y')
  2153. F, _, _ = laplace_transform(diff(y(t), t, 2)
  2154. + y(t)
  2155. - 4*(Heaviside(t - 1)
  2156. - Heaviside(t - 2)), t, s)
  2157. assert (F == s**2*LaplaceTransform(y(t), t, s) - s*y(0) +
  2158. LaplaceTransform(y(t), t, s) - Subs(Derivative(y(t), t), t, 0) -
  2159. 4*exp(-s)/s + 4*exp(-2*s)/s)
  2160. # TODO implement second part of test case
  2161. # Now, solve for Y(s) and then take the inverse Laplace transform
  2162. # => Y(s) = s/(s^2 + 1) + 4 [1/s - s/(s^2 + 1)] [e^(-s) - e^(-2 s)]
  2163. # => y(t) = cos t + 4 {[1 - cos(t - 1)] H(t - 1) - [1 - cos(t - 2)] H(t - 2)}
  2164. @XFAIL
  2165. def test_Y7():
  2166. # What is the Laplace transform of an infinite square wave?
  2167. # => 1/s + 2 sum( (-1)^n e^(- s n a)/s, n = 1..infinity )
  2168. # [Sanchez, Allen and Kyner, p. 213]
  2169. t = symbols('t', positive=True)
  2170. a = symbols('a', real=True)
  2171. s = symbols('s')
  2172. F, _, _ = laplace_transform(1 + 2*Sum((-1)**n*Heaviside(t - n*a),
  2173. (n, 1, oo)), t, s)
  2174. # returns 2*LaplaceTransform(Sum((-1)**n*Heaviside(-a*n + t),
  2175. # (n, 1, oo)), t, s) + 1/s
  2176. # https://github.com/sympy/sympy/issues/7177
  2177. assert F == 2*Sum((-1)**n*exp(-a*n*s)/s, (n, 1, oo)) + 1/s
  2178. @XFAIL
  2179. def test_Y8():
  2180. assert fourier_transform(1, x, z) == DiracDelta(z)
  2181. def test_Y9():
  2182. assert (fourier_transform(exp(-9*x**2), x, z) ==
  2183. sqrt(pi)*exp(-pi**2*z**2/9)/3)
  2184. def test_Y10():
  2185. assert (fourier_transform(abs(x)*exp(-3*abs(x)), x, z).cancel() ==
  2186. (-8*pi**2*z**2 + 18)/(16*pi**4*z**4 + 72*pi**2*z**2 + 81))
  2187. @SKIP("https://github.com/sympy/sympy/issues/7181")
  2188. @slow
  2189. def test_Y11():
  2190. # => pi cot(pi s) (0 < Re s < 1) [Gradshteyn and Ryzhik 17.43(5)]
  2191. x, s = symbols('x s')
  2192. # raises RuntimeError: maximum recursion depth exceeded
  2193. # https://github.com/sympy/sympy/issues/7181
  2194. # Update 2019-02-17 raises:
  2195. # TypeError: cannot unpack non-iterable MellinTransform object
  2196. F, _, _ = mellin_transform(1/(1 - x), x, s)
  2197. assert F == pi*cot(pi*s)
  2198. @XFAIL
  2199. def test_Y12():
  2200. # => 2^(s - 4) gamma(s/2)/gamma(4 - s/2) (0 < Re s < 1)
  2201. # [Gradshteyn and Ryzhik 17.43(16)]
  2202. x, s = symbols('x s')
  2203. # returns Wrong value -2**(s - 4)*gamma(s/2 - 3)/gamma(-s/2 + 1)
  2204. # https://github.com/sympy/sympy/issues/7182
  2205. F, _, _ = mellin_transform(besselj(3, x)/x**3, x, s)
  2206. assert F == -2**(s - 4)*gamma(s/2)/gamma(-s/2 + 4)
  2207. @XFAIL
  2208. def test_Y13():
  2209. # Z[H(t - m T)] => z/[z^m (z - 1)] (H is the Heaviside (unit step) function) z
  2210. raise NotImplementedError("z-transform not supported")
  2211. @XFAIL
  2212. def test_Y14():
  2213. # Z[H(t - m T)] => z/[z^m (z - 1)] (H is the Heaviside (unit step) function)
  2214. raise NotImplementedError("z-transform not supported")
  2215. def test_Z1():
  2216. r = Function('r')
  2217. assert (rsolve(r(n + 2) - 2*r(n + 1) + r(n) - 2, r(n),
  2218. {r(0): 1, r(1): m}).simplify() == n**2 + n*(m - 2) + 1)
  2219. def test_Z2():
  2220. r = Function('r')
  2221. assert (rsolve(r(n) - (5*r(n - 1) - 6*r(n - 2)), r(n), {r(0): 0, r(1): 1})
  2222. == -2**n + 3**n)
  2223. def test_Z3():
  2224. # => r(n) = Fibonacci[n + 1] [Cohen, p. 83]
  2225. r = Function('r')
  2226. # recurrence solution is correct, Wester expects it to be simplified to
  2227. # fibonacci(n+1), but that is quite hard
  2228. expected = ((S(1)/2 - sqrt(5)/2)**n*(S(1)/2 - sqrt(5)/10)
  2229. + (S(1)/2 + sqrt(5)/2)**n*(sqrt(5)/10 + S(1)/2))
  2230. sol = rsolve(r(n) - (r(n - 1) + r(n - 2)), r(n), {r(1): 1, r(2): 2})
  2231. assert sol == expected
  2232. @XFAIL
  2233. def test_Z4():
  2234. # => [c^(n+1) [c^(n+1) - 2 c - 2] + (n+1) c^2 + 2 c - n] / [(c-1)^3 (c+1)]
  2235. # [Joan Z. Yu and Robert Israel in sci.math.symbolic]
  2236. r = Function('r')
  2237. c = symbols('c')
  2238. # raises ValueError: Polynomial or rational function expected,
  2239. # got '(c**2 - c**n)/(c - c**n)
  2240. s = rsolve(r(n) - ((1 + c - c**(n-1) - c**(n+1))/(1 - c**n)*r(n - 1)
  2241. - c*(1 - c**(n-2))/(1 - c**(n-1))*r(n - 2) + 1),
  2242. r(n), {r(1): 1, r(2): (2 + 2*c + c**2)/(1 + c)})
  2243. assert (s - (c*(n + 1)*(c*(n + 1) - 2*c - 2) +
  2244. (n + 1)*c**2 + 2*c - n)/((c-1)**3*(c+1)) == 0)
  2245. @XFAIL
  2246. def test_Z5():
  2247. # Second order ODE with initial conditions---solve directly
  2248. # transform: f(t) = sin(2 t)/8 - t cos(2 t)/4
  2249. C1, C2 = symbols('C1 C2')
  2250. # initial conditions not supported, this is a manual workaround
  2251. # https://github.com/sympy/sympy/issues/4720
  2252. eq = Derivative(f(x), x, 2) + 4*f(x) - sin(2*x)
  2253. sol = dsolve(eq, f(x))
  2254. f0 = Lambda(x, sol.rhs)
  2255. assert f0(x) == C2*sin(2*x) + (C1 - x/4)*cos(2*x)
  2256. f1 = Lambda(x, diff(f0(x), x))
  2257. # TODO: Replace solve with solveset, when it works for solveset
  2258. const_dict = solve((f0(0), f1(0)))
  2259. result = f0(x).subs(C1, const_dict[C1]).subs(C2, const_dict[C2])
  2260. assert result == -x*cos(2*x)/4 + sin(2*x)/8
  2261. # Result is OK, but ODE solving with initial conditions should be
  2262. # supported without all this manual work
  2263. raise NotImplementedError('ODE solving with initial conditions \
  2264. not supported')
  2265. @XFAIL
  2266. def test_Z6():
  2267. # Second order ODE with initial conditions---solve using Laplace
  2268. # transform: f(t) = sin(2 t)/8 - t cos(2 t)/4
  2269. t = symbols('t', positive=True)
  2270. s = symbols('s')
  2271. eq = Derivative(f(t), t, 2) + 4*f(t) - sin(2*t)
  2272. F, _, _ = laplace_transform(eq, t, s)
  2273. # Laplace transform for diff() not calculated
  2274. # https://github.com/sympy/sympy/issues/7176
  2275. assert (F == s**2*LaplaceTransform(f(t), t, s) +
  2276. 4*LaplaceTransform(f(t), t, s) - 2/(s**2 + 4))
  2277. # rest of test case not implemented