Beispiel #1
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
Beispiel #2
0
def test__gf_trace_map():
    R, x = ring('x', FF(5))

    a = x + 2
    b = 4 * x + 4
    c = x + 1
    f = 3 * x**2 + 2 * x + 4

    assert R._gf_trace_map(a, b, c, 4, f) == (x + 3, x + 3)

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

    f = x**4 + x**3 + 4 * x**2 + 9 * x + 1
    a = x**2 + x + 1
    c = x
    b = pow(c, 11, f)

    assert R._gf_trace_map(a, b, c, 0, f) == (x**2 + x + 1, x**2 + x + 1)
    assert R._gf_trace_map(a, b, c, 1, f) == (5 * x**3 + 2 * x**2 + 10 * x + 3,
                                              5 * x**3 + 3 * x**2 + 4)
    assert R._gf_trace_map(a, b, c, 2, f) == (5 * x**3 + 9 * x**2 + 5 * x + 3,
                                              10 * x**3 + x**2 + 5 * x + 7)
    assert R._gf_trace_map(a, b, c, 3, f) == (x**3 + 10 * x**2 + 6 * x, 7)
    assert R._gf_trace_map(a, b, c, 4, f) == (x**2 + x + 1, x**2 + x + 8)
    assert R._gf_trace_map(a, b, c, 5, f) == (5 * x**3 + 2 * x**2 + 10 * x + 3,
                                              5 * x**3 + 3 * x**2)
    assert R._gf_trace_map(a, b, c, 11, f) == (x**3 + 10 * x**2 + 6 * x, 10)
Beispiel #3
0
def test_dup_gf_irreducible_p():
    R, x = ring('x', FF(11))

    f = R.to_dense(R(7))
    g = R.to_dense(7 * x + 3)
    h = R.to_dense(7 * x**2 + 3 * x + 1)

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert dup_gf_irreducible_p(f, R.domain) is True
            assert dup_gf_irreducible_p(g, R.domain) is True
            assert dup_gf_irreducible_p(h, R.domain) is False

    with using(gf_irred_method='other'):
        pytest.raises(KeyError, lambda: dup_gf_irreducible_p(f, R.domain))

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

    f = R.to_dense(2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6)
    g = R.to_dense(2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 8)

    with using(gf_irred_method='ben-or'):
        assert dup_gf_irreducible_p(f, R.domain) is False
        assert dup_gf_irreducible_p(g, R.domain) is True

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

    f = (x**10 + 9 * x**9 + 9 * x**8 + 13 * x**7 + 16 * x**6 + 15 * x**5 +
         6 * x**4 + 7 * x**3 + 7 * x**2 + 7 * x + 10)
    g = (x**10 + 7 * x**9 + 16 * x**8 + 7 * x**7 + 15 * x**6 + 13 * x**5 +
         13 * x**4 + 11 * x**3 + 16 * x**2 + 10 * x + 9)
    h = f * g
    f, g, h = map(R.to_dense, (f, g, h))

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert dup_gf_irreducible_p(f, R.domain) is True
            assert dup_gf_irreducible_p(g, R.domain) is True
            assert dup_gf_irreducible_p(h, R.domain) is False

    F9 = FF(3, [1, 2, 2])
    R, x = ring('x', F9)

    f = R.to_dense(x**3 + F9(8) * x**2 + F9(8) * x + F9(4))

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert dup_gf_irreducible_p(f, R.domain) is False

    F27 = FF(3, [1, 2, 0, 1])
    R, x = ring('x', F27)

    f = R.to_dense(x**3 + F27(8) * x**2 + F27(19) * x + F27(24))

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert dup_gf_irreducible_p(f, R.domain) is True
Beispiel #4
0
def test_dmp_normal():
    assert dmp_normal([0, 1.5, 2, 3], 0, ZZ) == [ZZ(1), ZZ(2), ZZ(3)]

    assert (dmp_normal([0, 0, 2, 1, 0, 11, 0], 0, ZZ) ==
            [ZZ(2), ZZ(1), ZZ(0), ZZ(11), ZZ(0)])

    assert (dmp_normal([[0], [], [0, 2, 1], [0], [11], []], 1, ZZ) ==
            [[ZZ(2), ZZ(1)], [], [ZZ(11)], []])

    F5 = FF(5)
    assert dmp_normal([5, 10, 21, -3], 0, F5) == [F5(1), F5(2)]
    F11 = FF(11)
    assert dmp_normal([11, 22, 17, 1, 0], 0, F11) == [F11(6), F11(1), F11(0)]
Beispiel #5
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 #6
0
def test_Modulus_postprocess():
    opt = {'modulus': 5}
    Modulus.postprocess(opt)

    assert opt == {
        'modulus': 5,
        'domain': FF(5),
    }

    opt = {'modulus': 5}
    Modulus.postprocess(opt)

    assert opt == {
        'modulus': 5,
        'domain': FF(5),
    }
Beispiel #7
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()
Beispiel #8
0
def test_sympyissue_23174():
    _, x = ring('x', FF(2))

    f = (x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**10 + x**9 + x**8 +
         x**7 + x**6 + x**5 + x**4 + x**3 + x**2 + x + 1)
    r = (1, [(x**8 + x**5 + x**4 + x**3 + 1, 1),
             (x**8 + x**7 + x**6 + x**4 + x**2 + x + 1, 1)])

    with using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == r
