def test_primitive_element():
    assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]])

    assert (primitive_element([sqrt(2), sqrt(3)]) ==
            (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0],
                                                    [QQ(-1, 2), 0, QQ(11, 2), 0]]))
    A = QQ.algebraic_field(sqrt(2))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))
    assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))

    A = QQ.algebraic_field(sqrt(2) + sqrt(3))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1],
             [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]]))

    pytest.raises(ValueError, lambda: primitive_element([]))

    # issue sympy/sympy#13849
    assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) ==
            (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0],
                                                    [QQ(-1, 24), 0, QQ(43, 12), 0]]))

    # issue sympy/sympy#14117
    assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) ==
            (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0],
                                                        [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
Exemple #2
0
def test_primitive_element():
    assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]])

    assert (primitive_element([sqrt(2), sqrt(3)]) ==
            (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0],
                                                    [QQ(-1, 2), 0, QQ(11, 2), 0]]))
    A = QQ.algebraic_field(sqrt(2))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))
    assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))

    A = QQ.algebraic_field(sqrt(2) + sqrt(3))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1],
             [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]]))

    pytest.raises(ValueError, lambda: primitive_element([]))

    # issue sympy/sympy#13849
    assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) ==
            (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0],
                                                    [QQ(-1, 24), 0, QQ(43, 12), 0]]))

    # issue sympy/sympy#14117
    assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) ==
            (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0],
                                                        [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
Exemple #3
0
def test_dmp_ext_factor():
    R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2)))

    assert R.dmp_ext_factor(0) == (R.domain(0), [])

    f = x + 1

    assert R.dmp_ext_factor(f) == (R.domain(1), [(f, 1)])

    g = 2 * x + 2

    assert R.dmp_ext_factor(g) == (R.domain(2), [(f, 1)])

    f = x**2 - 2 * y**2

    assert R.dmp_ext_factor(f) == (R.domain(1), [(x - sqrt(2) * y, 1),
                                                 (x + sqrt(2) * y, 1)])

    f = 2 * x**2 - 4 * y**2

    assert R.dmp_ext_factor(f) == (R.domain(2), [(x - sqrt(2) * y, 1),
                                                 (x + sqrt(2) * y, 1)])

    # issue sympy/sympy#5786
    R, x, y, z, t = ring("x, y, z, t", QQ.algebraic_field(I))

    f = -I * t * x - t * y + x * z - I * y * z
    assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I * t, 1),
                                                  (x - I * y, 1)]))

    R, x = ring("x", QQ.algebraic_field(I))
    f = x**2 + 1
    assert R.dmp_ext_factor(f) == (R.domain(1), [(x - I, 1), (x + I, 1)])
Exemple #4
0
def test_to_number_field():
    A = QQ.algebraic_field(sqrt(2))
    assert A.convert(sqrt(2)) == A([1, 0])
    B = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert B.convert(sqrt(2) + sqrt(3)) == B([1, 0])

    K = QQ.algebraic_field(sqrt(2) + sqrt(3))
    a = K([Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)])

    assert B.from_expr(sqrt(2)) == a

    pytest.raises(CoercionFailed,
                  lambda: QQ.algebraic_field(sqrt(3)).convert(sqrt(2)))

    p = x**6 - 6 * x**4 - 6 * x**3 + 12 * x**2 - 36 * x + 1
    r0, r1 = p.as_poly(x).all_roots()[:2]
    A = QQ.algebraic_field(r0)
    a = A([
        Rational(-96, 755),
        Rational(-54, 755),
        Rational(128, 151),
        Rational(936, 755),
        Rational(-1003, 755),
        Rational(2184, 755)
    ])
    assert A.from_expr(r1) == a
Exemple #5
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(a, "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])" % repr(QQ))
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(a, "AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])" % repr(QQ))
Exemple #6
0
def test_PolyElement_sqf_norm():
    R, x = ring("x", QQ.algebraic_field(sqrt(3)))
    X = R.to_ground().x

    assert (x**2 - 2).sqf_norm() == (1, x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1)

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))
    X = R.to_ground().x

    assert (x**2 - 3).sqf_norm() == (1, x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
Exemple #7
0
def test_PolyElement_sqf_norm():
    R, x = ring("x", QQ.algebraic_field(sqrt(3)))
    X = R.to_ground().x

    assert (x**2 - 2).sqf_norm() == (1, x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1)

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))
    X = R.to_ground().x

    assert (x**2 - 3).sqf_norm() == (1, x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
Exemple #8
0
def test_modgcd_algebraic_field():
    A = QQ.algebraic_field(sqrt(2))
    R, x = ring("x", A)
    one = A.one

    f, g = 2 * x, R(2)
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = 2 * x, R(sqrt(2))
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = 2 * x + 2, 6 * x**2 - 6
    assert func_field_modgcd(f, g) == (x + 1, R(2), 6 * x - 6)

    R, x, y = ring("x, y", A)

    f, g = x + sqrt(2) * y, x + y
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = x * y + sqrt(2) * y**2, R(sqrt(2)) * y
    assert func_field_modgcd(f, g) == (y, x + sqrt(2) * y, R(sqrt(2)))

    f, g = x**2 + 2 * sqrt(2) * x * y + 2 * y**2, x + sqrt(2) * y
    assert func_field_modgcd(f, g) == (g, g, one)

    A = QQ.algebraic_field(sqrt(2), sqrt(3))
    R, x, y, z = ring("x, y, z", A)

    h = x**2 * y**7 + sqrt(6) / 21 * z
    f, g = h * (27 * y**3 + 1), h * (y + x)
    assert func_field_modgcd(f, g) == (h, 27 * y**3 + 1, y + x)

    h = x**13 * y**3 + 1 / 2 * x**10 + 1 / sqrt(2)
    f, g = h * (x + 1), h * sqrt(2) / sqrt(3)
    assert func_field_modgcd(f, g) == (h, x + 1, R(sqrt(2) / sqrt(3)))

    h = x**4 * y**9 + sqrt(6) / 22 * z
    f, g = h * (21 * y**3 + 1), h * (y + x)

    assert func_field_modgcd(f, g) == (x**4 * y**9 + sqrt(6) / 22 * z,
                                       21 * y**3 + 1, x + y)

    h = x**4 * y**3 + sqrt(6) / 22 * z
    f, g = h * (11 * y**3 + 1), h * (y + x)
    assert func_field_modgcd(f, g) == (x**4 * y**3 + sqrt(6) / 22 * z,
                                       11 * y**3 + 1, x + y)

    A = QQ.algebraic_field(sqrt(2)**(-1) * sqrt(3))
    R, x = ring("x", A)

    f, g = x + 1, x - 1
    assert func_field_modgcd(f, g) == (A.one, f, g)
Exemple #9
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(
        a,
        "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])"
        % repr(QQ))
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(
        a,
        "AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])"
        % repr(QQ))
Exemple #10
0
def test_modgcd_algebraic_field():
    A = QQ.algebraic_field(sqrt(2))
    R, x = ring("x", A)
    one = A.one

    f, g = 2*x, R(2)
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = 2*x, R(sqrt(2))
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = 2*x + 2, 6*x**2 - 6
    assert func_field_modgcd(f, g) == (x + 1, R(2), 6*x - 6)

    R, x, y = ring("x, y", A)

    f, g = x + sqrt(2)*y, x + y
    assert func_field_modgcd(f, g) == (one, f, g)

    f, g = x*y + sqrt(2)*y**2, R(sqrt(2))*y
    assert func_field_modgcd(f, g) == (y, x + sqrt(2)*y, R(sqrt(2)))

    f, g = x**2 + 2*sqrt(2)*x*y + 2*y**2, x + sqrt(2)*y
    assert func_field_modgcd(f, g) == (g, g, one)

    A = QQ.algebraic_field(sqrt(2), sqrt(3))
    R, x, y, z = ring("x, y, z", A)

    h = x**2*y**7 + sqrt(6)/21*z
    f, g = h*(27*y**3 + 1), h*(y + x)
    assert func_field_modgcd(f, g) == (h, 27*y**3+1, y+x)

    h = x**13*y**3 + 1/2*x**10 + 1/sqrt(2)
    f, g = h*(x + 1), h*sqrt(2)/sqrt(3)
    assert func_field_modgcd(f, g) == (h, x + 1, R(sqrt(2)/sqrt(3)))

    h = x**4*y**9 + sqrt(6)/22*z
    f, g = h*(21*y**3 + 1), h*(y + x)

    assert func_field_modgcd(f, g) == (x**4*y**9 + sqrt(6)/22*z, 21*y**3 + 1, x + y)

    h = x**4*y**3 + sqrt(6)/22*z
    f, g = h*(11*y**3 + 1), h*(y + x)
    assert func_field_modgcd(f, g) == (x**4*y**3 + sqrt(6)/22*z, 11*y**3 + 1, x + y)

    A = QQ.algebraic_field(sqrt(2)**(-1)*sqrt(3))
    R, x = ring("x", A)

    f, g = x + 1, x - 1
    assert func_field_modgcd(f, g) == (A.one, f, g)
def test_dup_count_complex_roots_9():
    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = -x**3 + sqrt(2)*x - 1

    assert R.dup_count_complex_roots(f, a, b) == 2
    assert R.dup_count_complex_roots(f, c, d) == 1

    R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I))

    f = -x**3 + I*x**2 + sqrt(2)*x - 1

    assert R.dup_count_complex_roots(f, a, b) == 2
    assert R.dup_count_complex_roots(f, c, d) == 1
Exemple #12
0
def test_dup_count_complex_roots_9():
    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = -x**3 + sqrt(2) * x - 1

    assert R.dup_count_complex_roots(f, a, b) == 2
    assert R.dup_count_complex_roots(f, c, d) == 1

    R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I))

    f = -x**3 + I * x**2 + sqrt(2) * x - 1

    assert R.dup_count_complex_roots(f, a, b) == 2
    assert R.dup_count_complex_roots(f, c, d) == 1
Exemple #13
0
def test_dup_ext_factor():
    R, x = ring("x", QQ.algebraic_field(I))

    assert R.dup_ext_factor(0) == (R.domain(0), [])

    f = x + 1

    assert R.dup_ext_factor(f) == (R.domain(1), [(f, 1)])

    g = 2 * x + 2

    assert R.dup_ext_factor(g) == (R.domain(2), [(f, 1)])

    f = 7 * x**4 + 1
    g = x**4 + QQ(1, 7)

    assert R.dup_ext_factor(f) == (R.domain(7), [(g, 1)])

    f = x**4 + 1

    assert R.dup_ext_factor(f) == (R.domain(1), [(x**2 - I, 1), (x**2 + I, 1)])

    f = 4 * x**2 + 9

    assert R.dup_ext_factor(f) == (R.domain(4), [(x - 3 * I / 2, 1),
                                                 (x + 3 * I / 2, 1)])

    f = 4 * x**4 + 8 * x**3 + 77 * x**2 + 18 * x + 153

    assert R.dup_ext_factor(f) == (4, [(x - 3 * I / 2, 1), (x + 1 + 4 * I, 1),
                                       (x + 1 - 4 * I, 1), (x + 3 * I / 2, 1)])

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = x**4 + 1

    assert R.dup_ext_factor(f) == (R.domain(1), [(x**2 - sqrt(2) * x + 1, 1),
                                                 (x**2 + sqrt(2) * x + 1, 1)])

    f = x**2 + 2 * sqrt(2) * x + 2

    assert R.dup_ext_factor(f) == (R.domain(1), [(x + sqrt(2), 2)])
    assert R.dup_ext_factor(f**3) == (R.domain(1), [(x + sqrt(2), 6)])

    f *= 2

    assert R.dup_ext_factor(f) == (R.domain(2), [(x + sqrt(2), 2)])
    assert R.dup_ext_factor(f**3) == (R.domain(8), [(x + sqrt(2), 6)])
