Beispiel #1
0
def test_Domain_unify_algebraic():
    sqrt5 = QQ.algebraic_field(sqrt(5))
    sqrt7 = QQ.algebraic_field(sqrt(7))
    sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7))

    assert sqrt5.unify(sqrt7) == sqrt57

    assert sqrt5.unify(sqrt5.poly_ring(x, y)) == sqrt5.poly_ring(x, y)
    assert sqrt5.poly_ring(x, y).unify(sqrt5) == sqrt5.poly_ring(x, y)

    assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y)

    assert sqrt5.unify(sqrt7.poly_ring(x, y)) == sqrt57.poly_ring(x, y)
    assert sqrt5.poly_ring(x, y).unify(sqrt7) == sqrt57.poly_ring(x, y)

    assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y)

    sqrt2 = QQ.algebraic_field(sqrt(2))
    r = RootOf(x**7 - x + 1, 0)
    rootof = QQ.algebraic_field(r)
    ans = QQ.algebraic_field(r + sqrt(2))
    assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans

    # here domain created from tuple, not Expr
    p = Poly(x**3 - sqrt(2) * x - 1, x)
    sqrt2 = p.domain
    assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans
Beispiel #2
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.is_nonpositive(alg([-1, 1])) is True
    assert alg.is_nonnegative(alg([2, -1])) is True

    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
    pytest.raises(TypeError, lambda: int(alg([1, 1])))

    alg = QQ.algebraic_field(I)
    assert alg.algebraic_field(I) == alg
    assert alg.is_RealAlgebraicField is False

    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 * sqrt(2))])
    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.ext_root == RootOf(eq, 2)

    alg1 = QQ.algebraic_field(I)
    alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg1 != alg2
Beispiel #3
0
def test_sympyissue_8285():
    roots = (Poly(4 * x**8 - 1, x) * Poly(x**2 + 1)).all_roots()
    assert roots == _nsort(roots)
    f = Poly(x**4 + 5 * x**2 + 6, x)
    ro = [RootOf(f, i) for i in range(4)]
    roots = Poly(x**4 + 5 * x**2 + 6, x).all_roots()
    assert roots == ro
    assert roots == _nsort(roots)
    # more than 2 complex roots from which to identify the
    # imaginary ones
    roots = Poly(2 * x**8 - 1).all_roots()
    assert roots == _nsort(roots)
    assert len(Poly(2 * x**10 - 1).all_roots()) == 10  # doesn't fail
Beispiel #4
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([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: -S.Half}, {x: I/sqrt(2), y: -S.Half}])

    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: RootOf(x**6 + x - 1, 0)}, {x: RootOf(x**6 + x - 1, 1)},
             {x: RootOf(x**6 + x - 1, 2)}, {x: RootOf(x**6 + x - 1, 3)},
             {x: RootOf(x**6 + x - 1, 4)}, {x: RootOf(x**6 + x - 1, 5)}])
Beispiel #5
0
def test_AlgebraicElement():
    r = RootOf(x**7 + 3*x - 1, 3)
    K = QQ.algebraic_field(r)
    a = K([1, 2, 3, 0, 1])
    assert mcode(a) == ('AlgebraicNumber[Root[#^7 + 3*# - 1 &, 4],'
                        ' {1, 0, 3, 2, 1}]')
Beispiel #6
0
def test_RootOf():
    p = Poly(x**3 + y*x + 1, x)
    assert mcode(RootOf(p, 0)) == 'Root[#^3 + #*y + 1 &, 1]'
Beispiel #7
0
def test_RootOf():
    assert str(RootOf(x**5 + 2 * x - 1, 0)) == "RootOf(x**5 + 2*x - 1, x, 0)"
Beispiel #8
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: RootOf(x**6 + x - 1, 0)
    }, {
        x: RootOf(x**6 + x - 1, 1)
    }, {
        x: RootOf(x**6 + x - 1, 2)
    }, {
        x: RootOf(x**6 + x - 1, 3)
    }, {
        x: RootOf(x**6 + x - 1, 4)
    }, {
        x: RootOf(x**6 + x - 1, 5)
    }])

    # 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}]
Beispiel #9
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: RootOf(x**3 + y**2, x, 0)
    }, {
        x: RootOf(x**3 + y**2, x, 1)
    }, {
        x: RootOf(x**3 + y**2, x, 2)
    }]
    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:
        RootOf(x**3 * y**2 - 1, x, 0)
    }, {
        x:
        RootOf(x**3 * y**2 - 1, x, 1)
    }, {
        x:
        RootOf(x**3 * y**2 - 1, x, 2)
    }]
    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
    }]
Beispiel #10
0
def test_RootOf():
    assert str(RootOf(x**5 + 2 * x - 1, 0)) == "RootOf(x**5 + 2*x - 1, 0)"
    assert str(RootOf(x**3 + y * x + 1, x,
                      0)) == "RootOf(x**3 + x*y + 1, x, 0)"
Beispiel #11
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)
Beispiel #12
0
def test_AlgebraicNumber():
    r = RootOf(x**7 + 3 * x - 1, 3)
    a = AlgebraicNumber(r, (1, 2, 3, 0, 1))
    assert mcode(a) == ('AlgebraicNumber[Root[#^7 + 3*# - 1 &, 4],'
                        ' {1, 0, 3, 2, 1}]')