コード例 #1
0
ファイル: test_factortools.py プロジェクト: fagan2888/diofant
def test_dmp_zz_diophantine():
    R, x, y = ring('x,y', ZZ)

    H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
    H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y,
           x**2*y**2 - 9*x**2 + y - 9]
    H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y,
           x**2*y**2 - 9*x**2 + y - 9]
    c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
    c_2 = (9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y -
           324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y +
           2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y -
           945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 -
           54*x*y**2 - 3*x*y + 81*x + 12*y)
    c_3 = (-36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y -
           108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y)
    p = 6291469

    assert R.dmp_zz_diophantine(H_1, c_1, [ZZ(0)], 5, p) == [-3*x, -2, 1]
    assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
    assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]

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

    F = [47*x*y + 9*z**3 - 9, 45*x**3 - 9*y**3 - y**2 + 3*z**3 - 6*z]
    c = (-270*x**3*z**3 + 270*x**3 + 94*x*y*z + 54*y**3*z**3 - 54*y**3 +
         6*y**2*z**3 - 6*y**2 - 18*z**6 + 54*z**4 + 18*z**3 - 54*z)
    p = 2345258188817

    assert R.dmp_zz_diophantine(F, c, [ZZ(-2), ZZ(0)], 6,
                                p) == [-6*z**3 + 6, 2*z]
コード例 #2
0
ファイル: test_fields.py プロジェクト: diofant/diofant
def test_FractionField_methods():
    F = ZZ.inject('x').field

    assert F.domain_new(2) == ZZ(2)

    x = symbols('x')
    assert F(x**2 + x) == F.x**2 + F.x
コード例 #3
0
def test__dict_from_expr_no_gens():
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(Integer(17)))

    assert dict_from_expr(x) == ({(1, ): 1}, (x, ))
    assert dict_from_expr(y) == ({(1, ): 1}, (y, ))

    assert dict_from_expr(x * y) == ({(1, 1): 1}, (x, y))
    assert dict_from_expr(x + y) == ({(1, 0): 1, (0, 1): 1}, (x, y))

    assert dict_from_expr(sqrt(2)) == ({(1, ): 1}, (sqrt(2), ))
    pytest.raises(GeneratorsNeeded,
                  lambda: dict_from_expr(sqrt(2), greedy=False))

    assert dict_from_expr(x * y, domain=ZZ.inject(x)) == ({(1, ): x}, (y, ))
    assert dict_from_expr(x * y, domain=ZZ.inject(y)) == ({(1, ): y}, (x, ))

    assert dict_from_expr(3 * sqrt(2) * pi * x * y, extension=None) == ({
        (1, 1, 1, 1):
        3
    }, (x, y, pi, sqrt(2)))
    assert dict_from_expr(3 * sqrt(2) * pi * x * y, extension=True) == ({
        (1, 1, 1):
        3 * sqrt(2)
    }, (x, y, pi))

    f = cos(x) * sin(x) + cos(x) * sin(y) + cos(y) * sin(x) + cos(y) * sin(y)

    assert dict_from_expr(f) == ({
        (0, 1, 0, 1): 1,
        (0, 1, 1, 0): 1,
        (1, 0, 0, 1): 1,
        (1, 0, 1, 0): 1
    }, (cos(x), cos(y), sin(x), sin(y)))
コード例 #4
0
def test_dmp_mul_ground():
    R, x = ring('x', ZZ)

    f = 0

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

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

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

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

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

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

    f = f_polys()[0]

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

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

    f = f.set_ring(R) / 7

    assert (R.dmp_mul_ground(f, QQ(
        1, 2)) == x**2 * y * z**2 / 14 + x**2 * y * z / 7 + 3 * x**2 * y / 14 +
            x**2 / 7 + 3 * x / 14 + 2 * y**2 * z**2 / 7 + 5 * y**2 * z / 14 +
            3 * y**2 / 7 + y * z**2 / 14 + y * z / 7 + y / 14 + QQ(1, 14))
