예제 #1
0
def test_RootOf_diff():
    assert RootOf(x**3 + x + 1, 0).diff(x) == 0
    assert RootOf(x**3 + x + 1, 0).diff(y) == 0

    r = RootOf(x**7 + x * y + 1, x, 0)
    assert r.diff(y) == -r / (y + 7 * r**6)
    assert r.diff(x) == 0
예제 #2
0
def test_RootOf_is_complex():
    assert RootOf(x**3 + x + 3, 0).is_complex is True
    assert RootOf(x**3 + y * x + 3, x, 0).is_complex is None

    assert RootOf(x**3 + y * x + 3, x, 0).is_commutative

    assert RootOf(x**3 + I * x + 2, 0).is_complex is True
예제 #3
0
def test_RootOf_is_real():
    assert RootOf(x**3 + x + 3, 0).is_real is True
    assert RootOf(x**3 + x + 3, 1).is_real is False
    assert RootOf(x**3 + x + 3, 2).is_real is False

    r = RootOf(x**3 + y * x + 1, x, 0)
    assert r.is_real is None
예제 #4
0
def test_diofantissue_730():
    e = RootOf(x**3 + 10 * x**2 + 1, 2)
    assert e.is_real is False
    assert e.is_imaginary is False
    assert e.evalf(3) == Float('0.00498962',
                               dps=3) + I * Float('0.31604', dps=3)
    assert e.conjugate().conjugate() == e
예제 #5
0
def test_RootOf_is_imaginary():
    assert RootOf(x**3 + x + 3, 0).is_imaginary is False
    assert RootOf(x**3 + x + 3, 1).is_imaginary is False
    assert RootOf(x**3 + y * x + 1, x, 0).is_imaginary is None

    assert RootOf(x**3 + I * x + 2, 0).is_real is False

    assert RootOf(x**4 + 10 * x**2 + 1, 2).is_imaginary is True
예제 #6
0
def test_RootOf_evalf_caching_bug():
    r = RootOf(x**5 - 5 * x + 12, 1)
    r.evalf()
    a = r.interval
    r = RootOf(x**5 - 5 * x + 12, 1)
    r.evalf()
    b = r.interval
    assert a == b
예제 #7
0
def test_RootOf_evalf_caching_bug():
    r = RootOf(x**5 - 5 * x + 12, 1)
    r.n()
    a = r._get_interval()
    r = RootOf(x**5 - 5 * x + 12, 1)
    r.n()
    b = r._get_interval()
    assert a == b
예제 #8
0
def test_RootOf_attributes():
    r = RootOf(x**3 + x + 3, 0)
    assert r.is_number
    assert r.free_symbols == set()

    r = RootOf(x**3 + y * x + 1, x, 0)
    assert isinstance(r, RootOf) and r.expr == x**3 + y * x + 1
    assert r.free_symbols == {y}
    assert r.is_number is False
예제 #9
0
def test_RootOf_attributes():
    r = RootOf(x**3 + x + 3, 0)
    assert r.is_number
    assert r.free_symbols == set()
    # if the following assertion fails then multivariate polynomials
    # are apparently supported and the RootOf.free_symbols routine
    # should be changed to return whatever symbols would not be
    # the PurePoly dummy symbol
    pytest.raises(NotImplementedError,
                  lambda: RootOf(Poly(x**3 + y * x + 1, x), 0))
예제 #10
0
def test_RootOf_power():
    e = RootOf(y**3 - x, y, 0)
    assert e**3 == x
    assert e**2 == Pow(e, 2, evaluate=False)
    e2 = RootOf(y**3 - x * y, y, 0)
    assert e2**3 == Pow(e2, 3, evaluate=False)
    e3 = RootOf(3 * x**5 + 2 * x - 1, 0)
    assert e3**5 == -2 * e3 / 3 + Rational(1, 3)  # issue sympy/sympy#8543
    assert e3**4 == Pow(e3, 4, evaluate=False)
    assert e3**-1 == 3 * e3**4 + 2
