Example #1
0
def test_FracElement___mul__():
    F, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y
    assert f * g == g * f == 1 / (x * y)

    assert x * F.ring.gens[0] == F.ring.gens[0] * x == x**2

    F, x, y = field('x y', ZZ)
    assert x * 3 == 3 * x
    assert x * QQ(3, 7) == QQ(3, 7) * x == 3 * x / 7

    Fuv, u, v = field('u v', ZZ)
    _, x, y, z, t = field('x y z t', Fuv)

    f = ((u + 1) * x * y + 1) / ((v - 1) * z - t * u * v - 1)
    assert dict(f.numerator) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
    assert dict(f.denominator) == {
        (0, 0, 1, 0): v - 1,
        (0, 0, 0, 1): -u * v,
        (0, 0, 0, 0): -1
    }

    Ruv, u, v = ring('u v', ZZ)
    _, x, y, z, t = field('x y z t', Ruv)

    f = ((u + 1) * x * y + 1) / ((v - 1) * z - t * u * v - 1)
    assert dict(f.numerator) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
    assert dict(f.denominator) == {
        (0, 0, 1, 0): v - 1,
        (0, 0, 0, 1): -u * v,
        (0, 0, 0, 0): -1
    }
Example #2
0
def test_solve_lin_sys_2x2_one():
    domain,  x1, x2 = ring('x1 x2', QQ)
    eqs = [x1 + x2 - 5,
           2*x1 - x2]
    sol = {x1: QQ(5, 3), x2: QQ(10, 3)}
    _sol = solve_lin_sys(eqs, domain)
    assert _sol == sol and all(isinstance(s, domain.dtype) for s in _sol)
Example #3
0
def test_pickling_polys_polyclasses():
    for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)):
        check(c)
    for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)):
        check(c)
    for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)):
        check(c)
Example #4
0
def test_operations():
    F = QQ.old_poly_ring(x).free_module(2)
    G = QQ.old_poly_ring(x).free_module(3)
    f = F.identity_hom()
    g = homomorphism(F, F, [0, [1, x]])
    h = homomorphism(F, F, [[1, 0], 0])
    i = homomorphism(F, G, [[1, 0, 0], [0, 1, 0]])

    assert f == f
    assert f != g
    assert f != i
    assert (f != F.identity_hom()) is False
    assert 2 * f == f * 2 == homomorphism(F, F, [[2, 0], [0, 2]])
    assert f / 2 == homomorphism(F, F,
                                 [[Rational(1, 2), 0], [0, Rational(1, 2)]])
    assert f + g == homomorphism(F, F, [[1, 0], [1, x + 1]])
    assert f - g == homomorphism(F, F, [[1, 0], [-1, 1 - x]])
    assert f * g == g == g * f
    assert h * g == homomorphism(F, F, [0, [1, 0]])
    assert g * h == homomorphism(F, F, [0, 0])
    assert i * f == i
    assert f([1, 2]) == [1, 2]
    assert g([1, 2]) == [2, 2 * x]

    assert i.restrict_domain(F.submodule([x, x]))([x, x]) == i([x, x])
    h1 = h.quotient_domain(F.submodule([0, 1]))
    assert h1([1, 0]) == h([1, 0])
    assert h1.restrict_domain(h1.domain.submodule([x, 0]))([x, 0]) == h([x, 0])

    pytest.raises(TypeError, lambda: f / g)
    pytest.raises(TypeError, lambda: f + 1)
    pytest.raises(TypeError, lambda: f + i)
    pytest.raises(TypeError, lambda: f - 1)
    pytest.raises(TypeError, lambda: f * i)
Example #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))
Example #6
0
def test_FracElement___add__():
    F, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y
    assert f + g == g + f == (x + y) / (x * y)

    z = symbols('z')
    pytest.raises(TypeError, lambda: x + z)

    assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2 * x

    F, x, y = field('x y', ZZ)
    assert x + 3 == 3 + x
    assert x + QQ(3, 7) == QQ(3, 7) + x == (7 * x + 3) / 7

    Fuv, u, v = field('u v', ZZ)
    _, x, y, *_ = field('x y z t', Fuv)

    f = (u * v + x) / (y + u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u * v}

    Ruv, u, v = ring('u v', ZZ)
    _, x, y, *_ = field('x y z t', Ruv)

    f = (u * v + x) / (y + u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u * v}
