Ejemplo n.º 1
0
def test_almosteq():
    assert CC.almosteq(CC(2), 3) is False
    assert CC.almosteq(2, CC(3)) is False
    assert CC.almosteq(2, CC(2.5), 0.1) is False
    assert CC.almosteq(2, CC(2.5), 1.0) is True

    assert RR.almosteq(5, RR(2), 1) is True
    assert RR._context.almosteq(RR(2), 1, None, 1) is True
Ejemplo n.º 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
Ejemplo n.º 3
0
def test_meijerg_with_Floats():
    # see sympy/sympy#10681
    f = meijerg(((3.0, 1), ()), ((Rational(3, 2),), (0,)), z)
    a = -2.3632718012073
    g = a*z**Rational(3, 2)*hyper((-0.5, Rational(3, 2)),
                                  (Rational(5, 2),), z*exp_polar(I*pi))
    assert RR.almosteq((hyperexpand(f)/g).n(), 1.0, 1e-12)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_RR_double():
    assert RR(3.14) > 1e-50
    assert RR(1e-13) > 1e-50
    assert RR(1e-14) > 1e-50
    assert RR(1e-15) > 1e-50
    assert RR(1e-20) > 1e-50
    assert RR(1e-40) > 1e-50
Ejemplo n.º 6
0
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(
        1684996666696915,
        1684996666696914987166688442938726917102321526408785780068975640576)

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

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

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

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

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

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

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

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

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

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

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

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def test_sympyissue_10681():
    f = integrate(r**2 * (R**2 - r**2)**0.5, r, meijerg=True)
    g = (1.0 / 3) * R**1.0 * r**3 * hyper(
        (-0.5, Rational(3, 2)),
        (Rational(5, 2), ), r**2 * exp_polar(2 * I * pi) / R**2)
    assert RR.almosteq((f / g).n(), 1.0, 1e-12)
