Exemple #1
0
def test_DifferentialExtension_handle_first():
    assert DifferentialExtension(exp(x)*log(x), x, handle_first='log',
                                 dummy=False)._important_attrs == \
        (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(1/x, t0),
                                        Poly(t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(i))],
         [], [2], [x], [1], [x])
    assert DifferentialExtension(exp(x)*log(x), x, handle_first='exp',
                                 dummy=False)._important_attrs == \
        (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0),
                                        Poly(1/x, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, log(i))],
         [], [1], [x], [2], [x])

    # This one must have the log first, regardless of what we set it to
    # (because the log is inside of the exponential: x**x == exp(x*log(x)))
    assert DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x,
                                 handle_first='exp', dummy=False)._important_attrs == \
        DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x,
                              handle_first='log', dummy=False)._important_attrs == \
        (Poly((-1 + x - x*t0**2)*t1, t1), Poly(x, t1),
            [Poly(1, x), Poly(1/x, t0), Poly((1 + t0)*t1, t1)], [x, t0, t1],
            [Lambda(i, log(i)), Lambda(i, exp(t0*i))], [(exp(x*log(x)), x**x)],
            [2], [t0*x], [1], [x])
Exemple #2
0
def test_sympyissue_13545():
    assert Poly(x + 1, x, modulus=2) + 1 == Poly(x, x, modulus=2)
    pytest.raises(NotImplementedError,
                  lambda: Poly(x, modulus=2) + Poly(x, modulus=3))
Exemple #3
0
def test_sympyissue_5428_14337():
    assert Poly(x**2 + I, x).domain == QQ.algebraic_field(I)
    assert Poly(x**2 + sqrt(2), x).domain == QQ.algebraic_field(sqrt(2))
def test_ratint_logpart():
    assert ratint_logpart(x, x**2 - 9, x, t) == \
        [(Poly(x**2 - 9, x), Poly(2*t - 1, t))]
    assert ratint_logpart(x**2, x**3 - 5, x, t) == \
        [(Poly(x**3 - 5, x), Poly(3*t - 1, t))]
