Exemplo n.º 1
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)
Exemplo n.º 2
0
def test_PolyElement___call__():
    R, x = ring("x", ZZ)
    f = 3*x + 1

    assert f(0) == 1
    assert f(1) == 4

    pytest.raises(ValueError, lambda: f())
    pytest.raises(ValueError, lambda: f(0, 1))

    pytest.raises(CoercionFailed, lambda: f(QQ(1, 7)))

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

    assert f(0, 0) == 1
    assert f(1, 7) == 53

    Ry = R.drop(x)

    assert f(0) == Ry.y**2 + 1
    assert f(1) == Ry.y**2 + 4

    pytest.raises(ValueError, lambda: f())
    pytest.raises(ValueError, lambda: f(0, 1, 2))

    pytest.raises(CoercionFailed, lambda: f(1, QQ(1, 7)))
    pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), 1))
    pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), QQ(1, 7)))
Exemplo n.º 3
0
def test_PolyElement_evaluate():
    R, x = ring("x", ZZ)
    f = x**3 + 4*x**2 + 2*x + 3

    r = f.evaluate(x, 0)
    assert r == 3 and not isinstance(r, PolyElement)

    pytest.raises(CoercionFailed, lambda: f.evaluate(x, QQ(1, 7)))

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

    r = f.evaluate(x, 0)
    assert r == 3 and isinstance(r, R.drop(x).dtype)
    r = f.evaluate([(x, 0), (y, 0)])
    assert r == 3 and isinstance(r, R.drop(x, y).dtype)
    r = f.evaluate(y, 0)
    assert r == 3 and isinstance(r, R.drop(y).dtype)
    r = f.evaluate([(y, 0), (x, 0)])
    assert r == 3 and isinstance(r, R.drop(y, x).dtype)

    r = f.evaluate([(x, 0), (y, 0), (z, 0)])
    assert r == 3 and not isinstance(r, PolyElement)

    pytest.raises(CoercionFailed, lambda: f.evaluate([(x, 1), (y, QQ(1, 7))]))
    pytest.raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1, 7)), (y, 1)]))
    pytest.raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1, 7)), (y, QQ(1, 7))]))
Exemplo n.º 4
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)
    Fxyzt,  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)
    Fxyzt,  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}
Exemplo n.º 5
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
Exemplo n.º 6
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)
    Fxyzt,  x, y, z, t = 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)
    Fxyzt,  x, y, z, t = 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, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    f = u - x
    assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
Exemplo n.º 7
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)
    Fxyzt,  x, y, z, t = 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)
    Fxyzt,  x, y, z, t = 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}
Exemplo n.º 8
0
def test_modular():
    assert solve_congruence(*list(zip([3, 4, 2], [12, 35, 17]))) == (1719,
                                                                     7140)
    assert solve_congruence(*list(zip([3, 4, 2], [12, 6, 17]))) is None
    assert solve_congruence(*list(zip([3, 4, 2], [13, 7, 17]))) == (172, 1547)
    assert solve_congruence(*list(zip([-10, -3, -15], [13, 7, 17]))) == (172,
                                                                         1547)
    assert solve_congruence(
        *list(zip([-10, -3, 1, -15], [13, 7, 7, 17]))) is None
    assert solve_congruence(
        *list(zip([-10, -5, 2, -15], [13, 7, 7, 17]))) == (835, 1547)
    assert solve_congruence(
        *list(zip([-10, -5, 2, -15], [13, 7, 14, 17]))) == (2382, 3094)
    assert solve_congruence(
        *list(zip([-10, 2, 2, -15], [13, 7, 14, 17]))) == (2382, 3094)
    assert solve_congruence(*list(zip((1, 1, 2), (3, 2, 4)))) is None
    pytest.raises(
        ValueError,
        lambda: solve_congruence(*list(zip([3, 4, 2], [12.1, 35, 17]))))

    assert integer_rational_reconstruction(ZZ(2), 3, ZZ) == QQ(-1)
    assert integer_rational_reconstruction(ZZ(21), 33, ZZ) == QQ(-1)
    assert integer_rational_reconstruction(ZZ(-21), 17, ZZ) == QQ(-4)
    assert integer_rational_reconstruction(ZZ(17), 333, ZZ) is None
    assert integer_rational_reconstruction(ZZ(49), 335, ZZ) == QQ(8, 7)