コード例 #5
0
ファイル: test_densetools.py プロジェクト: fagan2888/diofant
def test_dmp_eval_in():
    R, x = ring('x', ZZ)

    assert R.dmp_eval_in(0, ZZ(7), 0) == 0
    assert R.dmp_eval_in(x + 2, ZZ(0), 0) == 2
    assert R.dmp_eval_in(x**2 + 2 * x + 3, ZZ(7), 0) == 66
    assert R.dmp_eval_in(x**2 + 2 * x + 3, ZZ(2), 0) == 11

    assert R.dmp_eval_in(0, ZZ(3), 0) == 0

    R, x, y = ring('x y', ZZ)
    R1 = R.drop(x)

    assert R.dmp_eval_in(0, 3, 0) == 0
    assert R.dmp_eval_in(y + 2, 0, 0) == R1.y + 2
    assert R.dmp_eval_in(3 * x * y + 2 * x + y + 2, 3, 0) == 10 * R1.y + 8
    assert R.dmp_eval_in(2 * x * y + 3 * x + y + 2, 2, 0) == 5 * R1.y + 8

    R, x, y, z = ring('x y z', ZZ)
    R1 = R.drop(x)
    R3 = R.drop(z)

    assert R.dmp_eval_in(0, 3, 0) == 0
    assert R.dmp_eval_in(1, 3, 0) == 1
    assert R.dmp_eval_in(z + 2, 3, 0) == R1.z + 2
    assert R.dmp_eval_in(3 * x * z + 2 * x + z + 2, 3, 0) == 10 * R1.z + 8

    f = 45 * x**3 - 9 * y**3 - y**2 + 3 * z**3 + 10 * z

    assert R.dmp_eval_in(f, -2, 2) == 45 * R3.x**3 - 9 * R3.y**3 - R3.y**2 - 44

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

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

    f = f_polys()[6]
    R2 = R.drop(y)
    R3 = R.drop(z)

    x, z, t = R2.gens

    assert (R.dmp_eval_in(f, -2, 1) == -4230 * x**4 + 45 * x**3 * z**3 * t**2 -
            45 * x**3 * t**2 - 282 * x * z**3 - 188 * x * z * t - 6392 * x +
            3 * z**6 * t**2 + 2 * z**4 * t**3 + 65 * z**3 * t**2 -
            2 * z * t**3 - 68 * t**2)
    assert (R.dmp_eval_in(
        f, 7, 1) == 14805 * x**4 + 45 * x**3 * z**3 * t**2 - 45 * x**3 * t**2 +
            987 * x * z**3 + 658 * x * z * t - 1031744 * x + 3 * z**6 * t**2 +
            2 * z**4 * t**3 - 3139 * z**3 * t**2 - 2 * z * t**3 + 3136 * t**2)

    x, y, t = R3.gens

    assert (R.dmp_eval_in(f, -2, 2) == 2115 * x**4 * y - 405 * x**3 * t**2 -
            423 * x * y**4 - 47 * x * y**3 - 188 * x * y * t - 1128 * x * y +
            81 * y**3 * t**2 + 9 * y**2 * t**2 + 36 * t**3 + 216 * t**2)
    assert (R.dmp_eval_in(f, 7, 2) == 2115 * x**4 * y + 15390 * x**3 * t**2 -
            423 * x * y**4 - 47 * x * y**3 + 658 * x * y * t + 48363 * x * y -
            3078 * y**3 * t**2 - 342 * y**2 * t**2 + 4788 * t**3 +
            351918 * t**2)
コード例 #6
0
ファイル: test_domains.py プロジェクト: X3nOph0N/diofant
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()
コード例 #7
0
ファイル: test_repr.py プロジェクト: fagan2888/diofant
def test_FractionField():
    sT(ZZ.inject('x').field, "FractionField(%s, (Symbol('x'),), "
                             'LexOrder())' % repr(ZZ))
    sT(QQ.frac_field('x', 'y', order=grlex),
       "FractionField(%s, (Symbol('x'), Symbol('y')), "
       'GradedLexOrder())' % repr(QQ))
    sT(ZZ.inject('x', 'y', 'z', 't').eject('t').field,
       "FractionField(PolynomialRing(%s, (Symbol('t'),), LexOrder()), "
       "(Symbol('x'), Symbol('y'), Symbol('z')), LexOrder())" % repr(ZZ))
コード例 #8
0
ファイル: test_domains.py プロジェクト: X3nOph0N/diofant
def test_Domain_field():
    assert EX.field == EX
    assert ZZ.field == QQ
    assert QQ.field == QQ
    assert RR.field == RR
    assert ALG.field == ALG
    assert ZZ.inject(x).field == ZZ.frac_field(x)
    assert QQ.inject(x).field == QQ.frac_field(x)
    assert ZZ.inject(x, y).field == ZZ.frac_field(x, y)
    assert QQ.inject(x, y).field == QQ.frac_field(x, y)
