コード例 #1
0
ファイル: test_densearith.py プロジェクト: cbm755/diofant
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
コード例 #2
0
ファイル: test_densebasic.py プロジェクト: cbm755/diofant
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)]
コード例 #3
0
ファイル: test_domains.py プロジェクト: cbm755/diofant
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))
コード例 #4
0
ファイル: test_polyoptions.py プロジェクト: cbm755/diofant
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),
    }
コード例 #5
0
ファイル: test_densetools.py プロジェクト: Blendify/diofant
def test_dmp_diff():
    assert dmp_diff([], 1, 0, ZZ) == []
    assert dmp_diff([[]], 1, 1, ZZ) == [[]]
    assert dmp_diff([[[]]], 1, 2, ZZ) == [[[]]]

    assert dmp_diff([[[1], [2]]], 1, 2, ZZ) == [[[]]]

    assert dmp_diff([[[1]], [[]]], 1, 2, ZZ) == [[[1]]]
    assert dmp_diff([[[3]], [[1]], [[]]], 1, 2, ZZ) == [[[6]], [[1]]]

    assert dmp_diff([1, -1, 0, 0, 2], 1, 0, ZZ) == \
        dup_diff([1, -1, 0, 0, 2], 1, ZZ)

    assert dmp_diff(f_6, 0, 3, ZZ) == f_6
    assert dmp_diff(f_6, 1, 3, ZZ) == dmp_diff(f_6, 1, 3, ZZ)
    assert dmp_diff(
        f_6, 2, 3, ZZ) == dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ)
    assert dmp_diff(f_6, 3, 3, ZZ) == dmp_diff(
        dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ), 1, 3, ZZ)

    K = FF(23)
    F_6 = dmp_normal(f_6, 3, K)

    assert dmp_diff(F_6, 0, 3, K) == F_6
    assert dmp_diff(F_6, 1, 3, K) == dmp_diff(F_6, 1, 3, K)
    assert dmp_diff(F_6, 2, 3, K) == dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K)
    assert dmp_diff(F_6, 3, 3, K) == dmp_diff(
        dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K), 1, 3, K)
コード例 #6
0
ファイル: test_densetools.py プロジェクト: Blendify/diofant
def test_dup_diff():
    assert dup_diff([], 1, ZZ) == []
    assert dup_diff([7], 1, ZZ) == []
    assert dup_diff([2, 7], 1, ZZ) == [2]
    assert dup_diff([1, 2, 1], 1, ZZ) == [2, 2]
    assert dup_diff([1, 2, 3, 4], 1, ZZ) == [3, 4, 3]
    assert dup_diff([1, -1, 0, 0, 2], 1, ZZ) == [4, -3, 0, 0]

    f = dmp_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], 0, ZZ)

    assert dup_diff(f, 0, ZZ) == f
    assert dup_diff(f, 1, ZZ) == dup_diff(f, 1, ZZ)
    assert dup_diff(f, 2, ZZ) == dup_diff(dup_diff(f, 1, ZZ), 1, ZZ)
    assert dup_diff(
        f, 3, ZZ) == dup_diff(dup_diff(dup_diff(f, 1, ZZ), 1, ZZ), 1, ZZ)

    K = FF(3)
    f = dmp_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], 0, K)

    assert dup_diff(f, 1, K) == dmp_normal([2, 0, 1, 0, 0, 2, 0, 0, 0, 0], 0, K)
    assert dup_diff(f, 2, K) == dmp_normal([1, 0, 0, 2, 0, 0, 0], 0, K)
    assert dup_diff(f, 3, K) == dmp_normal([], 0, K)

    assert dup_diff(f, 0, K) == f
    assert dup_diff(f, 1, K) == dup_diff(f, 1, K)
    assert dup_diff(f, 2, K) == dup_diff(dup_diff(f, 1, K), 1, K)
    assert dup_diff(
        f, 3, K) == dup_diff(dup_diff(dup_diff(f, 1, K), 1, K), 1, K)
コード例 #7
0
ファイル: test_polyoptions.py プロジェクト: Blendify/diofant
def test_Modulus_postprocess():
    opt = {'modulus': 5}
    Modulus.postprocess(opt)

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

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

    assert opt == {
        'modulus': 5,
        'domain': FF(5, False),
        'symmetric': False,
    }
コード例 #8
0
ファイル: test_gmpy.py プロジェクト: Blendify/diofant
def test_convert():
    F3 = FF(3)
    F3_gmpy = FF_gmpy(3)
    F3_python = FF_python(3)

    assert F3.convert(gmpy.mpz(2)) == F3.dtype(2)
    assert F3.convert(gmpy.mpq(2, 1)) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(gmpy.mpq(1, 2)))

    assert ZZ_gmpy.convert(F3_python(1)) == ZZ_gmpy.dtype(1)
    assert ZZ_gmpy.convert(F3_gmpy(1)) == ZZ_gmpy.dtype(1)

    assert ZZ_gmpy.convert(PythonRational(2)) == ZZ_gmpy.dtype(2)
    pytest.raises(CoercionFailed,
                  lambda: ZZ_gmpy.convert(PythonRational(2, 3)))

    assert QQ_python.convert(gmpy.mpz(3)) == QQ_python.dtype(3)
    assert QQ_python.convert(gmpy.mpq(2, 3)) == QQ_python.dtype(2, 3)

    assert QQ_gmpy.convert(PythonRational(2, 3)) == QQ_gmpy.dtype(2, 3)

    assert ZZ_python.convert(F3_gmpy(1)) == ZZ_python.dtype(1)
    assert ZZ_python.convert(gmpy.mpz(3)) == ZZ_python.dtype(3)
    assert ZZ_python.convert(gmpy.mpq(3, 1)) == ZZ_python.dtype(3)
    pytest.raises(CoercionFailed, lambda: ZZ_python.convert(gmpy.mpq(3, 2)))

    assert CC.convert(gmpy.mpz(3)) == CC(3)
    assert CC.convert(gmpy.mpq(1, 2)) == CC(0.5)
