Beispiel #1
0
def test_to_number_field():
    A = QQ.algebraic_field(sqrt(2))
    assert A.convert(sqrt(2)) == A([0, 1])
    B = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert B.convert(sqrt(2) + sqrt(3)) == B([0, 1])

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

    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(2184, 755),
        Rational(-1003, 755),
        Rational(936, 755),
        Rational(128, 151),
        Rational(-54, 755),
        Rational(-96, 755)
    ])
    assert A.from_expr(r1) == a
def test__real_imag():
    R, x, y = ring('x y', ZZ)
    Rx = R.drop(y)
    _y = R.symbols[1]

    assert Rx._real_imag(Rx.zero, _y) == (0, 0)
    assert Rx._real_imag(Rx.one, _y) == (1, 0)

    assert Rx._real_imag(Rx.x + 1, _y) == (x + 1, y)
    assert Rx._real_imag(Rx.x + 2, _y) == (x + 2, y)

    assert Rx._real_imag(Rx.x**2 + 2 * Rx.x + 3,
                         _y) == (x**2 - y**2 + 2 * x + 3, 2 * x * y + 2 * y)

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

    assert Rx._real_imag(f, _y) == (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)
    Rx = R.drop(y)

    pytest.raises(DomainError, lambda: Rx._real_imag(Rx.x + 1))

    R = QQ.algebraic_field(I).inject('x', 'y')
    Rx = R.drop(1)
    _y = R.symbols[1]
    x, y = R.to_ground().gens

    f = Rx.x**2 + I * Rx.x - 1
    r = x**2 - y**2 - y - 1
    i = 2 * x * y + x

    assert Rx._real_imag(f, _y) == (r, i)

    f = Rx.x**4 + I * Rx.x**3 - Rx.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 Rx._real_imag(f, _y) == (r, i)

    K = QQ.algebraic_field(sqrt(2))
    R = K.inject('x', 'y')
    Rx = R.drop(1)
    _y = R.symbols[1]
    x, y = R.gens

    f = Rx.x**2 + sqrt(2) * Rx.x - 1
    assert Rx._real_imag(f, _y) == (x**2 - y**2 + sqrt(2) * x - 1,
                                    2 * x * y + sqrt(2) * y)

    K = K.algebraic_field(I)
    R = K.inject('x', 'y')
    Rx = R.drop(1)
    _y = R.symbols[1]
    x, y = R.to_ground().gens

    f = Rx.x**2 + 2 * sqrt(2) * I * Rx.x - 1 + I
    assert Rx._real_imag(f, _y) == (x**2 - y**2 - 2 * sqrt(2) * y - 1,
                                    2 * x * y + 2 * sqrt(2) * x + 1)
Beispiel #3
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]]))
Beispiel #4
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))
Beispiel #5
0
def test_Domain_unify_algebraic_slow():
    sqrt2 = QQ.algebraic_field(sqrt(2))
    r = RootOf(x**7 - x + 1, 0)
    rootof = QQ.algebraic_field(r)
    ans = QQ.algebraic_field(r + sqrt(2))
    assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans

    # here domain created from tuple, not Expr
    p = Poly(x**3 - sqrt(2) * x - 1, x)
    sqrt2 = p.domain
    assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans
Beispiel #6
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(
        a,
        f'AlgebraicField({QQ!r}, Pow(Integer(2), Rational(1, 2)))([Integer(0), Integer(1)])'
    )
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(
        a,
        f'AlgebraicField({QQ!r}, Pow(Integer(-2), Rational(1, 3)))([Integer(0), Integer(1)])'
    )
def test__count_complex_roots_9():
    R, x = ring('x', QQ.algebraic_field(sqrt(2)))

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

    assert R._count_complex_roots(f, a, b) == 2
    assert R._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._count_complex_roots(f, a, b) == 2
    assert R._count_complex_roots(f, c, d) == 1
def test__count_real_roots():
    R, x = ring('x', ZZ)

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

    f = x - 1

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

    f = x**2 - 2

    assert R._count_real_roots(f) == 2
    assert R._count_real_roots(f, sup=0) == 1
    assert R._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._count_real_roots(f) == 0

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

    assert R._count_real_roots(f) == 2
Beispiel #9
0
def test_sympyissue_19196():
    A = QQ.algebraic_field(sqrt(2), root(3, 3))
    _, x, y, z = ring('x y z', A)

    f1, f2 = x - z / root(3, 3), x**2 + 2 * y + sqrt(2)
    f = f1 * f2
    assert f.factor_list() == (1, [(f1, 1), (f2, 1)])
Beispiel #10
0
def test_issue_1008():
    a = RootOf(4*x**2 + 2*x + 1, 0)
    A = QQ.algebraic_field(a)
    e = A.from_expr(a)

    assert A.unit.denominator == 1
    assert e.denominator == 2