コード例 #9
0
ファイル: test_repr.py プロジェクト: diofant/diofant
def test_FractionField():
    sT(
        ZZ.inject('x').field,
        f"FractionField({ZZ!r}, (Symbol('x'),), LexOrder())")
    sT(QQ.frac_field('x', 'y', order=grlex),
       f"FractionField({QQ!r}, (Symbol('x'), Symbol('y')), GradedLexOrder())")
    sT(
        ZZ.inject('x', 'y', 'z', 't').eject('t').field,
        f"FractionField(UnivarPolynomialRing({ZZ!r}, (Symbol('t'),), LexOrder()), "
        "(Symbol('x'), Symbol('y'), Symbol('z')), LexOrder())")
コード例 #10
0
ファイル: test_repr.py プロジェクト: fagan2888/diofant
def test_PolynomialRing():
    sT(ZZ.inject('x'), "PolynomialRing(%s, (Symbol('x'),), "
                       'LexOrder())' % repr(ZZ))
    sT(QQ.poly_ring('x', 'y', order=grlex),
       "PolynomialRing(%s, (Symbol('x'), Symbol('y')), "
       'GradedLexOrder())' % repr(QQ))
    sT(ZZ.inject('x', 'y', 'z', 't').eject('t'),
       "PolynomialRing(PolynomialRing(%s, (Symbol('t'),), "
       "LexOrder()), (Symbol('x'), Symbol('y'), Symbol('z')), "
       'LexOrder())' % repr(ZZ))
コード例 #11
0
ファイル: test_repr.py プロジェクト: diofant/diofant
def test_PolynomialRing():
    sT(ZZ.inject('x'),
       f"UnivarPolynomialRing({ZZ!r}, (Symbol('x'),), LexOrder())")
    sT(
        QQ.poly_ring('x', 'y', order=grlex),
        f"PolynomialRing({QQ!r}, (Symbol('x'), Symbol('y')), GradedLexOrder())"
    )
    sT(
        ZZ.inject('x', 'y', 'z', 't').eject('t'),
        f"PolynomialRing(UnivarPolynomialRing({ZZ!r}, (Symbol('t'),), "
        "LexOrder()), (Symbol('x'), Symbol('y'), Symbol('z')), LexOrder())")
コード例 #12
0
ファイル: test_densebasic.py プロジェクト: fagan2888/diofant
def test_dmp_convert():
    K0, K1 = ZZ.inject('x'), ZZ

    assert dmp_convert([K0(1), K0(2)], 0, K0, K1) == [ZZ(1), ZZ(2)]
    assert dmp_convert([K1(1), K1(2)], 0, K1, K0) == [K0(1), K0(2)]

    f = [K0(1), K0(2), K0(0), K0(3)]

    assert dmp_convert(f, 0, K0, K1) == [ZZ(1), ZZ(2), ZZ(0), ZZ(3)]

    f = [[K0(1)], [K0(2)], [], [K0(3)]]

    assert dmp_convert(f, 1, K0, K1) == [[ZZ(1)], [ZZ(2)], [], [ZZ(3)]]
コード例 #13
0
ファイル: test_fields.py プロジェクト: diofant/diofant
def test_FractionField___init__():
    F1 = ZZ.inject('x', 'y').field
    F2 = ZZ.inject('x', 'y').field
    F3 = ZZ.inject('x', 'y', 'z').field

    assert F1.x == F1.gens[0]
    assert F1.y == F1.gens[1]
    assert F1.x == F2.x
    assert F1.y == F2.y
    assert F1.x != F3.x
    assert F1.y != F3.y

    F4 = ZZ.inject('gens').field
    assert type(F4.gens) is tuple
コード例 #14
0
ファイル: test_domains.py プロジェクト: X3nOph0N/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.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))
コード例 #15
0
def test_QuotientRing():
    I = QQ.old_poly_ring(x).ideal(x**2 + 1)
    R = QQ.old_poly_ring(x) / I

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

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

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

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

    R = QQ.old_poly_ring(x, order="ilex")
    I = R.ideal(x)
    assert R.convert(1) + I == (R / I).convert(1)