コード例 #9
0
ファイル: test_domains.py プロジェクト: cbm755/diofant
def test___eq__():
    assert not QQ.poly_ring(x) == ZZ.poly_ring(x)
    assert not QQ.frac_field(x) == ZZ.frac_field(x)

    assert EX(1) != EX(2)

    F11 = FF(11)
    assert F11(2) != F11(3)
    assert F11(2) != object()
コード例 #10
0
ファイル: test_domains.py プロジェクト: skirpichev/diofant
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576)

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

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

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

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

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

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

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

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

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

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

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

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
コード例 #11
0
ファイル: test_domains.py プロジェクト: cbm755/diofant
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))
コード例 #12
0
ファイル: test_euclidtools.py プロジェクト: cbm755/diofant
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)
コード例 #13
0
def test_FiniteField():
    sT(FF(2), "GF(2)")

    F4 = FF(2, [1, 1, 1])
    repr(F4.one)  # not raises
コード例 #14
0
def test_dmp_sqf():
    R, x = ring("x", ZZ)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    assert f.is_squarefree is True

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

    assert f.is_squarefree is False

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

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

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

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

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

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

    f = x**11 + 1

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

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

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

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

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

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

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

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

    assert f.is_squarefree is True

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

    f = x**15 + x + 1

    assert f.is_squarefree is True

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    R, x, y = ring("x,y", QQ.algebraic_field(I))
    assert (x**2 + 2 * I * x - 1).sqf_list() == (1, [(x + I, 2)])
コード例 #15
0
def test_dmp_diff_in():
    R, x = ring('x', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(7, 1, 0) == 0
    assert R.dmp_diff_in(2 * x + 7, 1, 0) == 2
    assert R.dmp_diff_in(x**2 + 2 * x + 1, 1, 0) == 2 * x + 2
    assert R.dmp_diff_in(x**3 + 2 * x**2 + 3 * x + 4, 1,
                         0) == 3 * x**2 + 4 * x + 3
    assert R.dmp_diff_in(x**4 - x**3 + 2, 1, 0) == 4 * x**3 - 3 * x**2
    assert R.dmp_diff_in(x**3 + 2 * x**2 + 3 * x + 4, 2, 0) == 6 * x + 4

    f = 17 * x**10 + 34 * x**9 + 56 * x**8 - 345 * x**7 + 23 * x**6 + 76 * x**5 + 12 * x**2 + 3 * x + 7

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

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

    f = 2 * x**10 + x**9 + 2 * x**8 + 2 * x**6 + x**5 + 1

    assert R.dmp_diff_in(f, 1, 0) == 2 * x**9 + x**7 + 2 * x**4
    assert R.dmp_diff_in(f, 2, 0) == x**6 + 2 * x**3
    assert R.dmp_diff_in(f, 3, 0) == 0

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

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

    assert R.dmp_diff_in(0, 1, 0) == 0

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

    assert R.dmp_diff_in(f, 1, 0) == y**2 + 2 * y + 3
    assert R.dmp_diff_in(f, 2, 0) == 0
    assert R.dmp_diff_in(f, 1, 1) == 2 * x * y + 2 * x + 4 * y + 3

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

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(y + 2, 1, 0) == 0
    assert R.dmp_diff_in(x, 1, 0) == 1
    assert R.dmp_diff_in(3 * x**2 + x, 1, 0) == 6 * x + 1

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

    f = f_polys()[6]

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

    assert (R.dmp_diff_in(f, 2, 1) == -5076 * x * y**2 - 282 * x * y -
            54 * y * z**3 * t**2 + 54 * y * t**2 - 2 * z**3 * t**2 + 2 * t**2)
    assert R.dmp_diff_in(
        f, 3, 1) == -10152 * x * y - 282 * x - 54 * z**3 * t**2 + 54 * t**2
    assert (R.dmp_diff_in(f, 2, 2) == 270 * x**3 * z * t**2 + 846 * x * y * z -
            54 * y**3 * z * t**2 - 6 * y**2 * z * t**2 + 90 * z**4 * t**2 +
            24 * z**2 * t**3 - 18 * z * t**2)
    assert (R.dmp_diff_in(
        f, 3, 2) == 270 * x**3 * t**2 + 846 * x * y - 54 * y**3 * t**2 -
            6 * y**2 * t**2 + 360 * z**3 * t**2 + 48 * z * t**3 - 18 * t**2)

    pytest.raises(IndexError, lambda: R.dmp_diff_in(f, 2, -1))

    R, x, y, z, t = ring('x y z t', FF(23))

    f = R.from_dense(f_polys()[6].to_dense())

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)
コード例 #16
0
ファイル: test_euclidtools.py プロジェクト: cbm755/diofant
def test_PolyElement_lcm():
    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, 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 = 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", 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

    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", FF(5))

    assert (3*x**2 + 2*x + 4).lcm(2*x**2 + 2*x + 3) == x**3 + 2*x**2 + 4
コード例 #17
0
ファイル: test_polyoptions.py プロジェクト: cbm755/diofant
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    pytest.raises(OptionError, lambda: Domain.preprocess(()))
コード例 #18
0
def test_dup_factor_list():
    R, x = ring("x", ZZ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

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

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

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

    R, x = ring("x", ZZ)
    assert R.dup_factor_list_include(0) == [(0, 1)]
    assert R.dup_factor_list_include(7) == [(7, 1)]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    R, x = ring("x", EX)
    pytest.raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
コード例 #19
0
ファイル: test_domains.py プロジェクト: skirpichev/diofant
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

    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

    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]))

    assert F5.is_positive(a) is True
    assert F5.is_negative(a) is False
    assert F5.is_positive(F5.zero) is False
    assert F5.is_negative(F5.zero) is False

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

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

    assert F9.order == 9
    assert F9.characteristic == 3

    assert F9.zero == F9([0])
    assert F9.one == F9([1])
    assert F9(F9([2, 1])) == F9([2, 1])
    assert F9([2, 1]) + F9([1, 2]) == F9([1, 2]) + F9([2, 1]) == F9.zero
    assert F9([2, 1]) + F9([1, 3]) == F9.one
    assert F9([2, 1]) * F9([1, 3]) == 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([2, 0])) == int(F9(6)) == 6

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

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

    F8 = FF(8)

    assert F8.order == 8
    assert F8.characteristic == 2
    assert F8.dtype.mod.to_dense() == [1, 0, 1, 1]
    assert int(F8([1, 0, 1])) == int(F8(5)) == 5

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

    assert F4.order == 4
    assert F4.characteristic == 2