Exemple #5
0
def test_solve_poly_system2():
    assert solve_poly_system((x, y)) == [{x: 0, y: 0}]
    assert (solve_poly_system((x**3 + y**2, )) == [{
        x: r
    } for r in Poly(x**3 + y**2, x).all_roots()])
    assert solve_poly_system((x, y, z)) == [{x: 0, y: 0, z: 0}]
    assert solve_poly_system((x, y, z), x, y, z, t) == [{x: 0, y: 0, z: 0}]
    assert solve_poly_system((x * y - z, y * z - x, x * y - y)) == [{
        x: 0,
        y: 0,
        z: 0
    }, {
        x: 1,
        y: -1,
        z: -1
    }, {
        x: 1,
        y: 1,
        z: 1
    }]

    assert solve_poly_system((x + y, x - y)) == [{x: 0, y: 0}]
    assert solve_poly_system((x + y, 2 * x + 2 * y)) == [{x: -y}]
    assert solve_poly_system((x**2 + y**2, )) == [{x: -I * y}, {x: I * y}]
    assert (solve_poly_system((x**3 * y**2 - 1, )) == [{
        x: r
    } for r in Poly(x**3 * y**2 - 1, x).all_roots()])
    assert (solve_poly_system((x**3 - y**3, )) == [{
        x: y
    }, {
        x:
        y * (Rational(-1, 2) - sqrt(3) * I / 2)
    }, {
        x:
        y * (Rational(-1, 2) + sqrt(3) * I / 2)
    }])
    assert solve_poly_system((y - x, y - x - 1)) == []
    assert (solve_poly_system(
        (x * y - z**2 - z, x**2 + x - y * z, x * z - y**2 - y)) == [{
            x:
            -z / 2 - sqrt(-3 * z**2 - 2 * z + 1) / 2 - Rational(1, 2),
            y:
            -z / 2 + sqrt(Mul(-1, z + 1, 3 * z - 1, evaluate=False)) / 2 -
            Rational(1, 2)
        }, {
            x:
            -z / 2 + sqrt(-3 * z**2 - 2 * z + 1) / 2 - Rational(1, 2),
            y:
            -z / 2 - sqrt(Mul(-1, z + 1, 3 * z - 1, evaluate=False)) / 2 -
            Rational(1, 2)
        }, {
            x: 0,
            y: 0,
            z: 0
        }])

    assert solve_poly_system((x * y * z, )) == [{x: 0}, {y: 0}, {z: 0}]
    assert solve_poly_system((x**2 - 1, (x - 1) * y, (x + 1) * z)) == [{
        x: -1,
        y: 0
    }, {
        x: 1,
        z: 0
    }]
    assert (solve_poly_system((x**2 + y**2 + z**2, x + y - z, y + z**2)) == [{
        x:
        -1,
        y:
        Rational(1, 2) - sqrt(3) * I / 2,
        z:
        Rational(-1, 2) - sqrt(3) * I / 2
    }, {
        x:
        -1,
        y:
        Rational(1, 2) + sqrt(3) * I / 2,
        z:
        Rational(-1, 2) + sqrt(3) * I / 2
    }, {
        x:
        0,
        y:
        0,
        z:
        0
    }])

    assert (solve_poly_system(
        (x * z - 2 * y + 1, y * z - 1 + z, y * z + x * y * z + z)) == [{
            x:
            Rational(-3, 2) - sqrt(7) * I / 2,
            y:
            Rational(1, 4) - sqrt(7) * I / 4,
            z:
            Rational(5, 8) + sqrt(7) * I / 8
        }, {
            x:
            Rational(-3, 2) + sqrt(7) * I / 2,
            y:
            Rational(1, 4) + sqrt(7) * I / 4,
            z:
            Rational(5, 8) - sqrt(7) * I / 8
        }])

    assert solve_poly_system((x**3 * y * z - x * z**2,
                              x * y**2 * z - x * y * z, x**2 * y**2 - z)) == [{
                                  x:
                                  0,
                                  z:
                                  0
                              }, {
                                  x:
                                  -sqrt(z),
                                  y:
                                  1
                              }, {
                                  x:
                                  sqrt(z),
                                  y:
                                  1
                              }, {
                                  y:
                                  0,
                                  z:
                                  0
                              }]
    assert (solve_poly_system(
        (x * y**2 - z - z**2, x**2 * y - y, y**2 - z**2)) == [{
            y: 0,
            z: 0
        }, {
            x:
            -1,
            z:
            Rational(-1, 2),
            y:
            Rational(-1, 2)
        }, {
            x:
            -1,
            z:
            Rational(-1, 2),
            y:
            Rational(1, 2)
        }])

    assert solve_poly_system(
        (z * x - y - x + x * y, y * z - z + x**2 + y * x**2, x - x**2 + y,
         z)) == [{
             x: 0,
             y: 0,
             z: 0
         }]

    assert solve_poly_system(
        (x * y - x * z + y**2, y * z - x**2 + x**2 * y, x - x * y + y)) == [{
            x:
            0,
            y:
            0
        }, {
            x:
            Rational(1, 2) - sqrt(3) * I / 2,
            y:
            Rational(1, 2) + sqrt(3) * I / 2,
            z:
            Rational(-1, 2) + sqrt(3) * I / 2
        }, {
            x:
            Rational(1, 2) + sqrt(3) * I / 2,
            y:
            Rational(1, 2) - sqrt(3) * I / 2,
            z:
            Rational(-1, 2) - sqrt(3) * I / 2
        }]

    assert (solve_poly_system(
        (y * z + x**2 + z, x * y * z + x * z - y**3, x * z + y**2)) == [{
            x: 0,
            y: 0,
            z: 0
        }, {
            x:
            Rational(1, 2),
            y:
            Rational(-1, 2),
            z:
            Rational(-1, 2)
        }, {
            x:
            Rational(-1, 4) - sqrt(3) * I / 4,
            y:
            Rational(-1, 2),
            z:
            Rational(1, 4) - sqrt(3) * I / 4
        }, {
            x:
            Rational(-1, 4) + sqrt(3) * I / 4,
            y:
            Rational(-1, 2),
            z:
            Rational(1, 4) + sqrt(3) * I / 4
        }])
    assert solve_poly_system(
        (x**2 + z**2 * y + y * z, y**2 - z * x + x, x * y + z**2 - 1)) == [{
            x:
            0,
            y:
            0,
            z:
            -1
        }, {
            x: 0,
            y: 0,
            z: 1
        }]
    assert (solve_poly_system(
        (x + y**2 * z - 2 * y**2 + 4 * y - 2 * z - 1, -x + y**2 * z - 1)) == [{
            x: (z**3 - 2 * z**2 * sqrt(z**2 / (z**2 - 2 * z + 1)) - z**2 +
                2 * z * sqrt(z**2 / (z**2 - 2 * z + 1)) + 3 * z - 1) /
            (z**2 - 2 * z + 1),
            y:
            sqrt(z**2 / (z - 1)**2) - 1 / (z - 1)
        }, {
            x: (z**3 + 2 * z**2 * sqrt(z**2 / (z**2 - 2 * z + 1)) - z**2 -
                2 * z * sqrt(z**2 / (z**2 - 2 * z + 1)) + 3 * z - 1) /
            (z**2 - 2 * z + 1),
            y:
            -sqrt(z**2 / (z - 1)**2) - 1 / (z - 1)
        }, {
            x:
            0,
            y:
            1,
            z:
            1
        }])
    assert solve_poly_system((x, y - 1, z)) == [{x: 0, y: 1, z: 0}]

    V = A31, A32, A21, B1, B2, B3, C3, C2 = symbols(
        'A31 A32 A21 B1 B2 B3 C3 C2')
    S = (C2 - A21, C3 - A31 - A32, B1 + B2 + B3 - 1,
         B2 * C2 + B3 * C3 - Rational(1, 2),
         B2 * C2**2 + B3 * C3**2 - Rational(1, 3),
         B3 * A32 * C2 - Rational(1, 6))
    assert (solve_poly_system(S, *V) == [{
        A21:
        C2,
        A31:
        C3 * (3 * C2**2 - 3 * C2 + C3) / (C2 * (3 * C2 - 2)),
        A32:
        C3 * (C2 - C3) / (C2 * (3 * C2 - 2)),
        B1: (6 * C2 * C3 - 3 * C2 - 3 * C3 + 2) / (6 * C2 * C3),
        B2:
        Mul(-1, 3 * C3 - 2, evaluate=False) / (6 * C2 * (C2 - C3)),
        B3: (3 * C2 - 2) / (6 * C3 * (C2 - C3))
    }, {
        A21: Rational(2, 3),
        A31: -1 / (4 * B3),
        A32: 1 / (4 * B3),
        B1: -B3 + Rational(1, 4),
        B2: Rational(3, 4),
        C2: Rational(2, 3),
        C3: 0
    }, {
        A21: Rational(2, 3),
        A31: (8 * B3 - 3) / (12 * B3),
        A32: 1 / (4 * B3),
        B1: Rational(1, 4),
        B2: -B3 + Rational(3, 4),
        C2: Rational(2, 3),
        C3: Rational(2, 3)
    }])

    V = ax, bx, cx, gx, jx, lx, mx, nx, q = symbols(
        'ax bx cx gx jx lx mx nx q')
    S = (ax * q - lx * q - mx, ax - gx * q - lx,
         bx * q**2 + cx * q - jx * q - nx, q * (-ax * q + lx * q + mx),
         q * (-ax + gx * q + lx))
    assert solve_poly_system(S, *V) == [{
        ax: (lx * q + mx) / q,
        bx:
        Mul(-1, cx * q - jx * q - nx, evaluate=False) / q**2,
        gx:
        mx / q**2
    }, {
        ax: lx,
        mx: 0,
        nx: 0,
        q: 0
    }]
Exemple #6
0
def test_sympyissue_14291():
    p = Poly(((x - 1)**2 + 1) * ((x - 1)**2 + 2) * (x - 1))
    assert set(
        p.all_roots()) == {1, 1 - I, 1 + I, 1 - I * sqrt(2), 1 + sqrt(2) * I}
Exemple #7
0
def test_pickling_polys_polytools():
    for c in (Poly, Poly(x, x), PurePoly, PurePoly(x)):
        check(c)

    for c in (GroebnerBasis, GroebnerBasis([x**2 - 1], x)):
        check(c)