Exemple #14
0
def test_dup_count_real_roots():
    R, x = ring("x", ZZ)

    assert R.dup_count_real_roots(0) == 0
    assert R.dup_count_real_roots(7) == 0

    f = x - 1

    assert R.dup_count_real_roots(f) == 1
    assert R.dup_count_real_roots(f, inf=1) == 1
    assert R.dup_count_real_roots(f, sup=0) == 0
    assert R.dup_count_real_roots(f, sup=1) == 1
    assert R.dup_count_real_roots(f, inf=0, sup=1) == 1
    assert R.dup_count_real_roots(f, inf=0, sup=2) == 1
    assert R.dup_count_real_roots(f, inf=1, sup=2) == 1

    f = x**2 - 2

    assert R.dup_count_real_roots(f) == 2
    assert R.dup_count_real_roots(f, sup=0) == 1
    assert R.dup_count_real_roots(f, inf=-1, sup=1) == 0

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**3 + I * x + 2

    assert R.dup_count_real_roots(f) == 0

    f *= (x - 1) * (x + 1)

    assert R.dup_count_real_roots(f) == 2
def test_dup_count_real_roots():
    R, x = ring("x", ZZ)

    assert R.dup_count_real_roots(0) == 0
    assert R.dup_count_real_roots(7) == 0

    f = x - 1
    assert R.dup_count_real_roots(f) == 1
    assert R.dup_count_real_roots(f, inf=1) == 1
    assert R.dup_count_real_roots(f, sup=0) == 0
    assert R.dup_count_real_roots(f, sup=1) == 1
    assert R.dup_count_real_roots(f, inf=0, sup=1) == 1
    assert R.dup_count_real_roots(f, inf=0, sup=2) == 1
    assert R.dup_count_real_roots(f, inf=1, sup=2) == 1

    f = x**2 - 2
    assert R.dup_count_real_roots(f) == 2
    assert R.dup_count_real_roots(f, sup=0) == 1
    assert R.dup_count_real_roots(f, inf=-1, sup=1) == 0

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**3 + I*x + 2
    assert R.dup_count_real_roots(f) == 0

    f *= x**2 - 1
    assert R.dup_count_real_roots(f) == 2
Exemple #16
0
def test_minpoly_domain():
    F = QQ.algebraic_field(sqrt(2))

    assert minimal_polynomial(sqrt(2), domain=F) == PurePoly(x - sqrt(2), x, domain=F)
    assert minimal_polynomial(sqrt(8), domain=F)(x) == x - 2*sqrt(2)
    assert minimal_polynomial(sqrt(Rational(3, 2)), domain=F)(x) == 2*x**2 - 3

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(y, domain=QQ))

    # issue sympy/sympy#14494

    F = QQ.algebraic_field(I)
    assert minimal_polynomial(I, domain=F)(x) == x - I

    F = QQ.algebraic_field(sqrt(3)*I)
    assert minimal_polynomial(exp(I*pi/3), domain=F)(x) == 2*x - sqrt(3)*I - 1
def test_minpoly_domain():
    F = QQ.algebraic_field(sqrt(2))

    assert minimal_polynomial(sqrt(2), domain=F) == PurePoly(x - sqrt(2), x, domain=F)
    assert minimal_polynomial(sqrt(8), domain=F)(x) == x - 2*sqrt(2)
    assert minimal_polynomial(sqrt(Rational(3, 2)), domain=F)(x) == 2*x**2 - 3

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(y, domain=QQ))

    # issue sympy/sympy#14494

    F = QQ.algebraic_field(I)
    assert minimal_polynomial(I, domain=F)(x) == x - I

    F = QQ.algebraic_field(sqrt(3)*I)
    assert minimal_polynomial(exp(I*pi/3), domain=F)(x) == 2*x - sqrt(3)*I - 1
Exemple #18
0
def test_FracElement_diff():
    F, x, y, z = field("x,y,z", ZZ)

    assert ((x**2 + y) / (z + 1)).diff(x) == 2 * x / (z + 1)

    F, x, y = field('x,y', QQ.algebraic_field(I))

    assert ((x - y) / x).diff(x) == y / x**2
Exemple #19
0
def test_FracElement_diff():
    F,  x, y, z = field("x,y,z", ZZ)

    assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1)

    F,  x, y = field('x,y', QQ.algebraic_field(I))

    assert ((x - y)/x).diff(x) == y/x**2
Exemple #20
0
def test_dmp_lift():
    A = QQ.algebraic_field(I)
    f = [A(1), A(0), A(0), A(I), A(17*I)]

    assert dmp_lift(f, 0, A) == [1, 0, 0, 0, 0, 0, 2, 0, 578, 0, 0, 0,
                                 1, 0, -578, 0, 83521]

    pytest.raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
Exemple #21
0
def test_solve_lin_sys_2x2_2():
    domain = QQ.algebraic_field(sqrt(2))
    coeff_ring, A0, A1 = ring('A:2', domain)
    R, x = ring('x', coeff_ring)
    expr = (A1 - sqrt(2))*R.one
    sol = solve_lin_sys(expr.coeffs(), coeff_ring)
    assert all(isinstance(s, coeff_ring.dtype) for s in sol.values())
    assert sol == {A1: coeff_ring.convert(sqrt(2))}
Exemple #22
0
def test_Gaussian_postprocess():
    opt = {'gaussian': True}
    Gaussian.postprocess(opt)

    assert opt == {
        'gaussian': True,
        'extension': {I},
        'domain': QQ.algebraic_field(I),
    }
def test_Gaussian_postprocess():
    opt = {'gaussian': True}
    Gaussian.postprocess(opt)

    assert opt == {
        'gaussian': True,
        'extension': {I},
        'domain': QQ.algebraic_field(I),
    }
Exemple #24
0
def test_dup_real_imag():
    R, x, y = ring("x y", ZZ)

    assert R.dup_real_imag(R.zero) == (0, 0)
    assert R.dup_real_imag(R.one) == (1, 0)

    assert R.dup_real_imag(x + 1) == (x + 1, y)
    assert R.dup_real_imag(x + 2) == (x + 2, y)

    assert R.dup_real_imag(x**2 + 2 * x + 3) == (x**2 - y**2 + 2 * x + 3,
                                                 2 * x * y + 2 * y)

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

    assert R.dup_real_imag(f) == (x**3 + x**2 - 3 * x * y**2 + x - y**2 + 1,
                                  3 * x**2 * y + 2 * x * y - y**3 + y)

    R, x, y = ring("x y", EX)
    pytest.raises(DomainError, lambda: R.dup_real_imag(x + 1))

    R = QQ.algebraic_field(I).poly_ring("x", "y")
    x, y = R.to_ground().gens

    f = R.x**4 + I * R.x**3 - R.x + 1
    r = x**4 - 6 * x**2 * y**2 - 3 * x**2 * y - x + y**4 + y**3 + 1
    i = 4 * x**3 * y + x**3 - 4 * x * y**3 - 3 * x * y**2 - y

    assert R.dup_real_imag(f) == (r, i)

    K = QQ.algebraic_field(sqrt(2))
    R = K.poly_ring("x", "y")
    x, y = R.gens

    f = R.x**2 + sqrt(2) * R.x - 1
    assert R.dup_real_imag(f) == (x**2 - y**2 + sqrt(2) * x - 1,
                                  2 * x * y + sqrt(2) * y)

    K = K.algebraic_field(I)
    R = K.poly_ring("x", "y")
    x, y = R.to_ground().gens

    f = R.x**2 + 2 * sqrt(2) * I * R.x - 1 + I
    assert R.dup_real_imag(f) == (x**2 - y**2 - 2 * sqrt(2) * y - 1,
                                  2 * x * y + 2 * sqrt(2) * x + 1)
def test_to_number_field():
    A = QQ.algebraic_field(sqrt(2))
    assert A.convert(sqrt(2)) == A([1, 0])
    B = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert B.convert(sqrt(2) + sqrt(3)) == B([1, 0])

    K = QQ.algebraic_field(sqrt(2) + sqrt(3))
    a = K([Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)])

    assert B.from_expr(sqrt(2)) == a

    pytest.raises(CoercionFailed, lambda: QQ.algebraic_field(sqrt(3)).convert(sqrt(2)))

    p = x**6 - 6*x**4 - 6*x**3 + 12*x**2 - 36*x + 1
    r0, r1 = p.as_poly(x).all_roots()[:2]
    A = QQ.algebraic_field(r0)
    a = A([Rational(-96, 755), Rational(-54, 755), Rational(128, 151), Rational(936, 755),
           Rational(-1003, 755), Rational(2184, 755)])
    assert A.from_expr(r1) == a
Exemple #26
0
def test_dup_real_imag():
    R, x, y = ring("x y", ZZ)

    assert R.dup_real_imag(R.zero) == (0, 0)
    assert R.dup_real_imag(R.one) == (1, 0)

    assert R.dup_real_imag(x + 1) == (x + 1, y)
    assert R.dup_real_imag(x + 2) == (x + 2, y)

    assert R.dup_real_imag(x**2 + 2*x + 3) == (x**2 - y**2 + 2*x + 3,
                                               2*x*y + 2*y)

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

    assert R.dup_real_imag(f) == (x**3 + x**2 - 3*x*y**2 + x - y**2 + 1,
                                  3*x**2*y + 2*x*y - y**3 + y)

    R, x, y = ring("x y", EX)
    pytest.raises(DomainError, lambda: R.dup_real_imag(x + 1))

    R = QQ.algebraic_field(I).poly_ring("x", "y")
    x, y = R.to_ground().gens

    f = R.x**4 + I*R.x**3 - R.x + 1
    r = x**4 - 6*x**2*y**2 - 3*x**2*y - x + y**4 + y**3 + 1
    i = 4*x**3*y + x**3 - 4*x*y**3 - 3*x*y**2 - y

    assert R.dup_real_imag(f) == (r, i)

    K = QQ.algebraic_field(sqrt(2))
    R = K.poly_ring("x", "y")
    x, y = R.gens

    f = R.x**2 + sqrt(2)*R.x - 1
    assert R.dup_real_imag(f) == (x**2 - y**2 + sqrt(2)*x - 1, 2*x*y + sqrt(2)*y)

    K = K.algebraic_field(I)
    R = K.poly_ring("x", "y")
    x, y = R.to_ground().gens

    f = R.x**2 + 2*sqrt(2)*I*R.x - 1 + I
    assert R.dup_real_imag(f) == (x**2 - y**2 - 2*sqrt(2)*y - 1,
                                  2*x*y + 2*sqrt(2)*x + 1)
def test_sympyissue_5786():
    R,  x, y, z, t = ring("x, y, z, t", QQ.algebraic_field(I))

    f = (z - I*t)*(x - I*y)
    assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 1), (x - I*y, 1)]))

    f = (z - I*t)**2*(x - I*y)
    assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 2), (x - I*y, 1)]))

    f = (z - I*t)*(x - I*y)**3
    assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 1), (x - I*y, 3)]))
Exemple #28
0
def test_sympyissue_5786():
    R, x, y, z, t = ring("x, y, z, t", QQ.algebraic_field(I))

    f = (z - I * t) * (x - I * y)
    assert f.factor_list() == (1, [(z - I * t, 1), (x - I * y, 1)])

    f = (z - I * t)**2 * (x - I * y)
    assert f.factor_list() == (1, [(z - I * t, 2), (x - I * y, 1)])

    f = (z - I * t) * (x - I * y)**3
    assert f.factor_list() == (1, [(z - I * t, 1), (x - I * y, 3)])