Beispiel #11
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
Beispiel #12
0
def test_sympyissue_5786():
    R, x, y, z, t = ring('x y z t', QQ.algebraic_field(I))

    f, g = z - I * t, x - I * y

    assert (f * g).factor_list() == (1, [(f, 1), (g, 1)])
    assert (f**2 * g).factor_list() == (1, [(g, 1), (f, 2)])
    assert (f * g**3).factor_list() == (1, [(f, 1), (g, 3)])
Beispiel #13
0
def test_solve_lin_sys_2x2_2():
    domain = QQ.algebraic_field(sqrt(2))
    coeff_ring, _, A1 = ring('A:2', domain)
    R, _ = ring('x', coeff_ring)
    expr = (A1 - sqrt(2))*R.one
    sol = solve_lin_sys(expr.values(), coeff_ring)
    assert all(isinstance(s, coeff_ring.dtype) for s in sol.values())
    assert sol == {A1: coeff_ring.convert(sqrt(2))}
Beispiel #14
0
def test_FracElement_diff():
    _, x, y, z = field('x y z', ZZ)

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

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

    assert ((x - y) / x).diff(x) == y / x**2
Beispiel #15
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.inject(x, y)) == sqrt5.inject(x, y)
    assert sqrt5.inject(x, y).unify(sqrt5) == sqrt5.inject(x, y)

    assert sqrt5.unify(sqrt5.inject(x, y).field) == sqrt5.inject(x, y).field
    assert sqrt5.inject(x, y).field.unify(sqrt5) == sqrt5.inject(x, y).field

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

    assert sqrt5.unify(sqrt7.inject(x, y).field) == sqrt57.inject(x, y).field
    assert sqrt5.inject(x, y).field.unify(sqrt7) == sqrt57.inject(x, y).field
Beispiel #16
0
def test_Gaussian_postprocess():
    opt = {'gaussian': True}
    Gaussian.postprocess(opt)

    assert opt == {
        'gaussian': True,
        'extension': {I},
        'domain': QQ.algebraic_field(I),
    }
Beispiel #17
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).inject('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.inject('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.inject('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)
Beispiel #18
0
def test_dmp_ext_factor(method):
    with using(aa_factor_method=method):
        R, x = ring('x', QQ.algebraic_field(I))

        assert R(0).factor_list() == (0, [])
        assert (x + 1).factor_list() == (1, [(x + 1, 1)])
        assert (2 * x + 2).factor_list() == (2, [(x + 1, 1)])
        assert (7 * x**4 + 1).factor_list() == (7, [(x**4 + QQ(1, 7), 1)])
        assert (x**4 + 1).factor_list() == (1, [(x**2 - I, 1), (x**2 + I, 1)])
        assert (4 * x**2 + 9).factor_list() == (4, [(x - 3 * I / 2, 1),
                                                    (x + 3 * I / 2, 1)])
        assert (4 * x**4 + 8 * x**3 + 77 * x**2 + 18 * x +
                153).factor_list() == (4, [(x - 3 * I / 2, 1),
                                           (x + 1 + 4 * I, 1),
                                           (x + 1 - 4 * I, 1),
                                           (x + 3 * I / 2, 1)])
        assert (x**2 + 1).factor_list() == (1, [(x - I, 1), (x + I, 1)])

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

        assert (x**4 + 1).factor_list() == (1, [(x**2 - sqrt(2) * x + 1, 1),
                                                (x**2 + sqrt(2) * x + 1, 1)])

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

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

        f *= 2

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

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

        assert R(0).factor_list() == (0, [])
        assert (x + 1).factor_list() == (1, [(x + 1, 1)])
        assert (2 * x + 2).factor_list() == (2, [(x + 1, 1)])
        assert (x**2 - 2 * y**2).factor_list() == (1, [(x - sqrt(2) * y, 1),
                                                       (x + sqrt(2) * y, 1)])
        assert (2 * x**2 - 4 * y**2).factor_list() == (2,
                                                       [(x - sqrt(2) * y, 1),
                                                        (x + sqrt(2) * y, 1)])
Beispiel #19
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}
Beispiel #20
0
def test_modgcd_algebraic_field_random():
    A = QQ.algebraic_field(sqrt(2), sqrt(3))
    R, x, y, z = ring('x, y, z', A)

    with using(gcd_aa_method='modgcd'):
        h = x**2 * y**3 + 1111 * sqrt(6) / 12 * z
        a, b = 11 * y**3 + 2, (y + x - 1) * h

        assert (h * a).cofactors(h * b) == (h, a, b)

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

        assert (h * a).cofactors(h * b) == (h, a, b)
Beispiel #21
0
def test_efactor_wang():
    a = (-1 + sqrt(5)) / 4 - I * sqrt((sqrt(5) + 5) / 8)
    A = QQ.algebraic_field(a)
    a = A.unit
    _, x, y, z = ring('x y z', A)

    f1 = x**2 - 2 * a**2 * x + a**3 * z**2 - (a**3 + a**2 + a + 1) * y + 12 * a
    f2 = x**2 - 2 * a**3 * x + a**2 * z**2 + a * y - 12 * (a**3 + a**2 + a + 1)
    f3 = x**2 + 2 * (a**3 + a**2 + a + 1) * x + a * z**2 + a**3 * y + 12 * a**2
    f4 = x**2 - 2 * a * x - (a**3 + a**2 + a + 1) * z**2 + a**2 * y + 12 * a**3
    f = f1 * f2 * f3 * f4

    assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1), (f4, 1)])