예제 #11
0
def test_RootOf_algebraic():
    e = RootOf(sqrt(2)*x**4 + sqrt(2)*x**3 - I*x + sqrt(2), x, 0)
    assert e.interval.as_tuple() == ((Rational(-201, 100), 0),
                                     (Rational(-201, 200), Rational(201, 200)))
    assert e.evalf(7) == Float('-1.22731258', dps=7) + I*Float('0.6094138324', dps=7)

    t = RootOf(x**5 + 4*x + 2, 0)
    e = RootOf(x**4 + t*x + 1, 0)
    assert e.interval.as_tuple() == ((Rational(-201, 200), Rational(-201, 200)),
                                     (Rational(-201, 400), Rational(-201, 400)))
    assert e.evalf(7) == Float('-0.7123350278', dps=7) - I*Float('0.8248345032', dps=7)
예제 #12
0
def test_rewrite():
    r3 = RootOf(x**3 + x - 1, 0)
    assert r3.evalf() == r3.rewrite(Pow).evalf()
    assert r3.rewrite(Pow) == (-1/(3*root(Rational(1, 2) + sqrt(93)/18, 3)) +
                               root(Rational(1, 2) + sqrt(93)/18, 3))
    r4 = RootOf(x**4 - x + 5, 0)
    assert r4.evalf() == r4.rewrite(Pow).evalf()
    r11 = RootOf(x**11 + x - 3, 0)
    assert r11.rewrite(Pow) == r11
예제 #13
0
def test_nfloat():
    from diofant.core.basic import _aresame
    from diofant.polys.rootoftools import RootOf

    x = Symbol("x")
    eq = x**Rational(4, 3) + 4 * x**Rational(1, 3) / 3
    assert _aresame(nfloat(eq),
                    x**Rational(4, 3) + (4.0 / 3) * x**Rational(1, 3))
    assert _aresame(nfloat(eq, exponent=True),
                    x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3))
    eq = x**Rational(4, 3) + 4 * x**(x / 3) / 3
    assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(x / 3))
    big = 12345678901234567890
    # specify precision to match value used in nfloat
    Float_big = Float(big, 15)
    assert _aresame(nfloat(big), Float_big)
    assert _aresame(nfloat(big * x), Float_big * x)
    assert _aresame(nfloat(x**big, exponent=True), x**Float_big)
    assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))}
    assert nfloat({sqrt(2): x}) == {sqrt(2): x}
    assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2)))

    # issue sympy/sympy#6342
    lamda = Symbol('lamda')
    f = x * lamda + lamda**3 * (x / 2 + Rational(1, 2)) + lamda**2 + Rational(
        1, 4)
    assert not any(a.free_symbols for a in solve(f.subs(x, -0.139)))

    # issue sympy/sympy#6632
    assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \
        9.99999999800000e-11

    # issue sympy/sympy#7122
    eq = cos(3 * x**4 + y) * RootOf(x**5 + 3 * x**3 + 1, 0)
    assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
예제 #14
0
def test_solve_univariate_inequality():
    assert isolve(x**2 >= 4, x, relational=False) == Union(Interval(-oo, -2, True),
        Interval(2, oo, False, True))
    assert isolve(x**2 >= 4, x) == Or(And(Le(2, x), Lt(x, oo)), And(Le(x, -2),
        Lt(-oo, x)))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x, relational=False) == \
        Union(Interval(1, 2), Interval(3, oo, False, True))
    assert isolve((x - 1)*(x - 2)*(x - 3) >= 0, x) == \
        Or(And(Le(1, x), Le(x, 2)), And(Le(3, x), Lt(x, oo)))
    # issue sympy/sympy#2785:
    assert isolve(x**3 - 2*x - 1 > 0, x, relational=False) == \
        Union(Interval(-1, -sqrt(5)/2 + Rational(1, 2), True, True),
              Interval(Rational(1, 2) + sqrt(5)/2, oo, True, True))
    # issue sympy/sympy#2794:
    assert isolve(x**3 - x**2 + x - 1 > 0, x, relational=False) == \
        Interval(1, oo, True, True)

    # XXX should be limited in domain, e.g. between 0 and 2*pi
    assert isolve(sin(x) < S.Half, x) == \
        Or(And(-oo < x, x < pi/6), And(5*pi/6 < x, x < oo))
    assert isolve(sin(x) > S.Half, x) == And(pi/6 < x, x < 5*pi/6)

    # numerical testing in valid() is needed
    assert isolve(x**7 - x - 2 > 0, x) == \
        And(RootOf(x**7 - x - 2, 0) < x, x < oo)

    # handle numerator and denominator; although these would be handled as
    # rational inequalities, these test confirm that the right thing is done
    # when the domain is EX (e.g. when 2 is replaced with sqrt(2))
    assert isolve(1/(x - 2) > 0, x) == And(Integer(2) < x, x < oo)
    den = ((x - 1)*(x - 2)).expand()
    assert isolve((x - 1)/den <= 0, x) == \
        Or(And(-oo < x, x < 1), And(Integer(1) < x, x < 2))

    assert isolve(x > oo, x) is S.false