Exemple #8
0
def test_Poly():
    assert str(Poly(0, x)) == "Poly(0, x, domain='ZZ')"
    assert str(Poly(1, x)) == "Poly(1, x, domain='ZZ')"
    assert str(Poly(x, x)) == "Poly(x, x, domain='ZZ')"

    assert str(Poly(2 * x + 1, x)) == "Poly(2*x + 1, x, domain='ZZ')"
    assert str(Poly(2 * x - 1, x)) == "Poly(2*x - 1, x, domain='ZZ')"

    assert str(Poly(-1, x)) == "Poly(-1, x, domain='ZZ')"
    assert str(Poly(-x, x)) == "Poly(-x, x, domain='ZZ')"

    assert str(Poly(-2 * x + 1, x)) == "Poly(-2*x + 1, x, domain='ZZ')"
    assert str(Poly(-2 * x - 1, x)) == "Poly(-2*x - 1, x, domain='ZZ')"

    assert str(Poly(x - 1, x)) == "Poly(x - 1, x, domain='ZZ')"

    assert str(Poly(x**2 + 1 + y,
                    x)) == "Poly(x**2 + y + 1, x, domain='ZZ[y]')"
    assert str(Poly(x**2 - 1 + y,
                    x)) == "Poly(x**2 + y - 1, x, domain='ZZ[y]')"

    assert str(Poly(x**2 + I * x, x)) == "Poly(x**2 + I*x, x, domain='QQ<I>')"
    assert str(Poly(x**2 - I * x, x)) == "Poly(x**2 - I*x, x, domain='QQ<I>')"

    assert str(Poly(-x * y * z + x * y - 1, x, y,
                    z)) == "Poly(-x*y*z + x*y - 1, x, y, z, domain='ZZ')"
    assert str(Poly(-w*x**21*y**7*z + (1 + w)*z**3 - 2*x*z + 1, x, y, z)) == \
        "Poly(-w*x**21*y**7*z - 2*x*z + (w + 1)*z**3 + 1, x, y, z, domain='ZZ[w]')"

    assert str(Poly(x**2 + 1, x, modulus=2)) == 'Poly(x**2 + 1, x, modulus=2)'
    assert str(Poly(2 * x**2 + 3 * x + 4, x,
                    modulus=17)) == 'Poly(2*x**2 + 3*x + 4, x, modulus=17)'

    assert str(Poly(2**(2 * x), 2**x)) == "Poly((2**x)**2, 2**x, domain='ZZ')"
    assert str(Poly((x + 1)**2, x + 1,
                    expand=False)) == "Poly((x + 1)**2, x + 1, domain='ZZ')"

    assert str(Poly(y*x*sqrt(3), x, sqrt(3))) == \
        "Poly(y*x*sqrt(3), x, sqrt(3), domain='ZZ[y]')"
Exemple #9
0
def test_gosper_normal():
    assert gosper_normal(4*n + 5, 2*(4*n + 1)*(2*n + 3), n) == \
        (Poly(Rational(1, 4), n), Poly(n + Rational(3, 2)), Poly(n + Rational(1, 4)))
Exemple #10
0
def test_solve_poly_inequality():
    assert psolve(Poly(0, x), '==') == [S.Reals]
    assert psolve(Poly(1, x), '==') == [S.EmptySet]
    assert psolve(PurePoly(x + 1, x), ">") == [Interval(-1, oo, True, True)]
    pytest.raises(ValueError, lambda: psolve(x, '=='))
    pytest.raises(ValueError, lambda: psolve(Poly(x, x), '??'))
Exemple #11
0
def test_apart_list():
    assert apart_list(1) == 1

    w0, w1, w2 = Symbol("w0"), Symbol("w1"), Symbol("w2")
    _a = Dummy("a")

    f = (-2 * x - 2 * x**2) / (3 * x**2 - 6 * x)
    assert (apart_list(
        f, x, dummies=numbered_symbols("w")) == (-1, Poly(Rational(2, 3), x), [
            (Poly(w0 - 2, w0), Lambda(_a, 2), Lambda(_a, -_a + x), 1)
        ]))

    assert (apart_list(2 / (x**2 - 2), x,
                       dummies=numbered_symbols("w")) == (1, Poly(0, x), [
                           (Poly(w0**2 - 2,
                                 w0), Lambda(_a, _a / 2), Lambda(_a,
                                                                 -_a + x), 1)
                       ]))

    f = 36 / (x**5 - 2 * x**4 - 2 * x**3 + 4 * x**2 + x - 2)
    assert (apart_list(f, x,
                       dummies=numbered_symbols("w")) == (1, Poly(0, x), [
                           (Poly(w0 - 2,
                                 w0), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
                           (Poly(w1**2 - 1, w1), Lambda(_a, -3 * _a - 6),
                            Lambda(_a, -_a + x), 2),
                           (Poly(w2 + 1,
                                 w2), Lambda(_a, -4), Lambda(_a, -_a + x), 1)
                       ]))

    f = 1 / (2 * (x - 1)**2)
    assert (apart_list(f, x,
                       dummies=numbered_symbols("w")) == (1, Poly(0, x), [
                           (Poly(2, w0), Lambda(_a, 0), Lambda(_a, x - _a), 1),
                           (Poly(w1 - 1, w1), Lambda(_a, Rational(1, 2)),
                            Lambda(_a, x - _a), 2),
                           (Poly(1, w2), Lambda(_a, 0), Lambda(_a, x - _a), 1)
                       ]))
Exemple #12
0
def test_splitfactor():
    p = Poly(4*x**4*t**5 + (-4*x**3 - 4*x**4)*t**4 + (-3*x**2 + 2*x**3)*t**3 +
             (2*x + 7*x**2 + 2*x**3)*t**2 + (1 - 4*x - 4*x**2)*t - 1 + 2*x, t, field=True)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-t**2 - 3/(2*x)*t + 1/(2*x), t)]})
    assert splitfactor(p, DE) == (Poly(4*x**4*t**3 + (-8*x**3 - 4*x**4)*t**2 +
                                       (4*x**2 + 8*x**3)*t - 4*x**2, t), Poly(t**2 + 1/x*t + (1 - 2*x)/(4*x**2), t, domain='ZZ(x)'))
    assert splitfactor(Poly(x, t), DE) == (Poly(x, t), Poly(1, t))
    r = Poly(-4*x**4*z**2 + 4*x**6*z**2 - z*x**3 - 4*x**5*z**3 + 4*x**3*z**3 + x**4 + z*x**5 - x**6, t)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1/x, t)]})
    assert splitfactor(r, DE, coefficientD=True) == \
        (Poly(x*z - x**2 - z*x**3 + x**4, t), Poly(-x**2 + 4*x**2*z**2, t))
    assert splitfactor_sqf(r, DE, coefficientD=True) == \
        (((Poly(x*z - x**2 - z*x**3 + x**4, t), 1),), ((Poly(-x**2 + 4*x**2*z**2, t), 1),))
    assert splitfactor(Poly(0, t), DE) == (Poly(0, t), Poly(1, t))
    assert splitfactor_sqf(Poly(0, t), DE) == (((Poly(0, t), 1),), ())
