Пример #1
0
def test_Domain_interface():
    pytest.raises(TypeError, lambda: DomainElement().parent)

    assert RR(1).parent is RR
    assert CC(1).parent is CC

    assert RR.has_default_precision
    assert CC.has_default_precision

    RR3 = RealField(prec=53, dps=3)
    assert str(RR3(1.7611107002)) == '1.76'

    assert RealField(tol=3).tolerance == 3.0
    assert RealField(tol=0.1).tolerance == 0.1
    assert RealField(tol='0.1').tolerance == 0.1
    pytest.raises(ValueError, lambda: RealField(tol=object()))

    pytest.raises(AttributeError, lambda: CC.ring)
    pytest.raises(DomainError, lambda: CC.get_exact())

    assert str(EX(1)) == 'EX(1)'

    assert EX(1).as_expr() == Integer(1)
    assert bool(EX(1)) is True
    assert bool(EX(0)) is False
Пример #2
0
def test_RR_Float():
    f1 = Float('1.01', 15)
    f2 = Float('1.0000000000000000000001')
    assert f1._prec == 53
    assert f2._prec == 80
    assert RR(f1) - 1 > 1e-50
    assert RR(f2) - 1 < 1e-50  # RR's precision is lower than f2's

    RR2 = RealField(prec=f2._prec)
    assert RR2(f1) - 1 > 1e-50
    assert RR2(f2) - 1 > 1e-50  # RR's precision is equal to f2's

    a = RR(2.1)
    assert a.numerator == a and a.denominator == 1
Пример #3
0
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ.inject(x, y)) == ZZ.inject(x, y)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    pytest.raises(OptionError, lambda: Domain.preprocess(()))
Пример #4
0
def test_Domain_unify():
    F3 = GF(3)

    assert unify(F3, F3) == F3
    assert unify(F3, ZZ) == F3
    assert unify(F3, QQ) == QQ
    assert unify(F3, ALG) == ALG
    assert unify(F3, RR) == RR
    assert unify(F3, CC) == CC
    assert unify(F3, ZZ.inject(x)) == F3.inject(x)
    assert unify(F3, ZZ.inject(x).field) == F3.inject(x).field
    assert unify(F3, EX) == EX

    assert unify(ZZ, F3) == F3
    assert unify(ZZ, ZZ) == ZZ
    assert unify(ZZ, QQ) == QQ
    assert unify(ZZ, ALG) == ALG
    assert unify(ZZ, RR) == RR
    assert unify(ZZ, CC) == CC
    assert unify(ZZ, ZZ.inject(x)) == ZZ.inject(x)
    assert unify(ZZ, ZZ.inject(x).field) == ZZ.inject(x).field
    assert unify(ZZ, EX) == EX

    assert unify(QQ, F3) == QQ
    assert unify(QQ, ZZ) == QQ
    assert unify(QQ, QQ) == QQ
    assert unify(QQ, ALG) == ALG
    assert unify(QQ, RR) == RR
    assert unify(QQ, CC) == CC
    assert unify(QQ, ZZ.inject(x)) == QQ.inject(x)
    assert unify(QQ, ZZ.inject(x).field) == QQ.inject(x).field
    assert unify(QQ, EX) == EX

    assert unify(RR, F3) == RR
    assert unify(RR, ZZ) == RR
    assert unify(RR, QQ) == RR
    assert unify(RR, ALG) == RR
    assert unify(RR, RR) == RR
    assert unify(RR, CC) == CC
    assert unify(RR, ZZ.inject(x)) == RR.inject(x)
    assert unify(RR, ZZ.inject(x).field) == RR.inject(x).field
    assert unify(RR, EX) == EX

    assert unify(CC, F3) == CC
    assert unify(CC, ZZ) == CC
    assert unify(CC, QQ) == CC
    assert unify(CC, ALG) == CC
    assert unify(CC, RR) == CC
    assert unify(CC, CC) == CC
    assert unify(CC, ZZ.inject(x)) == CC.inject(x)
    assert unify(CC, ZZ.inject(x).field) == CC.inject(x).field
    assert unify(CC, EX) == EX

    CC2 = ComplexField(prec=20)
    assert unify(CC, CC2) == unify(CC2, CC) == ComplexField(prec=CC.precision,
                                                            tol=CC2.tolerance)
    RR2 = RealField(prec=20)
    assert unify(RR, RR2) == unify(RR2, RR) == RealField(prec=RR.precision,
                                                         tol=RR2.tolerance)

    assert unify(ZZ.inject(x), F3) == F3.inject(x)
    assert unify(ZZ.inject(x), ZZ) == ZZ.inject(x)
    assert unify(ZZ.inject(x), QQ) == QQ.inject(x)
    assert unify(ZZ.inject(x), ALG) == ALG.inject(x)
    assert unify(ZZ.inject(x), RR) == RR.inject(x)
    assert unify(ZZ.inject(x), CC) == CC.inject(x)
    assert unify(ZZ.inject(x), ZZ.inject(x)) == ZZ.inject(x)
    assert unify(ZZ.inject(x), ZZ.inject(x).field) == ZZ.inject(x).field
    assert unify(ZZ.inject(x), EX) == EX

    assert unify(ZZ.inject(x).field, F3) == F3.inject(x).field
    assert unify(ZZ.inject(x).field, ZZ) == ZZ.inject(x).field
    assert unify(ZZ.inject(x).field, QQ) == QQ.inject(x).field
    assert unify(ZZ.inject(x).field, ALG) == ALG.inject(x).field
    assert unify(ZZ.inject(x).field, RR) == RR.inject(x).field
    assert unify(ZZ.inject(x).field, CC) == CC.inject(x).field
    assert unify(ZZ.inject(x).field, ZZ.inject(x)) == ZZ.inject(x).field
    assert unify(ZZ.inject(x).field, ZZ.inject(x).field) == ZZ.inject(x).field
    assert unify(ZZ.inject(x).field, EX) == EX

    assert unify(EX, F3) == EX
    assert unify(EX, ZZ) == EX
    assert unify(EX, QQ) == EX
    assert unify(EX, ALG) == EX
    assert unify(EX, RR) == EX
    assert unify(EX, CC) == EX
    assert unify(EX, ZZ.inject(x)) == EX
    assert unify(EX, ZZ.inject(x).field) == EX
    assert unify(EX, EX) == EX