コード例 #20
0
def test_dmp_sqf():
    R, x, y = ring("x,y", ZZ)
    assert R.dmp_sqf_part(0) == 0
    assert R(0).is_squarefree is True

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

    assert R.dmp_sqf_list(3) == (3, [])
    assert R.dmp_sqf_list_include(3) == [(3, 1)]

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

    assert f_4.is_squarefree is True
    assert R.dmp_sqf_part(f_4) == -f_4

    assert R.dmp_sqf_part(f_5) == x + y - z

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

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

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

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

    assert R.dmp_sqf_list(f) == (2, [(x + 1, 2), (x + 2, 3)])
    assert R.dmp_sqf_list_include(f) == [(2, 1), (x + 1, 2), (x + 2, 3)]

    assert R.dmp_sqf_list(f, all=True) == (2, [(1, 1), (x + 1, 2), (x + 2, 3)])
    assert R.dmp_sqf_list_include(f, all=True) == [(2, 1), (x + 1, 2), (x + 2, 3)]

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

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

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

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

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

    R, x, y = ring("x,y", QQ.algebraic_field(I))
    assert R.dmp_sqf_list(x**2 + 2*I*x - 1) == (R.one.to_dense()[0][0],
                                                [(x + I, 2)])
コード例 #21
0
ファイル: test_domains.py プロジェクト: cbm755/diofant
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
コード例 #22
0
ファイル: test_densearith.py プロジェクト: cbm755/diofant
def test_dmp_mul():
    R, x = ring('x', ZZ)

    assert R.dmp_mul(0, 0) == 0
    assert R.dmp_mul(0, 1) == 0
    assert R.dmp_mul(1, 0) == 0
    assert R.dmp_mul(1, 1) == 1
    assert R.dmp_mul(5, 7) == 35

    f = 3 * x**5 + 6 * x**2 + x + 2
    g = 4 * x**3 + x
    h = 12 * x**8 + 3 * x**6 + 24 * x**5 + 4 * x**4 + 14 * x**3 + x**2 + 2 * x

    assert R.dmp_mul(f, g) == h
    assert R.dmp_mul(g, f) == h

    f = 2 * x**4 + x + 7
    h = 4 * x**8 + 4 * x**5 + 28 * x**4 + x**2 + 14 * x + 49

    assert R.dmp_mul(f, f) == h

    p1 = R.from_dense([
        79, -1, 78, -94, -10, 11, 32, -19, 78, 2, -89, 30, 73, 42, 85, 77, 83,
        -30, -34, -2, 95, -81, 37, -49, -46, -58, -16, 37, 35, -11, -57, -15,
        -31, 67, -20, 27, 76, 2, 70, 67, -65, 65, -26, -93, -44, -12, -92, 57,
        -90, -57, -11, -67, -98, -69, 97, -41, 89, 33, 89, -50, 81, -31, 60,
        -27, 43, 29, -77, 44, 21, -91, 32, -57, 33, 3, 53, -51, -38, -99, -84,
        23, -50, 66, -100, 1, -75, -25, 27, -60, 98, -51, -87, 6, 8, 78, -28,
        -95, -88, 12, -35, 26, -9, 16, -92, 55, -7, -86, 68, -39, -46, 84, 94,
        45, 60, 92, 68, -75, -74, -19, 8, 75, 78, 91, 57, 34, 14, -3, -49, 65,
        78, -18, 6, -29, -80, -98, 17, 13, 58, 21, 20, 9, 37, 7, -30, -53, -20,
        34, 67, -42, 89, -22, 73, 43, -6, 5, 51, -8, -15, -52, -22, -58, -72,
        -3, 43, -92, 82, 83, -2, -13, -23, -60, 16, -94, -8, -28, -95, -72, 63,
        -90, 76, 6, -43, -100, -59, 76, 3, 3, 46, -85, 75, 62, -71, -76, 88,
        97, -72, -1, 30, -64, 72, -48, 14, -78, 58, 63, -91, 24, -87, -27, -80,
        -100, -44, 98, 70, 100, -29, -38, 11, 77, 100, 52, 86, 65, -5, -42,
        -81, -38, -42, 43, -2, -70, -63, -52
    ])
    p2 = R.from_dense([
        65, -19, -47, 1, 90, 81, -15, -34, 25, -75, 9, -83, 50, -5, -44, 31, 1,
        70, -7, 78, 74, 80, 85, 65, 21, 41, 66, 19, -40, 63, -21, -27, 32, 69,
        83, 34, -35, 14, 81, 57, -75, 32, -67, -89, -100, -61, 46, 84, -78,
        -29, -50, -94, -24, -32, -68, -16, 100, -7, -72, -89, 35, 82, 58, 81,
        -92, 62, 5, -47, -39, -58, -72, -13, 84, 44, 55, -25, 48, -54, -31,
        -56, -11, -50, -84, 10, 67, 17, 13, -14, 61, 76, -64, -44, -40, -96,
        11, -11, -94, 2, 6, 27, -6, 68, -54, 66, -74, -14, -1, -24, -73, 96,
        89, -11, -89, 56, -53, 72, -43, 96, 25, 63, -31, 29, 68, 83, 91, -93,
        -19, -38, -40, 40, -12, -19, -79, 44, 100, -66, -29, -77, 62, 39, -8,
        11, -97, 14, 87, 64, 21, -18, 13, 15, -59, -75, -99, -88, 57, 54, 56,
        -67, 6, -63, -59, -14, 28, 87, -20, -39, 84, -91, -2, 49, -75, 11, -24,
        -95, 36, 66, 5, 25, -72, -40, 86, 90, 37, -33, 57, -35, 29, -18, 4,
        -79, 64, -17, -27, 21, 29, -5, -44, -87, -24, 52, 78, 11, -23, -53, 36,
        42, 21, -68, 94, -91, -51, -21, 51, -76, 72, 31, 24, -48, -80, -9, 37,
        -47, -6, -8, -63, -91, 79, -79, -100, 38, -20, 38, 100, 83, -90, 87,
        63, -36, 82, -19, 18, -98, -38, 26, 98, -70, 79, 92, 12, 12, 70, 74,
        36, 48, -13, 31, 31, -47, -71, -12, -64, 36, -42, 32, -86, 60, 83, 70,
        55, 0, 1, 29, -35, 8, -82, 8, -73, -46, -50, 43, 48, -5, -86, -72, 44,
        -90, 19, 19, 5, -20, 97, -13, -66, -5, 5, -69, 64, -30, 41, 51, 36, 13,
        -99, -61, 94, -12, 74, 98, 68, 24, 46, -97, -87, -6, -27, 82, 62, -11,
        -77, 86, 66, -47, -49, -50, 13, 18, 89, -89, 46, -80, 13, 98, -35, -36,
        -25, 12, 20, 26, -52, 79, 27, 79, 100, 8, 62, -58, -28, 37
    ])
    res = R.from_dense([
        5135, -1566, 1376, -7466, 4579, 11710, 8001, -7183, -3737, -7439, 345,
        -10084, 24522, -1201, 1070, -10245, 9582, 9264, 1903, 23312, 18953,
        10037, -15268, -5450, 6442, -6243, -3777, 5110, 10936, -16649, -6022,
        16255, 31300, 24818, 31922, 32760, 7854, 27080, 15766, 29596, 7139,
        31945, -19810, 465, -38026, -3971, 9641, 465, -19375, 5524, -30112,
        -11960, -12813, 13535, 30670, 5925, -43725, -14089, 11503, -22782,
        6371, 43881, 37465, -33529, -33590, -39798, -37854, -18466, -7908,
        -35825, -26020, -36923, -11332, -5699, 25166, -3147, 19885, 12962,
        -20659, -1642, 27723, -56331, -24580, -11010, -20206, 20087, -23772,
        -16038, 38580, 20901, -50731, 32037, -4299, 26508, 18038, -28357,
        31846, -7405, -20172, -15894, 2096, 25110, -45786, 45918, -55333,
        -31928, -49428, -29824, -58796, -24609, -15408, 69, -35415, -18439,
        10123, -20360, -65949, 33356, -20333, 26476, -32073, 33621, 930, 28803,
        -42791, 44716, 38164, 12302, -1739, 11421, 73385, -7613, 14297, 38155,
        -414, 77587, 24338, -21415, 29367, 42639, 13901, -288, 51027, -11827,
        91260, 43407, 88521, -15186, 70572, -12049, 5090, -12208, -56374,
        15520, -623, -7742, 50825, 11199, -14894, 40892, 59591, -31356, -28696,
        -57842, -87751, -33744, -28436, -28945, -40287, 37957, -35638, 33401,
        -61534, 14870, 40292, 70366, -10803, 102290, -71719, -85251, 7902,
        -22409, 75009, 99927, 35298, -1175, -762, -34744, -10587, -47574,
        -62629, -19581, -43659, -54369, -32250, -39545, 15225, -24454, 11241,
        -67308, -30148, 39929, 37639, 14383, -73475, -77636, -81048, -35992,
        41601, -90143, 76937, -8112, 56588, 9124, -40094, -32340, 13253, 10898,
        -51639, 36390, 12086, -1885, 100714, -28561, -23784, -18735, 18916,
        16286, 10742, -87360, -13697, 10689, -19477, -29770, 5060, 20189,
        -8297, 112407, 47071, 47743, 45519, -4109, 17468, -68831, 78325, -6481,
        -21641, -19459, 30919, 96115, 8607, 53341, 32105, -16211, 23538, 57259,
        -76272, -40583, 62093, 38511, -34255, -40665, -40604, -37606, -15274,
        33156, -13885, 103636, 118678, -14101, -92682, -100791, 2634, 63791,
        98266, 19286, -34590, -21067, -71130, 25380, -40839, -27614, -26060,
        52358, -15537, 27138, -6749, 36269, -33306, 13207, -91084, -5540,
        -57116, 69548, 44169, -57742, -41234, -103327, -62904, -8566, 41149,
        -12866, 71188, 23980, 1838, 58230, 73950, 5594, 43113, -8159, -15925,
        6911, 85598, -75016, -16214, -62726, -39016, 8618, -63882, -4299,
        23182, 49959, 49342, -3238, -24913, -37138, 78361, 32451, 6337, -11438,
        -36241, -37737, 8169, -3077, -24829, 57953, 53016, -31511, -91168,
        12599, -41849, 41576, 55275, -62539, 47814, -62319, 12300, -32076,
        -55137, -84881, -27546, 4312, -3433, -54382, 113288, -30157, 74469,
        18219, 79880, -2124, 98911, 17655, -33499, -32861, 47242, -37393,
        99765, 14831, -44483, 10800, -31617, -52710, 37406, 22105, 29704,
        -20050, 13778, 43683, 36628, 8494, 60964, -22644, 31550, -17693, 33805,
        -124879, -12302, 19343, 20400, -30937, -21574, -34037, -33380, 56539,
        -24993, -75513, -1527, 53563, 65407, -101, 53577, 37991, 18717, -23795,
        -8090, -47987, -94717, 41967, 5170, -14815, -94311, 17896, -17734,
        -57718, -774, -38410, 24830, 29682, 76480, 58802, -46416, -20348,
        -61353, -68225, -68306, 23822, -31598, 42972, 36327, 28968, -65638,
        -21638, 24354, -8356, 26777, 52982, -11783, -44051, -26467, -44721,
        -28435, -53265, -25574, -2669, 44155, 22946, -18454, -30718, -11252,
        58420, 8711, 67447, 4425, 41749, 67543, 43162, 11793, -41907, 20477,
        -13080, 6559, -6104, -13244, 42853, 42935, 29793, 36730, -28087, 28657,
        17946, 7503, 7204, 21491, -27450, -24241, -98156, -18082, -42613,
        -24928, 10775, -14842, -44127, 55910, 14777, 31151, -2194, 39206,
        -2100, -4211, 11827, -8918, -19471, 72567, 36447, -65590, -34861,
        -17147, -45303, 9025, -7333, -35473, 11101, 11638, 3441, 6626, -41800,
        9416, 13679, 33508, 40502, -60542, 16358, 8392, -43242, -35864, -34127,
        -48721, 35878, 30598, 28630, 20279, -19983, -14638, -24455, -1851,
        -11344, 45150, 42051, 26034, -28889, -32382, -3527, -14532, 22564,
        -22346, 477, 11706, 28338, -25972, -9185, -22867, -12522, 32120, -4424,
        11339, -33913, -7184, 5101, -23552, -17115, -31401, -6104, 21906,
        25708, 8406, 6317, -7525, 5014, 20750, 20179, 22724, 11692, 13297,
        2493, -253, -16841, -17339, -6753, -4808, 2976, -10881, -10228, -13816,
        -12686, 1385, 2316, 2190, -875, -1924
    ])

    assert R.dmp_mul(p1, p2) == res

    p1 = R.from_dense([
        83, -61, -86, -24, 12, 43, -88, -9, 42, 55, -66, 74, 95, -25, -12, 68,
        -99, 4, 45, 6, -15, -19, 78, 65, -55, 47, -13, 17, 86, 81, -58, -27,
        50, -40, -24, 39, -41, -92, 75, 90, -1, 40, -15, -27, -35, 68, 70, -64,
        -40, 78, -88, -58, -39, 69, 46, 12, 28, -94, -37, -50, -80, -96, -61,
        25, 1, 71, 4, 12, 48, 4, 34, -47, -75, 5, 48, 82, 88, 23, 98, 35, 17,
        -10, 48, -61, -95, 47, 65, -19, -66, -57, -6, -51, -42, -89, 66, -13,
        18, 37, 90, -23, 72, 96, -53, 0, 40, -73, -52, -68, 32, -25, -53, 79,
        -52, 18, 44, 73, -81, 31, -90, 70, 3, 36, 48, 76, -24, -44, 23, 98, -4,
        73, 69, 88, -70, 14, -68, 94, -78, -15, -64, -97, -70, -35, 65, 88, 49,
        -53, -7, 12, -45, -7, 59, -94, 99, -2, 67, -60, -71, 29, -62, -77, 1,
        51, 17, 80, -20, -47, -19, 24, -9, 39, -23, 21, -84, 10, 84, 56, -17,
        -21, -66, 85, 70, 46, -51, -22, -95, 78, -60, -96, -97, -45, 72, 35,
        30, -61, -92, -93, -60, -61, 4, -4, -81, -73, 46, 53, -11, 26, 94, 45,
        14, -78, 55, 84, -68, 98, 60, 23, 100, -63, 68, 96, -16, 3, 56, 21,
        -58, 62, -67, 66, 85, 41, -79, -22, 97, -67, 82, 82, -96, -20, -7, 48,
        -67, 48, -9, -39, 78
    ])
    p2 = R.from_dense([
        52, 88, 76, 66, 9, -64, 46, -20, -28, 69, 60, 96, -36, -92, -30, -11,
        -35, 35, 55, 63, -92, -7, 25, -58, 74, 55, -6, 4, 47, -92, -65, 67,
        -45, 74, -76, 59, -6, 69, 39, 24, -71, -7, 39, -45, 60, -68, 98, 97,
        -79, 17, 4, 94, -64, 68, -100, -96, -2, 3, 22, 96, 54, -77, -86, 67, 6,
        57, 37, 40, 89, -78, 64, -94, -45, -92, 57, 87, -26, 36, 19, 97, 25,
        77, -87, 24, 43, -5, 35, 57, 83, 71, 35, 63, 61, 96, -22, 8, -1, 96,
        43, 45, 94, -93, 36, 71, -41, -99, 85, -48, 59, 52, -17, 5, 87, -16,
        -68, -54, 76, -18, 100, 91, -42, -70, -66, -88, -12, 1, 95, -82, 52,
        43, -29, 3, 12, 72, -99, -43, -32, -93, -51, 16, -20, -12, -11, 5, 33,
        -38, 93, -5, -74, 25, 74, -58, 93, 59, -63, -86, 63, -20, -4, -74, -73,
        -95, 29, -28, 93, -91, -2, -38, -62, 77, -58, -85, -28, 95, 38, 19,
        -69, 86, 94, 25, -2, -4, 47, 34, -59, 35, -48, 29, -63, -53, 34, 29,
        66, 73, 6, 92, -84, 89, 15, 81, 93, 97, 51, -72, -78, 25, 60, 90, -45,
        39, 67, -84, -62, 57, 26, -32, -56, -14, -83, 76, 5, -2, 99, -100, 28,
        46, 94, -7, 53, -25, 16, -23, -36, 89, -78, -63, 31, 1, 84, -99, -52,
        76, 48, 90, -76, 44, -19, 54, -36, -9, -73, -100, -69, 31, 42, 25, -39,
        76, -26, -8, -14, 51, 3, 37, 45, 2, -54, 13, -34, -92, 17, -25, -65,
        53, -63, 30, 4, -70, -67, 90, 52, 51, 18, -3, 31, -45, -9, 59, 63, -87,
        22, -32, 29, -38, 21, 36, -82, 27, -11
    ])
    res = R.from_dense([
        4316, 4132, -3532, -7974, -11303, -10069, 5484, -3330, -5874, 7734,
        4673, 11327, -9884, -8031, 17343, 21035, -10570, -9285, 15893, 3780,
        -14083, 8819, 17592, 10159, 7174, -11587, 8598, -16479, 3602, 25596,
        9781, 12163, 150, 18749, -21782, -12307, 27578, -2757, -12573, 12565,
        6345, -18956, 19503, -15617, 1443, -16778, 36851, 23588, -28474, 5749,
        40695, -7521, -53669, -2497, -18530, 6770, 57038, 3926, -6927, -15399,
        1848, -64649, -27728, 3644, 49608, 15187, -8902, -9480, -7398, -40425,
        4824, 23767, -7594, -6905, 33089, 18786, 12192, 24670, 31114, 35334,
        -4501, -14676, 7107, -59018, -21352, 20777, 19661, 20653, 33754, -885,
        -43758, 6269, 51897, -28719, -97488, -9527, 13746, 11644, 17644,
        -21720, 23782, -10481, 47867, 20752, 33810, -1875, 39918, -7710,
        -40840, 19808, -47075, 23066, 46616, 25201, 9287, 35436, -1602, 9645,
        -11978, 13273, 15544, 33465, 20063, 44539, 11687, 27314, -6538, -37467,
        14031, 32970, -27086, 41323, 29551, 65910, -39027, -37800, -22232,
        8212, 46316, -28981, -55282, 50417, -44929, -44062, 73879, 37573,
        -2596, -10877, -21893, -133218, -33707, -25753, -9531, 17530, 61126,
        2748, -56235, 43874, -10872, -90459, -30387, 115267, -7264, -44452,
        122626, 14839, -599, 10337, 57166, -67467, -54957, 63669, 1202, 18488,
        52594, 7205, -97822, 612, 78069, -5403, -63562, 47236, 36873, -154827,
        -26188, 82427, -39521, 5628, 7416, 5276, -53095, 47050, 26121, -42207,
        79021, -13035, 2499, -66943, 29040, -72355, -23480, 23416, -12885,
        -44225, -42688, -4224, 19858, 55299, 15735, 11465, 101876, -39169,
        51786, 14723, 43280, -68697, 16410, 92295, 56767, 7183, 111850, 4550,
        115451, -38443, -19642, -35058, 10230, 93829, 8925, 63047, 3146, 29250,
        8530, 5255, -98117, -115517, -76817, -8724, 41044, 1312, -35974, 79333,
        -28567, 7547, -10580, -24559, -16238, 10794, -3867, 24848, 57770,
        -51536, -35040, 71033, 29853, 62029, -7125, -125585, -32169, -47907,
        156811, -65176, -58006, -15757, -57861, 11963, 30225, -41901, -41681,
        31310, 27982, 18613, 61760, 60746, -59096, 33499, 30097, -17997, 24032,
        56442, -83042, 23747, -20931, -21978, -158752, -9883, -73598, -7987,
        -7333, -125403, -116329, 30585, 53281, 51018, -29193, 88575, 8264,
        -40147, -16289, 113088, 12810, -6508, 101552, -13037, 34440, -41840,
        101643, 24263, 80532, 61748, 65574, 6423, -20672, 6591, -10834, -71716,
        86919, -92626, 39161, 28490, 81319, 46676, 106720, 43530, 26998, 57456,
        -8862, 60989, 13982, 3119, -2224, 14743, 55415, -49093, -29303, 28999,
        1789, 55953, -84043, -7780, -65013, 57129, -47251, 61484, 61994,
        -78361, -82778, 22487, -26894, 9756, -74637, -15519, -4360, 30115,
        42433, 35475, 15286, 69768, 21509, -20214, 78675, -21163, 13596, 11443,
        -10698, -53621, -53867, -24155, 64500, -42784, -33077, -16500, 873,
        -52788, 14546, -38011, 36974, -39849, -34029, -94311, 83068, -50437,
        -26169, -46746, 59185, 42259, -101379, -12943, 30089, -59086, 36271,
        22723, -30253, -52472, -70826, -23289, 3331, -31687, 14183, -857,
        -28627, 35246, -51284, 5636, -6933, 66539, 36654, 50927, 24783, 3457,
        33276, 45281, 45650, -4938, -9968, -22590, 47995, 69229, 5214, -58365,
        -17907, -14651, 18668, 18009, 12649, -11851, -13387, 20339, 52472,
        -1087, -21458, -68647, 52295, 15849, 40608, 15323, 25164, -29368,
        10352, -7055, 7159, 21695, -5373, -54849, 101103, -24963, -10511,
        33227, 7659, 41042, -69588, 26718, -20515, 6441, 38135, -63, 24088,
        -35364, -12785, -18709, 47843, 48533, -48575, 17251, -19394, 32878,
        -9010, -9050, 504, -12407, 28076, -3429, 25324, -4210, -26119, 752,
        -29203, 28251, -11324, -32140, -3366, -25135, 18702, -31588, -7047,
        -24267, 49987, -14975, -33169, 37744, -7720, -9035, 16964, -2807, -421,
        14114, -17097, -13662, 40628, -12139, -9427, 5369, 17551, -13232,
        -16211, 9804, -7422, 2677, 28635, -8280, -4906, 2908, -22558, 5604,
        12459, 8756, -3980, -4745, -18525, 7913, 5970, -16457, 20230, -6247,
        -13812, 2505, 11899, 1409, -15094, 22540, -18863, 137, 11123, -4516,
        2290, -8594, 12150, -10380, 3005, 5235, -7350, 2535, -858
    ])

    assert R.dmp_mul(p1, p2) == res

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

    assert R.dmp_mul(2 * x + 1, 3 * x + 4) == 6 * x**2 + 4 * x + 4

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

    assert R.dmp_mul(0, 0) == 0
    assert R.dmp_mul(0, QQ(1, 2)) == 0
    assert R.dmp_mul(QQ(1, 2), 0) == 0
    assert R.dmp_mul(QQ(1, 2), QQ(4, 7)) == QQ(2, 7)
    assert R.dmp_mul(QQ(5, 7), QQ(3, 7)) == QQ(15, 49)

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

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

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

    assert R.dmp_mul(0, 0) == 0
    assert R.dmp_mul(QQ(1, 2), 0) == 0
    assert R.dmp_mul(0, QQ(1, 2)) == 0
    assert R.dmp_mul(QQ(2, 7), QQ(1, 3)) == QQ(2, 21)
    assert R.dmp_mul(QQ(1, 7), QQ(2, 3)) == QQ(2, 21)

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

    assert R.dmp_mul(2 * x + 1, 3 * x + 4) == x**2 + x + 4