Exemple #13
0
def test_derivation():
    p = Poly(4*x**4*t**5 + (-4*x**3 - 4*x**4)*t**4 + (-3*x**2 + 2*x**3)*t**3 +
             (2*x + 7*x**2 + 2*x**3)*t**2 + (1 - 4*x - 4*x**2)*t - 1 + 2*x, t)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-t**2 - 3/(2*x)*t + 1/(2*x), t)]})
    assert derivation(p, DE) == Poly(-20*x**4*t**6 + (2*x**3 + 16*x**4)*t**5 +
                                     (21*x**2 + 12*x**3)*t**4 + (7*x/2 - 25*x**2 - 12*x**3)*t**3 +
                                     (-5 - 15*x/2 + 7*x**2)*t**2 - (3 - 8*x - 10*x**2 - 4*x**3)/(2*x)*t +
                                     (1 - 4*x**2)/(2*x), t)
    assert derivation(Poly(1, t), DE) == Poly(0, t)
    assert derivation(Poly(t, t), DE) == DE.d
    assert derivation(Poly(t**2 + 1/x*t + (1 - 2*x)/(4*x**2), t), DE) == \
        Poly(-2*t**3 - 4/x*t**2 - (5 - 2*x)/(2*x**2)*t - (1 - 2*x)/(2*x**3), t, domain='ZZ(x)')
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1/x, t1), Poly(t, t)]})
    assert derivation(Poly(x*t*t1, t), DE) == Poly(t*t1 + x*t*t1 + t, t)
    assert derivation(Poly(x*t*t1, t), DE, coefficientD=True) == \
        Poly((1 + t1)*t, t)
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert derivation(Poly(x, x), DE) == Poly(1, x)
    # Test basic option
    assert derivation((x + 1)/(x - 1), DE, basic=True) == -2/(1 - 2*x + x**2)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert derivation((t + 1)/(t - 1), DE, basic=True) == -2*t/(1 - 2*t + t**2)
    assert derivation(t + 1, DE, basic=True) == t
Exemple #14
0
def test_DifferentialExtension_misc():
    # Odd ends
    assert DifferentialExtension(sin(y)*exp(x), x, dummy=False)._important_attrs == \
        (Poly(sin(y)*t0, t0, domain='ZZ[sin(y)]'), Poly(1, t0, domain='ZZ'),
         [Poly(1, x, domain='ZZ'), Poly(t0, t0, domain='ZZ')], [x, t0],
         [Lambda(i, exp(i))], [], [1], [x], [], [])
    pytest.raises(NotImplementedError, lambda: DifferentialExtension(sin(x), x))
    assert DifferentialExtension(10**x, x, dummy=False)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(log(10)*t0, t0)], [x, t0],
         [Lambda(i, exp(i*log(10)))], [(exp(x*log(10)), 10**x)], [1], [x*log(10)],
         [], [])
    assert DifferentialExtension(log(x) + log(x**2), x, dummy=False)._important_attrs in [
        (Poly(3*t0, t0), Poly(2, t0), [Poly(1, x), Poly(2/x, t0)], [x, t0],
         [Lambda(i, log(i**2))], [], [], [], [1], [x**2]),
        (Poly(3*t0, t0), Poly(1, t0), [Poly(1, x), Poly(1/x, t0)], [x, t0],
         [Lambda(i, log(i))], [], [], [], [1], [x])]
    assert DifferentialExtension(Integer(0), x, dummy=False)._important_attrs == \
        (Poly(0, x), Poly(1, x), [Poly(1, x)], [x], [], [], [], [], [], [])
Exemple #15
0
def test_roots_composite():
    assert len(roots(Poly(y**3 + y**2 * sqrt(x) + y + x, y,
                          composite=True))) == 3
Exemple #16
0
def test_sympyissue_14293():
    roots = Poly(x**8 + 2 * x**6 + 37 * x**4 - 36 * x**2 + 324).all_roots()
    assert roots == _nsort(roots)
Exemple #17
0
def test_sympyissue_7724():
    e = x**4 * I + x**2 + I
    r1, r2 = roots(e, x), Poly(e, x).all_roots()
    assert len(r1) == 4
    assert {_.evalf() for _ in r1} == {_.evalf() for _ in r2}
Exemple #18
0
def test_roots_quartic():
    assert roots_quartic(Poly(x**4, x)) == [0, 0, 0, 0]
    assert roots_quartic(Poly(x**4 + x**3,
                              x)) in [[-1, 0, 0, 0], [0, -1, 0, 0],
                                      [0, 0, -1, 0], [0, 0, 0, -1]]
    assert roots_quartic(Poly(x**4 - x**3, x)) in [[1, 0, 0, 0], [0, 1, 0, 0],
                                                   [0, 0, 1, 0], [0, 0, 0, 1]]

    lhs = roots_quartic(Poly(x**4 + x, x))
    rhs = [
        Rational(1, 2) + I * sqrt(3) / 2,
        Rational(1, 2) - I * sqrt(3) / 2, 0, -1
    ]

    assert sorted(lhs, key=hash) == sorted(rhs, key=hash)

    # test of all branches of roots quartic
    for i, (a, b, c, d) in enumerate([(1, 2, 3, 0), (3, -7, -9, 9),
                                      (1, 2, 3, 4), (1, 2, 3, 4),
                                      (-7, -3, 3, -6), (-3, 5, -6, -4),
                                      (6, -5, -10, -3)]):
        if i == 2:
            c = -a * (a**2 / Integer(8) - b / Integer(2))
        elif i == 3:
            d = a * (a * (3 * a**2 / Integer(256) - b / Integer(16)) +
                     c / Integer(4))
        eq = x**4 + a * x**3 + b * x**2 + c * x + d
        ans = roots_quartic(Poly(eq, x))
        assert all(eq.subs({x: ai}).evalf(chop=True) == 0 for ai in ans)

    # not all symbolic quartics are unresolvable
    eq = Poly(q * x + q / 4 + x**4 + x**3 + 2 * x**2 - Rational(1, 3), x)
    sol = roots_quartic(eq)
    assert all(verify_numerically(eq.subs({x: i}), 0) for i in sol)
    z = symbols('z', negative=True)
    eq = x**4 + 2 * x**3 + 3 * x**2 + x * (z + 11) + 5
    zans = roots_quartic(Poly(eq, x))
    assert all(verify_numerically(eq.subs({x: i, z: -1}), 0) for i in zans)
    # but some are (see also issue sympy/sympy#4989)
    # it's ok if the solution is not Piecewise, but the tests below should pass
    eq = Poly(y * x**4 + x**3 - x + z, x)
    ans = roots_quartic(eq)
    assert all(type(i) == Piecewise for i in ans)
    reps = (
        {
            y: -Rational(1, 3),
            z: -Rational(1, 4)
        },  # 4 real
        {
            y: -Rational(1, 3),
            z: -Rational(1, 2)
        },  # 2 real
        {
            y: -Rational(1, 3),
            z: -2
        })  # 0 real
    for rep in reps:
        sol = roots_quartic(Poly(eq.subs(rep), x))
        assert all(
            verify_numerically(w.subs(rep) - s, 0) for w, s in zip(ans, sol))