예제 #15
0
def test_pickling_polys_rootoftools():
    f = x**3 + x + 3

    for c in (RootOf, RootOf(f, 0)):
        check(c)

    for c in (RootSum, RootSum(f, Lambda(x, exp(x)))):
        check(c)
예제 #16
0
def test_sympyissue_8235():
    assert reduce_inequalities(x**2 - 1 < 0) == \
        And(Integer(-1) < x, x < Integer(1))
    assert reduce_inequalities(x**2 - 1 <= 0) == \
        And(Integer(-1) <= x, x <= 1)
    assert reduce_inequalities(x**2 - 1 > 0) == \
        Or(And(-oo < x, x < -1), And(x < oo, Integer(1) < x))
    assert reduce_inequalities(x**2 - 1 >= 0) == \
        Or(And(-oo < x, x <= Integer(-1)), And(Integer(1) <= x, x < oo))

    eq = x**8 + x - 9  # we want RootOf solns here
    sol = reduce_inequalities(eq >= 0)
    tru = Or(And(RootOf(eq, 1) <= x, x < oo), And(-oo < x, x <= RootOf(eq, 0)))
    assert sol == tru

    # recast vanilla as real
    assert reduce_inequalities(sqrt((-x + 1)**2) < 1) == And(Integer(0) < x, x < 2)
예제 #17
0
def test_RootOf_expand_func2():
    r0 = RootOf(x**3 + I * x + 2, 0)
    assert expand_func(r0) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 1)
    r1 = RootOf(x**3 + I * x + 2, 1)
    assert expand_func(r1) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 3)
    r2 = RootOf(x**4 + sqrt(2) * x**3 - I * x + 1, 0)
    assert expand_func(r2) == RootOf(
        x**16 - 4 * x**14 + 8 * x**12 - 6 * x**10 + 10 * x**8 + 5 * x**4 +
        2 * x**2 + 1, 1)
    r3 = RootOf(x**3 - I * sqrt(2) * x + 5, 1)
    assert expand_func(r3) == RootOf(x**6 + 10 * x**3 + 2 * x**2 + 25, 2)
예제 #18
0
def test_RootOf_expand_func():
    r0 = RootOf(x**3 + x + 1, 0)
    assert expand_func(r0) == r0
    r0 = RootOf(x**3 + I * x + 2, 0, extension=True)
    assert expand_func(r0) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 1)
    r1 = RootOf(x**3 + I * x + 2, 1, extension=True)
    assert expand_func(r1) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 3)

    e = RootOf(x**4 + sqrt(2) * x**3 - I * x + 1, 0, extension=True)
    assert expand_func(e) == RootOf(
        x**16 - 4 * x**14 + 8 * x**12 - 6 * x**10 + 10 * x**8 + 5 * x**4 +
        2 * x**2 + 1, 1)
예제 #19
0
def test_RootOf_all_roots():
    assert Poly(x**5 + x + 1).all_roots() == [
        RootOf(x**3 - x**2 + 1, 0),
        -Rational(1, 2) - sqrt(3) * I / 2,
        -Rational(1, 2) + sqrt(3) * I / 2,
        RootOf(x**3 - x**2 + 1, 1),
        RootOf(x**3 - x**2 + 1, 2),
    ]

    assert Poly(x**5 + x + 1).all_roots(radicals=False) == [
        RootOf(x**3 - x**2 + 1, 0),
        RootOf(x**2 + x + 1, 0, radicals=False),
        RootOf(x**2 + x + 1, 1, radicals=False),
        RootOf(x**3 - x**2 + 1, 1),
        RootOf(x**3 - x**2 + 1, 2),
    ]
