def test_legendre_poly(): pytest.raises(ValueError, lambda: legendre_poly(-1, x)) assert legendre_poly(1, x, polys=True) == x.as_poly() assert legendre_poly(0, x) == 1 assert legendre_poly(1, x) == x assert legendre_poly(2, x) == 3 * x**2 / 2 - Rational(1, 2) assert legendre_poly(3, x) == 5 * x**3 / 2 - 3 * x / 2 assert legendre_poly(4, x) == 35 * x**4 / 8 - 30 * x**2 / 8 + Rational(3, 8) assert legendre_poly(5, x) == 63 * x**5 / 8 - 70 * x**3 / 8 + 15 * x / 8 assert legendre_poly(6, x) == (231 * x**6 / 16 - 315 * x**4 / 16 + 105 * x**2 / 16 - Rational(5, 16)) assert legendre_poly(1, polys=True) == x.as_poly()
def test_RootOf_eval_rational(): p = legendre_poly(4, x, polys=True) roots = [r.eval_rational(Rational(1, 10)**20) for r in p.real_roots()] for r in roots: assert isinstance(r, Rational) # All we know is that the Rational instance will be at most 1/10^20 from # the exact root. So if we evaluate to 17 digits, it must be exactly equal # to: roots = [str(r.n(17)) for r in roots] assert roots == [ "-0.86113631159405258", "-0.33998104358485626", "0.33998104358485626", "0.86113631159405258", ]
def test_RootOf_eval_rational(): p = legendre_poly(4, x, polys=True) roots = [r.eval_rational(Rational(1, 10)**20) for r in p.real_roots()] for r in roots: assert isinstance(r, Rational) # All we know is that the Rational instance will be at most 1/10^20 from # the exact root. So if we evaluate to 17 digits, it must be exactly equal # to: roots = [str(r.evalf(17)) for r in roots] assert roots == [ '-0.86113631159405258', '-0.33998104358485626', '0.33998104358485626', '0.86113631159405258', ] pytest.raises(NotImplementedError, lambda: RootOf(x**3 + x + 3, 1).eval_rational(1e-3))
def test_RootOf_eval_rational(): p = legendre_poly(4, x, polys=True) roots = [r.eval_rational(Rational(1, 10)**20) for r in p.real_roots()] for r in roots: assert isinstance(r, Rational) # All we know is that the Rational instance will be at most 1/10^20 from # the exact root. So if we evaluate to 17 digits, it must be exactly equal # to: roots = [str(r.evalf(17)) for r in roots] assert roots == [ "-0.86113631159405258", "-0.33998104358485626", "0.33998104358485626", "0.86113631159405258", ] pytest.raises(NotImplementedError, lambda: RootOf(x**3 + x + 3, 1).eval_rational(1e-3))
def test_nroots1(): n = 64 p = legendre_poly(n, x, polys=True) pytest.raises(mpmath.mp.NoConvergence, lambda: p.nroots(n=3, maxsteps=5)) roots = p.nroots(n=3) # The order of roots matters. They are ordered from smallest to the # largest. assert [str(r) for r in roots] == \ ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961', '-0.946', '-0.930', '-0.911', '-0.889', '-0.866', '-0.841', '-0.813', '-0.784', '-0.753', '-0.720', '-0.685', '-0.649', '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402', '-0.357', '-0.311', '-0.265', '-0.217', '-0.170', '-0.121', '-0.0730', '-0.0243', '0.0243', '0.0730', '0.121', '0.170', '0.217', '0.265', '0.311', '0.357', '0.402', '0.446', '0.489', '0.531', '0.572', '0.611', '0.649', '0.685', '0.720', '0.753', '0.784', '0.813', '0.841', '0.866', '0.889', '0.911', '0.930', '0.946', '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
def test_RootOf_evalf(): real = RootOf(x**3 + x + 3, 0).evalf(20) assert real.epsilon_eq(Float("-1.2134116627622296341")) re, im = RootOf(x**3 + x + 3, 1).evalf(20).as_real_imag() assert re.epsilon_eq(+Float("0.60670583138111481707")) assert im.epsilon_eq(-Float("1.45061224918844152650")) re, im = RootOf(x**3 + x + 3, 2).evalf(20).as_real_imag() assert re.epsilon_eq(Float("0.60670583138111481707")) assert im.epsilon_eq(Float("1.45061224918844152650")) p = legendre_poly(4, x, polys=True) roots = [str(r.evalf(17)) for r in p.real_roots()] assert roots == [ "-0.86113631159405258", "-0.33998104358485626", "0.33998104358485626", "0.86113631159405258", ] re = RootOf(x**5 - 5 * x + 12, 0).evalf(20) assert re.epsilon_eq(Float("-1.84208596619025438271")) re, im = RootOf(x**5 - 5 * x + 12, 1).evalf(20).as_real_imag() assert re.epsilon_eq(Float("-0.351854240827371999559")) assert im.epsilon_eq(Float("-1.709561043370328882010")) re, im = RootOf(x**5 - 5 * x + 12, 2).evalf(20).as_real_imag() assert re.epsilon_eq(Float("-0.351854240827371999559")) assert im.epsilon_eq(Float("+1.709561043370328882010")) re, im = RootOf(x**5 - 5 * x + 12, 3).evalf(20).as_real_imag() assert re.epsilon_eq(Float("+1.272897223922499190910")) assert im.epsilon_eq(Float("-0.719798681483861386681")) re, im = RootOf(x**5 - 5 * x + 12, 4).evalf(20).as_real_imag() assert re.epsilon_eq(Float("+1.272897223922499190910")) assert im.epsilon_eq(Float("+0.719798681483861386681")) # issue sympy/sympy#6393 assert str(RootOf(x**5 + 2 * x**4 + x**3 - 68719476736, 0).evalf(3)) == '147.' eq = (531441 * x**11 + 3857868 * x**10 + 13730229 * x**9 + 32597882 * x**8 + 55077472 * x**7 + 60452000 * x**6 + 32172064 * x**5 - 4383808 * x**4 - 11942912 * x**3 - 1506304 * x**2 + 1453312 * x + 512) a, b = RootOf(eq, 1).evalf(2).as_real_imag() c, d = RootOf(eq, 2).evalf(2).as_real_imag() assert a == c assert b < d assert b == -d # issue sympy/sympy#6451 r = RootOf(legendre_poly(64, x), 7) assert r.evalf(2) == r.evalf(100).evalf(2) # issue sympy/sympy#8617 ans = [w[x].evalf(2) for w in solve(x**3 - x - 4)] assert RootOf(exp(x)**3 - exp(x) - 4, 0).evalf(2) in ans # issue sympy/sympy#9019 r0 = RootOf(x**2 + 1, 0, radicals=False) r1 = RootOf(x**2 + 1, 1, radicals=False) assert r0.evalf(4, chop=True) == -1.0 * I assert r1.evalf(4, chop=True) == +1.0 * I # make sure verification is used in case a max/min traps the "root" assert str(RootOf(4 * x**5 + 16 * x**3 + 12 * x**2 + 7, 0).evalf(3)) == '-0.976' assert isinstance(RootOf(x**3 + y * x + 1, x, 0).evalf(2), RootOf) assert RootOf(x**3 + I * x + 2, 0).evalf(7) == (Float('-1.260785326', dps=7) + I * Float('0.2684419416', dps=7)) r = RootOf(x**2 - 4456178 * x + 60372201703370, 0, radicals=False) assert r.evalf(2) == Float('2.2282e+6', dps=2) - I * Float('7.4465e+6', dps=2)
def test_RootOf_evalf(): real = RootOf(x**3 + x + 3, 0).evalf(20) assert real.epsilon_eq(Float("-1.2134116627622296341")) re, im = RootOf(x**3 + x + 3, 1).evalf(20).as_real_imag() assert re.epsilon_eq( Float("0.60670583138111481707")) assert im.epsilon_eq(-Float("1.45061224918844152650")) re, im = RootOf(x**3 + x + 3, 2).evalf(20).as_real_imag() assert re.epsilon_eq(Float("0.60670583138111481707")) assert im.epsilon_eq(Float("1.45061224918844152650")) p = legendre_poly(4, x, polys=True) roots = [str(r.evalf(17)) for r in p.real_roots()] assert roots == [ "-0.86113631159405258", "-0.33998104358485626", "0.33998104358485626", "0.86113631159405258", ] re = RootOf(x**5 - 5*x + 12, 0).evalf(20) assert re.epsilon_eq(Float("-1.84208596619025438271")) re, im = RootOf(x**5 - 5*x + 12, 1).evalf(20).as_real_imag() assert re.epsilon_eq(Float("-0.351854240827371999559")) assert im.epsilon_eq(Float("-1.709561043370328882010")) re, im = RootOf(x**5 - 5*x + 12, 2).evalf(20).as_real_imag() assert re.epsilon_eq(Float("-0.351854240827371999559")) assert im.epsilon_eq(Float("+1.709561043370328882010")) re, im = RootOf(x**5 - 5*x + 12, 3).evalf(20).as_real_imag() assert re.epsilon_eq(Float("+1.272897223922499190910")) assert im.epsilon_eq(Float("-0.719798681483861386681")) re, im = RootOf(x**5 - 5*x + 12, 4).evalf(20).as_real_imag() assert re.epsilon_eq(Float("+1.272897223922499190910")) assert im.epsilon_eq(Float("+0.719798681483861386681")) # issue sympy/sympy#6393 assert str(RootOf(x**5 + 2*x**4 + x**3 - 68719476736, 0).evalf(3)) == '147.' eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 + 55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 - 11942912*x**3 - 1506304*x**2 + 1453312*x + 512) a, b = RootOf(eq, 1).evalf(2).as_real_imag() c, d = RootOf(eq, 2).evalf(2).as_real_imag() assert a == c assert b < d assert b == -d # issue sympy/sympy#6451 r = RootOf(legendre_poly(64, x), 7) assert r.evalf(2) == r.evalf(100).evalf(2) # issue sympy/sympy#8617 ans = [w[x].evalf(2) for w in solve(x**3 - x - 4)] assert RootOf(exp(x)**3 - exp(x) - 4, 0).evalf(2) in ans # issue sympy/sympy#9019 r0 = RootOf(x**2 + 1, 0, radicals=False) r1 = RootOf(x**2 + 1, 1, radicals=False) assert r0.evalf(4, chop=True) == -1.0*I assert r1.evalf(4, chop=True) == +1.0*I # make sure verification is used in case a max/min traps the "root" assert str(RootOf(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).evalf(3)) == '-0.976' assert isinstance(RootOf(x**3 + y*x + 1, x, 0).evalf(2), RootOf) assert RootOf(x**3 + I*x + 2, 0).evalf(7) == (Float('-1.260785326', dps=7) + I*Float('0.2684419416', dps=7)) r = RootOf(x**2 - 4456178*x + 60372201703370, 0, radicals=False) assert r.evalf(2) == Float('2.2282e+6', dps=2) - I*Float('7.4465e+6', dps=2)