Exemple #29
0
def test_modgcd_algebraic_field_random():
    A = QQ.algebraic_field(sqrt(2), sqrt(3))
    R, x, y, z = ring("x, y, z", A)

    h = x**2 * y**3 + 1111 * sqrt(6) / 12 * z
    a, b = 11 * y**3 + 2, (y + x - 1) * h

    assert func_field_modgcd(h * a, h * b) == (h, a, b)

    a, b = 12 * y + 2 * x - 1, (y + x - 1) * h

    assert func_field_modgcd(h * a, h * b) == (h, a, b)
Exemple #30
0
def test_conversion():
    L = QQ.poly_ring(x, y, order="ilex")
    G = QQ.poly_ring(x, y)

    assert L.convert(x) == L.convert(G.convert(x), G)
    assert G.convert(x) == G.convert(L.convert(x), L)
    pytest.raises(CoercionFailed, lambda: G.convert(L.convert(1 / (1 + x)), L))

    R = ALG.poly_ring(x, y)
    assert R.convert(ALG(1), ALG) == R(1)
    pytest.raises(CoercionFailed,
                  lambda: R.convert(ALG(1), QQ.algebraic_field(sqrt(2))))
Exemple #31
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
Exemple #32
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
def test_Extension_postprocess():
    opt = {'extension': {sqrt(2)}}
    Extension.postprocess(opt)

    assert opt == {
        'extension': {sqrt(2)},
        'domain': QQ.algebraic_field(sqrt(2)),
    }

    opt = {'extension': True}
    Extension.postprocess(opt)

    assert opt == {'extension': True}
Exemple #34
0
def test_Extension_postprocess():
    opt = {'extension': {sqrt(2)}}
    Extension.postprocess(opt)

    assert opt == {
        'extension': {sqrt(2)},
        'domain': QQ.algebraic_field(sqrt(2)),
    }

    opt = {'extension': True}
    Extension.postprocess(opt)

    assert opt == {'extension': True}
def test_ComplexInterval():
    R, x = ring("x", QQ.algebraic_field(I))

    f = x**3 + x + I

    _, r1, r2 = R.dup_isolate_complex_roots_sqf(f, blackbox=True)

    assert r1.is_disjoint(r2) is True
    assert r1.is_disjoint(r2, check_re_refinement=True) is False

    for i in range(4):
        r1, r2 = r1.refine(), r2.refine()

    assert r1.is_disjoint(r2, check_re_refinement=True) is True
Exemple #36
0
def test_dup_real_imag():
    assert dup_real_imag([], ZZ) == ([[]], [[]])
    assert dup_real_imag([ZZ(1)], ZZ) == ([[1]], [[]])

    assert dup_real_imag([ZZ(1), ZZ(1)], ZZ) == ([[1], [1]], [[1, 0]])
    assert dup_real_imag([ZZ(1), ZZ(2)], ZZ) == ([[1], [2]], [[1, 0]])

    assert dup_real_imag([ZZ(1), ZZ(2), ZZ(3)], ZZ) == ([[1], [2], [-1, 0, 3]],
                                                        [[2, 0], [2, 0]])

    f = dmp_normal([1, 1, 1, 1], 0, ZZ)

    assert dup_real_imag(f, ZZ) == ([[1], [1], [-3, 0, 1], [-1, 0, 1]],
                                    [[3, 0], [2, 0], [-1, 0, 1, 0]])

    f = dmp_normal([1, 1], 0, EX)

    pytest.raises(DomainError, lambda: dup_real_imag(f, EX))

    A = QQ.algebraic_field(I)
    f = [A(1), A(I), A(0), A(-1), A(1)]

    assert dup_real_imag(f, A) == ([[1], [], [-6, -3, 0], [-1],
                                    [1, 1, 0, 0, 1]],
                                   [[4, 1], [], [-4, -3, 0, 0], [-1, 0]])

    A = QQ.algebraic_field(sqrt(2))
    f = [A(1), A(sqrt(2)), A(-1)]

    assert dup_real_imag(f, A) == ([[1], [A.unit], [-1, 0, -1]],
                                   [[2, 0], [A.unit, 0]])

    A2 = A.algebraic_field(I)
    f = [A2(1), A2(2*sqrt(2)*I), A2(I - 1)]

    assert dup_real_imag(f, A2) == ([[1], [], [-1, -2*A.unit, -1]],
                                    [[2, 2*A.unit], [1]])
Exemple #37
0
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(
        1684996666696915,
        1684996666696914987166688442938726917102321526408785780068975640576)

    R, x = ring("x", ZZ)
    assert ZZ.convert(x - x) == 0
    assert ZZ.convert(x - x, R) == 0

    F3 = FF(3)
    assert F3.convert(Float(2.0)) == F3.dtype(2)
    assert F3.convert(PythonRational(2, 1)) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2)))
    assert F3.convert(2.0) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(2.1))

    assert RR.convert(CC(1)) == RR(1)
    pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2)))

    assert QQ.convert(ALG(1), ALG) == QQ(1)
    pytest.raises(CoercionFailed, lambda: QQ.convert(ALG([1, 1]), ALG))

    assert ZZ.convert(ALG(1), ALG) == ZZ(1)
    pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG([1, 1]), ALG))

    assert EX.convert(ALG([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1

    ALG2 = QQ.algebraic_field(sqrt(2))
    a2 = ALG2.convert(sqrt(2))
    a = ALG.convert(a2, ALG2)
    assert a.rep.to_dense() == [QQ(1, 2), 0, -QQ(9, 2), 0]
    assert RR.convert(a) == RR(1.4142135623730951)
    assert CC.convert(a) == CC(1.4142135623730951)

    assert ZZ_python.convert(3.0) == ZZ_python.dtype(3)
    pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2))

    assert CC.convert(QQ_python(1, 2)) == CC(0.5)
    CC01 = ComplexField(tol=0.1)
    assert CC.convert(CC01(0.3)) == CC(0.3)

    assert RR.convert(complex(2 + 0j)) == RR(2)
    pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j)))

    assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2))
    pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX))

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
def test_dup_count_complex_roots_exclude():
    R, x = ring("x", ZZ)

    f = (x**2 + 1) * (x**2 - 1) * x

    a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1))

    assert R.dup_count_complex_roots(f, a, b) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['S']) == 3
    assert R.dup_count_complex_roots(f, a, b, exclude=['N']) == 3

    assert R.dup_count_complex_roots(f, a, b, exclude=['S', 'N']) == 2

    assert R.dup_count_complex_roots(f, a, b, exclude=['E']) == 4
    assert R.dup_count_complex_roots(f, a, b, exclude=['W']) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['E', 'W']) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['N', 'S', 'E',
                                                       'W']) == 2

    assert R.dup_count_complex_roots(f, a, b, exclude=['SW']) == 3
    assert R.dup_count_complex_roots(f, a, b, exclude=['SE']) == 3

    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE']) == 2
    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S']) == 1
    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S',
                                                       'N']) == 0

    a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1))

    assert R.dup_count_complex_roots(f, a, b, exclude=True) == 1

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**4 + I * x**3 - x + 1

    assert R.dup_count_complex_roots(f, inf=(QQ(0), QQ(0)),
                                     sup=(QQ(1), QQ(1))) == 1

    r = R.dup_isolate_complex_roots_sqf(f)

    assert r == [((QQ(-201, 100), QQ(-201, 100)), (0, 0)),
                 ((QQ(-201, 100), 0), (0, QQ(201, 100))),
                 ((0, QQ(-201, 100)), (QQ(201, 100), 0)),
                 ((0, 0), (QQ(201, 100), QQ(201, 100)))]
    assert all(R.dup_count_complex_roots(f, inf=i, sup=s) == 1 for i, s in r)
def test_dup_count_complex_roots_exclude():
    R, x = ring("x", ZZ)

    f = (x**2 + 1)*(x**2 - 1)*x

    a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1))

    assert R.dup_count_complex_roots(f, a, b) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['S']) == 3
    assert R.dup_count_complex_roots(f, a, b, exclude=['N']) == 3

    assert R.dup_count_complex_roots(f, a, b, exclude=['S', 'N']) == 2

    assert R.dup_count_complex_roots(f, a, b, exclude=['E']) == 4
    assert R.dup_count_complex_roots(f, a, b, exclude=['W']) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['E', 'W']) == 4

    assert R.dup_count_complex_roots(f, a, b, exclude=['N', 'S', 'E', 'W']) == 2

    assert R.dup_count_complex_roots(f, a, b, exclude=['SW']) == 3
    assert R.dup_count_complex_roots(f, a, b, exclude=['SE']) == 3

    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE']) == 2
    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S']) == 1
    assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S', 'N']) == 0

    a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1))

    assert R.dup_count_complex_roots(f, a, b, exclude=True) == 1

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**4 + I*x**3 - x + 1

    assert R.dup_count_complex_roots(f, inf=(QQ(0), QQ(0)), sup=(QQ(1), QQ(1))) == 1

    r = R.dup_isolate_complex_roots_sqf(f)

    assert r == [((QQ(-201, 100), QQ(-201, 100)), (0, 0)),
                 ((QQ(-201, 100), 0), (0, QQ(201, 100))),
                 ((0, QQ(-201, 100)), (QQ(201, 100), 0)),
                 ((0, 0), (QQ(201, 100), QQ(201, 100)))]
    assert all(R.dup_count_complex_roots(f, inf=i, sup=s) == 1
               for i, s in r)
Exemple #40
0
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576)

    R, x = ring("x", ZZ)
    assert ZZ.convert(x - x) == 0
    assert ZZ.convert(x - x, R) == 0

    F3 = FF(3)
    assert F3.convert(Float(2.0)) == F3.dtype(2)
    assert F3.convert(PythonRational(2, 1)) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2)))
    assert F3.convert(2.0) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(2.1))

    assert RR.convert(CC(1)) == RR(1)
    pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2)))

    assert QQ.convert(ALG.new(1), ALG) == QQ(1)
    pytest.raises(CoercionFailed, lambda: QQ.convert(ALG.new([1, 1]), ALG))

    assert ZZ.convert(ALG.new(1), ALG) == ZZ(1)
    pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG.new([1, 1]), ALG))

    assert EX.convert(ALG.new([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1

    ALG2 = QQ.algebraic_field(sqrt(2))
    a2 = ALG2.convert(sqrt(2))
    a = ALG.convert(a2, ALG2)
    assert a.rep.to_dense() == [QQ(1, 2), 0, -QQ(9, 2), 0]

    assert ZZ_python.convert(3.0) == ZZ_python.dtype(3)
    pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2))

    assert CC.convert(QQ_python(1, 2)) == CC(0.5)
    CC01 = ComplexField(tol=0.1)
    assert CC.convert(CC01(0.3)) == CC(0.3)

    assert RR.convert(complex(2 + 0j)) == RR(2)
    pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j)))

    assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2))
    pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX))

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
Exemple #41
0
def test_to_ZZ_ANP_poly():
    A = QQ.algebraic_field(sqrt(2))
    R, x = ring("x", A)
    f = x * (sqrt(2) + 1)

    T, x_, z_ = ring("x_, z_", ZZ)
    f_ = x_ * z_ + x_

    assert _to_ZZ_poly(f, T) == f_
    assert _to_ANP_poly(f_, R) == f

    R, x, t, s = ring("x, t, s", A)
    f = x * t**2 + x * s + sqrt(2)

    D, t_, s_ = ring("t_, s_", ZZ)
    T, x_, z_ = ring("x_, z_", D)
    f_ = (t_**2 + s_) * x_ + z_

    assert _to_ZZ_poly(f, T) == f_
    assert _to_ANP_poly(f_, R) == f