Exemple #19
0
def test_sympify_poly():
    p = Poly(x**2 + x + 1, x)

    assert _sympify(p) is p
    assert sympify(p) is p
Exemple #20
0
def test_roots_quintic():
    assert roots_quintic(Poly(x**5 + x**4 + 1)) == []
    assert roots_quintic(Poly(x**5 - 6 * x + 3)) == []
    assert roots_quintic(Poly(6 * x**5 + 9 * x**3 - 10 * x**2 - 9 * x)) == []
Exemple #21
0
def test_RootOf():
    p = Poly(x**3 + y * x + 1, x)
    assert mathematica_code(RootOf(p, 0)) == 'Root[#^3 + #*y + 1 &, 1]'
Exemple #22
0
def test_roots_linear():
    assert roots_linear(Poly(2 * x + 1, x)) == [-Rational(1, 2)]
Exemple #23
0
def test_log_to_atan():
    f, g = (Poly(x + Rational(1, 2)), Poly(sqrt(3) / 2, x, domain=EX))
    fg_ans = 2 * atan(2 * sqrt(3) * x / 3 + sqrt(3) / 3)
    assert log_to_atan(f, g) == fg_ans
    assert log_to_atan(g, f) == -fg_ans
Exemple #24
0
def test_roots_quadratic():
    assert roots_quadratic(Poly(2 * x**2, x)) == [0, 0]
    assert roots_quadratic(Poly(2 * x**2 + 3 * x, x)) == [-Rational(3, 2), 0]
    assert roots_quadratic(Poly(2 * x**2 + 3,
                                x)) == [-I * sqrt(6) / 2, I * sqrt(6) / 2]
    assert roots_quadratic(
        Poly(2 * x**2 + 4 * x + 3,
             x)) == [-1 - I * sqrt(2) / 2, -1 + I * sqrt(2) / 2]

    f = x**2 + (2 * a * e + 2 * c * e) / (a - c) * x + (d - b + a * e**2 -
                                                        c * e**2) / (a - c)
    assert (roots_quadratic(Poly(f, x)) == [
        -e * (a + c) / (a - c) - sqrt(
            (a * b + 4 * a * c * e**2 - a * d - b * c + c * d) / (a - c)**2),
        -e * (a + c) / (a - c) + sqrt(
            (a * b + 4 * a * c * e**2 - a * d - b * c + c * d) / (a - c)**2)
    ])

    # check for simplification
    f = Poly(y * x**2 - 2 * x - 2 * y, x)
    assert roots_quadratic(f) == [
        -sqrt((2 * y**2 + 1) / y**2) + 1 / y,
        sqrt((2 * y**2 + 1) / y**2) + 1 / y
    ]
    f = Poly(x**2 + (-y**2 - 2) * x + y**2 + 1, x)
    assert roots_quadratic(f) == [
        y**2 / 2 - sqrt(y**4) / 2 + 1, y**2 / 2 + sqrt(y**4) / 2 + 1
    ]

    f = Poly(sqrt(2) * x**2 - 1, x)
    r = roots_quadratic(f)
    assert r == _nsort(r)

    # issue sympy/sympy#8255
    f = Poly(-24 * x**2 - 180 * x + 264)
    assert [w.evalf(2) for w in f.all_roots(radicals=True)] == \
           [w.evalf(2) for w in f.all_roots(radicals=False)]
    for _a, _b, _c in itertools.product((-2, 2), (-2, 2), (0, -1)):
        f = Poly(_a * x**2 + _b * x + _c)
        roots = roots_quadratic(f)
        assert roots == _nsort(roots)
