Beispiel #1
0
def test_sympyissue_8438():
    p = Poly([1, y, -2, -3], x).as_expr()
    roots = roots_cubic(Poly(p, x), x)
    z = -Rational(3, 2) - 7*I/2  # this will fail in code given in commit msg
    post = [r.subs({y: z}) for r in roots]
    assert set(post) == set(roots_cubic(Poly(p.subs({y: z}), x)))
    # /!\ if p is not made an expression, this is *very* slow
    assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
Beispiel #2
0
def test_sympyissue_8438():
    p = Poly([1, y, -2, -3], x).as_expr()
    roots = roots_cubic(Poly(p, x), x)
    z = -Rational(3,
                  2) - 7 * I / 2  # this will fail in code given in commit msg
    post = [r.subs({y: z}) for r in roots]
    assert set(post) == set(roots_cubic(Poly(p.subs({y: z}), x)))
    # /!\ if p is not made an expression, this is *very* slow
    assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
Beispiel #3
0
def test_roots_cubic():
    assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2 * x**2 + 3 * x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
Beispiel #4
0
def test_roots_cubic():
    assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2 * x**2 + 3 * x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic(Poly(x**3 + 2 * a / 27, x))
    assert res == [
        -root(a + sqrt(a**2), 3) / 3,
        Mul(Rational(-1, 3),
            Rational(-1, 2) + sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False),
        Mul(Rational(-1, 3),
            Rational(-1, 2) - sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False)
    ]
Beispiel #5
0
def test_roots1():
    assert roots(1) == {}
    assert roots(1, multiple=True) == []
    q = Symbol('q', real=True)
    assert roots(x**3 - q, x) == {cbrt(q): 1,
                                  -cbrt(q)/2 - sqrt(3)*I*cbrt(q)/2: 1,
                                  -cbrt(q)/2 + sqrt(3)*I*cbrt(q)/2: 1}
    assert roots_cubic(Poly(x**3 - 1)) == [1, Rational(-1, 2) + sqrt(3)*I/2,
                                           Rational(-1, 2) - sqrt(3)*I/2]

    assert roots([1, x, y]) == {-x/2 - sqrt(x**2 - 4*y)/2: 1,
                                -x/2 + sqrt(x**2 - 4*y)/2: 1}
    pytest.raises(ValueError, lambda: roots([1, x, y], z))
Beispiel #6
0
def test_roots_cubic():
    assert roots_cubic((2 * x**3).as_poly()) == [0, 0, 0]
    assert roots_cubic((x**3 - 3 * x**2 + 3 * x - 1).as_poly()) == [1, 1, 1]

    assert roots_cubic((x**3 + 1).as_poly()) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic((2*x**3 - 3*x**2 - 3*x - 1).as_poly())[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2 * x**2 + 3 * x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(eq.as_poly(), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic((x**3 + 2 * a / 27).as_poly(x))
    assert res == [
        -root(2, 3) * root(a, 3) / 3,
        -root(2, 3) * root(a, 3) * (-Rational(1, 2) + sqrt(3) * I / 2) / 3,
        -root(2, 3) * root(a, 3) * (-Rational(1, 2) - sqrt(3) * I / 2) / 3
    ]
    res = roots_cubic((x**3 - 2 * a / 27).as_poly(x))
    assert res == [
        root(2, 3) * root(a, 3) / 3,
        root(2, 3) * root(a, 3) * (-Rational(1, 2) + sqrt(3) * I / 2) / 3,
        root(2, 3) * root(a, 3) * (-Rational(1, 2) - sqrt(3) * I / 2) / 3
    ]

    # issue sympy/sympy#8438
    p = -3 * x**3 - 2 * x**2 + x * y + 1
    croots = roots_cubic(p.as_poly(x), x)
    z = -Rational(3,
                  2) - 7 * I / 2  # this will fail in code given in commit msg
    post = [r.subs({y: z}) for r in croots]
    assert set(post) == set(roots_cubic(p.subs({y: z}).as_poly(x)))
    # /!\ if p is not made an expression, this is *very* slow
    assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
Beispiel #7
0
def test_roots1():
    assert roots(1) == {}
    assert roots(1, multiple=True) == []
    q = Symbol('q', real=True)
    assert roots(x**3 - q, x) == {
        cbrt(q): 1,
        -cbrt(q) / 2 - sqrt(3) * I * cbrt(q) / 2: 1,
        -cbrt(q) / 2 + sqrt(3) * I * cbrt(q) / 2: 1
    }
    assert roots_cubic(Poly(x**3 - 1)) == [
        1,
        Rational(-1, 2) + sqrt(3) * I / 2,
        Rational(-1, 2) - sqrt(3) * I / 2
    ]

    assert roots([1, x, y]) == {
        -x / 2 - sqrt(x**2 - 4 * y) / 2: 1,
        -x / 2 + sqrt(x**2 - 4 * y) / 2: 1
    }
    pytest.raises(ValueError, lambda: roots([1, x, y], z))
Beispiel #8
0
def test_roots_cubic():
    assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2 * x**2 + 3 * x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic(Poly(x**3 + 2 * a / 27, x))
    assert res == [
        -root(a + sqrt(a**2), 3) / 3,
        Mul(Rational(-1, 3),
            Rational(-1, 2) + sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False),
        Mul(Rational(-1, 3),
            Rational(-1, 2) - sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False)
    ]

    # issue sympy/sympy#8438
    p = Poly([1, y, -2, -3], x).as_expr()
    croots = roots_cubic(Poly(p, x), x)
    z = -Rational(3,
                  2) - 7 * I / 2  # this will fail in code given in commit msg
    post = [r.subs({y: z}) for r in croots]
    assert set(post) == set(roots_cubic(Poly(p.subs({y: z}), x)))
    # /!\ if p is not made an expression, this is *very* slow
    assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
Beispiel #9
0
def test_roots_cubic():
    assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2*x**2 + 3*x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic(Poly(x**3 + 2*a/27, x))
    assert res == [-root(a + sqrt(a**2), 3)/3,
                   Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3)*I/2,
                       root(a + sqrt(a**2), 3), evaluate=False),
                   Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3)*I/2,
                       root(a + sqrt(a**2), 3), evaluate=False)]