Exemple #42
0
def test_ComplexInterval():
    R, x = ring("x", QQ.algebraic_field(I))

    f = x**3 + x + I

    _, r1, r2 = R.dup_isolate_complex_roots_sqf(f, blackbox=True)

    assert r1.is_disjoint(r2) is True
    assert r1.is_disjoint(r2, check_re_refinement=True) is False

    for i in range(4):
        r1, r2 = r1.refine(), r2.refine()

    assert r1.is_disjoint(r2, check_re_refinement=True) is True

    (u1, v1), (s1, t1) = r1.as_tuple()
    (u2, v2), (s2, t2) = r1.refine(vertical=True).as_tuple()

    assert v1 == v2 and t1 == t2
    assert u1 <= u2 < s2 < s1
Exemple #43
0
def test_to_ZZ_ANP_poly():
    A = QQ.algebraic_field(sqrt(2))
    R, x = ring("x", A)
    f = x*(sqrt(2) + 1)

    T, x_, z_ = ring("x_, z_", ZZ)
    f_ = x_*z_ + x_

    assert _to_ZZ_poly(f, T) == f_
    assert _to_ANP_poly(f_, R) == f

    R, x, t, s = ring("x, t, s", A)
    f = x*t**2 + x*s + sqrt(2)

    D, t_, s_ = ring("t_, s_", ZZ)
    T, x_, z_ = ring("x_, z_", D)
    f_ = (t_**2 + s_)*x_ + z_

    assert _to_ZZ_poly(f, T) == f_
    assert _to_ANP_poly(f_, R) == f
Exemple #44
0
def test_PolyElement_gcd():
    R,  x, y = ring("x,y", QQ)

    f = x**2/2 + x + QQ(1, 2)
    g = x/2 + QQ(1, 2)

    assert f.gcd(g) == x + 1

    with using(use_heu_gcd=False):
        assert f.gcd(g) == x + 1
    with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'):
        assert f.gcd(g) == x + 1

    R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2)))

    f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y

    assert f.gcd(g) == g
    with using(gcd_aa_method='modgcd'):
        assert f.gcd(g) == g
Exemple #45
0
def test_dup_isolate_real_roots_sqf():
    R, x = ring("x", ZZ)

    assert R.dup_isolate_real_roots_sqf(0) == []
    assert R.dup_isolate_real_roots_sqf(5) == []

    f = x * (x + 1)

    assert R.dup_isolate_real_roots_sqf(f) == [(-1, -1), (0, 0)]
    assert R.dup_isolate_real_roots_sqf(f, inf=+1) == []
    assert R.dup_isolate_real_roots_sqf(f, sup=-1) == [(-1, -1)]
    assert R.dup_isolate_real_roots_sqf(f, sup=-2) == []

    f = x * (x - 1)

    assert R.dup_isolate_real_roots_sqf(f) == [(0, 0), (1, 1)]

    assert R.dup_isolate_real_roots_sqf(x**4 + x + 1) == []

    i = [(-2, -1), (1, 2)]
    f = x**2 - 2

    assert R.dup_isolate_real_roots_sqf(+f) == i
    assert R.dup_isolate_real_roots_sqf(-f) == i

    for r in range(2, 7):
        for s in (1, 10, -1, -10):
            f = prod(x - s * _ for _ in range(1, r))
            ans = sorted((s * _, s * _) for _ in range(1, r))
            assert R.dup_isolate_real_roots_sqf(f) == ans

    assert R.dup_isolate_real_roots_sqf(x**2 - 5) == [(-3, -2), (2, 3)]
    assert R.dup_isolate_real_roots_sqf(x**3 - 5) == [(1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**4 - 5) == [(-2, -1), (1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**5 - 5) == [(1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**6 - 5) == [(-2, -1), (1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**7 - 5) == [(1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**8 - 5) == [(-2, -1), (1, 2)]
    assert R.dup_isolate_real_roots_sqf(x**9 - 5) == [(1, 2)]

    for roots in subsets(range(1, 4)):
        f = prod(x - r for r in roots)
        ans = sorted((_, _) for _ in roots)
        assert R.dup_isolate_real_roots_sqf(f) == ans

    assert R.dup_isolate_real_roots_sqf((x - 3)*(x - 2)*(x - 1)*(x + 1)*(x + 2)*(x + 3)*(2*x + 1)) == \
        [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (1, 1), (2, 2), (3, 3)]
    assert R.dup_isolate_real_roots_sqf((x - 3)*(x - 2)*(x - 1)*(x + 1)*(x + 2)*(x + 3)*(2*x - 1)*(2*x + 1)) == \
        [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (0, 1), (1, 1), (2, 2), (3, 3)]

    f = 9 * x**2 - 2

    assert R.dup_isolate_real_roots_sqf(f) == \
        [(-1, 0), (0, 1)]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10)) == \
        [(QQ(-1, 2), QQ(-3, 7)), (QQ(3, 7), QQ(1, 2))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
        [(QQ(-9, 19), QQ(-8, 17)), (QQ(8, 17), QQ(9, 19))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == \
        [(QQ(-33, 70), QQ(-8, 17)), (QQ(8, 17), QQ(33, 70))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10000)) == \
        [(QQ(-33, 70), QQ(-107, 227)), (QQ(107, 227), QQ(33, 70))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \
        [(QQ(-305, 647), QQ(-272, 577)), (QQ(272, 577), QQ(305, 647))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000000)) == \
        [(QQ(-1121, 2378), QQ(-272, 577)), (QQ(272, 577), QQ(1121, 2378))]

    f = (x - 2) * (x - 1) * (2 * x - 1) * (10002 * x - 1) * (10003 * x - 1)

    assert R.dup_isolate_real_roots_sqf(f) == \
        [(QQ(15, 150046), QQ(47, 470110)), (QQ(47, 470110), QQ(17, 170018)),
         (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \
        [(QQ(1, 10003), QQ(1, 10003)), (QQ(1, 10002), QQ(1, 10002)),
         (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)]

    a, b, c, d = 10000090000001, 2000100003, 10000300007, 10000005000008
    f = (x - d) * (x + a) * (b * x + 1) * (c * x - 1)

    assert R.dup_isolate_real_roots_sqf(f) == \
        [(-13194139533313, -8796093022209), (-1, 0), (0, 1),
         (8796093022209, 13194139533313)]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \
        [(-a, -a), (QQ(-7, 13958643719), QQ(-1, 2013265921)),
         (QQ(3, 30064771075), QQ(1, 9663676417)),
         (QQ(1328823874562668133568119, 132882321015),
          QQ(37336367728494399224248237, 3733634906029))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000000000000000000000000000)) == \
        [(-a, -a), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (d, d)]

    f = -2 * (x - 2) * (x + 2) * (5 * x**2 - 4 * x - 20)

    assert R.dup_isolate_real_roots_sqf(f) == \
        [(-2, -2), (-2, -1), (2, 2), (2, 3)]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
        [(-2, -2), (-QQ(23, 14), -QQ(18, 11)), (2, 2), (QQ(39, 16), QQ(22, 9))]

    f = x - 1

    assert R.dup_isolate_real_roots_sqf(f, inf=2) == []
    assert R.dup_isolate_real_roots_sqf(f, sup=0) == []
    assert R.dup_isolate_real_roots_sqf(f) == [(1, 1)]
    assert R.dup_isolate_real_roots_sqf(f, inf=1) == [(1, 1)]
    assert R.dup_isolate_real_roots_sqf(f, sup=1) == [(1, 1)]
    assert R.dup_isolate_real_roots_sqf(f, inf=1, sup=1) == [(1, 1)]

    f = x**2 - 2

    assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 4)) == []
    assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 5)) == [(QQ(7,
                                                                 5), QQ(3, 2))]
    assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 5)) == [(-2, -1)]
    assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 4)) == [(-2, -1),
                                                             (1, QQ(3, 2))]
    assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 4)) == []
    assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 5)) == [(-QQ(3, 2),
                                                               -QQ(7, 5))]
    assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 5)) == [(1, 2)]
    assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 4)) == [(-QQ(3, 2), -1),
                                                              (1, 2)]

    i = [(-2, -1), (1, 2)]

    assert R.dup_isolate_real_roots_sqf(f, inf=-2) == i
    assert R.dup_isolate_real_roots_sqf(f, sup=+2) == i
    assert R.dup_isolate_real_roots_sqf(f, inf=-2, sup=2) == i
    assert R.dup_isolate_real_roots_sqf(f, inf=+1) == [i[1]]
    assert R.dup_isolate_real_roots_sqf(f, sup=-1) == [i[0]]

    R, x = ring("x", QQ)

    f = (6 * x - 85) * (1028 * x + 1) / 3855

    assert R.dup_isolate_real_roots_sqf(f) == [(-1, 0), (14, 15)]
    assert [
        _.as_tuple() for _ in R.dup_isolate_real_roots_sqf(f, blackbox=True)
    ] == [(-1, 0), (14, 15)]

    R, x = ring("x", EX)

    pytest.raises(DomainError, lambda: R.dup_isolate_real_roots_sqf(x + 3))

    R, x = ring("x", QQ.algebraic_field(I))

    f = (x - 1) * (x**3 + I * x - 2)

    assert R.dup_isolate_real_roots_sqf(f) == [(1, 1)]
    assert R.dup_isolate_real_roots_sqf(f, sup=0) == []

    f = (x**2 - 2) * (x**3 - x + I)

    assert R.dup_isolate_real_roots_sqf(f) == [(QQ(-3, 2), QQ(-4, 3)),
                                               (QQ(4, 3), QQ(3, 2))]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10),
                                        inf=0) == [(QQ(7, 5), QQ(10, 7))]

    assert R.dup_isolate_real_roots_sqf(x) == [(0, 0)]
    assert R.dup_isolate_real_roots_sqf(x - 1) == [(1, 1)]
    assert R.dup_isolate_real_roots_sqf(x - I) == []

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = (-x**3 + sqrt(2) * x - 1) * (x**2 + 1)

    assert R.dup_isolate_real_roots_sqf(f) == [(-2, -1)]
    assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [(QQ(-132, 91),
                                                                 QQ(-29, 20))]

    f = (x - sqrt(2)) * (x + 2 * sqrt(2)) * (x - 7 + sqrt(2)) * (
        x + 3 * sqrt(2)) * (x - 1) * (x + 1 - sqrt(2))

    assert R.dup_isolate_real_roots_sqf(f) == [(-5, -4), (-3, -2), (0, 1),
                                               (1, 1), (1, 2), (5, 6)]

    R, x = ring("x", QQ.algebraic_field(sqrt(2), sqrt(3)))

    f = (x - sqrt(2)) * (x - sqrt(3)) * (x - 2 * sqrt(6)) * (x - sqrt(6)) * (
        x**2 + 2)

    assert R.dup_isolate_real_roots_sqf(f) == [(1, QQ(3, 2)), (QQ(3, 2), 2),
                                               (2, 3), (4, 5)]
    assert (R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [
        (QQ(41, 29), QQ(58, 41)), (QQ(71, 41), QQ(97, 56)),
        (QQ(218, 89), QQ(49, 20)), (QQ(436, 89), QQ(485, 99))
    ])
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)

    assert Domain.preprocess('Z') == ZZ
    assert Domain.preprocess('Q') == QQ

    assert Domain.preprocess('ZZ') == ZZ
    assert Domain.preprocess('QQ') == QQ

    assert Domain.preprocess('EX') == EX

    assert Domain.preprocess('FF(23)') == FF(23)
    assert Domain.preprocess('GF(23)') == GF(23)

    pytest.raises(OptionError, lambda: Domain.preprocess('Z[]'))

    assert Domain.preprocess('Z[x]') == ZZ.poly_ring(x)
    assert Domain.preprocess('Q[x]') == QQ.poly_ring(x)

    assert Domain.preprocess('ZZ[x]') == ZZ.poly_ring(x)
    assert Domain.preprocess('QQ[x]') == QQ.poly_ring(x)

    assert Domain.preprocess('Z[x,y]') == ZZ.poly_ring(x, y)
    assert Domain.preprocess('Q[x,y]') == QQ.poly_ring(x, y)

    assert Domain.preprocess('ZZ[x,y]') == ZZ.poly_ring(x, y)
    assert Domain.preprocess('QQ[x,y]') == QQ.poly_ring(x, y)

    pytest.raises(OptionError, lambda: Domain.preprocess('Z()'))

    assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('Q(x)') == QQ.frac_field(x)

    assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)

    assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
    assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)

    assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
    assert Domain.preprocess(
        'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)

    pytest.raises(OptionError, lambda: Domain.preprocess('abc'))

    assert Domain.preprocess('RR') == RR
    assert Domain.preprocess('RR_5') == RealField(prec=5)

    assert Domain.preprocess('CC') == CC
    assert Domain.preprocess('CC_5') == ComplexField(prec=5)

    pytest.raises(OptionError, lambda: Domain.preprocess(()))
Exemple #47
0
from diofant.abc import x, y, z
from diofant.domains import CC, EX, FF, GF, QQ, RR, ZZ, QQ_python, ZZ_python
from diofant.domains.algebraicfield import AlgebraicField
from diofant.domains.complexfield import ComplexField
from diofant.domains.domainelement import DomainElement
from diofant.domains.groundtypes import PythonRational
from diofant.domains.realfield import RealField
from diofant.polys import RootOf, field, ring, roots
from diofant.polys.polyerrors import (CoercionFailed, DomainError,
                                      GeneratorsError, GeneratorsNeeded,
                                      NotInvertible, UnificationFailed)


__all__ = ()

ALG = QQ.algebraic_field(sqrt(2), sqrt(3))


def unify(K0, K1):
    return K0.unify(K1)


def test_Domain_interface():
    pytest.raises(TypeError, lambda: DomainElement().parent)

    assert RR(1).parent is RR
    assert CC(1).parent is CC

    assert RR.has_default_precision
    assert CC.has_default_precision
Exemple #48
0
"""Tests for the PolynomialRing classes. """

import pytest

from diofant import sqrt
from diofant.abc import x, y
from diofant.domains import QQ, ZZ
from diofant.polys.orderings import build_product_order
from diofant.polys.polyerrors import CoercionFailed, GeneratorsNeeded

__all__ = ()

ALG = QQ.algebraic_field(sqrt(2), sqrt(3))


def test_build_order():
    R = QQ.poly_ring(x,
                     y,
                     order=build_product_order((("lex", x), ("ilex", y)),
                                               (x, y)))
    assert R.order((1, 5)) == ((1, ), (-5, ))


def test_globalring():
    Qxy = QQ.frac_field(x, y)
    R = QQ.poly_ring(x, y)
    X = R.convert(x)
    Y = R.convert(y)

    assert x in R
    assert 1 / x not in R
Exemple #49
0
def test_sympyissue_14294():
    A = QQ.algebraic_field(I)

    a = A.convert(I)
    assert A.convert(a) == a
Exemple #50
0
def test_dup_isolate_complex_roots_sqf():
    R, x = ring("x", ZZ)

    f = x**2 - 2 * x + 3

    assert R.dup_isolate_complex_roots_sqf(f) == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]
    assert [r.as_tuple() for r in R.dup_isolate_complex_roots_sqf(f, blackbox=True)] == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]

    assert R.dup_isolate_complex_roots_sqf(f, inf=1,
                                           sup=3) == [((1, -3), (3, 0)),
                                                      ((1, 0), (3, 3))]
    assert R.dup_isolate_complex_roots_sqf(f, inf=(1, 0),
                                           sup=3) == [((1, 0), (3, 3))]
    assert R.dup_isolate_complex_roots_sqf(f, inf=(1, QQ(-1, 2)),
                                           sup=3) == [((1, 0), (3, 3))]
    assert R.dup_isolate_complex_roots_sqf(f, inf=(1, -3),
                                           sup=(3, -1)) == [((1, -3), (3, -1))]
    assert R.dup_isolate_complex_roots_sqf(f, inf=0, sup=QQ(1, 6)) == []

    assert R.dup_isolate_complex_roots_sqf(R.zero) == []

    pytest.raises(ValueError,
                  lambda: R.dup_isolate_complex_roots_sqf(f, inf=1, sup=1))

    assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 10)) == \
        [((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))),
         ((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))]
    assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 100)) == \
        [((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))),
         ((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))]

    f = 7 * x**4 - 19 * x**3 + 20 * x**2 + 17 * x + 20

    assert R.dup_isolate_complex_roots_sqf(f) == \
        [((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))),
         ((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))]

    R, x = ring("x", QQ)

    f = x**2 / 2 - 3 * x / 7 + 1

    assert R.dup_isolate_complex_roots_sqf(f) == [((0, -4), (4, 0)),
                                                  ((0, 0), (4, 4))]

    R, x = ring("x", EX)

    pytest.raises(
        DomainError,
        lambda: R.dup_isolate_complex_roots_sqf(x, inf=(-1, 0), sup=(1, 1)))

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**4 + I * x**3 - x + 1

    assert R.dup_isolate_complex_roots_sqf(f, inf=(0, 0), sup=(1, 1)) == [
        ((0, 0), (1, QQ(1, 2)))
    ]
    assert R.dup_isolate_complex_roots_sqf(f,
                                           inf=(0, 0),
                                           sup=(1, 1),
                                           eps=QQ(1, 100)) == [
                                               ((QQ(79, 128), QQ(19, 64)),
                                                (QQ(5, 8), QQ(39, 128)))
                                           ]
    assert R.dup_isolate_complex_roots_sqf(f, inf=(0, -1), sup=(1, 1)) == [
        ((0, -1), (1, QQ(-1, 2))), ((0, 0), (1, QQ(1, 2)))
    ]
    assert R.dup_isolate_complex_roots_sqf(f,
                                           inf=(0, -1),
                                           sup=(1, 1),
                                           eps=QQ(1, 100)) == [
                                               ((QQ(79, 128), QQ(19, 64)),
                                                (QQ(5, 8), QQ(39, 128))),
                                               ((QQ(45, 64), QQ(-91, 128)),
                                                (QQ(91, 128), QQ(-45, 64)))
                                           ]

    f *= (x - 1)

    assert R.dup_isolate_complex_roots_sqf(f) == [
        ((QQ(-401, 100), QQ(-401, 100)), (0, 0)),
        ((QQ(-401, 100), 0), (0, QQ(401, 100))),
        ((0, QQ(-401, 100)), (QQ(401, 100), 0)),
        ((0, 0), (QQ(401, 100), QQ(401, 100)))
    ]

    f = x**7 + I * x**4 - (2 + I) * x**3 - 3 * x + 5

    assert R.dup_isolate_complex_roots_sqf(f) == [
        ((QQ(-1001, 100), 0), (0, QQ(1001, 100))),
        ((QQ(-1001, 400), QQ(-1001, 800)), (QQ(-1001, 800), 0)),
        ((QQ(-1001, 800), QQ(-1001, 800)), (0, 0)),
        ((0, QQ(-1001, 400)), (QQ(1001, 400), QQ(-1001, 800))),
        ((0, QQ(-1001, 800)), (QQ(1001, 400), 0)),
        ((0, 0), (QQ(1001, 400), QQ(1001, 800))),
        ((0, QQ(1001, 800)), (QQ(1001, 400), QQ(1001, 400)))
    ]

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = -x**3 + sqrt(2) * x - 1

    assert R.dup_isolate_complex_roots_sqf(f) == [
        ((0, QQ(-283, 100)), (QQ(283, 100), 0)),
        ((0, 0), (QQ(283, 100), QQ(283, 100)))
    ]

    R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I))

    f = -x**3 + I * x**2 + sqrt(2) * x - 1

    assert R.dup_isolate_complex_roots_sqf(f) == [
        ((QQ(-283, 100), 0), (0, QQ(283, 100))),
        ((0, QQ(-283, 100)), (QQ(283, 100), 0)),
        ((0, 0), (QQ(283, 100), QQ(283, 100)))
    ]

    R, x = ring("x", EX)

    pytest.raises(DomainError, lambda: R.dup_isolate_complex_roots_sqf(x))