Exemple #25
0
def test_solve_poly_system():
    assert solve_poly_system([x - 1], x) == [{x: 1}]

    pytest.raises(ComputationFailed, lambda: solve_poly_system([0, 1]))

    assert solve_poly_system([y - x, y - x - 1], x, y) == []

    assert solve_poly_system([x - y + 5, x + y - 3], x, y) == [{x: -1, y: 4}]
    assert solve_poly_system([x - 2 * y + 5, 2 * x - y - 3], x, y) == [{
        x:
        Rational(11, 3),
        y:
        Rational(13, 3)
    }]
    assert solve_poly_system([x**2 + y, x + y * 4], x, y) == [{
        x: 0,
        y: 0
    }, {
        x:
        Rational(1, 4),
        y:
        Rational(-1, 16)
    }]

    assert solve_poly_system([y - x**2, y + x**2], x, y) == [{x: 0, y: 0}]

    assert (solve_poly_system([2 * x - 3, 3 * y / 2 - 2 * x, z - 5 * y], x, y,
                              z) == [{
                                  x: Rational(3, 2),
                                  y: 2,
                                  z: 10
                              }])

    assert (solve_poly_system([x * y - 2 * y, 2 * y**2 - x**2], x, y) == [{
        x: 0,
        y: 0
    }, {
        x:
        2,
        y:
        -sqrt(2)
    }, {
        x:
        2,
        y:
        sqrt(2)
    }])

    assert (solve_poly_system([x * y - 2 * y, 2 * y**2 - x**3], x, y) == [{
        x: 0,
        y: 0
    }, {
        x: 2,
        y: -2
    }, {
        x: 2,
        y: 2
    }])

    assert (solve_poly_system([y - x**2, y + x**2 + 1], x, y) == [{
        x:
        -I / sqrt(2),
        y:
        Rational(-1, 2)
    }, {
        x:
        I / sqrt(2),
        y:
        Rational(-1, 2)
    }])

    f_1 = x**2 + y + z - 1
    f_2 = x + y**2 + z - 1
    f_3 = x + y + z**2 - 1

    a, b = sqrt(2) - 1, -sqrt(2) - 1

    assert (solve_poly_system([f_1, f_2, f_3], x, y, z) == [{
        x: 0,
        y: 0,
        z: 1
    }, {
        x: 0,
        y: 1,
        z: 0
    }, {
        x: 1,
        y: 0,
        z: 0
    }, {
        x: a,
        y: a,
        z: a
    }, {
        x: b,
        y: b,
        z: b
    }])

    solution = [{x: 1, y: -1}, {x: 1, y: 1}]

    assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution
    assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution
    assert solve_poly_system([x**2 - y**2, x - 1]) == solution

    assert (solve_poly_system([x + x * y - 3, y + x * y - 4], x, y) == [{
        x: -3,
        y: -2
    }, {
        x: 1,
        y: 2
    }])

    assert (solve_poly_system([x**3 - y**3], x, y) == [{
        x: y
    }, {
        x:
        y * (-1 / 2 - sqrt(3) * I / 2)
    }, {
        x:
        y * (-1 / 2 + sqrt(3) * I / 2)
    }])

    pytest.raises(PolynomialError, lambda: solve_poly_system([1 / x], x))

    assert (solve_poly_system([x**6 + x - 1], x) == [{
        x: r
    } for r in Poly(x**6 + x - 1).all_roots()])

    # Arnold's problem on two walking old women
    eqs = (4 * n + 9 * t - y, n * (12 - x) - 9 * t, -4 * n + t * (12 - x))
    res = solve_poly_system(eqs, n, t, x, y)
    assert res == [{
        n: 0,
        t: 0,
        y: 0
    }, {
        n: -y / 2,
        t: y / 3,
        x: 18
    }, {
        n: y / 10,
        t: y / 15,
        x: 6
    }]
    assert [_ for _ in res if 12 > _.get(x, 0) > 0] == [{
        n: y / 10,
        t: y / 15,
        x: 6
    }]
    # Now add redundant equation
    eqs = (n * (12 - x) + t * (12 - x) - y, 4 * n + 9 * t - y,
           n * (12 - x) - 9 * t, -4 * n + t * (12 - x))
    res = solve_poly_system(eqs, n, x, y, t)
    assert res == [{
        n: 0,
        t: 0,
        y: 0
    }, {
        n: -3 * t / 2,
        x: 18,
        y: 3 * t
    }, {
        n: 3 * t / 2,
        x: 6,
        y: 15 * t
    }]

    assert solve_poly_system(eqs[1:], n, t, y, x) != [{n: 0, t: 0, y: 0}]
Exemple #26
0
def test_roots_preprocessing():
    f = a * y * x**2 + y - b

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1
    assert poly == Poly(a * y * x**2 + y - b, x)

    f = c**3 * x**3 + c**2 * x**2 + c * x + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1 / c
    assert poly == Poly(x**3 + x**2 + x + a, x)

    f = c**3 * x**3 + c**2 * x**2 + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1 / c
    assert poly == Poly(x**3 + x**2 + a, x)

    f = c**3 * x**3 + c * x + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1 / c
    assert poly == Poly(x**3 + x + a, x)

    f = c**3 * x**3 + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1 / c
    assert poly == Poly(x**3 + a, x)

    E, F, J, L = symbols('E,F,J,L')

    f = -21601054687500000000*E**8*J**8/L**16 + \
        508232812500000000*F*x*E**7*J**7/L**14 - \
        4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
        16194716250000*E**5*F**3*J**5*x**3/L**10 - \
        27633173750*E**4*F**4*J**4*x**4/L**8 + \
        14840215*E**3*F**5*J**3*x**5/L**6 + \
        54794*E**2*F**6*J**2*x**6/(5*L**4) - \
        1153*E*J*F**7*x**7/(80*L**2) + \
        633*F**8*x**8/160000

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 20 * E * J / (F * L**2)
    assert poly == 633*x**8 - 115300*x**7 + 4383520*x**6 + 296804300*x**5 - 27633173750*x**4 + \
        809735812500*x**3 - 10673859375000*x**2 + 63529101562500*x - 135006591796875

    f = J**8 + 7 * E * x**2 * L**16 + 5 * F * x * E**5 * J**7 * L**2
    coeff, poly = preprocess_roots(Poly(f, x))
    assert coeff == 1
    assert poly == Poly(f, x)

    f = Poly(-y**2 + x**2 * exp(x), y, domain=ZZ.inject(x, exp(x)))
    g = Poly(y**2 - exp(x), y, domain=ZZ.inject(exp(x)))

    assert preprocess_roots(f) == (x, g)
