test_polyroots.py 26 KB

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