def test_dmp_gcd():
    R, x = ring("x", ZZ)

    f, g = 0, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0)

    f, g = 2, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0)

    f, g = -2, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0)

    f, g = 0, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1)

    f, g = 0, 2*x + 4
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)

    f, g = 2*x + 4, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)

    f, g = 2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1)

    f, g = -2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1)

    f, g = 2, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1)

    f, g = -2, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1)

    f, g = x**2 + 2*x + 1, 1
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)

    f, g = x**2 + 2*x + 1, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)

    f, g = 2*x**2 + 4*x + 2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)

    f, g = 2, 2*x**2 + 4*x + 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)

    f, g = 2*x**2 + 4*x + 2, x + 1
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)

    f, g = x + 1, 2*x**2 + 4*x + 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)

    f, g = x - 31, x
    assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g)

    f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
    g = x**3 + 6*x**2 + 11*x + 6

    h = x**2 + 3*x + 2

    cff = x**2 + 5*x + 4
    cfg = x + 3

    assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
    assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)

    f = x**4 - 4
    g = x**4 + 4*x**2 + 4

    h = x**2 + 2

    cff = x**2 - 2
    cfg = x**2 + 2

    assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
    assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)

    f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21

    h = 1

    cff = f
    cfg = g

    assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
    assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)

    R, x = ring("x", QQ)

    f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21

    h = 1

    cff = f
    cfg = g

    assert R.dmp_qq_heu_gcd(f, g) == (h, cff, cfg)
    assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg)

    assert R.dup_ff_prs_gcd(R.zero, R.zero) == ([], [], [])

    R, x = ring("x", ZZ)

    f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
        + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
        + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
        + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
        - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
        + 289127344604779611146960547954288113529690984687482920704*x**14 \
        + 19007977035740498977629742919480623972236450681*x**7 \
        + 311973482284542371301330321821976049

    g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
        + 197599133478719444145775798221171663643171734081650688*x**14 \
        - 9504116979659010018253915765478924103928886144*x**7 \
        - 311973482284542371301330321821976049

    assert R.dmp_zz_heu_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g
    assert R.dup_rr_prs_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g

    R, x = ring("x", QQ)

    f = x**2/2 + x + QQ(1, 2)
    g = x/2 + QQ(1, 2)

    h = x + 1

    assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2))
    assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1, 2))

    R, x = ring("x", ZZ)

    f = 1317378933230047068160*x + 2945748836994210856960
    g = 120352542776360960*x + 269116466014453760

    h = 120352542776360960*x + 269116466014453760
    cff = 10946
    cfg = 1

    assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)

    with using(heu_gcd_max=0):
        pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g))

    R, x = ring("x", CC)
    f, g = (x**2 - 1, x**3 - 3*x + 2)
    assert R.dmp_inner_gcd(f, g) == (1, f, g)

    R, x, y = ring("x,y", CC)
    f, g = (x**2 - y, x**3 - y*x + 2)
    assert R.dmp_inner_gcd(f, g) == (1, f, g)

    R,  x, y = ring("x,y", ZZ)

    f, g = 0, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0)

    f, g = 2, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0)

    f, g = -2, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0)

    f, g = 0, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1)

    f, g = 0, 2*x + 4
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)

    f, g = 2*x + 4, 0
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)

    f, g = 2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1)

    f, g = -2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1)

    f, g = 2, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1)

    f, g = -2, -2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1)

    f, g = x**2 + 2*x + 1, 1
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)

    f, g = x**2 + 2*x + 1, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
    with using(use_simplify_gcd=0):
        assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)

    f, g = 2*x**2 + 4*x + 2, 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)

    f, g = 2, 2*x**2 + 4*x + 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)

    f, g = 2*x**2 + 4*x + 2, x + 1
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)

    f, g = x + 1, 2*x**2 + 4*x + 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)

    with using(heu_gcd_max=0):
        pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g))

    f = x**2 + 2*x*y + y**2
    g = x**2 + x*y

    assert R.dmp_rr_prs_gcd(f, g) == (x + y, x + y, x)

    R, x, y, z, u = ring("x,y,z,u", ZZ)

    f, g = u**2 + 2*u + 1, 2*u + 2
    assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2)

    f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
    h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1

    assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
    assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg)

    assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff)
    assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff)

    R, x, y, z = ring("x,y,z", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    H, cff, cfg = R.dmp_rr_prs_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y, z = ring("x,y,z", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    H, cff, cfg = R.dmp_rr_prs_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ))
    H, cff, cfg = R.dmp_zz_heu_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    H, cff, cfg = R.dmp_rr_prs_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)

    f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ))
    H, cff, cfg = R.dmp_inner_gcd(f, g)

    assert H == h and R.dmp_mul(H, cff) == f \
        and R.dmp_mul(H, cfg) == g

    R, x, y = ring("x,y", QQ)

    f = x**2/2 + x + QQ(1, 2)
    g = x/2 + QQ(1, 2)

    h = x + 1

    assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2))
    assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2))
    with using(use_simplify_gcd=0):
        assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2))
        assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2))

    assert R.dmp_ff_prs_gcd(R.zero, R.zero) == (0, 0, 0)
    assert R.dmp_qq_heu_gcd(R.zero, R.zero) == (0, 0, 0)
    assert R.dmp_ff_prs_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2))
    assert R.dmp_qq_heu_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2))

    R, x, y = ring("x,y", RR)

    f = 2.1*x*y**2 - 2.2*x*y + 2.1*x
    g = 1.0*x**3

    assert R.dmp_ff_prs_gcd(f, g) == \
        (1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2)

    R, x, y = ring("x,y", ZZ)

    f = (-17434367009167300000000000000000000000000000000000000000000000000000000*x**4*y -
         250501827896299135568887342575961783764139560000000000000000000000000000000000000000000*x**3*y -
         2440935909299672540738135183426056447877858000000000000000000000000000000*x**3 -
         1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000*x**2*y -
         26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000*x**2 -
         3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000*x*y -
         94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800*x -
         2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993*y -
         113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290)

    g = (10000000000000000000000000000*x**2 + 71841388839807267676152024786000000000000000*x +
         129029628760809605749020969023932901278290735413660734705971)

    assert (R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) ==
            (g,
             -1743436700916730000000000000000000000000000*x**2*y -
             12525091394814956778444367128798089188206978000000000000000*x*y -
             244093590929967254073813518342605644787785800*x -
             22495499028725631994927113634418779135935898997901327211111875586270479483*y -
             876801128965234839118530545935732755107147297241756982389990, 1))

    R, x = ring("x", ZZ)

    f, g = x**2 - 1, x**2 - 3*x + 2
    assert R.dmp_gcd(f, g) == x - 1

    with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'):
        R.dmp_gcd(f, g) == x - 1

    R, x = ring("x", QQ)

    f, g = x**2/2 + x + QQ(1, 2), x/2 + QQ(1, 2)

    assert R.dmp_gcd(f, g) == x + 1
    with using(use_heu_gcd=False):
        R.dmp_gcd(f, g) == x + 1

    R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2)))

    f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y

    assert R.dmp_gcd(f, g) == g
    with using(gcd_aa_method='modgcd'):
        assert R.dmp_gcd(f, g) == g