Example #7
0
def test_QuotientRing():
    I = QQ.old_poly_ring(x).ideal(x**2 + 1)
    R = QQ.old_poly_ring(x) / I

    assert R == QQ.old_poly_ring(x) / [x**2 + 1]
    assert R == QQ.old_poly_ring(x) / QQ.old_poly_ring(x).ideal(x**2 + 1)
    assert R != QQ.old_poly_ring(x)

    assert R.convert(1) / x == -x + I
    assert -1 + I == x**2 + I
    assert R.convert(ZZ(1), ZZ) == 1 + I
    assert R.convert(R.convert(x), R) == R.convert(x)

    X = R.convert(x)
    Y = QQ.old_poly_ring(x).convert(x)
    assert -1 + I == X**2 + I
    assert -1 + I == Y**2 + I
    assert R.to_diofant(X) == x

    pytest.raises(
        ValueError,
        lambda: QQ.old_poly_ring(x) / QQ.old_poly_ring(x, y).ideal(x))

    R = QQ.old_poly_ring(x, order="ilex")
    I = R.ideal(x)
    assert R.convert(1) + I == (R / I).convert(1)
Example #8
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)
Example #10
0
def test_FracElement___sub__():
    F, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y
    assert f - g == (-x + y) / (x * y)

    assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0

    F, x, y = field('x y', ZZ)
    assert x - 3 == -(3 - x)
    assert x - QQ(3, 7) == -(QQ(3, 7) - x) == (7 * x - 3) / 7

    Fuv, u, v = field('u v', ZZ)
    _, x, y, *_ = field('x y z t', Fuv)

    f = (u * v - x) / (y - u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v}

    Ruv, u, v = ring('u v', ZZ)
    _, x, y, *_ = field('x y z t', Ruv)

    f = (u * v - x) / (y - u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v}

    Fuv, u, _ = field('u v', ZZ)
    _, x, *_ = ring('x y z', Fuv)

    f = u - x
    assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
Example #11
0
def test_dmp_mul_ground():
    R, x = ring('x', ZZ)

    f = 0

    assert R.dmp_mul_ground(f, ZZ(2)) == 0

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

    assert R.dmp_mul_ground(f, ZZ(3)) == 3 * x**2 + 6 * x - 3

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

    assert R.dmp_mul_ground(f, ZZ(0)) == 0
    assert R.dmp_mul_ground(f, ZZ(2)) == 2 * x**2 + 4 * x + 6

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

    f = f_polys()[0]

    assert (R.dmp_mul_ground(f,
                             ZZ(2)) == 2 * x**2 * y * z**2 + 4 * x**2 * y * z +
            6 * x**2 * y + 4 * x**2 + 6 * x + 8 * y**2 * z**2 + 10 * y**2 * z +
            12 * y**2 + 2 * y * z**2 + 4 * y * z + 2 * y + 2)

    R, x, y, z = ring('x y z', QQ)

    f = f.set_ring(R) / 7

    assert (R.dmp_mul_ground(f, QQ(
        1, 2)) == x**2 * y * z**2 / 14 + x**2 * y * z / 7 + 3 * x**2 * y / 14 +
            x**2 / 7 + 3 * x / 14 + 2 * y**2 * z**2 / 7 + 5 * y**2 * z / 14 +
            3 * y**2 / 7 + y * z**2 / 14 + y * z / 7 + y / 14 + QQ(1, 14))
Example #12
0
def test_sympyissue_18874():
    e = [sqrt(2) + sqrt(5), sqrt(2)]

    assert primitive_element(e) == (PurePoly(x**4 - 46 * x**2 + 169),
                                    [1, 2], [[0, QQ(-20, 39), 0,
                                              QQ(1, 39)],
                                             [0, QQ(59, 78), 0,
                                              QQ(-1, 78)]])