コード例 #16
0
ファイル: test_polyoptions.py プロジェクト: fagan2888/diofant
def test_Domain_postprocess():
    pytest.raises(GeneratorsError, lambda: Domain.postprocess({'gens': (x, y),
                                                               'domain': ZZ.inject(y, z)}))

    pytest.raises(GeneratorsError, lambda: Domain.postprocess({'gens': (),
                                                               'domain': EX}))
    pytest.raises(GeneratorsError, lambda: Domain.postprocess({'domain': EX}))
コード例 #17
0
def test_localring():
    Qxy = QQ.inject(x, y).field
    R = QQ.poly_ring(x, y, order='ilex')
    X = R.convert(x)
    Y = R.convert(y)

    assert x in R
    assert 1 / x not in R
    assert Y in R
    assert X.ring == R.ring
    assert X + Y == R.convert(x + y)
    assert X - Y == R.convert(x - y)
    assert X + 1 == R.convert(x + 1)
    assert X**2 // X == X

    assert R.convert(ZZ.inject(x, y).convert(x), ZZ.inject(x, y)) == X
    assert R.convert(Qxy.convert(x), Qxy) == X
コード例 #18
0
ファイル: test_factortools.py プロジェクト: X3nOph0N/diofant
def test__zz_hensel_lift():
    R, x = ring('x', ZZ)

    f = x**4 - 1
    F = [x - 1, x - 2, x + 2, x + 1]

    assert R._zz_hensel_lift(ZZ(5), f, F,
                             4) == [x - 1, x - 182, x + 182, x + 1]
コード例 #19
0
ファイル: test_domains.py プロジェクト: X3nOph0N/diofant
def test_Domain_get_exact():
    assert EX.get_exact() == EX
    assert ZZ.get_exact() == ZZ
    assert QQ.get_exact() == QQ
    assert RR.get_exact() == QQ
    assert ALG.get_exact() == ALG
    assert ZZ.inject(x).get_exact() == ZZ.inject(x)
    assert QQ.inject(x).get_exact() == QQ.inject(x)
    assert ZZ.inject(x, y).get_exact() == ZZ.inject(x, y)
    assert QQ.inject(x, y).get_exact() == QQ.inject(x, y)
    assert ZZ.inject(x).field.get_exact() == ZZ.inject(x).field
    assert QQ.inject(x).field.get_exact() == QQ.inject(x).field
    assert ZZ.inject(x, y).field.get_exact() == ZZ.inject(x, y).field
    assert QQ.inject(x, y).field.get_exact() == QQ.inject(x, y).field
コード例 #20
0
def test_pickling_polys_polyclasses():
    for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)):
        check(c)
    for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)):
        check(c)
    for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)):
        check(c)
コード例 #21
0
ファイル: test_domains.py プロジェクト: X3nOph0N/diofant
def test_Domain___eq__():
    assert (ZZ.inject(x, y) == ZZ.inject(x, y)) is True
    assert (QQ.inject(x, y) == QQ.inject(x, y)) is True

    assert (ZZ.inject(x, y) == QQ.inject(x, y)) is False
    assert (QQ.inject(x, y) == ZZ.inject(x, y)) is False

    assert (ZZ.inject(x, y).field == ZZ.inject(x, y).field) is True
    assert (QQ.inject(x, y).field == QQ.inject(x, y).field) is True

    assert (ZZ.inject(x, y).field == QQ.inject(x, y).field) is False
    assert (QQ.inject(x, y).field == ZZ.inject(x, y).field) is False
コード例 #22
0
ファイル: test_euclidtools.py プロジェクト: diofant/diofant
def test_sympyissue_21460():
    R = ZZ.inject('x')

    r = R.gcd(R(4), R(6))
    assert type(r) is R.dtype and r == 2

    R = QQ.inject('x')

    r = R.gcd(R(4), R(6))
    assert type(r) is R.dtype and r == 1