예제 #20
0
def test_RootOf_conjugate():
    p = x**7 + x + 1
    assert RootOf(p, 0).conjugate() == RootOf(p, 0)
    assert RootOf(p, 1).conjugate() == RootOf(p, 2)
    assert RootOf(p, 2).conjugate() == RootOf(p, 1)
    assert RootOf(p, 6).conjugate() == RootOf(p, 5)

    p2 = p*(x - 123)
    assert RootOf(p2, 0).conjugate() == RootOf(p2, 0)
    assert RootOf(p2, 1).conjugate() == RootOf(p2, 1)
    assert RootOf(p2, 2).conjugate() == RootOf(p2, 3)
    assert RootOf(p2, 3).conjugate() == RootOf(p2, 2)
    assert RootOf(p2, 7).conjugate() == RootOf(p2, 6)

    p3 = Poly(x**7 + x*y + 1, x)
    assert RootOf(p3, x, 0).conjugate() == conjugate(RootOf(p3, x, 0),
                                                     evaluate=False)

    p4 = x**12 - 4*x**8 + 2*x**6 + 4*x**4 + 4*x**2 + 1
    r4 = RootOf(p4, 4)
    r5 = RootOf(p4, 5)
    assert r4.conjugate() == r5
    assert r4.evalf() == -r5.evalf()
예제 #21
0
def test_RootOf___eval_Eq__():
    f = Function('f')
    r = RootOf(x**3 + x + 3, 2)
    r1 = RootOf(x**3 + x + 3, 1)
    assert Eq(r, r1) is S.false
    assert Eq(r, r) is S.true
    assert Eq(r, x) is S.false
    assert Eq(r, 0) is S.false
    assert Eq(r, S.Infinity) is S.false
    assert Eq(r, I) is S.false
    assert Eq(r, f(0)) is S.false
    assert Eq(r, f(0)) is S.false
    sol = solve(r.expr)
    for s in sol:
        if s.is_real:
            assert Eq(r, s) is S.false
    r = RootOf(r.expr, 0)
    for s in sol:
        if s.is_real:
            assert Eq(r, s) is S.true
    eq = (x**3 + x + 1)
    assert [Eq(RootOf(eq, i), j) for i in range(3) for j in solve(eq)] == \
        [False, False, True, False, True, False, True, False, False]
    assert Eq(RootOf(eq, 0), 1 + S.ImaginaryUnit) is S.false
예제 #22
0
def test_RootOf___eval_Eq__():
    f = Function('f')
    r = RootOf(x**3 + x + 3, 2)
    r1 = RootOf(x**3 + x + 3, 1)
    assert Eq(r, r1) is false
    assert Eq(r, r) is true
    assert Eq(r, x) is false
    assert Eq(r, 0) is false
    assert Eq(r, oo) is false
    assert Eq(r, I) is false
    assert Eq(r, f(0)) is false
    assert Eq(r, f(0)) is false
    sol = solve(r.expr, x)
    for s in sol:
        if s[x].is_real:
            assert Eq(r, s[x]) is false
    r = RootOf(r.expr, 0)
    for s in sol:
        if s[x].is_real:
            assert Eq(r, s[x]) is true
    eq = x**3 + x + 1
    assert ([Eq(RootOf(eq, i), j[x]) for i in range(3) for j in solve(eq)
             ] == [False, False, True, False, True, False, True, False, False])
    assert Eq(RootOf(eq, 0), 1 + I) is false