Beispiel #9
0
def test_gf_powering():
    R, x = ring('x', FF(11))

    f = R.to_dense(x**4 + x + 8)
    g = R.to_dense(2 * x**2 + 7)

    assert dup_gf_pow_mod(f, 0, g, R.domain) == [1]
    assert dup_gf_pow_mod(f, 1, g, R.domain) == [1, 1]
    assert dup_gf_pow_mod(f, 2, g, R.domain) == [2, 3]
    assert dup_gf_pow_mod(f, 5, g, R.domain) == [7, 8]
    assert dup_gf_pow_mod(f, 8, g, R.domain) == [1, 5]
    assert dup_gf_pow_mod(f, 45, g, R.domain) == [5, 4]
Beispiel #10
0
def test_dup_gf_compose_mod():
    R, x = ring('x', FF(11))

    g = []
    h = R.to_dense(x)
    f = h.copy()

    assert dup_gf_compose_mod(g, h, h, R.domain) == []

    f = R.to_dense(x**4 + x**3 + 4 * x**2 + 9 * x + 1)
    g = R.to_dense(x**2 + x + 1)
    h = R.to_dense(x**3 + 2)

    assert dup_gf_compose_mod(g, h, f, R.domain) == [3, 9, 6, 10]
Beispiel #11
0
def test_ratsimpmodprime():
    a = y**5 + x + y
    b = x - y
    F = [x*y**5 - x - y]
    assert ratsimpmodprime(a/b, F, x, y, order='lex') == \
        (x**2 + x*y + x + y)/(x**2 - x*y)

    a = x + y**2 - 2
    b = x + y**2 - y - 1
    F = [x*y - 1]
    assert ratsimpmodprime(a/b, F, x, y, order='lex') == \
        (x - y - 1)/(x - y)

    a = 5*x**3 + 21*x**2 + 4*x*y + 23*x + 12*y + 15
    b = 7*x**3 - y*x**2 + 31*x**2 + 2*x*y + 15*y + 37*x + 21
    F = [x**2 + y**2 - 1]
    assert ratsimpmodprime(a/b, F, x, y, order='lex') == \
        (5*x - 5*y - 1)/(6*x - 8*y)

    a = x*y - x - 2*y + 4
    b = x + y**2 - 2*y
    F = [x - 2, y - 3]
    assert ratsimpmodprime(a/b, F, x, y, order='lex') == \
        Rational(2, 5)

    # Test a bug where denominators would be dropped
    assert ratsimpmodprime(x, [y - 2*x], order='lex') == \
        y/2

    # coverage tests
    assert ratsimpmodprime(x, [x - y/2], x, y, order='lex') == 2*y
    pytest.raises(ValueError, lambda: ratsimpmodprime((x + y)/(x - y),
                                                      [y**2 - 1, x**2 - 1]))

    a = (x**5 + 2*x**4 + 2*x**3 + 2*x**2 + x + 2/x + x**(-2))
    assert ratsimpmodprime(a, [x + 1], domain=FF(2)) == 1
    assert ratsimpmodprime(a, [x + 1], domain=FF(3)) == 2
Beispiel #12
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))
Beispiel #13
0
def test_dup_gf_primitive_p():
    R, x = ring('x', FF(11))

    f = R.to_dense(x**2 + 2 * x)

    assert dup_gf_primitive_p(f, R.domain) is False

    f = R.to_dense(x**2 + 7 * x + 5)

    assert dup_gf_primitive_p(f, R.domain) is False

    f = R.to_dense(x**2 + 2 * x + 6)

    assert dup_gf_irreducible_p(f, R.domain) is True
    assert dup_gf_primitive_p(f, R.domain) is True
Beispiel #14
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 #15
0
def test_dup_gf_trace_map():
    R, x = ring('x', FF(11))

    f = R.to_dense(x**4 + x**3 + 4 * x**2 + 9 * x + 1)
    a = R.to_dense(x**2 + x + 1)
    c = R.to_dense(x)
    b = dup_gf_pow_mod(c, 11, f, R.domain)

    assert dup_gf_trace_map(a, b, c, 0, f, R.domain) == ([1, 1, 1], [1, 1, 1])
    assert dup_gf_trace_map(a, b, c, 1, f, R.domain) == ([5, 2, 10,
                                                          3], [5, 3, 0, 4])
    assert dup_gf_trace_map(a, b, c, 2, f, R.domain) == ([5, 9, 5,
                                                          3], [10, 1, 5, 7])
    assert dup_gf_trace_map(a, b, c, 3, f, R.domain) == ([1, 10, 6, 0], [7])
    assert dup_gf_trace_map(a, b, c, 4, f, R.domain) == ([1, 1, 1], [1, 1, 8])
    assert dup_gf_trace_map(a, b, c, 5, f, R.domain) == ([5, 2, 10,
                                                          3], [5, 3, 0, 0])
    assert dup_gf_trace_map(a, b, c, 11, f, R.domain) == ([1, 10, 6, 0], [10])
Beispiel #16
0
def test_gcdex():
    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)

    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)
Beispiel #17
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.lcm(QQ(2, 3), QQ(4, 9)) == QQ(4, 3)

    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))