Exemple #27
0
def test_Domain__algebraic_field():
    alg = ZZ.algebraic_field(sqrt(3))
    assert alg.minpoly == Poly(x**2 - 3)
    assert alg.domain == QQ
    assert alg.from_expr(sqrt(3)).denominator == 1
    assert alg.from_expr(2 * sqrt(3)).denominator == 1
    assert alg.from_expr(sqrt(3) / 2).denominator == 2
    assert alg([QQ(7, 38), QQ(3, 2)]).denominator == 38

    alg = QQ.algebraic_field(sqrt(2))
    assert alg.minpoly == Poly(x**2 - 2)
    assert alg.domain == QQ

    alg = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert alg.minpoly == Poly(x**4 - 10 * x**2 + 1)
    assert alg.domain == QQ

    assert alg(1).numerator == alg(1)
    assert alg.from_expr(sqrt(3) / 2).numerator == alg.from_expr(2 * sqrt(3))
    assert alg.from_expr(sqrt(3) / 2).denominator == 4

    pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2)))

    assert alg.characteristic == 0

    assert alg.is_RealAlgebraicField is True

    assert int(alg(2)) == 2
    assert int(alg.from_expr(Rational(3, 2))) == 1

    alg = QQ.algebraic_field(I)
    assert alg.algebraic_field(I) == alg
    assert alg.is_RealAlgebraicField is False
    pytest.raises(TypeError, lambda: int(alg([1, 1])))

    alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3))
    assert alg.minpoly == Poly(x**2 - 3, x, domain=QQ.algebraic_field(sqrt(2)))

    # issue sympy/sympy#14476
    assert QQ.algebraic_field(Rational(1, 7)) is QQ

    alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg.from_expr(2 * sqrt(2) + I / 3) == alg(
        [alg.domain([1]) / 3, alg.domain([2, 0])])
    alg2 = QQ.algebraic_field(sqrt(2))
    assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2)))

    eq = -x**3 + 2 * x**2 + 3 * x - 2
    rs = roots(eq, multiple=True)
    alg = QQ.algebraic_field(rs[0])
    assert alg.is_RealAlgebraicField

    alg1 = QQ.algebraic_field(I)
    alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg1 != alg2

    alg3 = QQ.algebraic_field(RootOf(4 * x**7 + x - 1, 0))
    assert alg3.is_RealAlgebraicField
    assert int(alg3.unit) == 2
    assert 2.772 > alg3.unit > 2.771
    assert int(alg3([3, 17, 11, -1, 2])) == 622
    assert int(
        alg3([
            1,
            QQ(-11, 4),
            QQ(125326976730518, 44208605852241),
            QQ(-16742151878022, 12894796053515),
            QQ(2331359268715, 10459004949272)
        ])) == 18

    alg4 = QQ.algebraic_field(sqrt(2) + I)
    assert alg4.convert(alg2.unit) == alg4.from_expr(I)
Exemple #28
0
def test_roots0():
    assert roots(1, x) == {}
    assert roots(x, x) == {0: 1}
    assert roots(x**9, x) == {0: 9}
    assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x) == {
        -3: 1,
        2: 1,
        4: 1
    }

    assert roots(x**2 - 2 * x + 1, x, auto=False) == {1: 2}

    assert roots(2 * x + 1, x) == {Rational(-1, 2): 1}
    assert roots((2 * x + 1)**2, x) == {Rational(-1, 2): 2}
    assert roots((2 * x + 1)**5, x) == {Rational(-1, 2): 5}
    assert roots((2 * x + 1)**10, x) == {Rational(-1, 2): 10}

    assert roots(x**4 - 1, x) == {I: 1, 1: 1, -1: 1, -I: 1}
    assert roots((x**4 - 1)**2, x) == {I: 2, 1: 2, -1: 2, -I: 2}

    assert roots(((2 * x - 3)**2).expand(), x) == {+Rational(3, 2): 2}
    assert roots(((2 * x + 3)**2).expand(), x) == {-Rational(3, 2): 2}

    assert roots(((2 * x - 3)**3).expand(), x) == {+Rational(3, 2): 3}
    assert roots(((2 * x + 3)**3).expand(), x) == {-Rational(3, 2): 3}

    assert roots(((2 * x - 3)**5).expand(), x) == {+Rational(3, 2): 5}
    assert roots(((2 * x + 3)**5).expand(), x) == {-Rational(3, 2): 5}

    assert roots(((a * x - b)**5).expand(), x) == {+b / a: 5}
    assert roots(((a * x + b)**5).expand(), x) == {-b / a: 5}

    assert roots(x**2 + (-a - 1) * x + a, x) == {a: 1, 1: 1}

    assert roots(x**4 - 2 * x**2 + 1, x) == {1: 2, -1: 2}

    assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \
        {1: 2, -1 - sqrt(2): 1, 0: 2, -1 + sqrt(2): 1}

    assert roots(x**8 - 1, x) == {
        sqrt(2) / 2 + I * sqrt(2) / 2: 1,
        sqrt(2) / 2 - I * sqrt(2) / 2: 1,
        -sqrt(2) / 2 + I * sqrt(2) / 2: 1,
        -sqrt(2) / 2 - I * sqrt(2) / 2: 1,
        1: 1,
        -1: 1,
        I: 1,
        -I: 1
    }

    f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \
        224*x**7 - 384*x**8 - 64*x**9

    assert roots(f) == {
        0: 2,
        -2: 2,
        2: 1,
        -Rational(7, 2): 1,
        -Rational(3, 2): 1,
        -Rational(1, 2): 1,
        Rational(3, 2): 1
    }

    assert roots((a + b + c) * x - (a + b + c + d), x) == {
        (a + b + c + d) / (a + b + c): 1
    }

    assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {}
    assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x, cubics=False) == {
        -3: 1,
        2: 1,
        4: 1
    }
    assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \
        {-3: 1, 2: 1, 4: 1, 5: 1}
    assert roots(x**3 + 2 * x**2 + 4 * x + 8, x) == {
        -2: 1,
        -2 * I: 1,
        2 * I: 1
    }
    assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \
        {-2*I: 1, 2*I: 1, -2: 1}
    assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x) == \
        {1: 1, 0: 1, -2: 1, -2*I: 1, 2*I: 1}

    r1_2, r1_3 = Rational(1, 2), Rational(1, 3)

    x0 = (3 * sqrt(33) + 19)**r1_3
    x1 = 4 / x0 / 3
    x2 = x0 / 3
    x3 = sqrt(3) * I / 2
    x4 = x3 - r1_2
    x5 = -x3 - r1_2
    assert roots(x**3 + x**2 - x + 1, x, cubics=True) == {
        -x1 - x2 - r1_3: 1,
        -x1 / x4 - x2 * x4 - r1_3: 1,
        -x1 / x5 - x2 * x5 - r1_3: 1,
    }

    f = (x**2 + 2 * x + 3).subs({x: 2 * x**2 + 3 * x}).subs({x: 5 * x - 4})

    r13_20, r1_20 = [Rational(*r) for r in ((13, 20), (1, 20))]

    s2 = sqrt(2)
    assert roots(f, x) == {
        r13_20 + r1_20 * sqrt(1 - 8 * I * s2): 1,
        r13_20 - r1_20 * sqrt(1 - 8 * I * s2): 1,
        r13_20 + r1_20 * sqrt(1 + 8 * I * s2): 1,
        r13_20 - r1_20 * sqrt(1 + 8 * I * s2): 1,
    }

    f = x**4 + x**3 + x**2 + x + 1

    r1_4, r1_8, r5_8 = [Rational(*r) for r in ((1, 4), (1, 8), (5, 8))]

    assert roots(f, x) == {
        -r1_4 + r1_4 * 5**r1_2 + I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1,
        -r1_4 + r1_4 * 5**r1_2 - I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1,
        -r1_4 - r1_4 * 5**r1_2 + I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1,
        -r1_4 - r1_4 * 5**r1_2 - I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1,
    }

    f = z**3 + (-2 - y) * z**2 + (1 + 2 * y - 2 * x**2) * z - y + 2 * x**2

    assert roots(f, z) == {
        1: 1,
        Rational(1, 2) + y / 2 + sqrt(1 - 2 * y + y**2 + 8 * x**2) / 2: 1,
        Rational(1, 2) + y / 2 - sqrt(1 - 2 * y + y**2 + 8 * x**2) / 2: 1,
    }

    assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x,
                 cubics=False) == {}
    assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x, cubics=True) != {}

    assert roots(x**4 - 1, x, filter='Z') == {1: 1, -1: 1}
    assert roots(x**4 - 1, x, filter='R') == {1: 1, -1: 1}
    assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1}

    pytest.raises(ValueError, lambda: roots(x**4 - 1, x, filter='spam'))

    assert roots((x - 1) * (x + 1), x) == {1: 1, -1: 1}
    assert roots((x - 1) * (x + 1), x, predicate=lambda r: r.is_positive) == {
        1: 1
    }

    assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-1, 1]
    assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I]

    assert roots(x**3, x, multiple=True) == [0, 0, 0]
    assert roots(1234, x, multiple=True) == []

    f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1

    assert roots(f) == {
        -I * sin(pi / 7) + cos(pi / 7): 1,
        -I * sin(2 * pi / 7) - cos(2 * pi / 7): 1,
        -I * sin(3 * pi / 7) + cos(3 * pi / 7): 1,
        I * sin(pi / 7) + cos(pi / 7): 1,
        I * sin(2 * pi / 7) - cos(2 * pi / 7): 1,
        I * sin(3 * pi / 7) + cos(3 * pi / 7): 1,
    }

    g = ((x**2 + 1) * f**2).expand()

    assert roots(g) == {
        -I * sin(pi / 7) + cos(pi / 7): 2,
        -I * sin(2 * pi / 7) - cos(2 * pi / 7): 2,
        -I * sin(3 * pi / 7) + cos(3 * pi / 7): 2,
        I * sin(pi / 7) + cos(pi / 7): 2,
        I * sin(2 * pi / 7) - cos(2 * pi / 7): 2,
        I * sin(3 * pi / 7) + cos(3 * pi / 7): 2,
        -I: 1,
        I: 1,
    }

    r = roots(x**3 + 40 * x + 64)
    real_root = [rx for rx in r if rx.is_extended_real][0]
    cr = 108 + 6 * sqrt(1074)
    assert real_root == -2 * root(cr, 3) / 3 + 20 / root(cr, 3)

    eq = Poly((7 + 5 * sqrt(2)) * x**3 + (-6 - 4 * sqrt(2)) * x**2 +
              (-sqrt(2) - 1) * x + 2,
              x,
              domain='EX')
    assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2 * sqrt(2): 1, -sqrt(2) + 1: 1}

    eq = Poly(41 * x**5 + 29 * sqrt(2) * x**5 - 153 * x**4 -
              108 * sqrt(2) * x**4 + 175 * x**3 + 125 * sqrt(2) * x**3 -
              45 * x**2 - 30 * sqrt(2) * x**2 - 26 * sqrt(2) * x - 26 * x + 24,
              x,
              domain='EX')
    assert roots(eq) == {
        -sqrt(2) + 1: 1,
        -2 + 2 * sqrt(2): 1,
        -1 + sqrt(2): 1,
        -4 + 4 * sqrt(2): 1,
        -3 + 3 * sqrt(2): 1
    }

    eq = Poly(x**3 - 2 * x**2 + 6 * sqrt(2) * x**2 - 8 * sqrt(2) * x + 23 * x -
              14 + 14 * sqrt(2),
              x,
              domain='EX')
    assert roots(eq) == {
        -2 * sqrt(2) + 2: 1,
        -2 * sqrt(2) + 1: 1,
        -2 * sqrt(2) - 1: 1
    }

    assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \
        {-sqrt(2) - root(7, 3)/2 - sqrt(3)*root(7, 3)*I/2: 1,
         -sqrt(2) - root(7, 3)/2 + sqrt(3)*root(7, 3)*I/2: 1,
         -sqrt(2) + root(7, 3): 1}

    pytest.raises(PolynomialError, lambda: roots(x * y, x, y))