コード例 #23
0
ファイル: test_factortools.py プロジェクト: cbm755/diofant
def test_gf_factor():
    R, x = ring('x', FF(11))

    assert R(0).factor_list() == (0, [])
    assert R(1).factor_list() == (1, [])
    assert (x + 1).factor_list() == (1, [(x + 1, 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)])

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

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

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

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

    g = (1, [(x + 1, 1), (x + 2, 2)])

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

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

    with config.using(gf_factor_method='shoup'):
        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)])

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

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

    with config.using(gf_factor_method='shoup'):
        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)])

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

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

    with config.using(gf_factor_method='shoup'):
        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**4 + 5 * x**2 + 7, 1),
             (x**4 + 6 * x**2 + 7, 1), (x**8 + x**4 + 6, 1),
             (x**8 + 10 * x**4 + 6, 1)])

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

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

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

    f = 8 * x**63 + 5
    g = (8,
         [(x + 7, 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 + 9 * x**3 + 4, 1),
          (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1),
          (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1),
          (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * 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 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1),
          (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)])

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

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

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

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

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

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

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

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

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

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

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

    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)
    ])

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

    with config.using(gf_factor_method='shoup'):
        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

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

    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)])

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

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g
コード例 #24
0
ファイル: test_factortools.py プロジェクト: cbm755/diofant
def test_dmp_factor_list():
    R, x = ring("x", ZZ)
    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

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

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

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

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

    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 (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)])

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

    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
    coeff, factors = f.factor_list()
    assert coeff == 1.0 and len(factors) == 1 and factors[0][0].almosteq(
        f, 1e-10) and factors[0][1] == 1

    # issue diofant/diofant#238
    f = 0.1 * x**2 + 1.1 * x + 1.0
    assert 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)

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

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

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

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

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

    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", EX)
    pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list())

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

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

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

    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), [])

    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)])

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

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

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

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

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

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

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

    assert 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
    coeff, factors = f.factor_list()
    assert coeff == 1.0 and len(factors) == 1 and factors[0][0].almosteq(
        f, 1e-10) and factors[0][1] == 1

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

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

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

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

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

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

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

    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)])

    assert R.dmp_factor_list(f) == r

    with config.using(aa_factor_method='trager'):
        assert R.dmp_factor_list(f) == r
