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.

597 lines
20 KiB

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