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.

730 lines
25 KiB

6 months ago
  1. """Tests for algorithms for computing symbolic roots of polynomials. """
  2. from sympy.core.numbers import (I, Rational, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import (Symbol, Wild, symbols)
  5. from sympy.functions.elementary.complexes import (conjugate, im, re)
  6. from sympy.functions.elementary.exponential import exp
  7. from sympy.functions.elementary.miscellaneous import (root, sqrt)
  8. from sympy.functions.elementary.piecewise import Piecewise
  9. from sympy.functions.elementary.trigonometric import (acos, cos, sin)
  10. from sympy.polys.domains.integerring import ZZ
  11. from sympy.sets.sets import Interval
  12. from sympy.simplify.powsimp import powsimp
  13. from sympy.polys import Poly, cyclotomic_poly, intervals, nroots, rootof
  14. from sympy.polys.polyroots import (root_factors, roots_linear,
  15. roots_quadratic, roots_cubic, roots_quartic, roots_cyclotomic,
  16. roots_binomial, preprocess_roots, roots)
  17. from sympy.polys.orthopolys import legendre_poly
  18. from sympy.polys.polyerrors import PolynomialError
  19. from sympy.polys.polyutils import _nsort
  20. from sympy.testing.pytest import raises, slow
  21. from sympy.core.random import verify_numerically
  22. import mpmath
  23. from itertools import product
  24. a, b, c, d, e, q, t, x, y, z = symbols('a,b,c,d,e,q,t,x,y,z')
  25. def _check(roots):
  26. # this is the desired invariant for roots returned
  27. # by all_roots. It is trivially true for linear
  28. # polynomials.
  29. nreal = sum([1 if i.is_real else 0 for i in roots])
  30. assert list(sorted(roots[:nreal])) == list(roots[:nreal])
  31. for ix in range(nreal, len(roots), 2):
  32. if not (
  33. roots[ix + 1] == roots[ix] or
  34. roots[ix + 1] == conjugate(roots[ix])):
  35. return False
  36. return True
  37. def test_roots_linear():
  38. assert roots_linear(Poly(2*x + 1, x)) == [Rational(-1, 2)]
  39. def test_roots_quadratic():
  40. assert roots_quadratic(Poly(2*x**2, x)) == [0, 0]
  41. assert roots_quadratic(Poly(2*x**2 + 3*x, x)) == [Rational(-3, 2), 0]
  42. assert roots_quadratic(Poly(2*x**2 + 3, x)) == [-I*sqrt(6)/2, I*sqrt(6)/2]
  43. assert roots_quadratic(Poly(2*x**2 + 4*x + 3, x)) == [-1 - I*sqrt(2)/2, -1 + I*sqrt(2)/2]
  44. _check(Poly(2*x**2 + 4*x + 3, x).all_roots())
  45. f = x**2 + (2*a*e + 2*c*e)/(a - c)*x + (d - b + a*e**2 - c*e**2)/(a - c)
  46. assert roots_quadratic(Poly(f, x)) == \
  47. [-e*(a + c)/(a - c) - sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c),
  48. -e*(a + c)/(a - c) + sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c)]
  49. # check for simplification
  50. f = Poly(y*x**2 - 2*x - 2*y, x)
  51. assert roots_quadratic(f) == \
  52. [-sqrt(2*y**2 + 1)/y + 1/y, sqrt(2*y**2 + 1)/y + 1/y]
  53. f = Poly(x**2 + (-y**2 - 2)*x + y**2 + 1, x)
  54. assert roots_quadratic(f) == \
  55. [1,y**2 + 1]
  56. f = Poly(sqrt(2)*x**2 - 1, x)
  57. r = roots_quadratic(f)
  58. assert r == _nsort(r)
  59. # issue 8255
  60. f = Poly(-24*x**2 - 180*x + 264)
  61. assert [w.n(2) for w in f.all_roots(radicals=True)] == \
  62. [w.n(2) for w in f.all_roots(radicals=False)]
  63. for _a, _b, _c in product((-2, 2), (-2, 2), (0, -1)):
  64. f = Poly(_a*x**2 + _b*x + _c)
  65. roots = roots_quadratic(f)
  66. assert roots == _nsort(roots)
  67. def test_issue_7724():
  68. eq = Poly(x**4*I + x**2 + I, x)
  69. assert roots(eq) == {
  70. sqrt(I/2 + sqrt(5)*I/2): 1,
  71. sqrt(-sqrt(5)*I/2 + I/2): 1,
  72. -sqrt(I/2 + sqrt(5)*I/2): 1,
  73. -sqrt(-sqrt(5)*I/2 + I/2): 1}
  74. def test_issue_8438():
  75. p = Poly([1, y, -2, -3], x).as_expr()
  76. roots = roots_cubic(Poly(p, x), x)
  77. z = Rational(-3, 2) - I*7/2 # this will fail in code given in commit msg
  78. post = [r.subs(y, z) for r in roots]
  79. assert set(post) == \
  80. set(roots_cubic(Poly(p.subs(y, z), x)))
  81. # /!\ if p is not made an expression, this is *very* slow
  82. assert all(p.subs({y: z, x: i}).n(2, chop=True) == 0 for i in post)
  83. def test_issue_8285():
  84. roots = (Poly(4*x**8 - 1, x)*Poly(x**2 + 1)).all_roots()
  85. assert _check(roots)
  86. f = Poly(x**4 + 5*x**2 + 6, x)
  87. ro = [rootof(f, i) for i in range(4)]
  88. roots = Poly(x**4 + 5*x**2 + 6, x).all_roots()
  89. assert roots == ro
  90. assert _check(roots)
  91. # more than 2 complex roots from which to identify the
  92. # imaginary ones
  93. roots = Poly(2*x**8 - 1).all_roots()
  94. assert _check(roots)
  95. assert len(Poly(2*x**10 - 1).all_roots()) == 10 # doesn't fail
  96. def test_issue_8289():
  97. roots = (Poly(x**2 + 2)*Poly(x**4 + 2)).all_roots()
  98. assert _check(roots)
  99. roots = Poly(x**6 + 3*x**3 + 2, x).all_roots()
  100. assert _check(roots)
  101. roots = Poly(x**6 - x + 1).all_roots()
  102. assert _check(roots)
  103. # all imaginary roots with multiplicity of 2
  104. roots = Poly(x**4 + 4*x**2 + 4, x).all_roots()
  105. assert _check(roots)
  106. def test_issue_14291():
  107. assert Poly(((x - 1)**2 + 1)*((x - 1)**2 + 2)*(x - 1)
  108. ).all_roots() == [1, 1 - I, 1 + I, 1 - sqrt(2)*I, 1 + sqrt(2)*I]
  109. p = x**4 + 10*x**2 + 1
  110. ans = [rootof(p, i) for i in range(4)]
  111. assert Poly(p).all_roots() == ans
  112. _check(ans)
  113. def test_issue_13340():
  114. eq = Poly(y**3 + exp(x)*y + x, y, domain='EX')
  115. roots_d = roots(eq)
  116. assert len(roots_d) == 3
  117. def test_issue_14522():
  118. eq = Poly(x**4 + x**3*(16 + 32*I) + x**2*(-285 + 386*I) + x*(-2824 - 448*I) - 2058 - 6053*I, x)
  119. roots_eq = roots(eq)
  120. assert all(eq(r) == 0 for r in roots_eq)
  121. def test_issue_15076():
  122. sol = roots_quartic(Poly(t**4 - 6*t**2 + t/x - 3, t))
  123. assert sol[0].has(x)
  124. def test_issue_16589():
  125. eq = Poly(x**4 - 8*sqrt(2)*x**3 + 4*x**3 - 64*sqrt(2)*x**2 + 1024*x, x)
  126. roots_eq = roots(eq)
  127. assert 0 in roots_eq
  128. def test_roots_cubic():
  129. assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0]
  130. assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1]
  131. # valid for arbitrary y (issue 21263)
  132. r = root(y, 3)
  133. assert roots_cubic(Poly(x**3 - y, x)) == [r,
  134. r*(-S.Half + sqrt(3)*I/2),
  135. r*(-S.Half - sqrt(3)*I/2)]
  136. # simpler form when y is negative
  137. assert roots_cubic(Poly(x**3 - -1, x)) == \
  138. [-1, S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
  139. assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
  140. S.Half + 3**Rational(1, 3)/2 + 3**Rational(2, 3)/2
  141. eq = -x**3 + 2*x**2 + 3*x - 2
  142. assert roots(eq, trig=True, multiple=True) == \
  143. roots_cubic(Poly(eq, x), trig=True) == [
  144. Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
  145. -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
  146. -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
  147. ]
  148. def test_roots_quartic():
  149. assert roots_quartic(Poly(x**4, x)) == [0, 0, 0, 0]
  150. assert roots_quartic(Poly(x**4 + x**3, x)) in [
  151. [-1, 0, 0, 0],
  152. [0, -1, 0, 0],
  153. [0, 0, -1, 0],
  154. [0, 0, 0, -1]
  155. ]
  156. assert roots_quartic(Poly(x**4 - x**3, x)) in [
  157. [1, 0, 0, 0],
  158. [0, 1, 0, 0],
  159. [0, 0, 1, 0],
  160. [0, 0, 0, 1]
  161. ]
  162. lhs = roots_quartic(Poly(x**4 + x, x))
  163. rhs = [S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2, S.Zero, -S.One]
  164. assert sorted(lhs, key=hash) == sorted(rhs, key=hash)
  165. # test of all branches of roots quartic
  166. for i, (a, b, c, d) in enumerate([(1, 2, 3, 0),
  167. (3, -7, -9, 9),
  168. (1, 2, 3, 4),
  169. (1, 2, 3, 4),
  170. (-7, -3, 3, -6),
  171. (-3, 5, -6, -4),
  172. (6, -5, -10, -3)]):
  173. if i == 2:
  174. c = -a*(a**2/S(8) - b/S(2))
  175. elif i == 3:
  176. d = a*(a*(a**2*Rational(3, 256) - b/S(16)) + c/S(4))
  177. eq = x**4 + a*x**3 + b*x**2 + c*x + d
  178. ans = roots_quartic(Poly(eq, x))
  179. assert all(eq.subs(x, ai).n(chop=True) == 0 for ai in ans)
  180. # not all symbolic quartics are unresolvable
  181. eq = Poly(q*x + q/4 + x**4 + x**3 + 2*x**2 - Rational(1, 3), x)
  182. sol = roots_quartic(eq)
  183. assert all(verify_numerically(eq.subs(x, i), 0) for i in sol)
  184. z = symbols('z', negative=True)
  185. eq = x**4 + 2*x**3 + 3*x**2 + x*(z + 11) + 5
  186. zans = roots_quartic(Poly(eq, x))
  187. assert all([verify_numerically(eq.subs(((x, i), (z, -1))), 0) for i in zans])
  188. # but some are (see also issue 4989)
  189. # it's ok if the solution is not Piecewise, but the tests below should pass
  190. eq = Poly(y*x**4 + x**3 - x + z, x)
  191. ans = roots_quartic(eq)
  192. assert all(type(i) == Piecewise for i in ans)
  193. reps = (
  194. dict(y=Rational(-1, 3), z=Rational(-1, 4)), # 4 real
  195. dict(y=Rational(-1, 3), z=Rational(-1, 2)), # 2 real
  196. dict(y=Rational(-1, 3), z=-2)) # 0 real
  197. for rep in reps:
  198. sol = roots_quartic(Poly(eq.subs(rep), x))
  199. assert all([verify_numerically(w.subs(rep) - s, 0) for w, s in zip(ans, sol)])
  200. def test_issue_21287():
  201. assert not any(isinstance(i, Piecewise) for i in roots_quartic(
  202. Poly(x**4 - x**2*(3 + 5*I) + 2*x*(-1 + I) - 1 + 3*I, x)))
  203. def test_roots_cyclotomic():
  204. assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1]
  205. assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1]
  206. assert roots_cyclotomic(cyclotomic_poly(
  207. 3, x, polys=True)) == [Rational(-1, 2) - I*sqrt(3)/2, Rational(-1, 2) + I*sqrt(3)/2]
  208. assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I]
  209. assert roots_cyclotomic(cyclotomic_poly(
  210. 6, x, polys=True)) == [S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
  211. assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [
  212. -cos(pi/7) - I*sin(pi/7),
  213. -cos(pi/7) + I*sin(pi/7),
  214. -cos(pi*Rational(3, 7)) - I*sin(pi*Rational(3, 7)),
  215. -cos(pi*Rational(3, 7)) + I*sin(pi*Rational(3, 7)),
  216. cos(pi*Rational(2, 7)) - I*sin(pi*Rational(2, 7)),
  217. cos(pi*Rational(2, 7)) + I*sin(pi*Rational(2, 7)),
  218. ]
  219. assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [
  220. -sqrt(2)/2 - I*sqrt(2)/2,
  221. -sqrt(2)/2 + I*sqrt(2)/2,
  222. sqrt(2)/2 - I*sqrt(2)/2,
  223. sqrt(2)/2 + I*sqrt(2)/2,
  224. ]
  225. assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [
  226. -sqrt(3)/2 - I/2,
  227. -sqrt(3)/2 + I/2,
  228. sqrt(3)/2 - I/2,
  229. sqrt(3)/2 + I/2,
  230. ]
  231. assert roots_cyclotomic(
  232. cyclotomic_poly(1, x, polys=True), factor=True) == [1]
  233. assert roots_cyclotomic(
  234. cyclotomic_poly(2, x, polys=True), factor=True) == [-1]
  235. assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \
  236. [-root(-1, 3), -1 + root(-1, 3)]
  237. assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \
  238. [-I, I]
  239. assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \
  240. [-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3]
  241. assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \
  242. [1 - root(-1, 3), root(-1, 3)]
  243. def test_roots_binomial():
  244. assert roots_binomial(Poly(5*x, x)) == [0]
  245. assert roots_binomial(Poly(5*x**4, x)) == [0, 0, 0, 0]
  246. assert roots_binomial(Poly(5*x + 2, x)) == [Rational(-2, 5)]
  247. A = 10**Rational(3, 4)/10
  248. assert roots_binomial(Poly(5*x**4 + 2, x)) == \
  249. [-A - A*I, -A + A*I, A - A*I, A + A*I]
  250. _check(roots_binomial(Poly(x**8 - 2)))
  251. a1 = Symbol('a1', nonnegative=True)
  252. b1 = Symbol('b1', nonnegative=True)
  253. r0 = roots_quadratic(Poly(a1*x**2 + b1, x))
  254. r1 = roots_binomial(Poly(a1*x**2 + b1, x))
  255. assert powsimp(r0[0]) == powsimp(r1[0])
  256. assert powsimp(r0[1]) == powsimp(r1[1])
  257. for a, b, s, n in product((1, 2), (1, 2), (-1, 1), (2, 3, 4, 5)):
  258. if a == b and a != 1: # a == b == 1 is sufficient
  259. continue
  260. p = Poly(a*x**n + s*b)
  261. ans = roots_binomial(p)
  262. assert ans == _nsort(ans)
  263. # issue 8813
  264. assert roots(Poly(2*x**3 - 16*y**3, x)) == {
  265. 2*y*(Rational(-1, 2) - sqrt(3)*I/2): 1,
  266. 2*y: 1,
  267. 2*y*(Rational(-1, 2) + sqrt(3)*I/2): 1}
  268. def test_roots_preprocessing():
  269. f = a*y*x**2 + y - b
  270. coeff, poly = preprocess_roots(Poly(f, x))
  271. assert coeff == 1
  272. assert poly == Poly(a*y*x**2 + y - b, x)
  273. f = c**3*x**3 + c**2*x**2 + c*x + a
  274. coeff, poly = preprocess_roots(Poly(f, x))
  275. assert coeff == 1/c
  276. assert poly == Poly(x**3 + x**2 + x + a, x)
  277. f = c**3*x**3 + c**2*x**2 + a
  278. coeff, poly = preprocess_roots(Poly(f, x))
  279. assert coeff == 1/c
  280. assert poly == Poly(x**3 + x**2 + a, x)
  281. f = c**3*x**3 + c*x + a
  282. coeff, poly = preprocess_roots(Poly(f, x))
  283. assert coeff == 1/c
  284. assert poly == Poly(x**3 + x + a, x)
  285. f = c**3*x**3 + a
  286. coeff, poly = preprocess_roots(Poly(f, x))
  287. assert coeff == 1/c
  288. assert poly == Poly(x**3 + a, x)
  289. E, F, J, L = symbols("E,F,J,L")
  290. f = -21601054687500000000*E**8*J**8/L**16 + \
  291. 508232812500000000*F*x*E**7*J**7/L**14 - \
  292. 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
  293. 16194716250000*E**5*F**3*J**5*x**3/L**10 - \
  294. 27633173750*E**4*F**4*J**4*x**4/L**8 + \
  295. 14840215*E**3*F**5*J**3*x**5/L**6 + \
  296. 54794*E**2*F**6*J**2*x**6/(5*L**4) - \
  297. 1153*E*J*F**7*x**7/(80*L**2) + \
  298. 633*F**8*x**8/160000
  299. coeff, poly = preprocess_roots(Poly(f, x))
  300. assert coeff == 20*E*J/(F*L**2)
  301. assert poly == 633*x**8 - 115300*x**7 + 4383520*x**6 + 296804300*x**5 - 27633173750*x**4 + \
  302. 809735812500*x**3 - 10673859375000*x**2 + 63529101562500*x - 135006591796875
  303. f = Poly(-y**2 + x**2*exp(x), y, domain=ZZ[x, exp(x)])
  304. g = Poly(-y**2 + exp(x), y, domain=ZZ[exp(x)])
  305. assert preprocess_roots(f) == (x, g)
  306. def test_roots0():
  307. assert roots(1, x) == {}
  308. assert roots(x, x) == {S.Zero: 1}
  309. assert roots(x**9, x) == {S.Zero: 9}
  310. assert roots(((x - 2)*(x + 3)*(x - 4)).expand(), x) == {-S(3): 1, S(2): 1, S(4): 1}
  311. assert roots(2*x + 1, x) == {Rational(-1, 2): 1}
  312. assert roots((2*x + 1)**2, x) == {Rational(-1, 2): 2}
  313. assert roots((2*x + 1)**5, x) == {Rational(-1, 2): 5}
  314. assert roots((2*x + 1)**10, x) == {Rational(-1, 2): 10}
  315. assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1}
  316. assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 2, -I: 2}
  317. assert roots(((2*x - 3)**2).expand(), x) == {Rational( 3, 2): 2}
  318. assert roots(((2*x + 3)**2).expand(), x) == {Rational(-3, 2): 2}
  319. assert roots(((2*x - 3)**3).expand(), x) == {Rational( 3, 2): 3}
  320. assert roots(((2*x + 3)**3).expand(), x) == {Rational(-3, 2): 3}
  321. assert roots(((2*x - 3)**5).expand(), x) == {Rational( 3, 2): 5}
  322. assert roots(((2*x + 3)**5).expand(), x) == {Rational(-3, 2): 5}
  323. assert roots(((a*x - b)**5).expand(), x) == { b/a: 5}
  324. assert roots(((a*x + b)**5).expand(), x) == {-b/a: 5}
  325. assert roots(x**2 + (-a - 1)*x + a, x) == {a: 1, S.One: 1}
  326. assert roots(x**4 - 2*x**2 + 1, x) == {S.One: 2, S.NegativeOne: 2}
  327. assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \
  328. {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1}
  329. assert roots(x**8 - 1, x) == {
  330. sqrt(2)/2 + I*sqrt(2)/2: 1,
  331. sqrt(2)/2 - I*sqrt(2)/2: 1,
  332. -sqrt(2)/2 + I*sqrt(2)/2: 1,
  333. -sqrt(2)/2 - I*sqrt(2)/2: 1,
  334. S.One: 1, -S.One: 1, I: 1, -I: 1
  335. }
  336. f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \
  337. 224*x**7 - 384*x**8 - 64*x**9
  338. assert roots(f) == {S.Zero: 2, -S(2): 2, S(2): 1, Rational(-7, 2): 1,
  339. Rational(-3, 2): 1, Rational(-1, 2): 1, Rational(3, 2): 1}
  340. assert roots((a + b + c)*x - (a + b + c + d), x) == {(a + b + c + d)/(a + b + c): 1}
  341. assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {}
  342. assert roots(((x - 2)*(
  343. x + 3)*(x - 4)).expand(), x, cubics=False) == {-S(3): 1, S(2): 1, S(4): 1}
  344. assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \
  345. {-S(3): 1, S(2): 1, S(4): 1, S(5): 1}
  346. assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {-S(2): 1, -2*I: 1, 2*I: 1}
  347. assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \
  348. {-2*I: 1, 2*I: 1, -S(2): 1}
  349. assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \
  350. {S.One: 1, S.Zero: 1, -S(2): 1, -2*I: 1, 2*I: 1}
  351. r1_2, r1_3 = S.Half, Rational(1, 3)
  352. x0 = (3*sqrt(33) + 19)**r1_3
  353. x1 = 4/x0/3
  354. x2 = x0/3
  355. x3 = sqrt(3)*I/2
  356. x4 = x3 - r1_2
  357. x5 = -x3 - r1_2
  358. assert roots(x**3 + x**2 - x + 1, x, cubics=True) == {
  359. -x1 - x2 - r1_3: 1,
  360. -x1/x4 - x2*x4 - r1_3: 1,
  361. -x1/x5 - x2*x5 - r1_3: 1,
  362. }
  363. f = (x**2 + 2*x + 3).subs(x, 2*x**2 + 3*x).subs(x, 5*x - 4)
  364. r13_20, r1_20 = [ Rational(*r)
  365. for r in ((13, 20), (1, 20)) ]
  366. s2 = sqrt(2)
  367. assert roots(f, x) == {
  368. r13_20 + r1_20*sqrt(1 - 8*I*s2): 1,
  369. r13_20 - r1_20*sqrt(1 - 8*I*s2): 1,
  370. r13_20 + r1_20*sqrt(1 + 8*I*s2): 1,
  371. r13_20 - r1_20*sqrt(1 + 8*I*s2): 1,
  372. }
  373. f = x**4 + x**3 + x**2 + x + 1
  374. r1_4, r1_8, r5_8 = [ Rational(*r) for r in ((1, 4), (1, 8), (5, 8)) ]
  375. assert roots(f, x) == {
  376. -r1_4 + r1_4*5**r1_2 + I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
  377. -r1_4 + r1_4*5**r1_2 - I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
  378. -r1_4 - r1_4*5**r1_2 + I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
  379. -r1_4 - r1_4*5**r1_2 - I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
  380. }
  381. f = z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2
  382. assert roots(f, z) == {
  383. S.One: 1,
  384. S.Half + S.Half*y + S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
  385. S.Half + S.Half*y - S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
  386. }
  387. assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=False) == {}
  388. assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=True) != {}
  389. assert roots(x**4 - 1, x, filter='Z') == {S.One: 1, -S.One: 1}
  390. assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1}
  391. assert roots((x - 1)*(x + 1), x) == {S.One: 1, -S.One: 1}
  392. assert roots(
  393. (x - 1)*(x + 1), x, predicate=lambda r: r.is_positive) == {S.One: 1}
  394. assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-S.One, S.One]
  395. assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I]
  396. ar, br = symbols('a, b', real=True)
  397. p = x**2*(ar-br)**2 + 2*x*(br-ar) + 1
  398. assert roots(p, x, filter='R') == {1/(ar - br): 2}
  399. assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero]
  400. assert roots(1234, x, multiple=True) == []
  401. f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1
  402. assert roots(f) == {
  403. -I*sin(pi/7) + cos(pi/7): 1,
  404. -I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
  405. -I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
  406. I*sin(pi/7) + cos(pi/7): 1,
  407. I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
  408. I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
  409. }
  410. g = ((x**2 + 1)*f**2).expand()
  411. assert roots(g) == {
  412. -I*sin(pi/7) + cos(pi/7): 2,
  413. -I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
  414. -I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
  415. I*sin(pi/7) + cos(pi/7): 2,
  416. I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
  417. I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
  418. -I: 1, I: 1,
  419. }
  420. r = roots(x**3 + 40*x + 64)
  421. real_root = [rx for rx in r if rx.is_real][0]
  422. cr = 108 + 6*sqrt(1074)
  423. assert real_root == -2*root(cr, 3)/3 + 20/root(cr, 3)
  424. eq = Poly((7 + 5*sqrt(2))*x**3 + (-6 - 4*sqrt(2))*x**2 + (-sqrt(2) - 1)*x + 2, x, domain='EX')
  425. assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2*sqrt(2): 1, -sqrt(2) + 1: 1}
  426. eq = Poly(41*x**5 + 29*sqrt(2)*x**5 - 153*x**4 - 108*sqrt(2)*x**4 +
  427. 175*x**3 + 125*sqrt(2)*x**3 - 45*x**2 - 30*sqrt(2)*x**2 - 26*sqrt(2)*x -
  428. 26*x + 24, x, domain='EX')
  429. assert roots(eq) == {-sqrt(2) + 1: 1, -2 + 2*sqrt(2): 1, -1 + sqrt(2): 1,
  430. -4 + 4*sqrt(2): 1, -3 + 3*sqrt(2): 1}
  431. eq = Poly(x**3 - 2*x**2 + 6*sqrt(2)*x**2 - 8*sqrt(2)*x + 23*x - 14 +
  432. 14*sqrt(2), x, domain='EX')
  433. assert roots(eq) == {-2*sqrt(2) + 2: 1, -2*sqrt(2) + 1: 1, -2*sqrt(2) - 1: 1}
  434. assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \
  435. {-sqrt(2) + root(7, 3)*(-S.Half - sqrt(3)*I/2): 1,
  436. -sqrt(2) + root(7, 3)*(-S.Half + sqrt(3)*I/2): 1,
  437. -sqrt(2) + root(7, 3): 1}
  438. def test_roots_slow():
  439. """Just test that calculating these roots does not hang. """
  440. a, b, c, d, x = symbols("a,b,c,d,x")
  441. f1 = x**2*c + (a/b) + x*c*d - a
  442. f2 = x**2*(a + b*(c - d)*a) + x*a*b*c/(b*d - d) + (a*d - c/d)
  443. assert list(roots(f1, x).values()) == [1, 1]
  444. assert list(roots(f2, x).values()) == [1, 1]
  445. (zz, yy, xx, zy, zx, yx, k) = symbols("zz,yy,xx,zy,zx,yx,k")
  446. e1 = (zz - k)*(yy - k)*(xx - k) + zy*yx*zx + zx - zy - yx
  447. e2 = (zz - k)*yx*yx + zx*(yy - k)*zx + zy*zy*(xx - k)
  448. assert list(roots(e1 - e2, k).values()) == [1, 1, 1]
  449. f = x**3 + 2*x**2 + 8
  450. R = list(roots(f).keys())
  451. assert not any(i for i in [f.subs(x, ri).n(chop=True) for ri in R])
  452. def test_roots_inexact():
  453. R1 = roots(x**2 + x + 1, x, multiple=True)
  454. R2 = roots(x**2 + x + 1.0, x, multiple=True)
  455. for r1, r2 in zip(R1, R2):
  456. assert abs(r1 - r2) < 1e-12
  457. f = x**4 + 3.0*sqrt(2.0)*x**3 - (78.0 + 24.0*sqrt(3.0))*x**2 \
  458. + 144.0*(2*sqrt(3.0) + 9.0)
  459. R1 = roots(f, multiple=True)
  460. R2 = (-12.7530479110482, -3.85012393732929,
  461. 4.89897948556636, 7.46155167569183)
  462. for r1, r2 in zip(R1, R2):
  463. assert abs(r1 - r2) < 1e-10
  464. def test_roots_preprocessed():
  465. E, F, J, L = symbols("E,F,J,L")
  466. f = -21601054687500000000*E**8*J**8/L**16 + \
  467. 508232812500000000*F*x*E**7*J**7/L**14 - \
  468. 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
  469. 16194716250000*E**5*F**3*J**5*x**3/L**10 - \
  470. 27633173750*E**4*F**4*J**4*x**4/L**8 + \
  471. 14840215*E**3*F**5*J**3*x**5/L**6 + \
  472. 54794*E**2*F**6*J**2*x**6/(5*L**4) - \
  473. 1153*E*J*F**7*x**7/(80*L**2) + \
  474. 633*F**8*x**8/160000
  475. assert roots(f, x) == {}
  476. R1 = roots(f.evalf(), x, multiple=True)
  477. R2 = [-1304.88375606366, 97.1168816800648, 186.946430171876, 245.526792947065,
  478. 503.441004174773, 791.549343830097, 1273.16678129348, 1850.10650616851]
  479. w = Wild('w')
  480. p = w*E*J/(F*L**2)
  481. assert len(R1) == len(R2)
  482. for r1, r2 in zip(R1, R2):
  483. match = r1.match(p)
  484. assert match is not None and abs(match[w] - r2) < 1e-10
  485. def test_roots_mixed():
  486. f = -1936 - 5056*x - 7592*x**2 + 2704*x**3 - 49*x**4
  487. _re, _im = intervals(f, all=True)
  488. _nroots = nroots(f)
  489. _sroots = roots(f, multiple=True)
  490. _re = [ Interval(a, b) for (a, b), _ in _re ]
  491. _im = [ Interval(re(a), re(b))*Interval(im(a), im(b)) for (a, b),
  492. _ in _im ]
  493. _intervals = _re + _im
  494. _sroots = [ r.evalf() for r in _sroots ]
  495. _nroots = sorted(_nroots, key=lambda x: x.sort_key())
  496. _sroots = sorted(_sroots, key=lambda x: x.sort_key())
  497. for _roots in (_nroots, _sroots):
  498. for i, r in zip(_intervals, _roots):
  499. if r.is_real:
  500. assert r in i
  501. else:
  502. assert (re(r), im(r)) in i
  503. def test_root_factors():
  504. assert root_factors(Poly(1, x)) == [Poly(1, x)]
  505. assert root_factors(Poly(x, x)) == [Poly(x, x)]
  506. assert root_factors(x**2 - 1, x) == [x + 1, x - 1]
  507. assert root_factors(x**2 - y, x) == [x - sqrt(y), x + sqrt(y)]
  508. assert root_factors((x**4 - 1)**2) == \
  509. [x + 1, x + 1, x - 1, x - 1, x - I, x - I, x + I, x + I]
  510. assert root_factors(Poly(x**4 - 1, x), filter='Z') == \
  511. [Poly(x + 1, x), Poly(x - 1, x), Poly(x**2 + 1, x)]
  512. assert root_factors(8*x**2 + 12*x**4 + 6*x**6 + x**8, x, filter='Q') == \
  513. [x, x, x**6 + 6*x**4 + 12*x**2 + 8]
  514. @slow
  515. def test_nroots1():
  516. n = 64
  517. p = legendre_poly(n, x, polys=True)
  518. raises(mpmath.mp.NoConvergence, lambda: p.nroots(n=3, maxsteps=5))
  519. roots = p.nroots(n=3)
  520. # The order of roots matters. They are ordered from smallest to the
  521. # largest.
  522. assert [str(r) for r in roots] == \
  523. ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
  524. '-0.946', '-0.930', '-0.911', '-0.889', '-0.866', '-0.841',
  525. '-0.813', '-0.784', '-0.753', '-0.720', '-0.685', '-0.649',
  526. '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
  527. '-0.357', '-0.311', '-0.265', '-0.217', '-0.170', '-0.121',
  528. '-0.0730', '-0.0243', '0.0243', '0.0730', '0.121', '0.170',
  529. '0.217', '0.265', '0.311', '0.357', '0.402', '0.446', '0.489',
  530. '0.531', '0.572', '0.611', '0.649', '0.685', '0.720', '0.753',
  531. '0.784', '0.813', '0.841', '0.866', '0.889', '0.911', '0.930',
  532. '0.946', '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
  533. def test_nroots2():
  534. p = Poly(x**5 + 3*x + 1, x)
  535. roots = p.nroots(n=3)
  536. # The order of roots matters. The roots are ordered by their real
  537. # components (if they agree, then by their imaginary components),
  538. # with real roots appearing first.
  539. assert [str(r) for r in roots] == \
  540. ['-0.332', '-0.839 - 0.944*I', '-0.839 + 0.944*I',
  541. '1.01 - 0.937*I', '1.01 + 0.937*I']
  542. roots = p.nroots(n=5)
  543. assert [str(r) for r in roots] == \
  544. ['-0.33199', '-0.83907 - 0.94385*I', '-0.83907 + 0.94385*I',
  545. '1.0051 - 0.93726*I', '1.0051 + 0.93726*I']
  546. def test_roots_composite():
  547. assert len(roots(Poly(y**3 + y**2*sqrt(x) + y + x, y, composite=True))) == 3
  548. def test_issue_19113():
  549. eq = cos(x)**3 - cos(x) + 1
  550. raises(PolynomialError, lambda: roots(eq))
  551. def test_issue_17454():
  552. assert roots([1, -3*(-4 - 4*I)**2/8 + 12*I, 0], multiple=True) == [0, 0]
  553. def test_issue_20913():
  554. assert Poly(x + 9671406556917067856609794, x).real_roots() == [-9671406556917067856609794]
  555. assert Poly(x**3 + 4, x).real_roots() == [-2**(S(2)/3)]