コード例 #25
0
def test_dup_sqf():
    R, x = ring("x", ZZ)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    assert R1.dmp_sqf_part(f) == f
    assert R2.dmp_sqf_part(g) == y + 1

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

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

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

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

    assert R.drop(x).dmp_sqf_list(res) == (45796, [(h, 3)])

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

    Rt, t = ring("t", ZZ)
    R, x = ring("x", Rt)
    assert R.dmp_sqf_list_include(t**3*x**2) == [(t**3, 1), (x, 2)]

    K = QQ.algebraic_field(sqrt(3))
    R, x = ring("x", K)
    _, X = ring("x", QQ)
    assert R.dmp_sqf_norm(x**2 - 2) == (1, x**2 + K([QQ(-2), QQ(0)])*x + 1, X**4 - 10*X**2 + 1)
コード例 #26
0
ファイル: test_domains.py プロジェクト: cbm755/diofant
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

    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

    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]))

    assert F5.is_positive(a) is True
    assert F5.is_negative(a) is False
    assert F5.is_positive(F5.zero) is False
    assert F5.is_negative(F5.zero) is False

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

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

    assert F9.order == 9
    assert F9.characteristic == 3

    assert F9.zero == F9([0])
    assert F9.one == F9([1])
    assert F9(F9([2, 1])) == F9([2, 1])
    assert F9([2, 1]) + F9([1, 2]) == F9([1, 2]) + F9([2, 1]) == F9.zero
    assert F9([2, 1]) + F9([1, 3]) == F9.one
    assert F9([2, 1]) * F9([1, 3]) == 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([2, 0])) == int(F9(6)) == 6

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

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

    F8 = FF(8)

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

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

    assert F4.order == 4
    assert F4.characteristic == 2