Ejemplo n.º 9
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([Integer(1), Integer(2),
                             Integer(3)]) == (ZZ, [ZZ(1), ZZ(2),
                                                   ZZ(3)])
    assert construct_domain(
        [Integer(1), Integer(2), Integer(3)],
        field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([Rational(1, 2),
                             Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain([3.14, 1, Rational(1, 2)
                             ]) == (RR, [RR(3.14), RR(1.0),
                                         RR(0.5)])

    assert construct_domain([3.14, sqrt(2)],
                            extension=False) == (EX, [EX(3.14),
                                                      EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)]) == (EX, [EX(3.14), EX(sqrt(2))])
    assert construct_domain([sqrt(2), 3.14]) == (EX, [EX(sqrt(2)), EX(3.14)])

    assert construct_domain([1, sqrt(2)],
                            extension=False) == (EX, [EX(1),
                                                      EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert (construct_domain(
        [7, Rational(1, 2),
         sqrt(2)]) == (alg, [alg([7]),
                             alg([Rational(1, 2)]),
                             alg([1, 0])]))

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert (construct_domain([7, sqrt(2), sqrt(3)]) == (alg, [
        alg([7]), alg.from_expr(sqrt(2)),
        alg.from_expr(sqrt(3))
    ]))

    dom = ZZ.inject(x)

    assert construct_domain([2 * x, 3]) == (dom, [dom(2 * x), dom(3)])

    dom = ZZ.inject(x, y)

    assert construct_domain([2 * x, 3 * y]) == (dom, [dom(2 * x), dom(3 * y)])

    dom = QQ.inject(x)

    assert construct_domain([x / 2, 3]) == (dom, [dom(x / 2), dom(3)])

    dom = QQ.inject(x, y)

    assert construct_domain([x / 2, 3 * y]) == (dom, [dom(x / 2), dom(3 * y)])

    dom = RR.inject(x)

    assert construct_domain([x / 2, 3.5]) == (dom, [dom(x / 2), dom(3.5)])

    dom = RR.inject(x, y)

    assert construct_domain([x / 2,
                             3.5 * y]) == (dom, [dom(x / 2),
                                                 dom(3.5 * y)])

    dom = ZZ.inject(x).field

    assert construct_domain([2 / x, 3]) == (dom, [dom(2 / x), dom(3)])

    dom = ZZ.inject(x, y).field

    assert construct_domain([2 / x, 3 * y]) == (dom, [dom(2 / x), dom(3 * y)])

    dom = RR.inject(x).field

    assert construct_domain([2 / x, 3.5]) == (dom, [dom(2 / x), dom(3.5)])

    dom = RR.inject(x, y).field

    assert construct_domain([2 / x,
                             3.5 * y]) == (dom, [dom(2 / x),
                                                 dom(3.5 * y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})

    assert construct_domain([-x * y + x * (y + 42) - 42 * x
                             ]) == (EX, [EX(-x * y + x * (y + 42) - 42 * x)])
Ejemplo n.º 10
0
def test_RealField_from_expr():
    assert RR.convert(Integer(0)) == RR.dtype(0)
    assert RR.convert(Float(0.0)) == RR.dtype(0.0)
    assert RR.convert(Integer(1)) == RR.dtype(1)
    assert RR.convert(Float(1.0)) == RR.dtype(1.0)
    assert RR.convert(sin(1)) == RR.dtype(sin(1).evalf())
    assert RR.convert(oo) == RR('+inf')
    assert RR.convert(-oo) == RR('-inf')
    pytest.raises(CoercionFailed, lambda: RR.convert(x))
Ejemplo n.º 11
0
def test_FractionField_convert():
    F, X, Y = field('x y', QQ)
    assert F.convert(QQ_python(1, 3)) == F.one / 3
    assert F.convert(RR(1)) == F.one
Ejemplo n.º 12
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
Ejemplo n.º 13
0
def test_Domain__contains__():
    assert (0 in EX) is True
    assert (0 in ZZ) is True
    assert (0 in QQ) is True
    assert (0 in RR) is True
    assert (0 in CC) is True
    assert (0 in ALG) is True
    assert (0 in ZZ.inject(x, y)) is True
    assert (0 in QQ.inject(x, y)) is True
    assert (0 in RR.inject(x, y)) is True

    assert (-7 in EX) is True
    assert (-7 in ZZ) is True
    assert (-7 in QQ) is True
    assert (-7 in RR) is True
    assert (-7 in CC) is True
    assert (-7 in ALG) is True
    assert (-7 in ZZ.inject(x, y)) is True
    assert (-7 in QQ.inject(x, y)) is True
    assert (-7 in RR.inject(x, y)) is True

    assert (17 in EX) is True
    assert (17 in ZZ) is True
    assert (17 in QQ) is True
    assert (17 in RR) is True
    assert (17 in CC) is True
    assert (17 in ALG) is True
    assert (17 in ZZ.inject(x, y)) is True
    assert (17 in QQ.inject(x, y)) is True
    assert (17 in RR.inject(x, y)) is True

    assert (-Rational(1, 7) in EX) is True
    assert (-Rational(1, 7) in ZZ) is False
    assert (-Rational(1, 7) in QQ) is True
    assert (-Rational(1, 7) in RR) is True
    assert (-Rational(1, 7) in CC) is True
    assert (-Rational(1, 7) in ALG) is True
    assert (-Rational(1, 7) in ZZ.inject(x, y)) is False
    assert (-Rational(1, 7) in QQ.inject(x, y)) is True
    assert (-Rational(1, 7) in RR.inject(x, y)) is True

    assert (Rational(3, 5) in EX) is True
    assert (Rational(3, 5) in ZZ) is False
    assert (Rational(3, 5) in QQ) is True
    assert (Rational(3, 5) in RR) is True
    assert (Rational(3, 5) in CC) is True
    assert (Rational(3, 5) in ALG) is True
    assert (Rational(3, 5) in ZZ.inject(x, y)) is False
    assert (Rational(3, 5) in QQ.inject(x, y)) is True
    assert (Rational(3, 5) in RR.inject(x, y)) is True

    assert (3.0 in EX) is True
    assert (3.0 in ZZ) is True
    assert (3.0 in QQ) is True
    assert (3.0 in RR) is True
    assert (3.0 in CC) is True
    assert (3.0 in ALG) is True
    assert (3.0 in ZZ.inject(x, y)) is True
    assert (3.0 in QQ.inject(x, y)) is True
    assert (3.0 in RR.inject(x, y)) is True

    assert (3.14 in EX) is True
    assert (3.14 in ZZ) is False
    assert (3.14 in QQ) is True
    assert (3.14 in RR) is True
    assert (3.14 in CC) is True
    assert (3.14 in ALG) is True
    assert (3.14 in ZZ.inject(x, y)) is False
    assert (3.14 in QQ.inject(x, y)) is True
    assert (3.14 in RR.inject(x, y)) is True

    assert (oo in EX) is True
    assert (oo in ZZ) is False
    assert (oo in QQ) is False
    assert (oo in RR) is True
    assert (oo in CC) is True
    assert (oo in ALG) is False
    assert (oo in ZZ.inject(x, y)) is False
    assert (oo in QQ.inject(x, y)) is False
    assert (oo in RR.inject(x, y)) is True

    assert (-oo in EX) is True
    assert (-oo in ZZ) is False
    assert (-oo in QQ) is False
    assert (-oo in RR) is True
    assert (-oo in CC) is True
    assert (-oo in ALG) is False
    assert (-oo in ZZ.inject(x, y)) is False
    assert (-oo in QQ.inject(x, y)) is False
    assert (-oo in RR.inject(x, y)) is True

    assert (sqrt(7) in EX) is True
    assert (sqrt(7) in ZZ) is False
    assert (sqrt(7) in QQ) is False
    assert (sqrt(7) in RR) is True
    assert (sqrt(7) in CC) is True
    assert (sqrt(7) in ALG) is False
    assert (sqrt(7) in ZZ.inject(x, y)) is False
    assert (sqrt(7) in QQ.inject(x, y)) is False
    assert (sqrt(7) in RR.inject(x, y)) is True

    assert (2 * sqrt(3) + 1 in EX) is True
    assert (2 * sqrt(3) + 1 in ZZ) is False
    assert (2 * sqrt(3) + 1 in QQ) is False
    assert (2 * sqrt(3) + 1 in RR) is True
    assert (2 * sqrt(3) + 1 in CC) is True
    assert (2 * sqrt(3) + 1 in ALG) is True
    assert (2 * sqrt(3) + 1 in ZZ.inject(x, y)) is False
    assert (2 * sqrt(3) + 1 in QQ.inject(x, y)) is False
    assert (2 * sqrt(3) + 1 in RR.inject(x, y)) is True

    assert (sin(1) in EX) is True
    assert (sin(1) in ZZ) is False
    assert (sin(1) in QQ) is False
    assert (sin(1) in RR) is True
    assert (sin(1) in CC) is True
    assert (sin(1) in ALG) is False
    assert (sin(1) in ZZ.inject(x, y)) is False
    assert (sin(1) in QQ.inject(x, y)) is False
    assert (sin(1) in RR.inject(x, y)) is True

    assert (x**2 + 1 in EX) is True
    assert (x**2 + 1 in ZZ) is False
    assert (x**2 + 1 in QQ) is False
    assert (x**2 + 1 in RR) is False
    assert (x**2 + 1 in CC) is False
    assert (x**2 + 1 in ALG) is False
    assert (x**2 + 1 in ZZ.inject(x)) is True
    assert (x**2 + 1 in QQ.inject(x)) is True
    assert (x**2 + 1 in RR.inject(x)) is True
    assert (x**2 + 1 in ZZ.inject(x, y)) is True
    assert (x**2 + 1 in QQ.inject(x, y)) is True
    assert (x**2 + 1 in RR.inject(x, y)) is True

    assert (x**2 + y**2 in EX) is True
    assert (x**2 + y**2 in ZZ) is False
    assert (x**2 + y**2 in QQ) is False
    assert (x**2 + y**2 in RR) is False
    assert (x**2 + y**2 in CC) is False
    assert (x**2 + y**2 in ALG) is False
    assert (x**2 + y**2 in ZZ.inject(x)) is False
    assert (x**2 + y**2 in QQ.inject(x)) is False
    assert (x**2 + y**2 in RR.inject(x)) is False
    assert (x**2 + y**2 in ZZ.inject(x, y)) is True
    assert (x**2 + y**2 in QQ.inject(x, y)) is True
    assert (x**2 + y**2 in RR.inject(x, y)) is True

    assert (Rational(3, 2) * x / (y + 1) - z in QQ.inject(x, y, z)) is False

    R = QQ.inject(x)

    assert R(1) in ZZ

    F = R.field

    assert F(1) in ZZ