コード例 #23
0
def test_globalring():
    Qxy = QQ.inject(x, y).field
    R = QQ.inject(x, y)
    X = R.convert(x)
    Y = R.convert(y)

    assert x in R
    assert 1 / x not in R
    assert 1 / (1 + x) not in R
    assert Y in R
    assert X.ring == R.ring
    assert X * (Y**2 + 1) == R.convert(x * (y**2 + 1))
    assert X * Y == R.convert(x * y)
    assert X + Y == R.convert(x + y)
    assert X - Y == R.convert(x - y)
    assert X + 1 == R.convert(x + 1)
    assert X**2 // X == X

    assert R.convert(ZZ.inject(x, y).convert(x), ZZ.inject(x, y)) == X
    assert R.convert(Qxy.convert(x), Qxy) == X
コード例 #24
0
def test__dict_from_expr_no_gens():
    pytest.raises(GeneratorsNeeded,
                  lambda: parallel_dict_from_expr([Integer(17)]))

    assert parallel_dict_from_expr([x]) == ([{(1, ): 1}], (x, ))
    assert parallel_dict_from_expr([y]) == ([{(1, ): 1}], (y, ))

    assert parallel_dict_from_expr([x * y]) == ([{(1, 1): 1}], (x, y))
    assert parallel_dict_from_expr([x + y]) == ([{
        (1, 0): 1,
        (0, 1): 1
    }], (x, y))

    assert parallel_dict_from_expr([sqrt(2)]) == ([{(1, ): 1}], (sqrt(2), ))
    pytest.raises(GeneratorsNeeded,
                  lambda: parallel_dict_from_expr([sqrt(2)], greedy=False))

    assert parallel_dict_from_expr([x * y], domain=ZZ.inject(x)) == ([{
        (1, ): x
    }], (y, ))
    assert parallel_dict_from_expr([x * y], domain=ZZ.inject(y)) == ([{
        (1, ): y
    }], (x, ))

    assert parallel_dict_from_expr([3 * sqrt(2) * pi * x * y],
                                   extension=None) == ([{
                                       (1, 1, 1, 1): 3
                                   }], (x, y, pi, sqrt(2)))
    assert parallel_dict_from_expr([3 * sqrt(2) * pi * x * y],
                                   extension=True) == ([{
                                       (1, 1, 1): 3 * sqrt(2)
                                   }], (x, y, pi))

    f = cos(x) * sin(x) + cos(x) * sin(y) + cos(y) * sin(x) + cos(y) * sin(y)

    assert parallel_dict_from_expr([f]) == ([{
        (0, 1, 0, 1): 1,
        (0, 1, 1, 0): 1,
        (1, 0, 0, 1): 1,
        (1, 0, 1, 0): 1
    }], (cos(x), cos(y), sin(x), sin(y)))
コード例 #25
0
ファイル: test_fields.py プロジェクト: diofant/diofant
def test_FracElement___call__():
    _, x, y, z = field('x y z', ZZ)
    f = (x**2 + 3 * y) / z

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

    r = f(1, 1, 1)
    assert r == 4 and not isinstance(r, FracElement)
    pytest.raises(ZeroDivisionError, lambda: f(1, 1, 0))

    Fz = ZZ.inject('z').field
    assert f(1, 1) == 4 / Fz.z
コード例 #26
0
def test_apart_undetermined_coeffs():
    p = (2 * x - 3).as_poly()
    q = (x**9 - x**8 - x**6 + x**5 - 2 * x**2 + 3 * x - 1).as_poly()
    r = (-x**7 - x**6 - x**5 + 4) / (x**8 - x**5 - 2 * x + 1) + 1 / (x - 1)

    assert apart_undetermined_coeffs(p, q) == r

    dom = ZZ.inject(a, b)
    p = Integer(1).as_poly(x, domain=dom)
    q = ((x + a) * (x + b)).as_poly(x, domain=dom)
    r = 1 / ((a - b) * (b + x)) - 1 / ((a - b) * (a + x))

    assert apart_undetermined_coeffs(p, q) == r
コード例 #27
0
def test_dmp_quo_ground():
    R, x = ring('x', ZZ)

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

    f = 3 * x**2 + 2

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

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

    f = 3 * x**2 + 2

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

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

    f = 0

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

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

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

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

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

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

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

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

    assert R.dmp_quo_ground(f, ZZ(1)) == f
    assert R.dmp_quo_ground(f, ZZ(2)) == 3 * x**2 + x + 4
    assert R.dmp_quo_ground(f, ZZ(3)) == 2 * x**2 + 2