Exemple #52
0
def test_AlgebraicElement():
    A = QQ.algebraic_field(I)

    rep = [QQ(1), QQ(1)]
    mod = [QQ(1), QQ(0), QQ(1)]

    f = A(rep)

    assert f.rep.to_dense() == rep
    assert f.mod.to_dense() == mod
    assert f.domain.domain == QQ

    f = A(1)

    assert f.rep.to_dense() == [QQ(1)]
    assert f.mod.to_dense() == mod
    assert f.domain.domain == QQ

    f = A([QQ(3, 2)])

    assert f.rep.to_dense() == [QQ(3, 2)]
    assert f.mod.to_dense() == mod
    assert f.domain.domain == QQ

    B = QQ.algebraic_field(I*sqrt(2))

    a = A([QQ(1), QQ(1)])
    b = B([QQ(1), QQ(1)])

    assert (a == a) is True
    assert (a != a) is False

    assert (a == b) is False
    assert (a != b) is True

    b = A([QQ(1), QQ(2)])

    assert (a == b) is False
    assert (a != b) is True

    assert A([1, 1]) == A([int(1), int(1)])
    assert hash(A([1, 1])) == hash(A([int(1), int(1)]))

    assert a.to_dict() == {(0,): QQ(1), (1,): QQ(1)}

    assert bool(A([])) is False
    assert bool(A([QQ(1)])) is True

    a = A([QQ(1), -QQ(1), QQ(2)])
    assert a.LC() == -1
    assert a.rep.to_dense() == [-1, 1]

    A = QQ.algebraic_field(root(2, 3))

    assert A.unit > 0
    assert A.unit >= 0
    assert (A.unit < 0) is False
    assert (A.unit <= 0) is False
    pytest.raises(TypeError, lambda: A.unit > x)
    pytest.raises(TypeError, lambda: QQ.algebraic_field(I).unit > 0)

    assert abs(+A.unit) == A.unit
    assert abs(-A.unit) == A.unit

    a = A([QQ(2), QQ(-1), QQ(1)])
    b = A([QQ(1), QQ(2)])

    c = A([QQ(-2), QQ(1), QQ(-1)])

    assert +a == a
    assert -a == c

    c = A([QQ(2), QQ(0), QQ(3)])

    assert a + b == c
    assert b + a == c

    assert c + 1 == A([QQ(2), QQ(0), QQ(4)])
    pytest.raises(TypeError, lambda: c + "x")
    pytest.raises(TypeError, lambda: "x" + c)

    c = A([QQ(2), QQ(-2), QQ(-1)])

    assert a - b == c

    c = A([QQ(-2), QQ(2), QQ(1)])

    assert b - a == c

    assert c - 1 == A([QQ(-2), QQ(2), QQ(0)])
    pytest.raises(TypeError, lambda: c - "x")
    pytest.raises(TypeError, lambda: "x" - c)

    c = A([QQ(3), QQ(-1), QQ(6)])

    assert a*b == c
    assert b*a == c

    assert c*2 == A([QQ(6), QQ(-2), QQ(12)])
    pytest.raises(TypeError, lambda: c*"x")
    pytest.raises(TypeError, lambda: "x"*c)

    c = A([QQ(11, 10), -QQ(1, 5), -QQ(3, 5)])

    assert c/2 == A([QQ(11, 20), -QQ(1, 10), -QQ(3, 10)])
    pytest.raises(TypeError, lambda: c/"x")
    pytest.raises(TypeError, lambda: "x"/c)

    c = A([QQ(-1, 43), QQ(9, 43), QQ(5, 43)])

    assert a**0 == A(1)
    assert a**1 == a
    assert a**-1 == c
    pytest.raises(TypeError, lambda: a**QQ(1, 2))

    assert a*a**(-1) == A(1)

    A = QQ.algebraic_field(I)

    a = A([QQ(1, 2), QQ(1), QQ(2)])
    b = A([ZZ(1), ZZ(1), ZZ(2)])
    c = A([QQ(3, 2), QQ(2), QQ(4)])
    assert a + b == b + a == c
def test_dmp_ext_factor():
    R, x = ring("x", QQ.algebraic_field(I))

    assert R.dmp_ext_factor(0) == (R.domain(0), [])

    f = x + 1

    assert R.dmp_ext_factor(f) == (R.domain(1), [(f, 1)])

    g = 2*x + 2

    assert R.dmp_ext_factor(g) == (R.domain(2), [(f, 1)])

    f = 7*x**4 + 1
    g = x**4 + QQ(1, 7)

    assert R.dmp_ext_factor(f) == (R.domain(7), [(g, 1)])

    f = x**4 + 1

    assert R.dmp_ext_factor(f) == (R.domain(1), [(x**2 - I, 1), (x**2 + I, 1)])

    f = 4*x**2 + 9

    assert R.dmp_ext_factor(f) == (R.domain(4), [(x - 3*I/2, 1), (x + 3*I/2, 1)])

    f = 4*x**4 + 8*x**3 + 77*x**2 + 18*x + 153

    assert R.dmp_ext_factor(f) == (4, [(x - 3*I/2, 1), (x + 1 + 4*I, 1),
                                       (x + 1 - 4*I, 1), (x + 3*I/2, 1)])

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))

    f = x**4 + 1

    assert R.dmp_ext_factor(f) == (R.domain(1), [(x**2 - sqrt(2)*x + 1, 1),
                                                 (x**2 + sqrt(2)*x + 1, 1)])

    f = x**2 + 2*sqrt(2)*x + 2

    assert R.dmp_ext_factor(f) == (R.domain(1), [(x + sqrt(2), 2)])
    assert R.dmp_ext_factor(f**3) == (R.domain(1), [(x + sqrt(2), 6)])

    f *= 2

    assert R.dmp_ext_factor(f) == (R.domain(2), [(x + sqrt(2), 2)])
    assert R.dmp_ext_factor(f**3) == (R.domain(8), [(x + sqrt(2), 6)])

    R,  x, y = ring("x,y", QQ.algebraic_field(sqrt(2)))

    assert R.dmp_ext_factor(0) == (R.domain(0), [])

    f = x + 1

    assert R.dmp_ext_factor(f) == (R.domain(1), [(f, 1)])

    g = 2*x + 2

    assert R.dmp_ext_factor(g) == (R.domain(2), [(f, 1)])

    f = x**2 - 2*y**2

    assert R.dmp_ext_factor(f) == (R.domain(1), [(x - sqrt(2)*y, 1),
                                                 (x + sqrt(2)*y, 1)])

    f = 2*x**2 - 4*y**2

    assert R.dmp_ext_factor(f) == (R.domain(2), [(x - sqrt(2)*y, 1),
                                                 (x + sqrt(2)*y, 1)])

    R,  x = ring("x", QQ.algebraic_field(I))
    f = x**2 + 1
    assert R.dmp_ext_factor(f) == (R.domain(1), [(x - I, 1), (x + I, 1)])