예제 #23
0
def test_RootOf_algebraic():
    e = RootOf(sqrt(2) * x**4 + sqrt(2) * x**3 - I * x + sqrt(2), x, 0)
    assert e.interval.as_tuple() == ((Rational(-201,
                                               100), 0), (Rational(-201, 200),
                                                          Rational(201, 200)))
    assert e.evalf(7) == Float('-1.22731258',
                               dps=7) + I * Float('0.6094138324', dps=7)

    t = RootOf(x**5 + 4 * x + 2, 0)
    e = RootOf(x**4 + t * x + 1, 0)
    assert e.interval.as_tuple() == ((Rational(-201, 200), Rational(-201,
                                                                    200)),
                                     (Rational(-201, 400), Rational(-201,
                                                                    400)))
    assert e.evalf(7) == Float('-0.7123350278',
                               dps=7) - I * Float('0.8248345032', dps=7)
예제 #24
0
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))
예제 #25
0
def test_RootOf_all_roots():
    assert Poly(x**5 + x + 1).all_roots() == [
        RootOf(x**3 - x**2 + 1, 0),
        -Rational(1, 2) - sqrt(3) * I / 2,
        -Rational(1, 2) + sqrt(3) * I / 2,
        RootOf(x**3 - x**2 + 1, 1),
        RootOf(x**3 - x**2 + 1, 2),
    ]

    assert Poly(x**5 + x + 1).all_roots(radicals=False) == [
        RootOf(x**3 - x**2 + 1, 0),
        RootOf(x**2 + x + 1, 0, radicals=False),
        RootOf(x**2 + x + 1, 1, radicals=False),
        RootOf(x**3 - x**2 + 1, 1),
        RootOf(x**3 - x**2 + 1, 2),
    ]

    r = Poly((x**3 + x + 20) * (x**3 + x + 21)).all_roots()

    assert r[0].is_real and r[1].is_real
    assert all(not _.is_real for _ in r[2:])

    assert r == [
        RootOf(x**3 + x + 21, 0),
        RootOf(x**3 + x + 20, 0),
        RootOf(x**3 + x + 20, 1),
        RootOf(x**3 + x + 20, 2),
        RootOf(x**3 + x + 21, 1),
        RootOf(x**3 + x + 21, 2)
    ]
예제 #26
0
def test_diofantissue_723():
    p = x**5 + sqrt(3) * x - 2
    for i in range(20):
        for j in (1, 2):
            RootOf(p, j)
