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___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()
def test_composite_option(): assert construct_domain({(1, ): sin(y)}, composite=False) == (EX, { (1, ): EX(sin(y)) }) assert construct_domain({(1, ): y}, composite=False) == (EX, { (1, ): EX(y) }) assert construct_domain({(1, 1): 1}, composite=False) == (ZZ, {(1, 1): 1}) assert construct_domain({(1, 0): y}, composite=False) == (EX, { (1, 0): EX(y) })
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))
def test_factor_list(): R, x = ring('x', FF(2)) assert (x**2 + 1).factor_list() == (1, [(x + 1, 2)]) R, x = ring('x', ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) 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 R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) 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', 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 assert f.factor_list() == (1.0, [(f, 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) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1), (x + t, 1)]) Rt, t = ring('t', QQ) R, x = ring('x', Rt) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x = ring('x', EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) R, x, y = ring('x y', FF(2)) pytest.raises(NotImplementedError, lambda: (x**2 + y**2).factor_list()) R, x, y = ring('x y', ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) assert (4 * x**2 * y + 4 * x * y**2).factor_list() == (4, [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring('x y', QQ) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) assert (x**2 * y / 2 + x * y**2 / 2).factor_list() == (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)]) 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)]) for method in ('trager', 'modular'): with using(aa_factor_method=method): assert f.factor_list() == r 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 assert f.factor_list() == (1.0, [(f, 1)]) Rt, t = ring('t', ZZ) R, x, y = ring('x y', Rt) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1), (x + t, 1)]) 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), []) assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x, y = ring('x y', EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) # 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, [(x + 0.1, 1), (0.1 * y + 0.1 * z, 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)]) 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)])
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)])
def test_sympyissue_11538(): assert construct_domain(E)[0] == ZZ.inject(E) assert (construct_domain(x**2 + 2 * x + E) == (ZZ.inject( x, E), ZZ.inject(x, E)(x**2 + 2 * x + E))) assert (construct_domain(x + y + GoldenRatio) == (EX, EX(x + y + GoldenRatio)))
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 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_EX(): assert EX.is_normal(EX(2)) is True assert EX.is_normal(EX(-1)) is False assert (EX(1) / 2).numerator == 1 assert (EX(1) / 2).denominator == 2