コード例 #28
0
ファイル: test_densetools.py プロジェクト: fagan2888/diofant
def test_dmp_ground_trunc():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_trunc(
        x**5 + 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6,
        ZZ(3)) == x**5 - x**4 + x**2 - x
    assert R.dmp_ground_trunc(
        6 * x**5 + 5 * x**4 + 4 * x**3 + 3 * x**2 + 2 * x + 1,
        ZZ(3)) == -x**4 + x**3 - x + 1

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

    assert R.dmp_ground_trunc(
        x**5 + 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6,
        ZZ(3)) == x**5 + 2 * x**4 + x**2 + 2 * x

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

    f = f_polys()[0]

    assert R.dmp_ground_trunc(
        f, ZZ(3)) == (x**2 * y * z**2 - x**2 * y * z - x**2 + y**2 * z**2 -
                      y**2 * z + y * z**2 - y * z + y + 1)
コード例 #29
0
def test_units():
    R = QQ.inject(x)
    assert R.convert(1) == R.one
    assert R.convert(x) != R.one
    assert R.convert(1 + x) != R.one

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

    R = ZZ.inject(x)
    assert R.convert(1) == R.one
    assert R.convert(2) != R.one
    assert R.convert(x) != R.one
    assert R.convert(1 + x) != R.one
コード例 #30
0
def test_gegenbauer_poly():
    pytest.raises(ValueError, lambda: gegenbauer_poly(-1, a, x))

    dom = ZZ.inject(a).field

    assert gegenbauer_poly(1, a, x,
                           polys=True) == (2 * a * x).as_poly(x, domain=dom)

    assert gegenbauer_poly(0, a, x) == 1
    assert gegenbauer_poly(1, a, x) == 2 * a * x
    assert gegenbauer_poly(2, a, x) == -a + x**2 * (2 * a**2 + 2 * a)
    assert gegenbauer_poly(
        3, a, x
    ) == x**3 * (4 * a**3 / 3 + 4 * a**2 + 8 * a / 3) + x * (-2 * a**2 - 2 * a)

    assert gegenbauer_poly(1, Rational(1, 2), x) == x
    assert gegenbauer_poly(1, a, polys=True) == (2 * a * x).as_poly(x,
                                                                    domain=dom)
コード例 #31
0
ファイル: test_polyroots.py プロジェクト: skirpichev/diofant
def test_roots_preprocessing():
    f = a*y*x**2 + y - b

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1
    assert poly == Poly(a*y*x**2 + y - b, x)

    f = c**3*x**3 + c**2*x**2 + c*x + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1/c
    assert poly == Poly(x**3 + x**2 + x + a, x)

    f = c**3*x**3 + c**2*x**2 + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1/c
    assert poly == Poly(x**3 + x**2 + a, x)

    f = c**3*x**3 + c*x + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1/c
    assert poly == Poly(x**3 + x + a, x)

    f = c**3*x**3 + a

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 1/c
    assert poly == Poly(x**3 + a, x)

    E, F, J, L = symbols("E,F,J,L")

    f = -21601054687500000000*E**8*J**8/L**16 + \
        508232812500000000*F*x*E**7*J**7/L**14 - \
        4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
        16194716250000*E**5*F**3*J**5*x**3/L**10 - \
        27633173750*E**4*F**4*J**4*x**4/L**8 + \
        14840215*E**3*F**5*J**3*x**5/L**6 + \
        54794*E**2*F**6*J**2*x**6/(5*L**4) - \
        1153*E*J*F**7*x**7/(80*L**2) + \
        633*F**8*x**8/160000

    coeff, poly = preprocess_roots(Poly(f, x))

    assert coeff == 20*E*J/(F*L**2)
    assert poly == 633*x**8 - 115300*x**7 + 4383520*x**6 + 296804300*x**5 - 27633173750*x**4 + \
        809735812500*x**3 - 10673859375000*x**2 + 63529101562500*x - 135006591796875

    f = J**8 + 7*E*x**2*L**16 + 5*F*x*E**5*J**7*L**2
    coeff, poly = preprocess_roots(Poly(f, x))
    assert coeff == 1
    assert poly == Poly(f, x)

    f = Poly(-y**2 + x**2*exp(x), y, domain=ZZ.poly_ring(x, exp(x)))
    g = Poly(y**2 - exp(x), y, domain=ZZ.poly_ring(exp(x)))

    assert preprocess_roots(f) == (x, g)