def test_dmp_factor_list():
    R, x = ring("x", ZZ)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(7) == (7, [])

    R, x = ring("x", QQ)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ.poly_ring('t'))
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(7) == (7, [])

    R, x = ring("x", QQ.poly_ring('t'))
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ)

    assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    # issue sympy/sympy#8037
    assert R.dmp_factor_list(6*x**2 - 5*x - 6) == (1, [(2*x - 3, 1), (3*x + 2, 1)])

    R, x = ring("x", QQ)
    assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert R.dmp_factor_list(x**2 + 1) == (1, [(x + 1, 2)])

    R, x = ring("x", RR)
    assert R.dmp_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
    assert R.dmp_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])

    f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
    coeff, factors = R.dmp_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

    # issue diofant/diofant#238
    f = 0.1*x**2 + 1.1*x + 1.0
    assert R.dmp_factor_list(f) == (10.0, [(0.1*x + 0.1, 1), (0.1*x + 1.0, 1)])
    f = 0.25 + 1.0*x + 1.0*x**2
    assert R.dmp_factor_list(f) == (4.0, [(0.25 + 0.5*x, 2)])

    Rt, t = ring("t", ZZ)
    R, x = ring("x", Rt)

    f = 4*t*x**2 + 4*t**2*x

    assert R.dmp_factor_list(f) == (4*t, [(x, 1), (x + t, 1)])

    Rt, t = ring("t", QQ)
    R, x = ring("x", Rt)

    f = t*x**2/2 + t**2*x/2

    assert R.dmp_factor_list(f) == (t/2, [(x, 1), (x + t, 1)])

    R, x = ring("x", QQ.algebraic_field(I))

    f = x**4 + 2*x**2

    assert R.dmp_factor_list(f) == (R.domain(1), [(x, 2), (x**2 + 2, 1)])

    R, x = ring("x", EX)
    pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(0) == (ZZ(0), [])
    assert R.dmp_factor_list(7) == (7, [])

    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(0) == (QQ(0), [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    Rt, t = ring("t", ZZ)
    R, x, y = ring("x,y", Rt)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(7) == (ZZ(7), [])

    Rt, t = ring("t", QQ)
    R, x, y = ring("x,y", Rt)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, *X = ring("x:200", ZZ)

    f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    R, x = ring("x", ZZ)
    assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    R, x = ring("x", QQ)
    assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    f = 4*x**2*y + 4*x*y**2

    assert R.dmp_factor_list(f) == (4, [(y, 1), (x, 1), (x + y, 1)])

    R,  x, y = ring("x,y", QQ)
    f = x**2*y/2 + x*y**2/2

    assert R.dmp_factor_list(f) == (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)])

    R,  x, y = ring("x,y", RR)
    f = 2.0*x**2 - 8.0*y**2

    assert R.dmp_factor_list(f) == (RR(2.0), [(1.0*x - 2.0*y, 1), (1.0*x + 2.0*y, 1)])

    f = 6.7225336055071*x**2*y**2 - 10.6463972754741*x*y - 0.33469524022264
    coeff, factors = R.dmp_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

    # issue diofant/diofant#238
    R,  x, y, z = ring("x,y,z", RR)
    f = x*y + x*z + 0.1*y + 0.1*z
    assert R.dmp_factor_list(f) == (10.0, [(0.1*y + 0.1*z, 1), (x + 0.1, 1)])
    f = 0.25*x**2 + 1.0*x*y*z + 1.0*y**2*z**2
    assert R.dmp_factor_list(f) == (4.0, [(0.25*x + 0.5*y*z, 2)])

    Rt, t = ring("t", ZZ)
    R, x, y = ring("x,y", Rt)
    f = 4*t*x**2 + 4*t**2*x

    assert R.dmp_factor_list(f) == (4*t, [(x, 1), (x + t, 1)])

    Rt, t = ring("t", QQ)
    R, x, y = ring("x,y", Rt)
    f = t*x**2/2 + t**2*x/2

    assert R.dmp_factor_list(f) == (t/2, [(x, 1), (x + t, 1)])

    R, x, y = ring("x,y", FF(2))
    pytest.raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))

    R, x, y = ring("x,y", EX)
    pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
Exemple #55
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    assert str(K([1, 0])) == 'sqrt(2)'
Exemple #56
0
def test_AlgebraicField():
    sT(QQ.algebraic_field(sqrt(2)),
       "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))" % repr(QQ))
def test_field_isomorphism():
    a = QQ.algebraic_field(sqrt(2))
    b = QQ.algebraic_field(sqrt(3))
    c = QQ.algebraic_field(sqrt(7))
    d = QQ.algebraic_field(sqrt(2) + sqrt(3))
    e = QQ.algebraic_field(sqrt(2) + sqrt(3) + sqrt(7))

    assert field_isomorphism(a, a) == [1, 0]
    assert field_isomorphism(a, b) is None
    assert field_isomorphism(a, c) is None
    assert field_isomorphism(a, d) == [QQ(1, 2), 0, -QQ(9, 2), 0]
    assert field_isomorphism(a, e) == [QQ(1, 80), 0, -QQ(1, 2), 0, QQ(59, 20), 0]

    assert field_isomorphism(b, a) is None
    assert field_isomorphism(b, b) == [1, 0]
    assert field_isomorphism(b, c) is None
    assert field_isomorphism(b, d) == [-QQ(1, 2), 0, QQ(11, 2), 0]
    assert field_isomorphism(b, e) == [-QQ(3, 640), 0, QQ(67, 320), 0, -QQ(297, 160), 0, QQ(313, 80), 0]

    assert field_isomorphism(c, a) is None
    assert field_isomorphism(c, b) is None
    assert field_isomorphism(c, c) == [1, 0]
    assert field_isomorphism(c, d) is None
    assert field_isomorphism(c, e) == [QQ(3, 640), 0, -QQ(71, 320), 0, QQ(377, 160), 0, -QQ(469, 80), 0]

    assert field_isomorphism(d, a) is None
    assert field_isomorphism(d, b) is None
    assert field_isomorphism(d, c) is None
    assert field_isomorphism(d, d) == [1, 0]
    assert field_isomorphism(d, e) == [-QQ(3, 640), 0, QQ(71, 320), 0, -QQ(377, 160), 0, QQ(549, 80), 0]

    assert field_isomorphism(e, a) is None
    assert field_isomorphism(e, b) is None
    assert field_isomorphism(e, c) is None
    assert field_isomorphism(e, d) is None
    assert field_isomorphism(e, e) == [1, 0]

    f = QQ.algebraic_field(3*sqrt(2) + 8*sqrt(7) - 5)

    assert field_isomorphism(f, e) == [QQ(3, 80), 0, -QQ(139, 80), 0, QQ(347, 20), 0, -QQ(761, 20), -5]

    assert field_isomorphism(QQ.algebraic_field(+I*sqrt(3)), QQ.algebraic_field(I*sqrt(3)/2)) == [QQ(+1, 2), 0]
    assert field_isomorphism(QQ.algebraic_field(-I*sqrt(3)), QQ.algebraic_field(I*sqrt(3)/2)) == [QQ(-1, 2), 0]

    assert field_isomorphism(QQ.algebraic_field(+I*sqrt(3)), QQ.algebraic_field(-I*sqrt(3)/2)) == [QQ(-1, 2), 0]
    assert field_isomorphism(QQ.algebraic_field(-I*sqrt(3)), QQ.algebraic_field(-I*sqrt(3)/2)) == [QQ(+1, 2), 0]

    assert field_isomorphism(QQ.algebraic_field(+2*I*sqrt(3)/7), QQ.algebraic_field(5*I*sqrt(3)/3)) == [QQ(+14, 5), 0]
    assert field_isomorphism(QQ.algebraic_field(-2*I*sqrt(3)/7), QQ.algebraic_field(5*I*sqrt(3)/3)) == [QQ(-14, 5), 0]

    assert field_isomorphism(QQ.algebraic_field(+2*I*sqrt(3)/7), QQ.algebraic_field(-5*I*sqrt(3)/3)) == [QQ(-14, 5), 0]
    assert field_isomorphism(QQ.algebraic_field(-2*I*sqrt(3)/7), QQ.algebraic_field(-5*I*sqrt(3)/3)) == [QQ(+14, 5), 0]

    assert field_isomorphism(QQ.algebraic_field(+2*I*sqrt(3)/7 + 27), QQ.algebraic_field(5*I*sqrt(3)/3)) == [QQ(+14, 5), 1323]
    assert field_isomorphism(QQ.algebraic_field(-2*I*sqrt(3)/7 + 27), QQ.algebraic_field(5*I*sqrt(3)/3)) == [QQ(-14, 5), 1323]

    assert field_isomorphism(QQ.algebraic_field(+2*I*sqrt(3)/7 + 27), QQ.algebraic_field(-5*I*sqrt(3)/3)) == [QQ(-14, 5), 1323]
    assert field_isomorphism(QQ.algebraic_field(-2*I*sqrt(3)/7 + 27), QQ.algebraic_field(-5*I*sqrt(3)/3)) == [QQ(+14, 5), 1323]

    p = QQ.algebraic_field(+sqrt(2) + sqrt(3))
    q = QQ.algebraic_field(-sqrt(2) + sqrt(3))
    r = QQ.algebraic_field(+sqrt(2) - sqrt(3))
    s = QQ.algebraic_field(-sqrt(2) - sqrt(3))
    c = QQ.algebraic_field(cbrt(2))

    pos_coeffs = [+QQ(1, 2), 0, -QQ(9, 2), 0]
    neg_coeffs = [-QQ(1, 2), 0, +QQ(9, 2), 0]

    a = QQ.algebraic_field(sqrt(2))

    assert is_isomorphism_possible(a, c) is False
    assert field_isomorphism(a, c) is None

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == pos_coeffs
    assert field_isomorphism(a, q, fast=True) == neg_coeffs
    assert field_isomorphism(a, r, fast=True) == pos_coeffs
    assert field_isomorphism(a, s, fast=True) == neg_coeffs

    assert field_isomorphism(a, p, fast=False) == pos_coeffs
    assert field_isomorphism(a, q, fast=False) == neg_coeffs
    assert field_isomorphism(a, r, fast=False) == pos_coeffs
    assert field_isomorphism(a, s, fast=False) == neg_coeffs

    a = QQ.algebraic_field(-sqrt(2))

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == neg_coeffs
    assert field_isomorphism(a, q, fast=True) == pos_coeffs
    assert field_isomorphism(a, r, fast=True) == neg_coeffs
    assert field_isomorphism(a, s, fast=True) == pos_coeffs

    assert field_isomorphism(a, p, fast=False) == neg_coeffs
    assert field_isomorphism(a, q, fast=False) == pos_coeffs
    assert field_isomorphism(a, r, fast=False) == neg_coeffs
    assert field_isomorphism(a, s, fast=False) == pos_coeffs

    pos_coeffs = [+QQ(1, 2), 0, -QQ(11, 2), 0]
    neg_coeffs = [-QQ(1, 2), 0, +QQ(11, 2), 0]

    a = QQ.algebraic_field(sqrt(3))

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == neg_coeffs
    assert field_isomorphism(a, q, fast=True) == neg_coeffs
    assert field_isomorphism(a, r, fast=True) == pos_coeffs
    assert field_isomorphism(a, s, fast=True) == pos_coeffs

    assert field_isomorphism(a, p, fast=False) == neg_coeffs
    assert field_isomorphism(a, q, fast=False) == neg_coeffs
    assert field_isomorphism(a, r, fast=False) == pos_coeffs
    assert field_isomorphism(a, s, fast=False) == pos_coeffs

    a = QQ.algebraic_field(-sqrt(3))

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == pos_coeffs
    assert field_isomorphism(a, q, fast=True) == pos_coeffs
    assert field_isomorphism(a, r, fast=True) == neg_coeffs
    assert field_isomorphism(a, s, fast=True) == neg_coeffs

    assert field_isomorphism(a, p, fast=False) == pos_coeffs
    assert field_isomorphism(a, q, fast=False) == pos_coeffs
    assert field_isomorphism(a, r, fast=False) == neg_coeffs
    assert field_isomorphism(a, s, fast=False) == neg_coeffs

    pos_coeffs = [+QQ(3, 2), 0, -QQ(33, 2), -8]
    neg_coeffs = [-QQ(3, 2), 0, +QQ(33, 2), -8]

    a = QQ.algebraic_field(3*sqrt(3) - 8)

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == neg_coeffs
    assert field_isomorphism(a, q, fast=True) == neg_coeffs
    assert field_isomorphism(a, r, fast=True) == pos_coeffs
    assert field_isomorphism(a, s, fast=True) == pos_coeffs

    assert field_isomorphism(a, p, fast=False) == neg_coeffs
    assert field_isomorphism(a, q, fast=False) == neg_coeffs
    assert field_isomorphism(a, r, fast=False) == pos_coeffs
    assert field_isomorphism(a, s, fast=False) == pos_coeffs

    a = QQ.algebraic_field(3*sqrt(2) + 2*sqrt(3) + 1)

    pos_1_coeffs = [+QQ(1, 2), 0, -QQ(5, 2), 1]
    neg_1_coeffs = [-QQ(1, 2), 0, +QQ(5, 2), 1]
    pos_5_coeffs = [+QQ(5, 2), 0, -QQ(49, 2), 1]
    neg_5_coeffs = [-QQ(5, 2), 0, +QQ(49, 2), 1]

    assert is_isomorphism_possible(a, p) is True
    assert is_isomorphism_possible(a, q) is True
    assert is_isomorphism_possible(a, r) is True
    assert is_isomorphism_possible(a, s) is True

    assert field_isomorphism(a, p, fast=True) == pos_1_coeffs
    assert field_isomorphism(a, q, fast=True) == neg_5_coeffs
    assert field_isomorphism(a, r, fast=True) == pos_5_coeffs
    assert field_isomorphism(a, s, fast=True) == neg_1_coeffs

    assert field_isomorphism(a, p, fast=False) == pos_1_coeffs
    assert field_isomorphism(a, q, fast=False) == neg_5_coeffs
    assert field_isomorphism(a, r, fast=False) == pos_5_coeffs
    assert field_isomorphism(a, s, fast=False) == neg_1_coeffs

    a = QQ.algebraic_field(sqrt(2))
    b = QQ.algebraic_field(sqrt(3))
    c = QQ.algebraic_field(sqrt(7))

    assert is_isomorphism_possible(a, b) is True
    assert is_isomorphism_possible(b, a) is True

    assert is_isomorphism_possible(c, p) is False

    assert field_isomorphism(a, b, fast=True) is None
    assert field_isomorphism(b, a, fast=True) is None

    assert field_isomorphism(a, b, fast=False) is None
    assert field_isomorphism(b, a, fast=False) is None

    pytest.raises(ValueError, lambda: field_isomorphism(1, 2))