Exemple #29
0
def test_canonical_representation():
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x), Poly(1 + t**2, t)]})
    assert canonical_representation(Poly(x - t, t), Poly(t**2, t), DE) == \
        (Poly(0, t), (Poly(0, t),
        Poly(1, t)), (Poly(-t + x, t),
        Poly(t**2, t)))
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]})
    assert canonical_representation(Poly(t**5 + t**3 + x**2*t + 1, t),
    Poly((t**2 + 1)**3, t), DE) == \
        (Poly(0, t), (Poly(t**5 + t**3 + x**2*t + 1, t),
        Poly(t**6 + 3*t**4 + 3*t**2 + 1, t)), (Poly(0, t), Poly(1, t)))
Exemple #30
0
def test_DifferentialExtension_symlog():
    # See comment on test_risch_integrate below
    assert DifferentialExtension(log(x**x), x, dummy=False)._important_attrs == \
        (Poly(t0*x, t1), Poly(1, t1), [Poly(1, x), Poly(1/x, t0), Poly((t0 +
                                                                        1)*t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(i*t0))],
            [(exp(x*log(x)), x**x)], [2], [t0*x], [1], [x])
    assert DifferentialExtension(log(x**y), x, dummy=False)._important_attrs == \
        (Poly(y*t0, t0), Poly(1, t0), [Poly(1, x), Poly(1/x, t0)], [x, t0],
         [Lambda(i, log(i))], [(y*log(x), log(x**y))], [], [], [1], [x])
    assert DifferentialExtension(log(sqrt(x)), x, dummy=False)._important_attrs == \
        (Poly(t0, t0), Poly(2, t0), [Poly(1, x), Poly(1/x, t0)], [x, t0],
         [Lambda(i, log(i))], [(log(x)/2, log(sqrt(x)))], [], [], [1], [x])