Example #13
0
def test_dmp_clear_denoms():
    R0, X = ring('x', QQ)
    R1 = R0.domain.ring.inject('x')

    assert R0.dmp_clear_denoms(0) == (1, 0)

    assert R0.dmp_clear_denoms(1) == (1, 1)
    assert R0.dmp_clear_denoms(7) == (1, 7)

    assert R0.dmp_clear_denoms(QQ(7, 3)) == (3, 7)

    assert R0.dmp_clear_denoms(3 * X**2 + X) == (1, 3 * X**2 + X)
    assert R0.dmp_clear_denoms(X**2 + X / 2) == (2, 2 * X**2 + X)

    assert R0.dmp_clear_denoms(3 * X**2 + X,
                               convert=True) == (1, 3 * R1.x**2 + R1.x)
    assert R0.dmp_clear_denoms(X**2 + X / 2,
                               convert=True) == (2, 2 * R1.x**2 + R1.x)

    assert R0.dmp_clear_denoms(X / 2 + QQ(1, 3)) == (6, 3 * X + 2)
    assert R0.dmp_clear_denoms(X / 2 + QQ(1, 3),
                               convert=True) == (6, 3 * R1.x + 2)

    assert R0.dmp_clear_denoms(3 * X**2 + X,
                               convert=True) == (1, 3 * R1.x**2 + R1.x)
    assert R0.dmp_clear_denoms(X**2 + X / 2,
                               convert=True) == (2, 2 * R1.x**2 + R1.x)

    R0, a = ring('a', EX)

    assert R0.dmp_clear_denoms(3 * a / 2 + Rational(9, 4)) == (4, 6 * a + 9)

    assert R0.dmp_clear_denoms(7) == (1, 7)
    assert R0.dmp_clear_denoms(sin(x) / x * a) == (x, a * sin(x))

    R0, X, Y = ring('x y', QQ)
    R1 = R0.domain.ring.inject('x', 'y')

    assert R0.dmp_clear_denoms(0) == (1, 0)

    assert R0.dmp_clear_denoms(1) == (1, 1)
    assert R0.dmp_clear_denoms(7) == (1, 7)

    assert R0.dmp_clear_denoms(QQ(7, 3)) == (3, 7)

    assert R0.dmp_clear_denoms(3 * X**2 + X) == (1, 3 * X**2 + X)
    assert R0.dmp_clear_denoms(X**2 + X / 2) == (2, 2 * X**2 + X)

    assert R0.dmp_clear_denoms(3 * X**2 + X,
                               convert=True) == (1, 3 * R1.x**2 + R1.x)
    assert R0.dmp_clear_denoms(X**2 + X / 2,
                               convert=True) == (2, 2 * R1.x**2 + R1.x)

    R0, a, b = ring('a b', EX)
    assert R0.dmp_clear_denoms(3 * a / 2 + Rational(9, 4)) == (4, 6 * a + 9)
    assert R0.dmp_clear_denoms(7) == (1, 7)
    assert R0.dmp_clear_denoms(sin(x) / x * b) == (x, b * sin(x))
Example #14
0
def test___eq__():
    assert not QQ.inject(x) == ZZ.inject(x)
    assert not QQ.inject(x).field == ZZ.inject(x).field

    assert EX(1) != EX(2)

    F11 = FF(11)
    assert F11(2) != F11(3)
    assert F11(2) != object()
Example #15
0
def test_Domain_field():
    assert EX.field == EX
    assert ZZ.field == QQ
    assert QQ.field == QQ
    assert RR.field == RR
    assert ALG.field == ALG
    assert ZZ.inject(x).field == ZZ.frac_field(x)
    assert QQ.inject(x).field == QQ.frac_field(x)
    assert ZZ.inject(x, y).field == ZZ.frac_field(x, y)
    assert QQ.inject(x, y).field == QQ.frac_field(x, y)
def test__count_complex_roots_implicit():
    R, x = ring('x', ZZ)

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

    assert R._count_complex_roots(f) == 5

    assert R._count_complex_roots(f, sup=(0, 0)) == 3
    assert R._count_complex_roots(f, inf=(0, 0)) == 3

    assert R._count_complex_roots(f, inf=QQ(-2), sup=QQ(-1)) == 1
Example #17
0
def test_dup_sturm():
    R, x = ring('x', QQ)

    assert R(5).sturm() == [1]
    assert x.sturm() == [x, 1]

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

    assert f.sturm() == [
        f, 3 * x**2 - 4 * x + 3, -10 * x / 9 + QQ(13, 3), -QQ(3303, 100)
    ]
Example #18
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
Example #19
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)])'
    )
Example #20
0
def test_methods():
    R = QQ.inject(x)
    X = R.convert(x)

    assert R.is_normal(-X) is False
    assert R.is_normal(+X) is True

    assert R.gcdex(X**3 - X, X**2) == (-1, X, X)

    F = QQ.inject(y).field
    Y = F.convert(y)
    assert F.is_normal(-Y) is False
    assert F.is_normal(+Y) is True
Example #21
0
def test_RootOf():
    p = MpmathPrinter()

    e = RootOf(x**3 + x - 1, x, 0)
    r = ('findroot(lambda x: x**3 + x - 1, (%s, %s), '
         "method='bisection')" % (p.doprint(QQ(0)), p.doprint(QQ(1))))

    assert p.doprint(e) == r

    e = RootOf(x**3 + x - 1, x, 1)
    r = ('findroot(lambda x: x**3 + x - 1, mpc(%s, %s), '
         "method='secant')" % (p.doprint(QQ(-3, 8)), p.doprint(QQ(-9, 8))))

    assert p.doprint(e) == r