コード例 #27
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

    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

    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

    assert (F5(1) < F5(2)) is True
    assert (F5(1) <= F5(2)) is True
    assert (F5(1) > F5(2)) is False
    assert (F5(1) >= F5(2)) is False

    assert (F5(3) < F5(2)) is False
    assert (F5(3) <= F5(2)) is False
    assert (F5(3) > F5(2)) is True
    assert (F5(3) >= F5(2)) is True

    assert (F5(1) < F5(7)) is True
    assert (F5(1) <= F5(7)) is True
    assert (F5(1) > F5(7)) is False
    assert (F5(1) >= F5(7)) is False

    assert (F5(3) < F5(7)) is False
    assert (F5(3) <= F5(7)) is False
    assert (F5(3) > F5(7)) is True
    assert (F5(3) >= F5(7)) is True

    assert (F5(1) < 2) is True
    assert (F5(1) <= 2) is True
    assert (F5(1) > 2) is False
    assert (F5(1) >= 2) is False

    assert (F5(3) < 2) is False
    assert (F5(3) <= 2) is False
    assert (F5(3) > 2) is True
    assert (F5(3) >= 2) is True

    assert (F5(1) < 7) is True
    assert (F5(1) <= 7) is True
    assert (F5(1) > 7) is False
    assert (F5(1) >= 7) is False

    assert (F5(3) < 7) is False
    assert (F5(3) <= 7) is False
    assert (F5(3) > 7) is True
    assert (F5(3) >= 7) is True

    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))