예제 #27
0
def test_minpoly_compose():
    # issue 6868
    eq = (-1 / (800 * sqrt(
        Rational(-1, 240) + 1 /
        (18000 *
         (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3)) +
        2 *
        (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3))))
    mp = minimal_polynomial(eq + 3, x)
    assert mp == 8000 * x**2 - 48000 * x + 71999

    # issue 5888
    assert minimal_polynomial(exp(I * pi / 8), x) == x**8 + 1

    mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x)
    assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
        770912*x**4 - 268432*x**2 + 28561
    mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x)
    assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
            232*x - 239
    mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x)
    assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127

    mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x)
    assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
        770912*x**4 - 268432*x**2 + 28561
    mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x)
    assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
            232*x - 239
    mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x)
    assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127

    mp = minimal_polynomial(exp(2 * I * pi / 7), x)
    assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1
    mp = minimal_polynomial(exp(2 * I * pi / 15), x)
    assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1
    mp = minimal_polynomial(cos(2 * pi / 7), x)
    assert mp == 8 * x**3 + 4 * x**2 - 4 * x - 1
    mp = minimal_polynomial(sin(2 * pi / 7), x)
    ex = (5 * cos(2 * pi / 7) - 7) / (9 * cos(pi / 7) - 5 * cos(3 * pi / 7))
    mp = minimal_polynomial(ex, x)
    assert mp == x**3 + 2 * x**2 - x - 1
    assert minimal_polynomial(-1 / (2 * cos(pi / 7)),
                              x) == x**3 + 2 * x**2 - x - 1
    assert minimal_polynomial(sin(2*pi/15), x) == \
            256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1
    assert minimal_polynomial(sin(5 * pi / 14),
                              x) == 8 * x**3 - 4 * x**2 - 4 * x + 1
    assert minimal_polynomial(cos(
        pi / 15), x) == 16 * x**4 + 8 * x**3 - 16 * x**2 - 8 * x + 1

    ex = RootOf(x**3 + x * 4 + 1, 0)
    mp = minimal_polynomial(ex, x)
    assert mp == x**3 + 4 * x + 1
    mp = minimal_polynomial(ex + 1, x)
    assert mp == x**3 - 3 * x**2 + 7 * x - 4
    assert minimal_polynomial(exp(I * pi / 3), x) == x**2 - x + 1
    assert minimal_polynomial(exp(I * pi / 4), x) == x**4 + 1
    assert minimal_polynomial(exp(I * pi / 6), x) == x**4 - x**2 + 1
    assert minimal_polynomial(exp(I * pi / 9), x) == x**6 - x**3 + 1
    assert minimal_polynomial(exp(I * pi / 10),
                              x) == x**8 - x**6 + x**4 - x**2 + 1
    assert minimal_polynomial(sin(pi / 9),
                              x) == 64 * x**6 - 96 * x**4 + 36 * x**2 - 3
    assert minimal_polynomial(sin(pi/11), x) == 1024*x**10 - 2816*x**8 + \
            2816*x**6 - 1232*x**4 + 220*x**2 - 11

    ex = 2**Rational(1, 3) * exp(Rational(2, 3) * I * pi)
    assert minimal_polynomial(ex, x) == x**3 - 2

    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(cos(pi * sqrt(2)), x))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(sin(pi * sqrt(2)), x))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(exp(I * pi * sqrt(2)), x))

    # issue 5934
    ex = 1 / (-36000 - 7200 * sqrt(5) +
              (12 * sqrt(10) * sqrt(sqrt(5) + 5) +
               24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2) + 1
    pytest.raises(ZeroDivisionError, lambda: minimal_polynomial(ex, x))

    ex = sqrt(1 + 2**Rational(1, 3)) + sqrt(1 + 2**Rational(1, 4)) + sqrt(2)
    mp = minimal_polynomial(ex, x)
    assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576
예제 #28
0
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)
예제 #29
0
def test_RootOf___new__():
    assert RootOf(x, 0) == 0
    assert RootOf(x, -1) == 0

    assert RootOf(x - 1, 0) == 1
    assert RootOf(x - 1, -1) == 1

    assert RootOf(x + 1, 0) == -1
    assert RootOf(x + 1, -1) == -1

    assert RootOf(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2)

    r = RootOf(x**2 + 2*x + 3, 0, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, 1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, -1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, -2, radicals=False)
    assert isinstance(r, RootOf) is True

    assert RootOf((x - 1)*(x + 1), 0, radicals=False) == -1
    assert RootOf((x - 1)*(x + 1), 1, radicals=False) == 1
    assert RootOf((x - 1)*(x + 1), -1, radicals=False) == 1
    assert RootOf((x - 1)*(x + 1), -2, radicals=False) == -1

    assert RootOf((x - 1)*(x + 1), 0, radicals=True) == -1
    assert RootOf((x - 1)*(x + 1), 1, radicals=True) == 1
    assert RootOf((x - 1)*(x + 1), -1, radicals=True) == 1
    assert RootOf((x - 1)*(x + 1), -2, radicals=True) == -1

    assert RootOf((x - 1)*(x**3 + x + 3), 0) == RootOf(x**3 + x + 3, 0)
    assert RootOf((x - 1)*(x**3 + x + 3), 1) == 1
    assert RootOf((x - 1)*(x**3 + x + 3), 2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1)*(x**3 + x + 3), 3) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1)*(x**3 + x + 3), -1) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1)*(x**3 + x + 3), -2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1)*(x**3 + x + 3), -3) == 1
    assert RootOf((x - 1)*(x**3 + x + 3), -4) == RootOf(x**3 + x + 3, 0)

    assert RootOf(x**4 + 3*x**3, 0) == -3
    assert RootOf(x**4 + 3*x**3, 1) == 0
    assert RootOf(x**4 + 3*x**3, 2) == 0
    assert RootOf(x**4 + 3*x**3, 3) == 0

    pytest.raises(GeneratorsNeeded, lambda: RootOf(0, 0))
    pytest.raises(GeneratorsNeeded, lambda: RootOf(1, 0))

    pytest.raises(PolynomialError, lambda: RootOf(Poly(0, x), 0))
    pytest.raises(PolynomialError, lambda: RootOf(Poly(1, x), 0))

    pytest.raises(PolynomialError, lambda: RootOf(x - y, 0))

    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -4))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -3))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 2))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 3))
    pytest.raises(ValueError, lambda: RootOf(x**2 - 1, x))
    pytest.raises(NotImplementedError,
                  lambda: RootOf(Symbol('a', nonzero=False)*x**5 +
                                 2*x - 1, x, 0))
    pytest.raises(NotImplementedError,
                  lambda: Poly(Symbol('a', nonzero=False)*x**5 +
                               2*x - 1, x).all_roots())

    assert RootOf(Poly(x - y, x), 0) == y

    assert RootOf(Poly(x**2 - y, x), 0) == -sqrt(y)
    assert RootOf(Poly(x**2 - y, x), 1) == sqrt(y)

    assert isinstance(RootOf(x**3 - y, x, 0), RootOf)
    p = Symbol('p', positive=True)
    assert RootOf(x**3 - p, x, 0) == root(p, 3)*RootOf(x**3 - 1, 0)

    assert RootOf(y*x**3 + y*x + 2*y, x, 0) == -1

    assert RootOf(x**3 + x + 1, 0).is_commutative is True

    e = RootOf(x**2 - 4, x, 1, evaluate=False)
    assert isinstance(e, RootOf)
    assert e.doit() == 2
    assert e.args == (x**2 - 4, x, 1)
    assert e.poly == PurePoly(x**2 - 4, x)
    assert e.index == 1

    assert RootOf(x**7 - 0.1*x + 1, 0) == RootOf(10*x**7 - x + 10, 0)
