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)
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
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
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))
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))
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))
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))
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.poly_ring(x)) == F3.poly_ring(x) assert unify(F3, ZZ.frac_field(x)) == F3.frac_field(x) 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.poly_ring(x)) == ZZ.poly_ring(x) assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x) 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.poly_ring(x)) == QQ.poly_ring(x) assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x) 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.poly_ring(x)) == RR.poly_ring(x) assert unify(RR, ZZ.frac_field(x)) == RR.frac_field(x) 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.poly_ring(x)) == CC.poly_ring(x) assert unify(CC, ZZ.frac_field(x)) == CC.frac_field(x) 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.poly_ring(x), F3) == F3.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), QQ) == QQ.poly_ring(x) assert unify(ZZ.poly_ring(x), ALG) == ALG.poly_ring(x) assert unify(ZZ.poly_ring(x), RR) == RR.poly_ring(x) assert unify(ZZ.poly_ring(x), CC) == CC.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x)) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.poly_ring(x), EX) == EX assert unify(ZZ.frac_field(x), F3) == F3.frac_field(x) assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x) assert unify(ZZ.frac_field(x), ALG) == ALG.frac_field(x) assert unify(ZZ.frac_field(x), RR) == RR.frac_field(x) assert unify(ZZ.frac_field(x), CC) == CC.frac_field(x) assert unify(ZZ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), 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.poly_ring(x)) == EX assert unify(EX, ZZ.frac_field(x)) == EX assert unify(EX, EX) == EX
def test_to_expr(): assert CC.to_expr(1 - 2j) == 1 - 2 * I
def test_CC_double(): assert CC(3.14).real > 1e-50 assert CC(1e-13).real > 1e-50 assert CC(1e-14).real > 1e-50 assert CC(1e-15).real > 1e-50 assert CC(1e-20).real > 1e-50 assert CC(1e-40).real > 1e-50 assert CC(3.14j).imag > 1e-50 assert CC(1e-13j).imag > 1e-50 assert CC(1e-14j).imag > 1e-50 assert CC(1e-15j).imag > 1e-50 assert CC(1e-20j).imag > 1e-50 assert CC(1e-40j).imag > 1e-50 a = CC(2.1 + 1j) assert a.numerator == a and a.denominator == 1
def test_to_expr(): assert CC.to_expr(1 - 2j) == 1 - 2*I