Beispiel #22
0
def test_efactor_random():
    A3 = QQ.algebraic_field(sqrt(3))
    _, x, y, z, t = ring('x y z t', A3)

    f1 = x * y - sqrt(3)
    f2 = z * t + 1
    f3 = x**2 + 1
    f4 = x**2 + z * t
    f = f1 * f2 * f3 * f4

    for seed in [0, 1, 2, 6]:
        random.seed(seed)
        assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1), (f4, 1)])
Beispiel #23
0
def test_solve_triangualted():
    f_1 = x**2 + y + z - 1
    f_2 = x + y**2 + z - 1
    f_3 = x + y + z**2 - 1

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

    assert solve_triangulated([f_1, f_2, f_3], x, y, z) == \
        [(0, 0, 1), (0, 1, 0), (1, 0, 0)]

    dom = QQ.algebraic_field(sqrt(2))

    assert solve_triangulated([f_1, f_2, f_3], x, y, z, domain=dom) == \
        [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
Beispiel #24
0
def test_Domain_get_exact():
    assert EX.get_exact() == EX
    assert ZZ.get_exact() == ZZ
    assert QQ.get_exact() == QQ
    assert RR.get_exact() == QQ
    assert CC.get_exact() == QQ.algebraic_field(I)
    assert ALG.get_exact() == ALG
    assert ZZ.inject(x).get_exact() == ZZ.inject(x)
    assert QQ.inject(x).get_exact() == QQ.inject(x)
    assert ZZ.inject(x, y).get_exact() == ZZ.inject(x, y)
    assert QQ.inject(x, y).get_exact() == QQ.inject(x, y)
    assert ZZ.inject(x).field.get_exact() == ZZ.inject(x).field
    assert QQ.inject(x).field.get_exact() == QQ.inject(x).field
    assert ZZ.inject(x, y).field.get_exact() == ZZ.inject(x, y).field
    assert QQ.inject(x, y).field.get_exact() == QQ.inject(x, y).field
Beispiel #25
0
def test_sympyissue_20617():
    dom = QQ.algebraic_field(sqrt(3)).algebraic_field(I)

    w = exp(I * 2 * pi / 3)
    l1 = [w**2, w, 1]

    r1 = Poly(l1, x, extension=True)

    w2 = w.expand()
    l2 = [w2**2, w2, 1]

    r2 = Poly(l2, x, extension=True)

    assert r1 == r2
    assert r1.domain == dom
Beispiel #26
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.all_coeffs() == [0, -QQ(9, 2), 0, QQ(1, 2)]
    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))
Beispiel #27
0
def test_dup_count_complex_roots_exclude():
    R, x = ring('x', ZZ)

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

    a, b = (-1, 0), (1, 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 = (0, 0), (1, 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=(0, 0), sup=(1, 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)
Beispiel #28
0
def test_conversion():
    L = QQ.poly_ring(x, y, order='ilex')
    G = QQ.inject(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.inject(x, y)
    assert R.convert(ALG(1), ALG) == R(1)
    pytest.raises(CoercionFailed,
                  lambda: R.convert(ALG(1), QQ.algebraic_field(sqrt(2))))

    R = R.drop(y)
    pytest.raises(CoercionFailed, lambda: R.convert(G(y), R))

    pytest.raises(CoercionFailed, lambda: R.convert(FF(8)(2)))
Beispiel #29
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
def test_ComplexInterval():
    R, x = ring('x', QQ.algebraic_field(I))

    f = x**3 + x + I

    _, r1, r2 = R._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
Beispiel #31
0
def test_AlgebraicElement():
    r = RootOf(x**7 + 3*x - 1, 3)
    K = QQ.algebraic_field(r)
    a = K([1, 2, 3, 0, 1])
    assert mcode(a) == ('AlgebraicNumber[Root[#^7 + 3*# - 1 &, 4],'
                        ' {1, 0, 3, 2, 1}]')