Exemplo n.º 9
0
def test_Domain__algebraic_field():
    alg = ZZ.algebraic_field(sqrt(3))
    assert alg.minpoly == Poly(x**2 - 3)
    assert alg.domain == QQ
    assert alg.from_expr(sqrt(3)).denominator == 1
    assert alg.from_expr(2 * sqrt(3)).denominator == 1
    assert alg.from_expr(sqrt(3) / 2).denominator == 2
    assert alg([QQ(7, 38), QQ(3, 2)]).denominator == 38

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

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

    assert alg.is_nonpositive(alg([-1, 1])) is True
    assert alg.is_nonnegative(alg([2, -1])) is True

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

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

    assert alg.characteristic == 0

    assert alg.is_RealAlgebraicField is True

    assert int(alg(2)) == 2
    assert int(alg.from_expr(Rational(3, 2))) == 1
    pytest.raises(TypeError, lambda: int(alg([1, 1])))

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

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

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

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

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

    alg1 = QQ.algebraic_field(I)
    alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg1 != alg2
Exemplo n.º 10
0
def test_dmp_from_diofant():
    assert dmp_from_diofant([Integer(1), Integer(2)], 0, ZZ) == [ZZ(1), ZZ(2)]
    assert dmp_from_diofant([Rational(1, 2), Integer(3)], 0,
                            QQ) == [QQ(1, 2), QQ(3, 1)]

    assert dmp_from_diofant([[Integer(1), Integer(2)], [Integer(0)]], 1,
                            ZZ) == [[ZZ(1), ZZ(2)], []]
    assert dmp_from_diofant([[Rational(1, 2), Integer(2)]], 1,
                            QQ) == [[QQ(1, 2), QQ(2, 1)]]
Exemplo n.º 11
0
def test_dmp_clear_denoms():
    R0, X = ring('x', QQ)
    R1 = R0.domain.ring.poly_ring('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.poly_ring('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))
Exemplo n.º 12
0
def test_dup_sturm():
    R, x = ring("x", QQ)

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

    f = x**3 - 2 * x**2 + 3 * x - 5
    assert R.dup_sturm(f) == [
        f, 3 * x**2 - 4 * x + 3, -10 * x / 9 + QQ(13, 3), -QQ(3303, 100)
    ]
Exemplo n.º 13
0
def test_dup_euclidean_prs():
    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

    assert R.dup_euclidean_prs(f, g) == [
        f, g, -5 * x**4 / 9 + x**2 / 9 - QQ(1, 3),
        -117 * x**2 / 25 - 9 * x + QQ(441, 25),
        233150 * x / 19773 - QQ(102500, 6591), -QQ(1288744821, 543589225)
    ]
Exemplo n.º 14
0
def test_dup_count_complex_roots_implicit():
    R, x = ring("x", ZZ)

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

    assert R.dup_count_complex_roots(f) == 5

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

    assert R.dup_count_complex_roots(f, inf=QQ(-2), sup=QQ(-1)) == 1
Exemplo n.º 15
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))
Exemplo n.º 16
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
Exemplo n.º 17
0
def test_PolyElement_compose():
    R, x = ring("x", ZZ)
    f = x**3 + 4*x**2 + 2*x + 3

    r = f.compose(x, 0)
    assert r == 3 and isinstance(r, R.dtype)

    assert f.compose(x, x) == f
    assert f.compose(x, x**2) == x**6 + 4*x**4 + 2*x**2 + 3

    pytest.raises(CoercionFailed, lambda: f.compose(x, QQ(1, 7)))

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

    r = f.compose(x, 0)
    assert r == 3 and isinstance(r, R.dtype)
    r = f.compose([(x, 0), (y, 0)])
    assert r == 3 and isinstance(r, R.dtype)
    r = f.compose({x: 0, y: 0})
    assert r == 3 and isinstance(r, R.dtype)
    pytest.raises(ValueError, lambda: f.compose("spam"))

    r = (x**3 + 4*x**2 + 2*x*y*z + 3).compose(x, y*z**2 - 1)
    q = (y*z**2 - 1)**3 + 4*(y*z**2 - 1)**2 + 2*(y*z**2 - 1)*y*z + 3
    assert r == q and isinstance(r, R.dtype)
Exemplo n.º 18
0
def test_dup_count_complex_roots_1():
    R, x = ring("x", ZZ)

    f = x - 1

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

    f = -f

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

    f = x + 1

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

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

    f = x - QQ(1, 2)

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

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

    pytest.raises(DomainError, lambda: R.dup_count_complex_roots(x))
Exemplo n.º 19
0
def test_PolyElement_terms():
    R,  x, y, z = ring("x,y,z", QQ)
    terms = (x**2/3 + y**3/4 + z**4/5).terms()
    assert terms == [((2, 0, 0), QQ(1, 3)), ((0, 3, 0), QQ(1, 4)), ((0, 0, 4), QQ(1, 5))]

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

    assert f.terms() == f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
    assert f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]

    R,  x, y = ring("x,y", ZZ, grlex)
    f = x*y**7 + 2*x**2*y**3

    assert f.terms() == f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
    assert f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