예제 #30
0
def test_RootOf_real_roots():
    assert Poly(x**5 + x + 1).real_roots() == [RootOf(x**3 - x**2 + 1, 0)]
    assert Poly(x**5 + x +
                1).real_roots(radicals=False) == [RootOf(x**3 - x**2 + 1, 0)]
    assert Poly(x**7 - 0.1 * x + 1,
                x).real_roots() == [RootOf(10 * x**7 - x + 10, 0)]
예제 #31
0
def test_RootOf_expand_func1():
    r0 = RootOf(x**3 + x + 1, 0)
    assert expand_func(r0) == r0
    r1 = RootOf(x**3 - sqrt(2) * x + I, 1)
    assert expand_func(r1) == RootOf(
        x**12 - 4 * x**8 + 2 * x**6 + 4 * x**4 + 4 * x**2 + 1, 7)
예제 #32
0
def test_rewrite():
    r3 = RootOf(x**3 + x - 1, 0)
    assert r3.evalf() == r3.rewrite(Pow).evalf()
    assert r3.rewrite(Pow) == (-1 /
                               (3 * root(Rational(1, 2) + sqrt(93) / 18, 3)) +
                               root(Rational(1, 2) + sqrt(93) / 18, 3))
    r4 = RootOf(x**4 - x + 5, 0)
    assert r4.evalf() == r4.rewrite(Pow).evalf()
    r11 = RootOf(x**11 + x - 3, 0)
    assert r11.rewrite(Pow) == r11
예제 #33
0
def test_sympyissue_7876():
    l1 = Poly(x**6 - x + 1, x).all_roots()
    l2 = [RootOf(x**6 - x + 1, i) for i in range(6)]
    assert frozenset(l1) == frozenset(l2)
예제 #34
0
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)
예제 #35
0
def test_slow_general_univariate():
    r = RootOf(x**5 - x**2 + 1, 0)
    assert reduce_inequalities(sqrt(x) + 1/root(x, 3) > 1) == \
        Or(And(Integer(0) < x, x < r**6), And(r**6 < x, x < oo))
예제 #36
0
def test_diofantissue_730():
    e = RootOf(x**3 + 10*x**2 + 1, 2)
    assert e.is_real is False
    assert e.is_imaginary is False
    assert e.evalf(3) == Float('0.00498962', dps=3) + I*Float('0.31604', dps=3)
    assert e.conjugate().conjugate() == e