def test_dmp_sqf():
    R, x = ring("x", ZZ)

    assert R(0).sqf_part() == 0
    assert R(0).is_squarefree is True

    assert R(7).sqf_part() == 1
    assert R(7).is_squarefree is True

    assert (2*x + 2).sqf_part() == x + 1
    assert (2*x + 2).is_squarefree is True

    assert (x**3 + x + 1).sqf_part() == x**3 + x + 1
    assert (x**3 + x + 1).is_squarefree is True

    assert (-x**3 + x + 1).sqf_part() == x**3 - x - 1
    assert (-x**3 + x + 1).is_squarefree is True

    assert (2*x**3 + 3*x**2).sqf_part() == 2*x**2 + 3*x
    assert (2*x**3 + 3*x**2).is_squarefree is False

    assert (-2*x**3 + 3*x**2).sqf_part() == 2*x**2 - 3*x
    assert (-2*x**3 + 3*x**2).is_squarefree is False

    assert (x**3 - 3*x - 2).sqf_part() == x**2 - x - 2
    assert (x**3 - 3*x - 2).is_squarefree is False

    assert R(0).sqf_list() == (0, [])
    assert R(1).sqf_list() == (1, [])

    assert x.sqf_list() == (1, [(x, 1)])
    assert (2*x**2).sqf_list() == (2, [(x, 2)])
    assert (3*x**3).sqf_list() == (3, [(x, 3)])

    assert (-x**5 + x**4 + x - 1).sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1),
                                                      (x - 1, 2)])
    assert (x**8 + 6*x**6 + 12*x**4 + 8*x**2).sqf_list() == (1, [(x, 2),
                                                                 (x**2 + 2, 3)])

    assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)])

    R, x = ring("x", QQ)
    assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert (x**2 + 1).sqf_list() == (1, [(x + 1, 2)])

    R, x = ring("x", FF(3))
    assert (x**10 + 2*x**7 + 2*x**4 + x).sqf_list() == (1, [(x, 1), (x + 1, 3),
                                                            (x + 2, 6)])

    R1, x = ring("x", ZZ)
    R2, y = ring("y", FF(3))

    f = x**3 + 1
    g = y**3 + 1

    assert f.sqf_part() == f
    assert g.sqf_part() == y + 1

    assert f.is_squarefree is True
    assert g.is_squarefree is False

    R, x = ring("x", FF(5))

    f = x**8 + x**7 + 3*x**6 + x**4 + 2*x**2 + 2*x + 1

    assert f.sqf_part() == x**2 + 4*x + 3

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

    assert f.is_squarefree is True

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

    assert f.is_squarefree is False

    R, x = ring("x", FF(11))

    f = x**3 + 5*x**2 + 8*x + 4

    assert f.sqf_part() == x**2 + 3*x + 2

    assert R(0).is_squarefree is True
    assert R(0).sqf_list() == (0, [])

    assert R(1).is_squarefree is True
    assert R(1).sqf_list() == (1, [])

    assert (x + 1).is_squarefree is True
    assert (x + 1).sqf_list() == (1, [(x + 1, 1)])

    f = x**11 + 1

    assert f.is_squarefree is False
    assert f.sqf_list() == (1, [(x + 1, 11)])

    f = x**3 + 5*x**2 + 8*x + 4

    assert f.is_squarefree is False
    assert f.sqf_list() == (1, [(x + 1, 1), (x + 2, 2)])

    R, x = ring('x', FF(3))

    f = x**10 + 2*x**7 + 2*x**4 + x

    assert f.is_squarefree is False
    assert f.sqf_list() == (1, [(x, 1), (x + 1, 3), (x + 2, 6)])

    R, x = ring('x', FF(53))

    f = x**6 + 2*x**5 + 5*x**4 + 26*x**3 + 41*x**2 + 39*x + 38

    assert f.is_squarefree is True

    R, x = ring('x', FF(102953))

    f = x**15 + x + 1

    assert f.is_squarefree is True

    R, x, y = ring("x,y", ZZ)

    A = x**4 - 3*x**2 + 6
    D = x**6 - 5*x**4 + 5*x**2 + 4

    f, g = D, R.dmp_sub(A, R.dmp_mul(R.dmp_diff_in(D, 1, 0), y))
    res = R.dmp_resultant(f, g)
    h = (4*y**2 + 1).drop(x)

    assert res.sqf_list() == (45796, [(h, 3)])

    pytest.raises(DomainError, lambda: (x**2 - 1).sqf_norm())

    K = QQ.algebraic_field(sqrt(3))
    R, x = ring("x", K)
    _, X = ring("x", QQ)
    assert (x**2 - 2).sqf_norm() == (1, x**2 + K([QQ(-2), QQ(0)])*x + 1,
                                     X**4 - 10*X**2 + 1)

    R, x, y = ring("x,y", ZZ)
    assert R(0).sqf_part() == 0
    assert R(0).is_squarefree is True

    assert R(7).sqf_part() == 1
    assert R(7).is_squarefree is True

    assert R(3).sqf_list() == (3, [])

    R, x, y, z = ring("x,y,z", ZZ)
    assert f_0.is_squarefree is True
    assert (f_0**2).is_squarefree is False
    assert f_1.is_squarefree is True
    assert (f_1**2).is_squarefree is False
    assert f_2.is_squarefree is True
    assert (f_2**2).is_squarefree is False
    assert f_3.is_squarefree is True
    assert (f_3**2).is_squarefree is False
    assert f_5.is_squarefree is False
    assert (f_5**2).is_squarefree is False

    assert f_4.is_squarefree is True
    assert f_4.sqf_part() == -f_4
    assert f_5.sqf_part() == x + y - z

    R, x, y, z, t = ring("x,y,z,t", ZZ)
    assert f_6.is_squarefree is True
    assert f_6.sqf_part() == f_6

    R, x = ring("x", ZZ)
    f = -x**5 + x**4 + x - 1

    assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])

    f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

    assert f.sqf_list() == (2, [(x + 1, 2), (x + 2, 3)])

    R, x, y = ring("x,y", ZZ)
    f = -x**5 + x**4 + x - 1

    assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])

    pytest.raises(DomainError, lambda: (x**2 + y**2).sqf_norm())

    R, x, y = ring("x,y", FF(2))
    pytest.raises(NotImplementedError, lambda: (y**2 + 1).sqf_list())
    pytest.raises(NotImplementedError, lambda: (x**3 + 2*x**2*y + x*y**2).sqf_part())

    R, x, y = ring("x,y", QQ.algebraic_field(I))
    assert (x**2 + 2*I*x - 1).sqf_list() == (1, [(x + I, 2)])
Exemple #59
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([Integer(1), Integer(2),
                             Integer(3)]) == (ZZ, [ZZ(1), ZZ(2),
                                                   ZZ(3)])
    assert construct_domain(
        [Integer(1), Integer(2), Integer(3)],
        field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([Rational(1, 2),
                             Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain([3.14, 1, Rational(1, 2)
                             ]) == (RR, [RR(3.14), RR(1.0),
                                         RR(0.5)])

    assert construct_domain([3.14, sqrt(2)],
                            extension=None) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)],
                            extension=True) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([sqrt(2), 3.14],
                            extension=True) == (EX, [EX(sqrt(2)),
                                                     EX(3.14)])

    assert construct_domain([1, sqrt(2)],
                            extension=None) == (EX, [EX(1), EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert (construct_domain(
        [7, Rational(1, 2), sqrt(2)],
        extension=True) == (alg, [alg(7),
                                  alg(Rational(1, 2)),
                                  alg(sqrt(2))]))

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert (construct_domain([7, sqrt(2), sqrt(3)], extension=True) == (alg, [
        alg(7), alg(sqrt(2)), alg(sqrt(3))
    ]))

    dom = ZZ.poly_ring(x)

    assert construct_domain([2 * x, 3]) == (dom, [dom(2 * x), dom(3)])

    dom = ZZ.poly_ring(x, y)

    assert construct_domain([2 * x, 3 * y]) == (dom, [dom(2 * x), dom(3 * y)])

    dom = QQ.poly_ring(x)

    assert construct_domain([x / 2, 3]) == (dom, [dom(x / 2), dom(3)])

    dom = QQ.poly_ring(x, y)

    assert construct_domain([x / 2, 3 * y]) == (dom, [dom(x / 2), dom(3 * y)])

    dom = RR.poly_ring(x)

    assert construct_domain([x / 2, 3.5]) == (dom, [dom(x / 2), dom(3.5)])

    dom = RR.poly_ring(x, y)

    assert construct_domain([x / 2,
                             3.5 * y]) == (dom, [dom(x / 2),
                                                 dom(3.5 * y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2 / x, 3]) == (dom, [dom(2 / x), dom(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2 / x, 3 * y]) == (dom, [dom(2 / x), dom(3 * y)])

    dom = RR.frac_field(x)

    assert construct_domain([2 / x, 3.5]) == (dom, [dom(2 / x), dom(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2 / x,
                             3.5 * y]) == (dom, [dom(2 / x),
                                                 dom(3.5 * y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})
Exemple #60
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.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 2.772 > alg3.unit > 2.771

    alg4 = QQ.algebraic_field(sqrt(2) + I)
    assert alg4.convert(alg2.unit) == alg4.from_expr(I)