Exemplo n.º 20
0
def test_PolyElement_coeffs():
    R,  x, y, z = ring("x,y,z", QQ)
    coeffs = (x**2/3 + y**3/4 + z**4/5).coeffs()
    assert coeffs == [QQ(1, 3), QQ(1, 4), QQ(1, 5)]

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

    assert f.coeffs() == f.coeffs(lex) == f.coeffs('lex') == [2, 1]
    assert f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]

    R,  x, y = ring("x,y", ZZ, grlex)
    f = x*y**7 + 2*x**2*y**3

    assert f.coeffs() == f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]
    assert f.coeffs(lex) == f.coeffs('lex') == [2, 1]
Exemplo n.º 21
0
def _do_test_benchmark_minimal_polynomial():
    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
Exemplo n.º 22
0
def test_arithmetics():
    assert ZZ.rem(ZZ(2), ZZ(3)) == 2
    assert ZZ.div(ZZ(2), ZZ(3)) == (0, 2)
    assert QQ.rem(QQ(2, 3), QQ(4, 7)) == 0
    assert QQ.div(QQ(2, 3), QQ(4, 7)) == (QQ(7, 6), 0)

    assert QQ_python.factorial(QQ_python(7, 2)) == 6

    assert CC.gcd(CC(1), CC(2)) == 1
    assert CC.lcm(CC(1), CC(2)) == 2

    assert EX(Rational(2, 3)).numerator == 2
    assert EX(Rational(2, 3)).denominator == 3

    assert abs(EX(-2)) == 2

    assert -EX(2) == -2
    assert 2 + EX(3) == EX(3) + 2 == 5
    assert 2 - EX(3) == EX(2) - 3 == -1
    assert 2 * EX(3) == EX(3) * 2 == 6
    assert 2 / EX(3) == EX(2) / 3 == EX(Rational(2, 3))
    assert EX(2)**2 == 4

    pytest.raises(TypeError, lambda: EX(2) + object())
    pytest.raises(TypeError, lambda: EX(2) - object())
    pytest.raises(TypeError, lambda: EX(2) * object())
    pytest.raises(TypeError, lambda: EX(2)**object())
    pytest.raises(TypeError, lambda: EX(2) / object())

    F11 = FF(11)
    assert +F11(2) == F11(2)
    assert F11(5) + 7 == 7 + F11(5) == F11(1)
    assert F11(5) - 7 == 5 - F11(7) == F11(9)
    assert F11(5) * 7 == 7 * F11(5) == F11(2)
    assert F11(5) / 9 == 5 / F11(9) == F11(3)
    assert F11(4) % 9 == 4 % F11(9) == F11(4)

    pytest.raises(TypeError, lambda: F11(2) + object())
    pytest.raises(TypeError, lambda: F11(2) - object())
    pytest.raises(TypeError, lambda: F11(2) * object())
    pytest.raises(TypeError, lambda: F11(2)**object())
    pytest.raises(TypeError, lambda: F11(2) / object())
    pytest.raises(TypeError, lambda: F11(2) % object())
    pytest.raises(TypeError, lambda: object() % F11(2))
Exemplo n.º 23
0
def test_dmp_quo_ground():
    R, x = ring('x', ZZ)

    pytest.raises(ZeroDivisionError,
                  lambda: R.dmp_quo_ground(x**2 + 2 * x + 3, ZZ(0)))

    f = 3 * x**2 + 2

    assert R.dmp_quo_ground(f, ZZ(2)) == x**2 + 1

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

    f = 3 * x**2 + 2

    assert R.dmp_quo_ground(f, QQ(2)) == 3 * x**2 / 2 + 1

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

    f = 0

    assert R.dmp_quo_ground(f, ZZ(3)) == 0

    f = 6 * x**2 + 2 * x + 8

    assert R.dmp_quo_ground(f, ZZ(1)) == f
    assert R.dmp_quo_ground(f, ZZ(2)) == 3 * x**2 + x + 4
    assert R.dmp_quo_ground(f, ZZ(3)) == 2 * x**2 + 2

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

    f = 6 * x**2 + 2 * x + 8

    assert R.dmp_quo_ground(f, QQ(1)) == f
    assert R.dmp_quo_ground(f, QQ(2)) == 3 * x**2 + x + 4
    assert R.dmp_quo_ground(f, QQ(7)) == 6 * x**2 / 7 + 2 * x / 7 + QQ(8, 7)

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

    f = 6 * x**2 + 2 * x + 8

    assert R.dmp_quo_ground(f, ZZ(1)) == f
    assert R.dmp_quo_ground(f, ZZ(2)) == 3 * x**2 + x + 4
    assert R.dmp_quo_ground(f, ZZ(3)) == 2 * x**2 + 2