def test_benchmark_minimal_polynomial(method):
    with config.using(groebner=method):
        R, x, y, z = ring('x y z', QQ, lex)

        F = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)]
        G = [
            x + 155 * z**5 / 2067 - 355 * z**4 / 689 + 6062 * z**3 / 2067 -
            3687 * z**2 / 689 + 6878 * z / 2067 - QQ(25, 53),
            y + 4 * z**5 / 53 - 91 * z**4 / 159 + 523 * z**3 / 159 -
            387 * z**2 / 53 + 1043 * z / 159 - QQ(308, 159),
            z**6 - 7 * z**5 + 41 * z**4 - 82 * z**3 + 89 * z**2 - 46 * z + 13
        ]

        assert groebner(F, R) == G
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
Example #24
0
def test_dup_gcdex():
    R, x = ring('x', QQ)

    f = x**4 - 2 * x**3 - 6 * x**2 + 12 * x + 15
    g = x**3 + x**2 - 4 * x - 4

    s = -x / 5 + QQ(3, 5)
    t = x**2 / 5 - 6 * x / 5 + 2
    h = x + 1

    assert f.half_gcdex(g) == (s, h)
    assert f.gcdex(g) == (s, t, h)

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

    s, t, h = f.gcdex(g)
    S, T, H = g.gcdex(f)

    assert s * f + t * g == h
    assert S * g + T * f == H

    f = 2 * x
    g = x**2 - 16

    s = x / 32
    t = -QQ(1, 16)
    h = 1

    assert f.half_gcdex(g) == (s, h)
    assert f.gcdex(g) == (s, t, h)

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

    assert R.zero.gcdex(R(2)) == (0, 6, 1)
    assert R(2).gcdex(R(2)) == (0, 6, 1)

    assert R.zero.gcdex(3 * x) == (0, 4, x)

    assert (3 * x).gcdex(3 * x) == (0, 4, x)

    assert (x**2 + 8 * x + 7).gcdex(x**3 + 7 * x**2 + x + 7) == (5 * x + 6, 6,
                                                                 x + 7)

    _, x, y = ring('x,y', QQ)

    pytest.raises(MultivariatePolynomialError, lambda:
                  (x + y).half_gcdex(x * y))
    pytest.raises(MultivariatePolynomialError, lambda: (x + y).gcdex(x * y))
Example #25
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
Example #26
0
def test_units():
    R = QQ.inject(x)
    assert R.convert(1) == R.one
    assert R.convert(x) != R.one
    assert R.convert(1 + x) != R.one

    R = QQ.poly_ring(x, order='ilex')
    assert R.convert(1) == R.one
    assert R.convert(x) != R.one

    R = ZZ.inject(x)
    assert R.convert(1) == R.one
    assert R.convert(2) != R.one
    assert R.convert(x) != R.one
    assert R.convert(1 + x) != R.one
Example #27
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)])
def test__count_complex_roots_1():
    R, x = ring('x', ZZ)

    f = x - 1

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

    f = -f

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

    f = x + 1

    assert R._count_complex_roots(f, a, b) == 1
    assert R._count_complex_roots(f, c, d) == 0

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

    f = x - QQ(1, 2)

    assert R._count_complex_roots(f, c, d) == 1

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

    pytest.raises(DomainError, lambda: R._count_complex_roots(x))
Example #29
0
def test_dup_invert():
    R, x = ring('x', QQ)

    assert R.invert(2 * x, x**2 - 16) == x / 32
    assert R.invert(x**2 - 1, 2 * x - 1) == QQ(-4, 3)

    pytest.raises(NotInvertible, lambda: R.invert(x**2 - 1, x - 1))
Example #30
0
def test_dmp_ground_monic():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_monic(3 * x**2 + 6 * x + 9) == x**2 + 2 * x + 3

    pytest.raises(ExactQuotientFailed,
                  lambda: R.dmp_ground_monic(3 * x**2 + 4 * x + 5))

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

    assert R.dmp_ground_monic(0) == 0
    assert R.dmp_ground_monic(1) == 1
    assert R.dmp_ground_monic(7 * x**2 + x + 21) == x**2 + x / 7 + 3
    assert R.dmp_ground_monic(3 * x**2 + 4 * x +
                              2) == x**2 + 4 * x / 3 + QQ(2, 3)

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

    assert R.dmp_ground_monic(3 * x**2 + 6 * x + 9) == x**2 + 2 * x + 3

    pytest.raises(ExactQuotientFailed,
                  lambda: R.dmp_ground_monic(3 * x**2 + 4 * x + 5))

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

    assert R.dmp_ground_monic(0) == 0
    assert R.dmp_ground_monic(1) == 1
    assert R.dmp_ground_monic(7 * x**2 + x + 21) == x**2 + x / 7 + 3
Example #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}]')