Beispiel #18
0
def test_sympyissue_16620():
    R, x = ring('x', FF(2))

    f = x**17 + 1
    g = (1, [(x + 1, 1), (x**8 + x**5 + x**4 + x**3 + 1, 1),
             (x**8 + x**7 + x**6 + x**4 + x**2 + x + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**31 + 1
    g = (1, [(x + 1, 1), (x**5 + x**2 + 1, 1), (x**5 + x**3 + 1, 1),
             (x**5 + x**3 + x**2 + x + 1, 1), (x**5 + x**4 + x**2 + x + 1, 1),
             (x**5 + x**4 + x**3 + x + 1, 1),
             (x**5 + x**4 + x**3 + x**2 + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g
Beispiel #19
0
def test_PolyElement_is_irreducible():
    R, x = ring('x', ZZ)

    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2*x + 1).is_irreducible is False
    assert (x**2 - 1).is_irreducible is False

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

    assert R(2).is_irreducible is True
    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2*x + 1).is_irreducible is False
    assert ((x - 2*y)*(x + y)).is_irreducible is False
    assert (x**2 + y**2).is_irreducible is True

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

    assert (x**2 + x + 1).is_irreducible
    assert (x**2 + 2*x + 1).is_irreducible is False

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

    assert (7*x + 3).is_irreducible
    assert (7*x**2 + 3*x + 1).is_irreducible is False
Beispiel #20
0
def test_dup_gf_irreducible():
    F11 = FF(11)

    for n in range(8):
        assert dup_gf_irreducible_p(dup_gf_irreducible(n, F11), F11) is True
Beispiel #21
0
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.inject(x, y)) == ZZ.inject(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.inject(x)
    assert Domain.preprocess('Q[x]') == QQ.inject(x)

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

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

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

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

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

    assert Domain.preprocess('ZZ(x)') == ZZ.inject(x).field
    assert Domain.preprocess('QQ(x)') == QQ.inject(x).field

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

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

    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(()))
Beispiel #22
0
def test_gf_factor():
    R, x = ring('x', FF(2))

    f = x**4 + x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**18 + x**17 + x**16 + x**14 + x**12 + x**11 + x**8 + x**5 + x**3 + 1
    g = (1, [(x + 1, 4), (x**4 + x**3 + 1, 1),
             (x**10 + x**8 + x**7 + x**5 + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**63 + 1
    g = (1, [(x + 1, 1), (x**2 + x + 1, 1),
             (x**3 + x + 1, 1), (x**6 + x + 1, 1), (x**3 + x**2 + 1, 1),
             (x**6 + x**3 + 1, 1), (x**6 + x**5 + 1, 1),
             (x**6 + x**4 + x**2 + x + 1, 1), (x**6 + x**5 + x**2 + x + 1, 1),
             (x**6 + x**4 + x**3 + x + 1, 1), (x**6 + x**5 + x**4 + x + 1, 1),
             (x**6 + x**5 + x**3 + x**2 + 1, 1),
             (x**6 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = (x**28 + x**27 + x**26 + x**25 + x**24 + x**20 + x**19 + x**17 +
         x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**9 + x**8 + x**5 +
         x**4 + x**2 + x)
    g = (1, [(x, 1), (x + 1, 2), (x**5 + x**4 + x**3 + x + 1, 1),
             (x**10 + x**9 + x**8 + x**7 + 1, 1),
             (x**10 + x**9 + x**8 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

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

    f = x**6 - x**5 + x**4 + x**3 - x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**4 + x**3 + x + 2
    g = (1, [(x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

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

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert R(0).factor_list() == (0, [])
            assert R(1).factor_list() == (1, [])
            assert x.factor_list() == (1, [(x, 1)])
            assert (x + 1).factor_list() == (1, [(x + 1, 1)])
            assert (2 * x + 3).factor_list() == (2, [(x + 7, 1)])

    assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1),
                                                                   (x + 8, 2)])

    f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1
    g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1),
             (x**3 + 2 * x**2 + 3 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**3 + 5 * x**2 + 8 * x + 4
    g = (1, [(x + 1, 1), (x + 2, 2)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2
    g = (1, [(x, 2), (x**2 + 9 * x + 5, 1),
             (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**32 + 1
    g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**32 + 5
    g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1),
             (x**2 + 9 * x + 2, 1), (x**8 + x**4 + 6, 1),
             (x**8 + 10 * x**4 + 6, 1), (x**4 + 5 * x**2 + 7, 1),
             (x**4 + 6 * x**2 + 7, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**63 + 5
    g = (8,
         [(x + 7, 1), (x**6 + 9 * x**3 + 4, 1), (x**2 + 4 * x + 5, 1),
          (x**3 + 6 * x**2 + 8 * x + 2, 1), (x**3 + 9 * x**2 + 9 * x + 2, 1),
          (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1),
          (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1),
          (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1),
          (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1),
          (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1),
          (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1),
          (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**15 - 1
    g = (1, [(x + 2, 1), (x + 6, 1), (x + 7, 1), (x + 8, 1), (x + 10, 1),
             (x**2 + x + 1, 1), (x**2 + 5 * x + 3, 1), (x**2 + 9 * x + 4, 1),
             (x**2 + 4 * x + 5, 1), (x**2 + 3 * x + 9, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    with using(gf_factor_method='other'):
        pytest.raises(KeyError, lambda: (x + 1).factor_list())

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

    f = x**8 + x**6 + 10 * x**4 + 10 * x**3 + 8 * x**2 + 2 * x + 8
    g = (1, [(x + 3, 1), (x**3 + 8 * x**2 + 4 * x + 12, 1),
             (x**4 + 2 * x**3 + 3 * x**2 + 4 * x + 6, 1)])

    with using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

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

    f = (x**10 + 2 * x**9 + 5 * x**8 + 26 * x**7 + 677 * x**6 + 436 * x**5 +
         791 * x**4 + 325 * x**3 + 456 * x**2 + 24 * x + 577)
    g = (1, [(x + 701, 1),
             (x**9 + 110 * x**8 + 559 * x**7 + 532 * x**6 + 694 * x**5 +
              151 * x**4 + 110 * x**3 + 70 * x**2 + 735 * x + 122, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    R, x = ring('x', FF(nextprime(2**15 * pi)))

    f = x**15 + x + 1
    g = (1, [
        (x**2 + 22730 * x + 68144, 1),
        (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1),
        (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1),
        (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335,
         1)
    ])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
    # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1

    R, x = ring('x', FF(nextprime(2**4 * pi)))

    f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38
    g = (1, [(x**2 + 44 * x + 26, 1),
             (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F8 = FF(2, [1, 1, 0, 1])
    R, x = ring('x', F8)

    f = x**10 + x**9 + F8(2) * x**8 + F8(2) * x**7 + F8(5) * x**6 + F8(
        3) * x**5
    g = (F8(1), [(x, 5), (x + F8(3), 1), (x + F8(6), 1),
                 (x**3 + F8(4) * x**2 + x + F8(3), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F9 = FF(3, [2, 2, 1])
    R, x = ring('x', F9)

    f = x**5 + F9(2) * x**4 + F9(6) * x**3 + F9(8) * x**2 + F9(5) * x + F9(4)
    g = (1, [(x + F9(8), 1), (x**2 + 2 * x + F9(4), 1),
             (x**2 + F9(4) * x + F9(4), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g
Beispiel #23
0
def test_PolyElement_is_irreducible():
    R, x = ring('x', FF(5))

    f = (x**10 + 4 * x**9 + 2 * x**8 + 2 * x**7 + 3 * x**6 + 2 * x**5 +
         4 * x**4 + x**3 + 4 * x**2 + 4)
    g = 3 * x**2 + 2 * x + 4

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is False

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

    f = R(7)
    g = 7 * x + 3
    h = 7 * x**2 + 3 * x + 1

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is True
            assert h.is_irreducible is False

    with using(gf_irred_method='other'):
        pytest.raises(KeyError, lambda: f.is_irreducible)

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

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

    with using(gf_irred_method='ben-or'):
        assert f.is_irreducible is False
        assert g.is_irreducible is True

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

    f = (x**10 + 9 * x**9 + 9 * x**8 + 13 * x**7 + 16 * x**6 + 15 * x**5 +
         6 * x**4 + 7 * x**3 + 7 * x**2 + 7 * x + 10)
    g = (x**10 + 7 * x**9 + 16 * x**8 + 7 * x**7 + 15 * x**6 + 13 * x**5 +
         13 * x**4 + 11 * x**3 + 16 * x**2 + 10 * x + 9)
    h = f * g

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is True
            assert h.is_irreducible is False

    F9 = FF(3, [2, 2, 1])
    R, x = ring('x', F9)

    f = x**3 + F9(8) * x**2 + F9(8) * x + F9(4)

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is False

    F27 = FF(3, [1, 0, 2, 1])
    R, x = ring('x', F27)

    f = x**3 + F27(8) * x**2 + F27(19) * x + F27(24)

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True

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

    assert x.is_irreducible is True
    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2 * x + 1).is_irreducible is False
    assert (x**2 - 1).is_irreducible is False

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

    assert (f + 7).is_irreducible is True
    assert (f + 4).is_irreducible is True
    assert (f + 10).is_irreducible is True
    assert (f + 14).is_irreducible is True

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

    assert R(2).is_irreducible is True
    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2 * x + 1).is_irreducible is False
    assert ((x - 2 * y) * (x + y)).is_irreducible is False
    assert (x**2 + y**2).is_irreducible is True

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

    assert (x**2 + x + 1).is_irreducible
    assert (x**2 + 2 * x + 1).is_irreducible is False
Beispiel #24
0
def test_FiniteField():
    sT(FF(2), 'GF(2)')

    F4 = FF(2, [1, 1, 1])
    repr(F4.one)  # not raises
Beispiel #25
0
def test_factor_list():
    R, x = ring('x', FF(2))

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

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

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

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

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

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

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)])

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

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

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

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

    assert (1.0 * x**2 + 2.0 * x + 1.0).factor_list() == (1.0, [(1.0 * x + 1.0,
                                                                 2)])
    assert (2.0 * x**2 + 4.0 * x + 2.0).factor_list() == (2.0, [(1.0 * x + 1.0,
                                                                 2)])

    f = 6.7225336055071 * x**2 - 10.6463972754741 * x - 0.33469524022264

    assert f.factor_list() == (1.0, [(f, 1)])

    # issue diofant/diofant#238
    f = 0.1 * x**2 + 1.1 * x + 1.0

    assert f.factor_list() == (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 f.factor_list() == (4.0, [(0.25 + 0.5 * x, 2)])

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

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

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

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

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1),
                                                                   (x + t, 1)])

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

    pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list())

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

    pytest.raises(NotImplementedError, lambda: (x**2 + y**2).factor_list())

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

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

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

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

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)])
    assert (x**2 * y / 2 + x * y**2 / 2).factor_list() == (QQ(1,
                                                              2), [(y, 1),
                                                                   (x, 1),
                                                                   (x + y, 1)])

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

    f, r = x**2 + y**2, (1, [(x - I * y, 1), (x + I * y, 1)])

    for method in ('trager', 'modular'):
        with using(aa_factor_method=method):
            assert f.factor_list() == r

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

    f = 2.0 * x**2 - 8.0 * y**2

    assert f.factor_list() == (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

    assert f.factor_list() == (1.0, [(f, 1)])

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

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

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

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

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1),
                                                                   (x + t, 1)])

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

    pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list())

    # 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 f.factor_list() == (10.0, [(x + 0.1, 1), (0.1 * y + 0.1 * z, 1)])

    f = 0.25 * x**2 + 1.0 * x * y * z + 1.0 * y**2 * z**2

    assert f.factor_list() == (4.0, [(0.25 * x + 0.5 * y * z, 2)])

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

    f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1

    assert f.factor_list() == (1, [(g, 2)])

    f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1

    assert f.factor_list() == (1, [(g, 2)])
Beispiel #26
0
def test_ModularInteger():
    F3 = FF(3)

    a = F3(0)
    assert isinstance(a, F3.dtype) and a == 0
    a = F3(1)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2)
    assert isinstance(a, F3.dtype) and a == 2
    a = F3(3)
    assert isinstance(a, F3.dtype) and a == 0
    a = F3(4)
    assert isinstance(a, F3.dtype) and a == 1

    assert a.numerator == a
    assert a.denominator == F3.one

    a = F3(F3(0))
    assert isinstance(a, F3.dtype) and a == 0
    a = F3(F3(1))
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(F3(2))
    assert isinstance(a, F3.dtype) and a == 2
    a = F3(F3(3))
    assert isinstance(a, F3.dtype) and a == 0
    a = F3(F3(4))
    assert isinstance(a, F3.dtype) and a == 1

    a = -F3(1)
    assert isinstance(a, F3.dtype) and a == 2
    a = -F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = 2 + F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) + 2
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) + F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) + F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = 3 - F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(3) - 2
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(3) - F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(3) - F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = 2 * F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) * 2
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) * F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) * F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = 2 / F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) / 2
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) / F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2) / F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = 1 % F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(1) % 2
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(1) % F3(2)
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(1) % F3(2)
    assert isinstance(a, F3.dtype) and a == 1

    a = F3(2)**0
    assert isinstance(a, F3.dtype) and a == 1
    a = F3(2)**1
    assert isinstance(a, F3.dtype) and a == 2
    a = F3(2)**2
    assert isinstance(a, F3.dtype) and a == 1

    assert bool(F3(3)) is False
    assert bool(F3(4)) is True

    assert F3(2).is_primitive is True

    F5 = FF(5)

    a = F5(1)**(-1)
    assert isinstance(a, F5.dtype) and a == 1
    a = F5(2)**(-1)
    assert isinstance(a, F5.dtype) and a == 3
    a = F5(3)**(-1)
    assert isinstance(a, F5.dtype) and a == 2
    a = F5(4)**(-1)
    assert isinstance(a, F5.dtype) and a == 4

    pytest.raises(NotInvertible, lambda: F5(0)**(-1))
    pytest.raises(NotInvertible, lambda: F5(5)**(-1))

    pytest.raises(ValueError, lambda: FF(0))
    pytest.raises(ValueError, lambda: FF(2.1))
    pytest.raises(ValueError, lambda: FF(6))
    pytest.raises(ValueError, lambda: FF(9, [1, 0]))
    pytest.raises(ValueError, lambda: FF(9, [1, 1, 1]))
    pytest.raises(ValueError, lambda: FF(36))

    assert F5.is_normal(a) is True
    assert F5.is_normal(F5.zero) is True

    assert F5 == FF(5, [1, 0])

    assert F5(2).is_primitive is True

    F7 = FF(7)

    assert F7(2).is_primitive is False

    F9 = FF(9, [1, 0, 1])

    assert F9.order == 9
    assert F9.characteristic == 3
    assert F9.inject(x).characteristic == 3
    assert F9.inject(x).field.characteristic == 3

    assert F9.zero == F9([0])
    assert F9.one == F9([1])
    assert F9(F9([2, 1])) == F9([2, 1])
    assert F9([1, 2]) + F9([2, 1]) == F9([2, 1]) + F9([1, 2]) == F9.zero
    assert F9([1, 2]) + F9([3, 1]) == F9.one
    assert F9([1, 2]) * F9([3, 1]) == F9([1, 1])
    assert sum(F9.one for _ in range(3)) == F9.zero

    assert int(F9.zero) == 0
    assert int(F9.one) == 1
    assert int(F9([1, 1])) == int(F9(4)) == 4
    assert int(F9([0, 2])) == int(F9(6)) == 6

    F81 = FF(3, [2, 1, 0, 0, 1])

    assert F81([1, 2, 1]) * F81([2, 2, 2]) == F81([1, 1, 2])
    assert F81([1, 2, 3, 4, 5]) == F81([0, 0, 0, 1])
    assert F81([0, 1, 0]) == F81([0, 1])
    assert 1 + F81([1, 1]) == F81([2, 1])

    F8 = FF(8)

    assert F8.order == 8
    assert F8.characteristic == 2
    assert F8.dtype.mod.all_coeffs() == [1, 1, 0, 1]
    assert int(F8([1, 0, 1])) == int(F8(5)) == 5
    assert int(F8(-1)) == int(F8(7)) == 7
    assert str(F8([1, 0, 1])) == 'GF(2, [1, 1, 0, 1])(5)'

    F4 = FF(2, [1, 1, 1])

    assert F4.order == 4
    assert F4.characteristic == 2

    assert F4(1) == F4.one
    assert F4.one + F4.one == 2 * F4.one == F4.one * 2 == F4.zero
    assert F4(2) * F4.one == F4.one * F4(2)
    assert 2 * F4.one != F4(2) * F4.one

    pytest.raises(TypeError, lambda: object() * F4.one)

    F1331 = FF(11, [7, 8, 4, 1])

    assert F1331([0, 2, 1]).is_primitive is False
    assert F1331([5, 7, 1]).is_primitive is False
    assert F1331([6, 2, 1]).is_primitive is True
Beispiel #27
0
def test_FF_of_type():
    assert isinstance(FF(3)(1), FF(3).dtype) is True
    assert isinstance(FF(5)(3), FF(5).dtype) is True
    assert isinstance(FF(7)(3), FF(5).dtype) is False
Beispiel #28
0
def test_dmp_gcd():
    R, x = ring('x', FF(5))

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

    assert f.cofactors(g) == (x + 3, 3 * x + 3, 2 * x + 1)

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

    assert R(0).cofactors(R(0)) == (0, 0, 0)
    assert R(2).cofactors(R(0)) == (1, 2, 0)
    assert R(0).cofactors(R(2)) == (1, 0, 2)
    assert R(2).cofactors(R(2)) == (1, 2, 2)

    assert R(0).cofactors(x) == (x, 0, 1)
    assert x.cofactors(R(0)) == (x, 1, 0)

    assert (3 * x).cofactors(3 * x) == (x, 3, 3)
    assert (x**2 + 8 * x + 7).cofactors(x**3 + 7 * x**2 + x + 7) == (x + 7,
                                                                     x + 1,
                                                                     x**2 + 1)

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

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                assert R(0).cofactors(R(0)) == (0, 0, 0)
                assert R(0).cofactors(x) == (x, 0, 1)
                assert x.cofactors(R(0)) == (x, 1, 0)
                assert R(0).cofactors(-x) == (x, 0, -1)
                assert (-x).cofactors(R(0)) == (x, -1, 0)
                assert (2 * x).cofactors(R(2)) == (2, x, 1)
                assert R(2).cofactors(R(0)) == (2, 1, 0)
                assert R(-2).cofactors(R(0)) == (2, -1, 0)
                assert R(0).cofactors(R(-2)) == (2, 0, -1)
                assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1)
                assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0)
                assert R(2).cofactors(R(2)) == (2, 1, 1)
                assert R(-2).cofactors(R(2)) == (2, -1, 1)
                assert R(2).cofactors(R(-2)) == (2, 1, -1)
                assert R(-2).cofactors(R(-2)) == (2, -1, -1)
                assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              1)
                assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              2)
                assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 +
                                                                  2 * x + 1, 1)
                assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 +
                                                                2 * x + 1)
                assert (2 * x**2 + 4 * x + 2).cofactors(x + 1) == (x + 1,
                                                                   2 * x + 2,
                                                                   1)
                assert (x + 1).cofactors(2 * x**2 + 4 * x + 2) == (x + 1, 1,
                                                                   2 * x + 2)
                assert (x - 31).cofactors(x) == (1, x - 31, x)

                f, g = 2 * x + 2, 6 * x**2 - 6

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

                f, g = [1000000000000 * x + 998549000000] * 2

                assert f.cofactors(g) == (f, 1, 1)

                f, g = 999530000000 * x + 1000000000000, 999530000000 * x + 999999000000

                assert f.cofactors(g) == (1000000, 999530 * x + 1000000,
                                          999530 * x + 999999)

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

                assert f.cofactors(g) == (x - 1, x + 1, x - 2)

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

                assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4,
                                          x + 3)

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

                assert f.cofactors(g) == (x**2 + 2, x**2 - 2, x**2 + 2)

                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 f.cofactors(g) == (1, f, g)

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

                h = (
                    365431878023781158602430064717380211405897160759702125019136
                    * x**21 +
                    197599133478719444145775798221171663643171734081650688 *
                    x**14 -
                    9504116979659010018253915765478924103928886144 * x**7 -
                    311973482284542371301330321821976049)
                cff = (-964661685087874498642420170752 * x**28 +
                       649736296036977287118848 * x**21 +
                       658473216967637120 * x**14 - 30463679113 * x**7 - 1)
                cfg = (-47268422569305850433478588366848 * x**27 +
                       30940259392972115602096128 * x**20 +
                       18261628279718027904 * x**13 - 426497272383 * x**6)

                assert f.cofactors(f.diff()) == (h, cff, cfg)

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

                H, cff, cfg = 120352542776360960 * x + 269116466014453760, 10946, 1

                assert f.cofactors(g) == (H, cff, cfg)

                with using(heu_gcd_max=0):
                    assert f.cofactors(g) == (H, cff, cfg)

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            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 f.cofactors(g) == (1, f, g)

            assert R(0).cofactors(R(0)) == (0, 0, 0)

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

            assert f.cofactors(g) == (x + 1, g, QQ(1, 2))

            f, g = x**2 - 1, x**2 - 3 * x + 2

            assert f.cofactors(g) == (x - 1, x + 1, x - 2)

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

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = 2 * x, R(2)

            assert f.cofactors(g) == (1, f, g)

            f, g = 2 * x, R(sqrt(2))

            assert f.cofactors(g) == (1, f, g)

            f, g = 2 * x + 2, 6 * x**2 - 6

            assert f.cofactors(g) == (x + 1, 2, 6 * x - 6)

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

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = x + 1, x - 1

            assert f.cofactors(g) == (1, f, g)

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

    f, g = x**2 - 1, x**3 - 3 * x + 2

    assert f.cofactors(g) == (x - 1, x + 1, x**2 + x - 2)

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

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                assert R(0).cofactors(R(0)) == (0, 0, 0)
                assert R(2).cofactors(R(0)) == (2, 1, 0)
                assert R(-2).cofactors(R(0)) == (2, -1, 0)
                assert R(0).cofactors(R(-2)) == (2, 0, -1)
                assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1)
                assert (2 * x).cofactors(R(2)) == (2, x, 1)
                assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0)
                assert R(2).cofactors(R(2)) == (2, 1, 1)
                assert R(-2).cofactors(R(2)) == (2, -1, 1)
                assert R(2).cofactors(R(-2)) == (2, 1, -1)
                assert R(-2).cofactors(R(-2)) == (2, -1, -1)
                assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              1)
                assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              2)
                assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 +
                                                                  2 * x + 1, 1)
                assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 +
                                                                2 * x + 1)

                f, g = 2 * x**2 + 4 * x + 2, x + 1

                assert f.cofactors(g) == (g, 2 * x + 2, 1)
                assert g.cofactors(f) == (g, 1, 2 * x + 2)

                with using(heu_gcd_max=0):
                    assert f.cofactors(g) == (g, 2 * x + 2, 1)

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

                assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4,
                                          x + 3)

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

                assert f.cofactors(g) == (1, f, g)

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

                assert f.cofactors(g) == (x + y, x + y, x)

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

                assert f.cofactors(g) == (x + y, x + y, x**2 - x * y + y**2)

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

                assert f.cofactors(g) == (x * y + x, y + 1, y**2 - y + 1)

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

                assert f.cofactors(g) == (1, f, g)

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

                assert f.cofactors(g) == (2 * x * y + 2 * x + y + 1, y + 1,
                                          y**2 - y + 1)

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

                assert f.cofactors(g) == (x - 1, 2 * x + 4 * y, x + 2)

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

                assert f.cofactors(g) == (x + y, 2 * x - 3, 4 * y - 2)

                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
                )

                h = (
                    -1743436700916730000000000000000000000000000 * x**2 * y -
                    12525091394814956778444367128798089188206978000000000000000
                    * x * y -
                    244093590929967254073813518342605644787785800 * x -
                    22495499028725631994927113634418779135935898997901327211111875586270479483
                    * y -
                    876801128965234839118530545935732755107147297241756982389990
                )

                assert f.cofactors(g) == (g, h, 1)

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            f, g = x**2 / 2 + x + QQ(1, 2), x / 2 + QQ(1, 2)

            assert f.cofactors(g) == (x + 1, g, QQ(1, 2))
            assert g.cofactors(f) == (x + 1, QQ(1, 2), g)

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

            assert R(0).cofactors(R(0)) == (0, 0, 0)
            assert R(0).cofactors(g) == (x + 1, 0, QQ(1, 2))

            f, g = x**2 / 4 + x * y + y**2, x**2 / 2 + x * y

            assert f.cofactors(g) == (x + 2 * y, x / 4 + y / 2, x / 2)

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

            assert f.cofactors(g) == (x + y, x / 2 + y / 2, x)

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

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = (x + sqrt(2) * y)**2, x + sqrt(2) * y

            assert f.cofactors(g) == (g, g, 1)

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

            assert f.cofactors(g) == (1, f, g)

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

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

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

            assert f.cofactors(g) == (g, g, 1)

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            f, g = 2.1 * x * y**2 - 2.2 * x * y + 2.1 * x, 1.0 * x**3
            h = 1.0 * x

            assert f.cofactors(g) == (h, 2.1 * y**2 - 2.2 * y + 2.1,
                                      1.0 * x**2)

            f, g = 2.1 * x * y**2 - 2.1 * x * y + 2.1 * x, 2.1 * x**3

            assert f.cofactors(g) == (h, f // h, g // h)
            assert g.cofactors(f) == (h, g // h, f // h)

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

    f, g = x**2 - y, x**3 - y * x + 2

    assert f.cofactors(g) == (1, f, g)

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

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                f, g = x - y * z, x - y * z

                assert f.cofactors(g) == (x - y * z, 1, 1)

                f, g, h = R.fateman_poly_F_1()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = R.fateman_poly_F_2()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = R.fateman_poly_F_3()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

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

    with using(gcd_aa_method='modgcd'):
        h = x**2 * y**7 + sqrt(6) / 21 * z
        f, g = h * (27 * y**3 + 1), h * (y + x)

        assert f.cofactors(g) == (h, 27 * y**3 + 1, x + y)

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

        assert f.cofactors(g) == (h, x + 1, sqrt(2) / sqrt(3))

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

        assert f.cofactors(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 f.cofactors(g) == (x**4 * y**3 + sqrt(6) / 22 * z,
                                  11 * y**3 + 1, x + y)

        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)

    R, x, y, z = ring('x y z', QQ.algebraic_field(I))

    for method in ('prs', 'modgcd'):
        with using(gcd_aa_method=method):
            f, g = R.one, I * z

            assert f.cofactors(g) == (1, f, g)
            assert g.cofactors(f) == (1, g, f)

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

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                f, g = u**2 + 2 * u + 1, 2 * u + 2

                assert f.cofactors(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 f.cofactors(g) == (h, cff, cfg)
                assert g.cofactors(f) == (h, cfg, cff)

                f, g = x + y + z, -x - y - z - u

                assert f.cofactors(g) == (1, f, g)

                f, g, h = R.fateman_poly_F_3()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = (1199999999999991 * x**17 - y,
                           2 * y - 19989798798 + x**211,
                           12 * x * y**7 + x**4 - 1)

                for _ in range(10):
                    assert (f * h).cofactors(g * h) == (h, f, g)

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

            f, g, h = R.fateman_poly_F_3()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

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

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

    F, x = field('x', QQ)
    R, _ = ring('t', F)

    assert R(x).gcd(R(0)) == 1
Beispiel #29
0
def test_PolyElement_lcm():
    R, x = ring('x', FF(5))

    assert (3 * x**2 + 2 * x + 4).lcm(2 * x**2 + 2 * x +
                                      3) == x**3 + 2 * x**2 + 4

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

    assert R.zero.lcm(R(2)) == 0
    assert R(2).lcm(R(2)) == 1

    assert R.zero.lcm(x) == 0

    assert (3 * x).lcm(3 * x) == x
    assert (x**2 + 8 * x + 7).lcm(x**3 + 7 * x**2 + x +
                                  7) == (x**4 + 8 * x**3 + 8 * x**2 + 8 * x +
                                         7)

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

    assert R(2).lcm(R(6)) == 6

    assert (2 * x**3).lcm(6 * x) == 6 * x**3
    assert (2 * x**3).lcm(3 * x) == 6 * x**3

    assert (x**2 + x).lcm(x) == x**2 + x
    assert (x**2 + x).lcm(2 * x) == 2 * x**2 + 2 * x
    assert (x**2 + 2 * x).lcm(x) == x**2 + 2 * x
    assert (2 * x**2 + x).lcm(x) == 2 * x**2 + x
    assert (2 * x**2 + x).lcm(2 * x) == 4 * x**2 + 2 * x
    assert (x**2 - 1).lcm(x**2 - 3 * x + 2) == x**3 - 2 * x**2 - x + 2

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

    f = (x**2 + 7 * x / 2 + 3) / 2
    g = x**2 / 2 + x
    h = x**3 + 7 / 2 * x**2 + 3 * x

    assert f.lcm(g) == h

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

    assert R(2).lcm(R(6)) == 6
    assert x.lcm(y) == x * y

    assert (2 * x**3).lcm(6 * x * y**2) == 6 * x**3 * y**2
    assert (2 * x**3).lcm(3 * x * y**2) == 6 * x**3 * y**2

    assert (x**2 * y).lcm(x * y**2) == x**2 * y**2

    f = 2 * x * y**5 - 3 * x * y**4 - 2 * x * y**3 + 3 * x * y**2
    g = y**5 - 2 * y**3 + y
    h = 2 * x * y**7 - 3 * x * y**6 - 4 * x * y**5 + 6 * x * y**4 + 2 * x * y**3 - 3 * x * y**2

    assert f.lcm(g) == h

    f = x**3 - 3 * x**2 * y - 9 * x * y**2 - 5 * y**3
    g = x**4 + 6 * x**3 * y + 12 * x**2 * y**2 + 10 * x * y**3 + 3 * y**4
    h = x**5 + x**4 * y - 18 * x**3 * y**2 - 50 * x**2 * y**3 - 47 * x * y**4 - 15 * y**5

    assert f.lcm(g) == h

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

    assert f.lcm(g) == h

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

    f = 2 * x * y - x**2 / 2 + QQ(1, 3)
    g = 3 * x**3 - x * y**2 - QQ(1, 2)
    h = (x**5 - 4 * x**4 * y - x**3 * y**2 / 3 - 2 * x**3 / 3 +
         4 * x**2 * y**3 / 3 - x**2 / 6 + 2 * x * y**2 / 9 + 2 * x * y / 3 +
         QQ(1, 9))

    assert f.lcm(g) == h

    f = x**2 / 4 + x * y + y**2
    g = x**2 / 2 + x * y
    h = x**3 + 4 * x**2 * y + 4 * x * y**2

    assert f.lcm(g) == h
Beispiel #30
0
def test_dmp_prem():
    _, x = ring('x', FF(7))

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

    assert f.prem(g) == 5  # issue sympy/sympy#20397

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

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

    r = 52 * x + 111

    assert f.prem(g) == r

    pytest.raises(ZeroDivisionError, lambda: f.prem(0))

    f = x**2 + 1
    g = 2 * x - 4
    r = 20

    assert f.prem(g) == r

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

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

    r = 52 * x + 111

    assert g.prem(f) == g
    assert f.prem(g) == r

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

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

    assert f.prem(g) == 0

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

    r = 2 * y**2

    assert f.prem(g) == r

    pytest.raises(ZeroDivisionError, lambda: f.prem(0))

    g = 2 * x - 2 * y

    r = 8 * y**2

    assert g.prem(f) == g
    assert f.prem(g) == r

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

    r = -4 * y + 4

    assert f.prem(g) == r