test_rootoftools.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. """Tests for the implementation of RootOf class and related tools. """
  2. from sympy.polys.polytools import Poly
  3. import sympy.polys.rootoftools as rootoftools
  4. from sympy.polys.rootoftools import (rootof, RootOf, CRootOf, RootSum,
  5. _pure_key_dict as D)
  6. from sympy.polys.polyerrors import (
  7. MultivariatePolynomialError,
  8. GeneratorsNeeded,
  9. PolynomialError,
  10. )
  11. from sympy.core.function import (Function, Lambda)
  12. from sympy.core.numbers import (Float, I, Rational)
  13. from sympy.core.relational import Eq
  14. from sympy.core.singleton import S
  15. from sympy.functions.elementary.exponential import (exp, log)
  16. from sympy.functions.elementary.miscellaneous import sqrt
  17. from sympy.functions.elementary.trigonometric import tan
  18. from sympy.integrals.integrals import Integral
  19. from sympy.polys.orthopolys import legendre_poly
  20. from sympy.solvers.solvers import solve
  21. from sympy.testing.pytest import raises, slow
  22. from sympy.core.expr import unchanged
  23. from sympy.abc import a, b, x, y, z, r
  24. def test_CRootOf___new__():
  25. assert rootof(x, 0) == 0
  26. assert rootof(x, -1) == 0
  27. assert rootof(x, S.Zero) == 0
  28. assert rootof(x - 1, 0) == 1
  29. assert rootof(x - 1, -1) == 1
  30. assert rootof(x + 1, 0) == -1
  31. assert rootof(x + 1, -1) == -1
  32. assert rootof(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2)
  33. assert rootof(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2)
  34. assert rootof(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2)
  35. assert rootof(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2)
  36. r = rootof(x**2 + 2*x + 3, 0, radicals=False)
  37. assert isinstance(r, RootOf) is True
  38. r = rootof(x**2 + 2*x + 3, 1, radicals=False)
  39. assert isinstance(r, RootOf) is True
  40. r = rootof(x**2 + 2*x + 3, -1, radicals=False)
  41. assert isinstance(r, RootOf) is True
  42. r = rootof(x**2 + 2*x + 3, -2, radicals=False)
  43. assert isinstance(r, RootOf) is True
  44. assert rootof((x - 1)*(x + 1), 0, radicals=False) == -1
  45. assert rootof((x - 1)*(x + 1), 1, radicals=False) == 1
  46. assert rootof((x - 1)*(x + 1), -1, radicals=False) == 1
  47. assert rootof((x - 1)*(x + 1), -2, radicals=False) == -1
  48. assert rootof((x - 1)*(x + 1), 0, radicals=True) == -1
  49. assert rootof((x - 1)*(x + 1), 1, radicals=True) == 1
  50. assert rootof((x - 1)*(x + 1), -1, radicals=True) == 1
  51. assert rootof((x - 1)*(x + 1), -2, radicals=True) == -1
  52. assert rootof((x - 1)*(x**3 + x + 3), 0) == rootof(x**3 + x + 3, 0)
  53. assert rootof((x - 1)*(x**3 + x + 3), 1) == 1
  54. assert rootof((x - 1)*(x**3 + x + 3), 2) == rootof(x**3 + x + 3, 1)
  55. assert rootof((x - 1)*(x**3 + x + 3), 3) == rootof(x**3 + x + 3, 2)
  56. assert rootof((x - 1)*(x**3 + x + 3), -1) == rootof(x**3 + x + 3, 2)
  57. assert rootof((x - 1)*(x**3 + x + 3), -2) == rootof(x**3 + x + 3, 1)
  58. assert rootof((x - 1)*(x**3 + x + 3), -3) == 1
  59. assert rootof((x - 1)*(x**3 + x + 3), -4) == rootof(x**3 + x + 3, 0)
  60. assert rootof(x**4 + 3*x**3, 0) == -3
  61. assert rootof(x**4 + 3*x**3, 1) == 0
  62. assert rootof(x**4 + 3*x**3, 2) == 0
  63. assert rootof(x**4 + 3*x**3, 3) == 0
  64. raises(GeneratorsNeeded, lambda: rootof(0, 0))
  65. raises(GeneratorsNeeded, lambda: rootof(1, 0))
  66. raises(PolynomialError, lambda: rootof(Poly(0, x), 0))
  67. raises(PolynomialError, lambda: rootof(Poly(1, x), 0))
  68. raises(PolynomialError, lambda: rootof(x - y, 0))
  69. # issue 8617
  70. raises(PolynomialError, lambda: rootof(exp(x), 0))
  71. raises(NotImplementedError, lambda: rootof(x**3 - x + sqrt(2), 0))
  72. raises(NotImplementedError, lambda: rootof(x**3 - x + I, 0))
  73. raises(IndexError, lambda: rootof(x**2 - 1, -4))
  74. raises(IndexError, lambda: rootof(x**2 - 1, -3))
  75. raises(IndexError, lambda: rootof(x**2 - 1, 2))
  76. raises(IndexError, lambda: rootof(x**2 - 1, 3))
  77. raises(ValueError, lambda: rootof(x**2 - 1, x))
  78. assert rootof(Poly(x - y, x), 0) == y
  79. assert rootof(Poly(x**2 - y, x), 0) == -sqrt(y)
  80. assert rootof(Poly(x**2 - y, x), 1) == sqrt(y)
  81. assert rootof(Poly(x**3 - y, x), 0) == y**Rational(1, 3)
  82. assert rootof(y*x**3 + y*x + 2*y, x, 0) == -1
  83. raises(NotImplementedError, lambda: rootof(x**3 + x + 2*y, x, 0))
  84. assert rootof(x**3 + x + 1, 0).is_commutative is True
  85. def test_CRootOf_attributes():
  86. r = rootof(x**3 + x + 3, 0)
  87. assert r.is_number
  88. assert r.free_symbols == set()
  89. # if the following assertion fails then multivariate polynomials
  90. # are apparently supported and the RootOf.free_symbols routine
  91. # should be changed to return whatever symbols would not be
  92. # the PurePoly dummy symbol
  93. raises(NotImplementedError, lambda: rootof(Poly(x**3 + y*x + 1, x), 0))
  94. def test_CRootOf___eq__():
  95. assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 0)) is True
  96. assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 1)) is False
  97. assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 1)) is True
  98. assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 2)) is False
  99. assert (rootof(x**3 + x + 3, 2) == rootof(x**3 + x + 3, 2)) is True
  100. assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 0)) is True
  101. assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 1)) is False
  102. assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 1)) is True
  103. assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 2)) is False
  104. assert (rootof(x**3 + x + 3, 2) == rootof(y**3 + y + 3, 2)) is True
  105. def test_CRootOf___eval_Eq__():
  106. f = Function('f')
  107. eq = x**3 + x + 3
  108. r = rootof(eq, 2)
  109. r1 = rootof(eq, 1)
  110. assert Eq(r, r1) is S.false
  111. assert Eq(r, r) is S.true
  112. assert unchanged(Eq, r, x)
  113. assert Eq(r, 0) is S.false
  114. assert Eq(r, S.Infinity) is S.false
  115. assert Eq(r, I) is S.false
  116. assert unchanged(Eq, r, f(0))
  117. sol = solve(eq)
  118. for s in sol:
  119. if s.is_real:
  120. assert Eq(r, s) is S.false
  121. r = rootof(eq, 0)
  122. for s in sol:
  123. if s.is_real:
  124. assert Eq(r, s) is S.true
  125. eq = x**3 + x + 1
  126. sol = solve(eq)
  127. assert [Eq(rootof(eq, i), j) for i in range(3) for j in sol
  128. ].count(True) == 3
  129. assert Eq(rootof(eq, 0), 1 + S.ImaginaryUnit) == False
  130. def test_CRootOf_is_real():
  131. assert rootof(x**3 + x + 3, 0).is_real is True
  132. assert rootof(x**3 + x + 3, 1).is_real is False
  133. assert rootof(x**3 + x + 3, 2).is_real is False
  134. def test_CRootOf_is_complex():
  135. assert rootof(x**3 + x + 3, 0).is_complex is True
  136. def test_CRootOf_subs():
  137. assert rootof(x**3 + x + 1, 0).subs(x, y) == rootof(y**3 + y + 1, 0)
  138. def test_CRootOf_diff():
  139. assert rootof(x**3 + x + 1, 0).diff(x) == 0
  140. assert rootof(x**3 + x + 1, 0).diff(y) == 0
  141. @slow
  142. def test_CRootOf_evalf():
  143. real = rootof(x**3 + x + 3, 0).evalf(n=20)
  144. assert real.epsilon_eq(Float("-1.2134116627622296341"))
  145. re, im = rootof(x**3 + x + 3, 1).evalf(n=20).as_real_imag()
  146. assert re.epsilon_eq( Float("0.60670583138111481707"))
  147. assert im.epsilon_eq(-Float("1.45061224918844152650"))
  148. re, im = rootof(x**3 + x + 3, 2).evalf(n=20).as_real_imag()
  149. assert re.epsilon_eq(Float("0.60670583138111481707"))
  150. assert im.epsilon_eq(Float("1.45061224918844152650"))
  151. p = legendre_poly(4, x, polys=True)
  152. roots = [str(r.n(17)) for r in p.real_roots()]
  153. # magnitudes are given by
  154. # sqrt(3/S(7) - 2*sqrt(6/S(5))/7)
  155. # and
  156. # sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
  157. assert roots == [
  158. "-0.86113631159405258",
  159. "-0.33998104358485626",
  160. "0.33998104358485626",
  161. "0.86113631159405258",
  162. ]
  163. re = rootof(x**5 - 5*x + 12, 0).evalf(n=20)
  164. assert re.epsilon_eq(Float("-1.84208596619025438271"))
  165. re, im = rootof(x**5 - 5*x + 12, 1).evalf(n=20).as_real_imag()
  166. assert re.epsilon_eq(Float("-0.351854240827371999559"))
  167. assert im.epsilon_eq(Float("-1.709561043370328882010"))
  168. re, im = rootof(x**5 - 5*x + 12, 2).evalf(n=20).as_real_imag()
  169. assert re.epsilon_eq(Float("-0.351854240827371999559"))
  170. assert im.epsilon_eq(Float("+1.709561043370328882010"))
  171. re, im = rootof(x**5 - 5*x + 12, 3).evalf(n=20).as_real_imag()
  172. assert re.epsilon_eq(Float("+1.272897223922499190910"))
  173. assert im.epsilon_eq(Float("-0.719798681483861386681"))
  174. re, im = rootof(x**5 - 5*x + 12, 4).evalf(n=20).as_real_imag()
  175. assert re.epsilon_eq(Float("+1.272897223922499190910"))
  176. assert im.epsilon_eq(Float("+0.719798681483861386681"))
  177. # issue 6393
  178. assert str(rootof(x**5 + 2*x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
  179. eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 +
  180. 55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 -
  181. 11942912*x**3 - 1506304*x**2 + 1453312*x + 512)
  182. a, b = rootof(eq, 1).n(2).as_real_imag()
  183. c, d = rootof(eq, 2).n(2).as_real_imag()
  184. assert a == c
  185. assert b < d
  186. assert b == -d
  187. # issue 6451
  188. r = rootof(legendre_poly(64, x), 7)
  189. assert r.n(2) == r.n(100).n(2)
  190. # issue 9019
  191. r0 = rootof(x**2 + 1, 0, radicals=False)
  192. r1 = rootof(x**2 + 1, 1, radicals=False)
  193. assert r0.n(4) == Float(-1.0, 4) * I
  194. assert r1.n(4) == Float(1.0, 4) * I
  195. # make sure verification is used in case a max/min traps the "root"
  196. assert str(rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).n(3)) == '-0.976'
  197. # watch out for UnboundLocalError
  198. c = CRootOf(90720*x**6 - 4032*x**4 + 84*x**2 - 1, 0)
  199. assert c._eval_evalf(2) # doesn't fail
  200. # watch out for imaginary parts that don't want to evaluate
  201. assert str(RootOf(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
  202. 39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
  203. 877969, 10).n(2)) == '-3.4*I'
  204. assert abs(RootOf(x**4 + 10*x**2 + 1, 0).n(2)) < 0.4
  205. # check reset and args
  206. r = [RootOf(x**3 + x + 3, i) for i in range(3)]
  207. r[0]._reset()
  208. for ri in r:
  209. i = ri._get_interval()
  210. ri.n(2)
  211. assert i != ri._get_interval()
  212. ri._reset()
  213. assert i == ri._get_interval()
  214. assert i == i.func(*i.args)
  215. def test_CRootOf_evalf_caching_bug():
  216. r = rootof(x**5 - 5*x + 12, 1)
  217. r.n()
  218. a = r._get_interval()
  219. r = rootof(x**5 - 5*x + 12, 1)
  220. r.n()
  221. b = r._get_interval()
  222. assert a == b
  223. def test_CRootOf_real_roots():
  224. assert Poly(x**5 + x + 1).real_roots() == [rootof(x**3 - x**2 + 1, 0)]
  225. assert Poly(x**5 + x + 1).real_roots(radicals=False) == [rootof(
  226. x**3 - x**2 + 1, 0)]
  227. # https://github.com/sympy/sympy/issues/20902
  228. p = Poly(-3*x**4 - 10*x**3 - 12*x**2 - 6*x - 1, x, domain='ZZ')
  229. assert CRootOf.real_roots(p) == [S(-1), S(-1), S(-1), S(-1)/3]
  230. def test_CRootOf_all_roots():
  231. assert Poly(x**5 + x + 1).all_roots() == [
  232. rootof(x**3 - x**2 + 1, 0),
  233. Rational(-1, 2) - sqrt(3)*I/2,
  234. Rational(-1, 2) + sqrt(3)*I/2,
  235. rootof(x**3 - x**2 + 1, 1),
  236. rootof(x**3 - x**2 + 1, 2),
  237. ]
  238. assert Poly(x**5 + x + 1).all_roots(radicals=False) == [
  239. rootof(x**3 - x**2 + 1, 0),
  240. rootof(x**2 + x + 1, 0, radicals=False),
  241. rootof(x**2 + x + 1, 1, radicals=False),
  242. rootof(x**3 - x**2 + 1, 1),
  243. rootof(x**3 - x**2 + 1, 2),
  244. ]
  245. def test_CRootOf_eval_rational():
  246. p = legendre_poly(4, x, polys=True)
  247. roots = [r.eval_rational(n=18) for r in p.real_roots()]
  248. for root in roots:
  249. assert isinstance(root, Rational)
  250. roots = [str(root.n(17)) for root in roots]
  251. assert roots == [
  252. "-0.86113631159405258",
  253. "-0.33998104358485626",
  254. "0.33998104358485626",
  255. "0.86113631159405258",
  256. ]
  257. def test_CRootOf_lazy():
  258. # irreducible poly with both real and complex roots:
  259. f = Poly(x**3 + 2*x + 2)
  260. # real root:
  261. CRootOf.clear_cache()
  262. r = CRootOf(f, 0)
  263. # Not yet in cache, after construction:
  264. assert r.poly not in rootoftools._reals_cache
  265. assert r.poly not in rootoftools._complexes_cache
  266. r.evalf()
  267. # In cache after evaluation:
  268. assert r.poly in rootoftools._reals_cache
  269. assert r.poly not in rootoftools._complexes_cache
  270. # complex root:
  271. CRootOf.clear_cache()
  272. r = CRootOf(f, 1)
  273. # Not yet in cache, after construction:
  274. assert r.poly not in rootoftools._reals_cache
  275. assert r.poly not in rootoftools._complexes_cache
  276. r.evalf()
  277. # In cache after evaluation:
  278. assert r.poly in rootoftools._reals_cache
  279. assert r.poly in rootoftools._complexes_cache
  280. # composite poly with both real and complex roots:
  281. f = Poly((x**2 - 2)*(x**2 + 1))
  282. # real root:
  283. CRootOf.clear_cache()
  284. r = CRootOf(f, 0)
  285. # In cache immediately after construction:
  286. assert r.poly in rootoftools._reals_cache
  287. assert r.poly not in rootoftools._complexes_cache
  288. # complex root:
  289. CRootOf.clear_cache()
  290. r = CRootOf(f, 2)
  291. # In cache immediately after construction:
  292. assert r.poly in rootoftools._reals_cache
  293. assert r.poly in rootoftools._complexes_cache
  294. def test_RootSum___new__():
  295. f = x**3 + x + 3
  296. g = Lambda(r, log(r*x))
  297. s = RootSum(f, g)
  298. assert isinstance(s, RootSum) is True
  299. assert RootSum(f**2, g) == 2*RootSum(f, g)
  300. assert RootSum((x - 7)*f**3, g) == log(7*x) + 3*RootSum(f, g)
  301. # issue 5571
  302. assert hash(RootSum((x - 7)*f**3, g)) == hash(log(7*x) + 3*RootSum(f, g))
  303. raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y))
  304. raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x))
  305. assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x)))
  306. assert RootSum(f, log) == RootSum(f, Lambda(x, log(x)))
  307. assert isinstance(RootSum(f, auto=False), RootSum) is True
  308. assert RootSum(f) == 0
  309. assert RootSum(f, Lambda(x, x)) == 0
  310. assert RootSum(f, Lambda(x, x**2)) == -2
  311. assert RootSum(f, Lambda(x, 1)) == 3
  312. assert RootSum(f, Lambda(x, 2)) == 6
  313. assert RootSum(f, auto=False).is_commutative is True
  314. assert RootSum(f, Lambda(x, 1/(x + x**2))) == Rational(11, 3)
  315. assert RootSum(f, Lambda(x, y/(x + x**2))) == Rational(11, 3)*y
  316. assert RootSum(x**2 - 1, Lambda(x, 3*x**2), x) == 6
  317. assert RootSum(x**2 - y, Lambda(x, 3*x**2), x) == 6*y
  318. assert RootSum(x**2 - 1, Lambda(x, z*x**2), x) == 2*z
  319. assert RootSum(x**2 - y, Lambda(x, z*x**2), x) == 2*z*y
  320. assert RootSum(
  321. x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1)
  322. assert RootSum(x**3 + a*x + a**3, tan, x) == \
  323. RootSum(x**3 + x + 1, Lambda(x, tan(a*x)))
  324. assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \
  325. RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
  326. def test_RootSum_free_symbols():
  327. assert RootSum(x**3 + x + 3, Lambda(r, exp(r))).free_symbols == set()
  328. assert RootSum(x**3 + x + 3, Lambda(r, exp(a*r))).free_symbols == {a}
  329. assert RootSum(
  330. x**3 + x + y, Lambda(r, exp(a*r)), x).free_symbols == {a, y}
  331. def test_RootSum___eq__():
  332. f = Lambda(x, exp(x))
  333. assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 1, f)) is True
  334. assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 1, f)) is True
  335. assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 2, f)) is False
  336. assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 2, f)) is False
  337. def test_RootSum_doit():
  338. rs = RootSum(x**2 + 1, exp)
  339. assert isinstance(rs, RootSum) is True
  340. assert rs.doit() == exp(-I) + exp(I)
  341. rs = RootSum(x**2 + a, exp, x)
  342. assert isinstance(rs, RootSum) is True
  343. assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
  344. def test_RootSum_evalf():
  345. rs = RootSum(x**2 + 1, exp)
  346. assert rs.evalf(n=20, chop=True).epsilon_eq(Float("1.0806046117362794348"))
  347. assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628"))
  348. rs = RootSum(x**2 + a, exp, x)
  349. assert rs.evalf() == rs
  350. def test_RootSum_diff():
  351. f = x**3 + x + 3
  352. g = Lambda(r, exp(r*x))
  353. h = Lambda(r, r*exp(r*x))
  354. assert RootSum(f, g).diff(x) == RootSum(f, h)
  355. def test_RootSum_subs():
  356. f = x**3 + x + 3
  357. g = Lambda(r, exp(r*x))
  358. F = y**3 + y + 3
  359. G = Lambda(r, exp(r*y))
  360. assert RootSum(f, g).subs(y, 1) == RootSum(f, g)
  361. assert RootSum(f, g).subs(x, y) == RootSum(F, G)
  362. def test_RootSum_rational():
  363. assert RootSum(
  364. z**5 - z + 1, Lambda(z, z/(x - z))) == (4*x - 5)/(x**5 - x + 1)
  365. f = 161*z**3 + 115*z**2 + 19*z + 1
  366. g = Lambda(z, z*log(
  367. -3381*z**4/4 - 3381*z**3/4 - 625*z**2/2 - z*Rational(125, 2) - 5 + exp(x)))
  368. assert RootSum(f, g).diff(x) == -(
  369. (5*exp(2*x) - 6*exp(x) + 4)*exp(x)/(exp(3*x) - exp(2*x) + 1))/7
  370. def test_RootSum_independent():
  371. f = (x**3 - a)**2*(x**4 - b)**3
  372. g = Lambda(x, 5*tan(x) + 7)
  373. h = Lambda(x, tan(x))
  374. r0 = RootSum(x**3 - a, h, x)
  375. r1 = RootSum(x**4 - b, h, x)
  376. assert RootSum(f, g, x).as_ordered_terms() == [10*r0, 15*r1, 126]
  377. def test_issue_7876():
  378. l1 = Poly(x**6 - x + 1, x).all_roots()
  379. l2 = [rootof(x**6 - x + 1, i) for i in range(6)]
  380. assert frozenset(l1) == frozenset(l2)
  381. def test_issue_8316():
  382. f = Poly(7*x**8 - 9)
  383. assert len(f.all_roots()) == 8
  384. f = Poly(7*x**8 - 10)
  385. assert len(f.all_roots()) == 8
  386. def test__imag_count():
  387. from sympy.polys.rootoftools import _imag_count_of_factor
  388. def imag_count(p):
  389. return sum([_imag_count_of_factor(f)*m for f, m in
  390. p.factor_list()[1]])
  391. assert imag_count(Poly(x**6 + 10*x**2 + 1)) == 2
  392. assert imag_count(Poly(x**2)) == 0
  393. assert imag_count(Poly([1]*3 + [-1], x)) == 0
  394. assert imag_count(Poly(x**3 + 1)) == 0
  395. assert imag_count(Poly(x**2 + 1)) == 2
  396. assert imag_count(Poly(x**2 - 1)) == 0
  397. assert imag_count(Poly(x**4 - 1)) == 2
  398. assert imag_count(Poly(x**4 + 1)) == 0
  399. assert imag_count(Poly([1, 2, 3], x)) == 0
  400. assert imag_count(Poly(x**3 + x + 1)) == 0
  401. assert imag_count(Poly(x**4 + x + 1)) == 0
  402. def q(r1, r2, p):
  403. return Poly(((x - r1)*(x - r2)).subs(x, x**p), x)
  404. assert imag_count(q(-1, -2, 2)) == 4
  405. assert imag_count(q(-1, 2, 2)) == 2
  406. assert imag_count(q(1, 2, 2)) == 0
  407. assert imag_count(q(1, 2, 4)) == 4
  408. assert imag_count(q(-1, 2, 4)) == 2
  409. assert imag_count(q(-1, -2, 4)) == 0
  410. def test_RootOf_is_imaginary():
  411. r = RootOf(x**4 + 4*x**2 + 1, 1)
  412. i = r._get_interval()
  413. assert r.is_imaginary and i.ax*i.bx <= 0
  414. def test_is_disjoint():
  415. eq = x**3 + 5*x + 1
  416. ir = rootof(eq, 0)._get_interval()
  417. ii = rootof(eq, 1)._get_interval()
  418. assert ir.is_disjoint(ii)
  419. assert ii.is_disjoint(ir)
  420. def test_pure_key_dict():
  421. p = D()
  422. assert (x in p) is False
  423. assert (1 in p) is False
  424. p[x] = 1
  425. assert x in p
  426. assert y in p
  427. assert p[y] == 1
  428. raises(KeyError, lambda: p[1])
  429. def dont(k):
  430. p[k] = 2
  431. raises(ValueError, lambda: dont(1))
  432. @slow
  433. def test_eval_approx_relative():
  434. CRootOf.clear_cache()
  435. t = [CRootOf(x**3 + 10*x + 1, i) for i in range(3)]
  436. assert [i.eval_rational(1e-1) for i in t] == [
  437. Rational(-21, 220), Rational(15, 256) - I*805/256,
  438. Rational(15, 256) + I*805/256]
  439. t[0]._reset()
  440. assert [i.eval_rational(1e-1, 1e-4) for i in t] == [
  441. Rational(-21, 220), Rational(3275, 65536) - I*414645/131072,
  442. Rational(3275, 65536) + I*414645/131072]
  443. assert S(t[0]._get_interval().dx) < 1e-1
  444. assert S(t[1]._get_interval().dx) < 1e-1
  445. assert S(t[1]._get_interval().dy) < 1e-4
  446. assert S(t[2]._get_interval().dx) < 1e-1
  447. assert S(t[2]._get_interval().dy) < 1e-4
  448. t[0]._reset()
  449. assert [i.eval_rational(1e-4, 1e-4) for i in t] == [
  450. Rational(-2001, 20020), Rational(6545, 131072) - I*414645/131072,
  451. Rational(6545, 131072) + I*414645/131072]
  452. assert S(t[0]._get_interval().dx) < 1e-4
  453. assert S(t[1]._get_interval().dx) < 1e-4
  454. assert S(t[1]._get_interval().dy) < 1e-4
  455. assert S(t[2]._get_interval().dx) < 1e-4
  456. assert S(t[2]._get_interval().dy) < 1e-4
  457. # in the following, the actual relative precision is
  458. # less than tested, but it should never be greater
  459. t[0]._reset()
  460. assert [i.eval_rational(n=2) for i in t] == [
  461. Rational(-202201, 2024022), Rational(104755, 2097152) - I*6634255/2097152,
  462. Rational(104755, 2097152) + I*6634255/2097152]
  463. assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-2
  464. assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-2
  465. assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-2
  466. assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-2
  467. assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-2
  468. t[0]._reset()
  469. assert [i.eval_rational(n=3) for i in t] == [
  470. Rational(-202201, 2024022), Rational(1676045, 33554432) - I*106148135/33554432,
  471. Rational(1676045, 33554432) + I*106148135/33554432]
  472. assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-3
  473. assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-3
  474. assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-3
  475. assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-3
  476. assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-3
  477. t[0]._reset()
  478. a = [i.eval_approx(2) for i in t]
  479. assert [str(i) for i in a] == [
  480. '-0.10', '0.05 - 3.2*I', '0.05 + 3.2*I']
  481. assert all(abs(((a[i] - t[i])/t[i]).n()) < 1e-2 for i in range(len(a)))
  482. def test_issue_15920():
  483. r = rootof(x**5 - x + 1, 0)
  484. p = Integral(x, (x, 1, y))
  485. assert unchanged(Eq, r, p)
  486. def test_issue_19113():
  487. eq = y**3 - y + 1
  488. # generator is a canonical x in RootOf
  489. assert str(Poly(eq).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]'
  490. assert str(Poly(eq.subs(y, tan(y))).real_roots()
  491. ) == '[CRootOf(x**3 - x + 1, 0)]'
  492. assert str(Poly(eq.subs(y, tan(x))).real_roots()
  493. ) == '[CRootOf(x**3 - x + 1, 0)]'