コード例 #28
0
def test_dmp_factor_list():
    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(0) == (ZZ(0), [])
    assert R.dmp_factor_list(7) == (7, [])

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

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

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

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

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

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

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

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

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

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

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

    assert R.dmp_factor_list_include(f) == \
        [(4*y, 1),
         (x, 1),
         (x + y, 1)]

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

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

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

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

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

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

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

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

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

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

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

    R, x, y = ring("x,y", EX)
    pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
コード例 #29
0
ファイル: test_galoistools.py プロジェクト: Blendify/diofant
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'shoup')

    assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, [])
    assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, [])
    assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]])

    f, p = ZZ.map([1, 0, 0, 1, 0]), 2

    g = (1, [([1, 0], 1), ([1, 1], 1), ([1, 1, 1], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 0], [1, 1], [1, 1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11

    g = (1, [([1, 1], 1), ([1, 5, 3], 1), ([1, 2, 3, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 5, 8, 4], 11

    g = (1, [([1, 1], 1), ([1, 2], 2)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
             ([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (8, [([1, 3], 1), ([1, 8], 1), ([1, 0, 9], 1), ([1, 2, 2], 1),
             ([1, 9, 2], 1), ([1, 0, 5, 0, 7], 1), ([1, 0, 6, 0, 7], 1),
             ([1, 0, 0, 0, 1, 0, 0, 0, 6], 1), ([1, 0, 0, 0, 10, 0, 0, 0,
                                                 6], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (8, [([1, 7], 1), ([1, 4, 5], 1), ([1, 6, 8, 2], 1), ([1, 9, 9, 2], 1),
             ([1, 0, 0, 9, 0, 0, 4], 1), ([1, 2, 0, 8, 4, 6, 4], 1),
             ([1, 2, 3, 8, 0, 6, 4], 1), ([1, 2, 6, 0, 8, 4, 4], 1),
             ([1, 3, 3, 1, 6, 8, 4], 1), ([1, 5, 6, 0, 8, 6, 4], 1),
             ([1, 6, 2, 7, 9, 8, 4], 1), ([1, 10, 4, 7, 10, 7, 4], 1),
             ([1, 10, 10, 1, 4, 9, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)

    assert gf_sqf_p(f, p, ZZ) is True

    g = (1, [([1, 22730, 68144], 1), ([1, 81553, 77449, 86810, 4724], 1),
             ([1, 86276, 56779, 14859, 31575], 1),
             ([1, 15347, 95022, 84569, 94508, 92335], 1)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 22730, 68144], [1, 81553, 77449, 86810, 4724],
             [1, 86276, 56779, 14859, 31575],
             [1, 15347, 95022, 84569, 94508, 92335]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == 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

    p = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = ZZ.map([1, 2, 5, 26, 41, 39, 38])

    assert gf_sqf_p(f, p, ZZ) is True

    g = (1, [([1, 44, 26], 1), ([1, 11, 25, 18, 30], 1)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 44, 26], [1, 11, 25, 18, 30]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'other')
    pytest.raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
    config.setup('GF_FACTOR_METHOD')

    # IPoly interface:
    R, t = ring("t", FF(11))
    assert R.gf_factor_sqf(2 * t + 3) == (2, [t + 7])
コード例 #30
0
def test_PolyElement_is_():
    R,  x, y, z = ring("x,y,z", QQ)

    assert (x - x).is_generator is False
    assert (x - x).is_ground
    assert (x - x).is_monomial
    assert (x - x).is_term

    assert (x - x + 1).is_generator is False
    assert (x - x + 1).is_ground
    assert (x - x + 1).is_monomial
    assert (x - x + 1).is_term

    assert x.is_generator
    assert x.is_ground is False
    assert x.is_monomial
    assert x.is_term

    assert (x*y).is_generator is False
    assert (x*y).is_ground is False
    assert (x*y).is_monomial
    assert (x*y).is_term

    assert (3*x).is_generator is False
    assert (3*x).is_ground is False
    assert (3*x).is_monomial is False
    assert (3*x).is_term

    assert (3*x + 1).is_generator is False
    assert (3*x + 1).is_ground is False
    assert (3*x + 1).is_monomial is False
    assert (3*x + 1).is_term is False

    assert R(0).is_zero
    assert R(1).is_zero is False

    assert R(0).is_one is False
    assert R(1).is_one

    assert (x - 1).is_monic
    assert (2*x - 1).is_monic is False

    assert (3*x + 2).is_primitive
    assert (4*x + 2).is_primitive is False

    assert (x + y + z + 1).is_linear
    assert (x*y*z + 1).is_linear is False

    assert (x*y + z + 1).is_quadratic
    assert (x*y*z + 1).is_quadratic is False

    assert R(0).is_squarefree
    assert (x - 1).is_squarefree
    assert ((x - 1)**2).is_squarefree is False
    assert (x**2 + y**2).is_squarefree
    assert ((x + y)**2).is_squarefree is False

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

    _, t = ring("t", FF(11))

    assert (7*t + 3).is_irreducible
    assert (7*t**2 + 3*t + 1).is_irreducible is False

    _, u = ring("u", ZZ)
    f = u**16 + u**14 - u**10 - u**8 - u**6 + u**2

    assert f.is_cyclotomic is False
    assert (f + 1).is_cyclotomic

    pytest.raises(MultivariatePolynomialError, lambda: x.is_cyclotomic)

    assert f.is_positive is True
    assert f.is_negative is False
    assert f.is_nonnegative is True
    assert f.is_nonpositive is False