Exemplo n.º 24
0
def test_dmp_validate():
    assert dmp_validate([]) == ([], 0)
    assert dmp_validate([0, 0, 0, 1, 0]) == ([1, 0], 0)

    assert dmp_validate([[[]]]) == ([[[]]], 2)
    assert dmp_validate([[0], [], [0], [1], [0]]) == ([[1], []], 1)

    pytest.raises(ValueError, lambda: dmp_validate([[0], 0, [0], [1], [0]]))
    pytest.raises(TypeError,
                  lambda: dmp_validate([[], [0, QQ(1, 2)], [1]], ZZ))
Exemplo n.º 25
0
def test_FracElement___truediv__():
    F, x, y = field("x,y", QQ)

    f, g = 1 / x, 1 / y
    assert f / g == y / x

    assert x / F.ring.gens[0] == F.ring.gens[0] / x == 1

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

    pytest.raises(ZeroDivisionError, lambda: x / 0)
    pytest.raises(ZeroDivisionError, lambda: 1 / (x - x))
    pytest.raises(ZeroDivisionError, lambda: x / (x - x))

    Fuv, u, v = field("u,v", ZZ)
    Fxyzt, x, y, z, t = field("x,y,z,t", Fuv)

    f = (u * v) / (x * y)
    assert dict(f.numer) == {(0, 0, 0, 0): u * v}
    assert dict(f.denom) == {(1, 1, 0, 0): 1}

    g = (x * y) / (u * v)
    assert dict(g.numer) == {(1, 1, 0, 0): 1}
    assert dict(g.denom) == {(0, 0, 0, 0): u * v}

    Ruv, u, v = ring("u,v", ZZ)
    Fxyzt, x, y, z, t = field("x,y,z,t", Ruv)

    f = (u * v) / (x * y)
    assert dict(f.numer) == {(0, 0, 0, 0): u * v}
    assert dict(f.denom) == {(1, 1, 0, 0): 1}

    g = (x * y) / (u * v)
    assert dict(g.numer) == {(1, 1, 0, 0): 1}
    assert dict(g.denom) == {(0, 0, 0, 0): u * v}

    Fuv, u, v = field("u,v", ZZ)
    Rxyz, x, y, z = ring("x,y,z", Fuv)

    pytest.raises(TypeError, lambda: u / x)
Exemplo n.º 26
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)
Exemplo n.º 27
0
def test_dmp_sqr():
    R, x = ring('x', ZZ)

    assert R.dmp_sqr(0) == 0
    assert R.dmp_sqr(2) == 4
    assert R.dmp_sqr(x + 2) == x**2 + 4 * x + 4

    assert R.dmp_sqr(2 * x**4 + x +
                     7) == 4 * x**8 + 4 * x**5 + 28 * x**4 + x**2 + 14 * x + 49

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

    assert R.dmp_sqr(0) == 0
    assert R.dmp_sqr(QQ(2, 3)) == QQ(4, 9)
    assert R.dmp_sqr(x / 3 + QQ(2, 3)) == x**2 / 9 + 4 * x / 9 + QQ(4, 9)

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

    assert R.dmp_sqr(3 * x + 4) == 2 * x**2 + 3 * x + 2

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

    assert R.dmp_sqr(0) == 0
    assert R.dmp_sqr(2) == 4

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

    assert R.dmp_sqr(0) == 0
    assert R.dmp_sqr(QQ(2, 3)) == QQ(4, 9)

    R, x, y = ring('x y', FF(7))

    assert R.dmp_sqr(3 * x + 4) == 2 * x**2 + 3 * x + 2
Exemplo n.º 28
0
def test_dup_isolate_real_roots_list_QQ():
    R, x = ring("x", ZZ)

    f, g = x**5 - 200, x**5 - 201

    assert R.dup_isolate_real_roots_list([f, g]) == \
        [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]

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

    f, g = -x**5 / 200 + 1, -x**5 / 201 + 1

    assert R.dup_isolate_real_roots_list([f, g]) == \
        [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]
Exemplo n.º 29
0
def test_PolyElement_gcdex():
    _, x = ring("x", QQ)

    f, g = 2*x, x**2 - 16
    s, t, h = x/32, -QQ(1, 16), 1

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

    _, 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))
Exemplo n.º 30
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)