def test_composite(): assert Integer(-1).is_composite is False assert Integer(-2).is_composite is False assert Integer(-4).is_composite is False assert Integer(0).is_composite is False assert Integer(2).is_composite is False assert Integer(17).is_composite is False assert Integer(4).is_composite is True
def test_deltasummation_mul_add_x_y_add_y_kd(): assert ds((x + y) * (y + Kd(i, j)), (j, 1, 3)) == Piecewise( (3 * (x + y) * y + x + y, And(Integer(1) <= i, i <= 3)), (3 * (x + y) * y, True)) assert ds((x + y)*(y + Kd(i, j)), (j, 1, 1)) == \ Piecewise(((x + y)*y + x + y, Eq(i, 1)), ((x + y)*y, True)) assert ds((x + y)*(y + Kd(i, j)), (j, 2, 2)) == \ Piecewise(((x + y)*y + x + y, Eq(i, 2)), ((x + y)*y, True)) assert ds((x + y)*(y + Kd(i, j)), (j, 3, 3)) == \ Piecewise(((x + y)*y + x + y, Eq(i, 3)), ((x + y)*y, True)) assert ds((x + y) * (y + Kd(i, j)), (j, 1, k)) == Piecewise( (k * (x + y) * y + x + y, And(Integer(1) <= i, i <= k)), (k * (x + y) * y, True)) assert ds((x + y) * (y + Kd(i, j)), (j, k, 3)) == Piecewise( ((4 - k) * (x + y) * y + x + y, And(k <= i, i <= 3)), ((4 - k) * (x + y) * y, True)) assert ds((x + y) * (y + Kd(i, j)), (j, k, l)) == Piecewise( ((l - k + 1) * (x + y) * y + x + y, And(k <= i, i <= l)), ((l - k + 1) * (x + y) * y, True))
def test_deltasummation_mul_add_x_kd_add_y_kd(): assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, 3)) == piecewise_fold( Piecewise((Kd(i, k) + x, And(Integer(1) <= i, i <= 3)), (0, True)) + 3 * (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, 1)) == piecewise_fold( Piecewise((Kd(i, k) + x, Eq(i, 1)), (0, True)) + (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 2, 2)) == piecewise_fold( Piecewise((Kd(i, k) + x, Eq(i, 2)), (0, True)) + (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 3, 3)) == piecewise_fold( Piecewise((Kd(i, k) + x, Eq(i, 3)), (0, True)) + (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, k)) == piecewise_fold( Piecewise((Kd(i, k) + x, And(Integer(1) <= i, i <= k)), (0, True)) + k * (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, k, 3)) == piecewise_fold( Piecewise((Kd(i, k) + x, And(k <= i, i <= 3)), (0, True)) + (4 - k) * (Kd(i, k) + x) * y) assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, k, l)) == piecewise_fold( Piecewise((Kd(i, k) + x, And(k <= i, i <= l)), (0, True)) + (l - k + 1) * (Kd(i, k) + x) * y)
def test_sympyissue_6079(): # since x + 2.0 == x + 2 we can't do a simple equality test x = symbols('x') assert _aresame((x + 2.0).subs(2, 3), x + 2.0) assert _aresame((x + 2.0).subs(2.0, 3), x + 3) assert not _aresame(x + 2, x + 2.0) assert not _aresame(Basic(cos, 1), Basic(cos, 1.)) assert _aresame(cos, cos) assert not _aresame(1, Integer(1)) assert not _aresame(x, symbols('x', positive=True))
def test_to_number_field(): A = QQ.algebraic_field(sqrt(2)) assert A.convert(sqrt(2)) == A([1, 0]) B = QQ.algebraic_field(sqrt(2), sqrt(3)) assert B.convert(sqrt(2) + sqrt(3)) == B([1, 0]) K = QQ.algebraic_field(sqrt(2) + sqrt(3)) a = K([Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)]) assert B.from_expr(sqrt(2)) == a pytest.raises(CoercionFailed, lambda: QQ.algebraic_field(sqrt(3)).convert(sqrt(2))) p = x**6 - 6*x**4 - 6*x**3 + 12*x**2 - 36*x + 1 r0, r1 = p.as_poly(x).all_roots()[:2] A = QQ.algebraic_field(r0) a = A([Rational(-96, 755), Rational(-54, 755), Rational(128, 151), Rational(936, 755), Rational(-1003, 755), Rational(2184, 755)]) assert A.from_expr(r1) == a
def test_clear_coefficients(): assert clear_coefficients(4*y*(6*x + 3)) == (y*(2*x + 1), 0) assert clear_coefficients(4*y*(6*x + 3) - 2) == (y*(2*x + 1), Rational(1, 6)) assert clear_coefficients(4*y*(6*x + 3) - 2, x) == (y*(2*x + 1), x/12 + Rational(1, 6)) assert clear_coefficients(sqrt(2) - 2) == (sqrt(2), 2) assert clear_coefficients(4*sqrt(2) - 2) == (sqrt(2), Rational(1, 2)) assert clear_coefficients(Integer(3), x) == (0, x - 3) assert clear_coefficients(oo, x) == (oo, x) assert clear_coefficients(-pi, x) == (pi, -x) assert clear_coefficients(2 - pi/3, x) == (pi, -3*x + 6)
def test_Rational(): n1 = Rational(1, 4) n2 = Rational(1, 3) n3 = Rational(2, 4) n4 = Rational(2, -4) n5 = Integer(0) n7 = Integer(3) n8 = Integer(-3) assert str(n1 * n2) == "1/12" assert str(n1 * n2) == "1/12" assert str(n3) == "1/2" assert str(n1 * n3) == "1/8" assert str(n1 + n3) == "3/4" assert str(n1 + n2) == "7/12" assert str(n1 + n4) == "-1/4" assert str(n4 * n4) == "1/4" assert str(n4 + n2) == "-1/6" assert str(n4 + n5) == "-1/2" assert str(n4 * n5) == "0" assert str(n3 + n4) == "0" assert str(n1**n7) == "1/64" assert str(n2**n7) == "1/27" assert str(n2**n8) == "27" assert str(n7**n8) == "1/27" assert str(Rational("-25")) == "-25" assert str(Rational("1.25")) == "5/4" assert str(Rational("-2.6e-2")) == "-13/500" assert str(Rational(25, 7)) == "25/7" assert str(Rational(-123, 569)) == "-123/569" assert str(sqrt(Rational(1, 4))) == "1/2" assert str(sqrt(Rational(1, 36))) == "1/6" assert str(root(123**25, 25)) == "123" assert str(root(123**25 + 1, 25)) != "123" assert str(root(123**25 - 1, 25)) != "123" assert str(root(123**25 - 1, 25)) != "122" assert str(sqrt(Rational(81, 36))**3) == "27/8" assert str(1 / sqrt(Rational(81, 36))**3) == "8/27" assert str(sqrt(-4)) == str(2 * I) assert str(root(2, 10**10)) == "2**(1/10000000000)"
def test_as_poly_1t(): assert as_poly_1t(2/t + t, t, z) in [ Poly(t + 2*z, t, z), Poly(t + 2*z, z, t)] assert as_poly_1t(2/t + 3/t**2, t, z) in [ Poly(2*z + 3*z**2, t, z), Poly(2*z + 3*z**2, z, t)] assert as_poly_1t(2/((exp(2) + 1)*t), t, z) in [ Poly(2/(exp(2) + 1)*z, t, z), Poly(2/(exp(2) + 1)*z, z, t)] assert as_poly_1t(2/((exp(2) + 1)*t) + t, t, z) in [ Poly(t + 2/(exp(2) + 1)*z, t, z), Poly(t + 2/(exp(2) + 1)*z, z, t)] assert as_poly_1t(Integer(0), t, z) == Poly(0, t, z)
def test_Rational(): n1 = Rational(1, 4) n2 = Rational(1, 3) n3 = Rational(2, 4) n4 = Rational(2, -4) n5 = Integer(0) n7 = Integer(3) n8 = Integer(-3) assert str(n1 * n2) == '1/12' assert str(n1 * n2) == '1/12' assert str(n3) == '1/2' assert str(n1 * n3) == '1/8' assert str(n1 + n3) == '3/4' assert str(n1 + n2) == '7/12' assert str(n1 + n4) == '-1/4' assert str(n4 * n4) == '1/4' assert str(n4 + n2) == '-1/6' assert str(n4 + n5) == '-1/2' assert str(n4 * n5) == '0' assert str(n3 + n4) == '0' assert str(n1**n7) == '1/64' assert str(n2**n7) == '1/27' assert str(n2**n8) == '27' assert str(n7**n8) == '1/27' assert str(Rational('-25')) == '-25' assert str(Rational('1.25')) == '5/4' assert str(Rational('-2.6e-2')) == '-13/500' assert str(Rational(25, 7)) == '25/7' assert str(Rational(-123, 569)) == '-123/569' assert str(sqrt(Rational(1, 4))) == '1/2' assert str(sqrt(Rational(1, 36))) == '1/6' assert str(root(123**25, 25)) == '123' assert str(root(123**25 + 1, 25)) != '123' assert str(root(123**25 - 1, 25)) != '123' assert str(root(123**25 - 1, 25)) != '122' assert str(sqrt(Rational(81, 36))**3) == '27/8' assert str(1 / sqrt(Rational(81, 36))**3) == '8/27' assert str(sqrt(-4)) == str(2 * I) assert str(root(2, 10**10)) == '2**(1/10000000000)'
def test_compare(): assert compare(Integer(2), x, x) < 0 assert compare(x, exp(x), x) < 0 assert compare(exp(x), exp(x**2), x) < 0 assert compare(exp(x**2), exp(exp(x)), x) < 0 assert compare(Integer(1), exp(exp(x)), x) < 0 assert compare(x, Integer(2), x) > 0 assert compare(exp(x), x, x) > 0 assert compare(exp(x**2), exp(x), x) > 0 assert compare(exp(exp(x)), exp(x**2), x) > 0 assert compare(exp(exp(x)), Integer(1), x) > 0 assert compare(Integer(2), Integer(3), x) == 0 assert compare(Integer(3), Integer(-5), x) == 0 assert compare(Integer(2), Integer(-5), x) == 0 assert compare(x, x**2, x) == 0 assert compare(x**2, x**3, x) == 0 assert compare(x**3, 1 / x, x) == 0 assert compare(1 / x, x**m, x) == 0 assert compare(x**m, -x, x) == 0 assert compare(exp(x), exp(-x), x) == 0 assert compare(exp(-x), exp(2 * x), x) == 0 assert compare(exp(2 * x), exp(x)**2, x) == 0 assert compare(exp(x)**2, exp(x + exp(-x)), x) == 0 assert compare(exp(x), exp(x + exp(-x)), x) == 0 assert compare(exp(x**2), 1 / exp(x**2), x) == 0 assert compare(exp(x), x**5, x) > 0 assert compare(exp(x**2), exp(x)**2, x) > 0 assert compare(exp(x), exp(x + exp(-x)), x) == 0 assert compare(exp(x + exp(-x)), exp(x), x) == 0 assert compare(exp(x + exp(-x)), exp(-x), x) == 0 assert compare(exp(-x), x, x) > 0 assert compare(x, exp(-x), x) < 0 assert compare(exp(x + 1 / x), x, x) > 0 assert compare(exp(-exp(x)), exp(x), x) > 0 assert compare(exp(exp(-exp(x)) + x), exp(-exp(x)), x) < 0 assert compare(exp(exp(x)), exp(x + exp(-exp(x))), x) > 0
def test_rationals(): Q = S.Rationals assert 5 in Q assert -5 in Q assert Rational(1, 3) in Q assert pi not in Q assert Q.contains(x) == Contains(x, Q, evaluate=False) assert Q.inf == -oo assert Q.sup == oo assert Q.boundary == Q assert (list(itertools.islice(S.Rationals, 17)) == [Integer(0), Integer(1), Rational(1, 2), Rational(1, 3), Integer(-1), Rational(-1, 2), Rational(-1, 3), Rational(1, 4), Rational(-1, 4), Integer(2), Rational(2, 3), Rational(1, 5), Rational(-1, 5), Rational(2, 5), Integer(-2), Rational(-2, 3), Rational(-2, 5)])
def test_airybiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == 3**Rational(1, 6) / gamma(Rational(1, 3)) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z * airybi(z) assert series(airybiprime(z), z, 0, 3) == (3**Rational(1, 6) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * z**2 / (6 * gamma(Rational(2, 3))) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**Rational(5, 6) * z**2 * hyper((), (Rational(5, 3), ), z**Integer(3) / 9) / (6 * gamma(Rational(2, 3))) + 3**Rational(1, 6) * hyper( (), (Rational(1, 3), ), z**Integer(3) / 9) / gamma(Rational(1, 3))) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert airyai(t).rewrite(besselj) == ( sqrt(-t) * (besselj(-Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3) + besselj(Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3) * (z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) / (z**Rational(3, 2))**Rational(2, 3) + (z**Rational(3, 2))**Rational(2, 3) * besseli(-Rational(2, 3), 2 * z**Rational(3, 2) / 3)) / 3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3) * p * (besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert expand_func(airybiprime(2 * (3 * z**5)**Rational(1, 3))) == ( sqrt(3) * (z**Rational(5, 3) / (z**5)**Rational(1, 3) - 1) * airyaiprime(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 2 + (z**Rational(5, 3) / (z**5)**Rational(1, 3) + 1) * airybiprime(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 2)
def test_basics(): assert Integral(0, x) != 0 assert Integral(x, (x, 1, 1)) != 0 assert Integral(oo, x) != oo assert Integral(S.NaN, x) == S.NaN assert diff(Integral(y, y), x) == 0 assert diff(Integral(x, (x, 0, 1)), x) == 0 assert diff(Integral(x, x), x) == x assert diff(Integral(t, (t, 0, x)), x) == x + Integral(0, (t, 0, x)) e = (t + 1)**2 assert diff(integrate(e, (t, 0, x)), x) == \ diff(Integral(e, (t, 0, x)), x).doit().expand() == \ ((1 + x)**2).expand() assert diff(integrate(e, (t, 0, x)), t) == \ diff(Integral(e, (t, 0, x)), t) == 0 assert diff(integrate(e, (t, 0, x)), a) == \ diff(Integral(e, (t, 0, x)), a) == 0 assert diff(integrate(e, t), a) == diff(Integral(e, t), a) == 0 assert integrate(e, (t, a, x)).diff(x) == \ Integral(e, (t, a, x)).diff(x).doit().expand() assert Integral(e, (t, a, x)).diff(x).doit() == ((1 + x)**2) assert integrate(e, (t, x, a)).diff(x).doit() == (-(1 + x)**2).expand() assert integrate(t**2, (t, x, 2 * x)).diff(x) == 7 * x**2 assert Integral(x, x).atoms() == {x} assert Integral(f(x), (x, 0, 1)).atoms() == {Integer(0), Integer(1), x} assert diff_test(Integral(x, (x, 3 * y))) == {y} assert diff_test(Integral(x, (a, 3 * y))) == {x, y} assert integrate(x, (x, oo, oo)) == 0 # issue 8171 assert integrate(x, (x, -oo, -oo)) == 0 # sum integral of terms assert integrate(y + x + exp(x), x) == x * y + x**2 / 2 + exp(x) assert Integral(x).is_commutative n = Symbol('n', commutative=False) assert Integral(n + x, x).is_commutative is False
def test_subs(): n3 = Integer(3) e = x e = e.subs({x: n3}) assert e == 3 e = 2 * x assert e == 2 * x e = e.subs({x: n3}) assert e == 6
def test_mul(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) t = TensMul.from_data(Integer(1), [], [], []) assert str(t) == '1' A, B = tensorhead('A B', [Lorentz]*2, [[1]*2]) t = (1 + x)*A(a, b) assert str(t) == '(x + 1)*A(a, b)' assert t.types == [Lorentz] assert t.rank == 2 assert t.dum == [] assert t.coeff == 1 + x assert sorted(t.free) == [(a, 0, 0), (b, 1, 0)] assert t.components == [A] ts = A(a, b) assert str(ts) == 'A(a, b)' assert ts.types == [Lorentz] assert ts.rank == 2 assert ts.dum == [] assert ts.coeff == 1 assert sorted(ts.free) == [(a, 0, 0), (b, 1, 0)] assert ts.components == [A] t = A(-b, a)*B(-a, c)*A(-c, d) t1 = tensor_mul(*t.split()) assert t == t(-b, d) assert t == t1 assert tensor_mul(*[]) == TensMul.from_data(Integer(1), [], [], []) t = TensMul.from_data(1, [], [], []) zsym = tensorsymmetry() typ = TensorType([], zsym) C = typ('C') assert str(C()) == 'C' assert str(t) == '1' assert t.split()[0] == t pytest.raises(ValueError, lambda: TIDS.free_dum_from_indices(a, a)) pytest.raises(ValueError, lambda: TIDS.free_dum_from_indices(-a, -a)) pytest.raises(ValueError, lambda: A(a, b)*A(a, c)) t = A(a, b)*A(-a, c) pytest.raises(ValueError, lambda: t(a, b, c))
def test_dmp_from_diofant(): assert dmp_from_diofant([Integer(1), Integer(2)], 0, ZZ) == [ZZ(1), ZZ(2)] assert dmp_from_diofant([Rational(1, 2), Integer(3)], 0, QQ) == [QQ(1, 2), QQ(3, 1)] assert dmp_from_diofant([[Integer(1), Integer(2)], [Integer(0)]], 1, ZZ) == [[ZZ(1), ZZ(2)], []] assert dmp_from_diofant([[Rational(1, 2), Integer(2)]], 1, QQ) == [[QQ(1, 2), QQ(2, 1)]]
def test_pde_1st_linear_constant_coeff(): f, F = map(Function, ['f', 'F']) u = f(x, y) eq = -2 * u.diff(x) + 4 * u.diff(y) + 5 * u - exp(x + 3 * y) sol = pdsolve(eq) assert sol == Eq(f( x, y), (F(4 * x + 2 * y) + exp(x / Integer(2) + 4 * y) / Integer(15)) * exp(x / Integer(2) - y)) assert classify_pde(eq) == ('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral') assert checkpdesol(eq, sol)[0] eq = (u.diff(x) / u) + (u.diff(y) / u) + 1 - (exp(x + y) / u) sol = pdsolve(eq) assert sol == Eq(f(x, y), F(x - y) * exp(-x / 2 - y / 2) + exp(x + y) / Integer(3)) assert classify_pde(eq) == ('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral') assert checkpdesol(eq, sol)[0] eq = 2 * u + -u.diff(x) + 3 * u.diff(y) + sin(x) sol = pdsolve(eq) assert sol == Eq( f(x, y), F(3 * x + y) * exp(x / Integer(5) - 3 * y / Integer(5)) - 2 * sin(x) / Integer(5) - cos(x) / Integer(5)) assert classify_pde(eq) == ('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral') assert checkpdesol(eq, sol)[0] eq = u + u.diff(x) + u.diff(y) + x * y sol = pdsolve(eq) assert sol == Eq( f(x, y), -x * y + x + y + F(x - y) * exp(-x / Integer(2) - y / Integer(2)) - 2) assert classify_pde(eq) == ('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral') assert checkpdesol(eq, sol)[0] eq = u + u.diff(x) + u.diff(y) + log(x) assert classify_pde(eq) == ('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral')
def test_assemble_partfrac_list(): f = 36 / (x**5 - 2 * x**4 - 2 * x**3 + 4 * x**2 + x - 2) pfd = apart_list(f) assert assemble_partfrac_list( pfd) == -4 / (x + 1) - 3 / (x + 1)**2 - 9 / (x - 1)**2 + 4 / (x - 2) a = Dummy('a') pfd = (1, Integer(0).as_poly(x), [([sqrt(2), -sqrt(2)], Lambda(a, a / 2), Lambda(a, -a + x), 1)]) assert assemble_partfrac_list( pfd) == -1 / (sqrt(2) * (x + sqrt(2))) + 1 / (sqrt(2) * (x - sqrt(2)))
def test_expand_complex(): assert ((2 + 3 * I)**10).expand(complex=True) == -341525 - 145668 * I # the following two tests are to ensure the Diofant uses an efficient # algorithm for calculating powers of complex numbers. They should execute # in something like 0.01s. assert ((2 + 3*I)**1000).expand(complex=True) == \ -81079464736246615951519029367296227340216902563389546989376269312984127074385455204551402940331021387412262494620336565547972162814110386834027871072723273110439771695255662375718498785908345629702081336606863762777939617745464755635193139022811989314881997210583159045854968310911252660312523907616129080027594310008539817935736331124833163907518549408018652090650537035647520296539436440394920287688149200763245475036722326561143851304795139005599209239350981457301460233967137708519975586996623552182807311159141501424576682074392689622074945519232029999 + \ 46938745946789557590804551905243206242164799136976022474337918748798900569942573265747576032611189047943842446167719177749107138603040963603119861476016947257034472364028585381714774667326478071264878108114128915685688115488744955550920239128462489496563930809677159214598114273887061533057125164518549173898349061972857446844052995037423459472376202251620778517659247970283904820245958198842631651569984310559418135975795868314764489884749573052997832686979294085577689571149679540256349988338406458116270429842222666345146926395233040564229555893248370000*I assert ((2 + 3*I/4)**1000).expand(complex=True) == \ Integer(1)*37079892761199059751745775382463070250205990218394308874593455293485167797989691280095867197640410033222367257278387021789651672598831503296531725827158233077451476545928116965316544607115843772405184272449644892857783761260737279675075819921259597776770965829089907990486964515784097181964312256560561065607846661496055417619388874421218472707497847700629822858068783288579581649321248495739224020822198695759609598745114438265083593711851665996586461937988748911532242908776883696631067311443171682974330675406616373422505939887984366289623091300746049101284856530270685577940283077888955692921951247230006346681086274961362500646889925803654263491848309446197554307105991537357310209426736453173441104334496173618419659521888945605315751089087820455852582920963561495787655250624781448951403353654348109893478206364632640344111022531861683064175862889459084900614967785405977231549003280842218501570429860550379522498497412180001/114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376 + \ I*421638390580169706973991429333213477486930178424989246669892530737775352519112934278994501272111385966211392610029433824534634841747911783746811994443436271013377059560245191441549885048056920190833693041257216263519792201852046825443439142932464031501882145407459174948712992271510309541474392303461939389368955986650538525895866713074543004916049550090364398070215427272240155060576252568700906004691224321432509053286859100920489253598392100207663785243368195857086816912514025693453058403158416856847185079684216151337200057494966741268925263085619240941610301610538225414050394612058339070756009433535451561664522479191267503989904464718368605684297071150902631208673621618217106272361061676184840810762902463998065947687814692402219182668782278472952758690939877465065070481351343206840649517150634973307937551168752642148704904383991876969408056379195860410677814566225456558230131911142229028179902418223009651437985670625/1793954211366022694113801876840128100034871409513586250746316776290259783425578615401030447369541046747571819748417910583511123376348523955353017744010395602173906080395504375010762174191250701116076984219741972574712741619474818186676828531882286780795390571221287481389759837587864244524002565968286448146002639202882164150037179450123657170327105882819203167448541028601906377066191895183769810676831353109303069033234715310287563158747705988305326397404720186258671215368588625611876280581509852855552819149745718992630449787803625851701801184123166018366180137512856918294030710215034138299203584 assert ((2 + 3*I)**-1000).expand(complex=True) == \ Integer(1)*-81079464736246615951519029367296227340216902563389546989376269312984127074385455204551402940331021387412262494620336565547972162814110386834027871072723273110439771695255662375718498785908345629702081336606863762777939617745464755635193139022811989314881997210583159045854968310911252660312523907616129080027594310008539817935736331124833163907518549408018652090650537035647520296539436440394920287688149200763245475036722326561143851304795139005599209239350981457301460233967137708519975586996623552182807311159141501424576682074392689622074945519232029999/8777125472973511649630750050295188683351430110097915876250894978429797369155961290321829625004920141758416719066805645579710744290541337680113772670040386863849283653078324415471816788604945889094925784900885812724984087843737442111926413818245854362613018058774368703971604921858023116665586358870612944209398056562604561248859926344335598822815885851096698226775053153403320782439987679978321289537645645163767251396759519805603090332694449553371530571613352311006350058217982509738362083094920649452123351717366337410243853659113315547584871655479914439219520157174729130746351059075207407866012574386726064196992865627149566238044625779078186624347183905913357718850537058578084932880569701242598663149911276357125355850792073635533676541250531086757377369962506979378337216411188347761901006460813413505861461267545723590468627854202034450569581626648934062198718362303420281555886394558137408159453103395918783625713213314350531051312551733021627153081075080140680608080529736975658786227362251632725009435866547613598753584705455955419696609282059191031962604169242974038517575645939316377801594539335940001 - Integer(1)*46938745946789557590804551905243206242164799136976022474337918748798900569942573265747576032611189047943842446167719177749107138603040963603119861476016947257034472364028585381714774667326478071264878108114128915685688115488744955550920239128462489496563930809677159214598114273887061533057125164518549173898349061972857446844052995037423459472376202251620778517659247970283904820245958198842631651569984310559418135975795868314764489884749573052997832686979294085577689571149679540256349988338406458116270429842222666345146926395233040564229555893248370000*I/8777125472973511649630750050295188683351430110097915876250894978429797369155961290321829625004920141758416719066805645579710744290541337680113772670040386863849283653078324415471816788604945889094925784900885812724984087843737442111926413818245854362613018058774368703971604921858023116665586358870612944209398056562604561248859926344335598822815885851096698226775053153403320782439987679978321289537645645163767251396759519805603090332694449553371530571613352311006350058217982509738362083094920649452123351717366337410243853659113315547584871655479914439219520157174729130746351059075207407866012574386726064196992865627149566238044625779078186624347183905913357718850537058578084932880569701242598663149911276357125355850792073635533676541250531086757377369962506979378337216411188347761901006460813413505861461267545723590468627854202034450569581626648934062198718362303420281555886394558137408159453103395918783625713213314350531051312551733021627153081075080140680608080529736975658786227362251632725009435866547613598753584705455955419696609282059191031962604169242974038517575645939316377801594539335940001 assert ((2 + 3*I/4)**-1000).expand(complex=True) == \ Integer(1)*4257256305661027385394552848555894604806501409793288342610746813288539790051927148781268212212078237301273165351052934681382567968787279534591114913777456610214738290619922068269909423637926549603264174216950025398244509039145410016404821694746262142525173737175066432954496592560621330313807235750500564940782099283410261748370262433487444897446779072067625787246390824312580440138770014838135245148574339248259670887549732495841810961088930810608893772914812838358159009303794863047635845688453859317690488124382253918725010358589723156019888846606295866740117645571396817375322724096486161308083462637370825829567578309445855481578518239186117686659177284332344643124760453112513611749309168470605289172320376911472635805822082051716625171429727162039621902266619821870482519063133136820085579315127038372190224739238686708451840610064871885616258831386810233957438253532027049148030157164346719204500373766157143311767338973363806106967439378604898250533766359989107510507493549529158818602327525235240510049484816090584478644771183158342479140194633579061295740839490629457435283873180259847394582069479062820225159699506175855369539201399183443253793905149785994830358114153241481884290274629611529758663543080724574566578220908907477622643689220814376054314972190402285121776593824615083669045183404206291739005554569305329760211752815718335731118664756831942466773261465213581616104242113894521054475516019456867271362053692785300826523328020796670205463390909136593859765912483565093461468865534470710132881677639651348709376/2103100954337624833663208713697737151593634525061637972297915388685604042449504336765884978184588688426595940401280828953096857809292320006227881797146858511436638446932833617514351442216409828605662238790280753075176269765767010004889778647709740770757817960711900340755635772183674511158570690702969774966791073165467918123298694584729211212414462628433370481195120564586361368504153395406845170075275051749019600057116719726628746724489572189061061036426955163696859127711110719502594479795200686212257570291758725259007379710596548777812659422174199194837355646482046783616494013289495563083118517507178847555801163089723056310287760875135196081975602765511153122381201303871673391366630940702817360340900568748719988954847590748960761446218262344767250783946365392689256634180417145926390656439421745644011831124277463643383712803287985472471755648426749842410972650924240795946699346613614779460399530274263580007672855851663196114585312432954432654691485867618908420370875753749297487803461900447407917655296784879220450937110470920633595689721819488638484547259978337741496090602390463594556401615298457456112485536498177883358587125449801777718900375736758266215245325999241624148841915093787519330809347240990363802360596034171167818310322276373120180985148650099673289383722502488957717848531612020897298448601714154586319660314294591620415272119454982220034319689607295960162971300417552364254983071768070124456169427638371140064235083443242844616326538396503937972586505546495649094344512270582463639152160238137952390380581401171977159154009407415523525096743009110916334144716516647041176989758534635251844947906038080852185583742296318878233394998111078843229681030277039104786225656992262073797524057992347971177720807155842376332851559276430280477639539393920006008737472164850104411971830120295750221200029811143140323763349636629725073624360001 - Integer(1)*3098214262599218784594285246258841485430681674561917573155883806818465520660668045042109232930382494608383663464454841313154390741655282039877410154577448327874989496074260116195788919037407420625081798124301494353693248757853222257918294662198297114746822817460991242508743651430439120439020484502408313310689912381846149597061657483084652685283853595100434135149479564507015504022249330340259111426799121454516345905101620532787348293877485702600390665276070250119465888154331218827342488849948540687659846652377277250614246402784754153678374932540789808703029043827352976139228402417432199779415751301480406673762521987999573209628597459357964214510139892316208670927074795773830798600837815329291912002136924506221066071242281626618211060464126372574400100990746934953437169840312584285942093951405864225230033279614235191326102697164613004299868695519642598882914862568516635347204441042798206770888274175592401790040170576311989738272102077819127459014286741435419468254146418098278519775722104890854275995510700298782146199325790002255362719776098816136732897323406228294203133323296591166026338391813696715894870956511298793595675308998014158717167429941371979636895553724830981754579086664608880698350866487717403917070872269853194118364230971216854931998642990452908852258008095741042117326241406479532880476938937997238098399302185675832474590293188864060116934035867037219176916416481757918864533515526389079998129329045569609325290897577497835388451456680707076072624629697883854217331728051953671643278797380171857920000*I/2103100954337624833663208713697737151593634525061637972297915388685604042449504336765884978184588688426595940401280828953096857809292320006227881797146858511436638446932833617514351442216409828605662238790280753075176269765767010004889778647709740770757817960711900340755635772183674511158570690702969774966791073165467918123298694584729211212414462628433370481195120564586361368504153395406845170075275051749019600057116719726628746724489572189061061036426955163696859127711110719502594479795200686212257570291758725259007379710596548777812659422174199194837355646482046783616494013289495563083118517507178847555801163089723056310287760875135196081975602765511153122381201303871673391366630940702817360340900568748719988954847590748960761446218262344767250783946365392689256634180417145926390656439421745644011831124277463643383712803287985472471755648426749842410972650924240795946699346613614779460399530274263580007672855851663196114585312432954432654691485867618908420370875753749297487803461900447407917655296784879220450937110470920633595689721819488638484547259978337741496090602390463594556401615298457456112485536498177883358587125449801777718900375736758266215245325999241624148841915093787519330809347240990363802360596034171167818310322276373120180985148650099673289383722502488957717848531612020897298448601714154586319660314294591620415272119454982220034319689607295960162971300417552364254983071768070124456169427638371140064235083443242844616326538396503937972586505546495649094344512270582463639152160238137952390380581401171977159154009407415523525096743009110916334144716516647041176989758534635251844947906038080852185583742296318878233394998111078843229681030277039104786225656992262073797524057992347971177720807155842376332851559276430280477639539393920006008737472164850104411971830120295750221200029811143140323763349636629725073624360001 a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert exp(a*(2 + I*b)).expand(complex=True) == \ I*exp(2*a)*sin(a*b) + exp(2*a)*cos(a*b)
def test_to_number_field(): assert to_number_field(sqrt(2)) == AlgebraicNumber(sqrt(2)) assert to_number_field([sqrt(2), sqrt(3)]) == AlgebraicNumber(sqrt(2) + sqrt(3)) a = AlgebraicNumber( sqrt(2) + sqrt(3), [Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)]) assert to_number_field(sqrt(2), sqrt(2) + sqrt(3)) == a assert to_number_field(sqrt(2), AlgebraicNumber(sqrt(2) + sqrt(3))) == a pytest.raises(IsomorphismFailed, lambda: to_number_field(sqrt(2), sqrt(3))) # issue sympy/sympy#5649 assert AlgebraicNumber(1).rep == to_number_field(1, AlgebraicNumber(1)).rep assert AlgebraicNumber(sqrt(2)).rep == to_number_field( sqrt(2), AlgebraicNumber(sqrt(2))).rep
def test_monomial_key(): assert monomial_key() == lex assert monomial_key('lex') == lex assert monomial_key('grlex') == grlex assert monomial_key('grevlex') == grevlex pytest.raises(ValueError, lambda: monomial_key('foo')) pytest.raises(ValueError, lambda: monomial_key(1)) M = [ x, x**2 * z**2, x * y, x**2, Integer(1), y**2, x**3, y, z, x * y**2 * z, x**2 * y**2 ] assert sorted(M, key=monomial_key('lex', [z, y, x])) == \ [Integer(1), x, x**2, x**3, y, x*y, y**2, x**2*y**2, z, x*y**2*z, x**2*z**2] assert sorted(M, key=monomial_key('grlex', [z, y, x])) == \ [Integer(1), x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x*y**2*z, x**2*z**2] assert sorted(M, key=monomial_key('grevlex', [z, y, x])) == \ [Integer(1), x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x**2*z**2, x*y**2*z]
def test_nan_equality_exceptions(): # See issue sympy/sympy#7774 assert Equality(nan, nan) is S.false assert Unequality(nan, nan) is S.true # See issue sympy/sympy#7773 A = (x, Integer(0), Rational(1, 3), pi, oo, -oo) assert Equality(nan, random.choice(A)) is S.false assert Equality(random.choice(A), nan) is S.false assert Unequality(nan, random.choice(A)) is S.true assert Unequality(random.choice(A), nan) is S.true
def test_beta(): a, b = symbols('alpha beta', positive=True) B = Beta('x', a, b) assert pspace(B).domain.set == Interval(0, 1) dens = density(B) x = Symbol('x') assert dens(x) == x**(a - 1)*(1 - x)**(b - 1) / beta(a, b) # This is too slow # assert E(B) == a / (a + b) # assert variance(B) == (a*b) / ((a+b)**2 * (a+b+1)) # Full symbolic solution is too much, test with numeric version a, b = Integer(1), Integer(2) B = Beta('x', a, b) assert expand_func(E(B)) == a/(a + b) assert expand_func(variance(B)) == (a*b)/(a + b)**2/(a + b + 1)
def test_symbol(): a, b, c, p, q = map(Wild, 'abcpq') e = x assert e.match(x) == {} assert e.match(a) == {a: x} e = Integer(5) assert e.match(c) == {c: 5} assert e.match(e) == {} assert e.match(e + 1) is None
def test_mathml_tuples(): mml_1 = mp._print([2]) assert mml_1.nodeName == 'list' assert mml_1.childNodes[0].nodeName == 'cn' assert len(mml_1.childNodes) == 1 mml_2 = mp._print([2, Integer(1)]) assert mml_2.nodeName == 'list' assert mml_2.childNodes[0].nodeName == 'cn' assert mml_2.childNodes[1].nodeName == 'cn' assert len(mml_2.childNodes) == 2
def test_count_ops_non_visual(): def count(val): return count_ops(val, visual=False) assert count(x) == 0 assert count(x) is not Integer(0) assert count(x + y) == 1 assert count(x + y) is not Integer(1) assert count(x + y*x + 2*y) == 4 assert count({x + y: x}) == 1 assert count({x + y: 2 + x}) is not Integer(1) assert count(Or(x, y)) == 1 assert count(And(x, y)) == 1 assert count(Not(x)) == 1 assert count(Nor(x, y)) == 2 assert count(Nand(x, y)) == 2 assert count(Xor(x, y)) == 1 assert count(Implies(x, y)) == 1 assert count(Equivalent(x, y)) == 1 assert count(ITE(x, y, z)) == 1 assert count(ITE(True, x, y)) == 0
def test_expand_non_commutative(): A = Symbol('A', commutative=False) B = Symbol('B', commutative=False) C = Symbol('C', commutative=False) a = Symbol('a') b = Symbol('b') i = Symbol('i', integer=True) n = Symbol('n', negative=True) m = Symbol('m', negative=True) p = Symbol('p', polar=True) np = Symbol('p', polar=False) assert (C * (A + B)).expand() == C * A + C * B assert (C * (A + B)).expand() != A * C + B * C assert ((A + B)**2).expand() == A**2 + A * B + B * A + B**2 assert ((A + B)**3).expand() == (A**2 * B + B**2 * A + A * B**2 + B * A**2 + A**3 + B**3 + A * B * A + B * A * B) # issue 6219 assert ((a * A * B * A**-1)**2).expand() == a**2 * A * B**2 / A # Note that (a*A*B*A**-1)**2 is automatically converted to a**2*(A*B*A**-1)**2 assert ((a * A * B * A**-1)**2).expand(deep=False) == a**2 * (A * B * A**-1)**2 assert ((a * A * B * A**-1)**2).expand() == a**2 * (A * B**2 * A**-1) assert ((a * A * B * A**-1)**2).expand(force=True) == a**2 * A * B**2 * A**(-1) assert ((a * A * B)**2).expand() == a**2 * A * B * A * B assert ((a * A)**2).expand() == a**2 * A**2 assert ((a * A * B)**i).expand() == a**i * (A * B)**i assert ((a * A * (B * (A * B / A)**2))**i).expand() == a**i * (A * B * A * B**2 / A)**i # issue 6558 assert (A * B * (A * B)**-1).expand() == A * B * (A * B)**-1 assert ((a * A)**i).expand() == a**i * A**i assert ((a * A * B * A**-1)**3).expand() == a**3 * A * B**3 / A assert ((a*A*B*A*B/A)**3).expand() == \ a**3*A*B*(A*B**2)*(A*B**2)*A*B*A**(-1) assert ((a*A*B*A*B/A)**-3).expand() == \ a**-3*(A*B*(A*B**2)*(A*B**2)*A*B*A**(-1))**-1 assert ((a * b * A * B * A**-1)**i).expand() == a**i * b**i * (A * B / A)**i assert ((a * (a * b)**i)**i).expand() == a**i * a**(i**2) * b**(i**2) e = Pow(Mul(a, 1 / a, A, B, evaluate=False), Integer(2), evaluate=False) assert e.expand() == A * B * A * B assert sqrt(a * (A * b)**i).expand() == sqrt(a * b**i * A**i) assert (sqrt(-a)**a).expand() == sqrt(-a)**a assert expand((-2 * n)**(i / 3)) == 2**(i / 3) * (-n)**(i / 3) assert expand( (-2 * n * m)**(i / a)) == (-2)**(i / a) * (-n)**(i / a) * (-m)**(i / a) assert expand((-2 * a * p)**b) == 2**b * p**b * (-a)**b assert expand((-2 * a * np)**b) == 2**b * (-a * np)**b assert expand(sqrt(A * B)) == sqrt(A * B) assert expand(sqrt(-2 * a * b)) == sqrt(2) * sqrt(-a * b)
def test_solve_poly_system(): assert solve_poly_system([x - 1], x) == [(S.One,)] pytest.raises(ComputationFailed, lambda: solve_poly_system([0, 1])) assert solve_poly_system([y - x, y - x - 1], x, y) is None assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)] assert solve_poly_system([2*x - 3, 3*y/2 - 2*x, z - 5*y], x, y, z) == \ [(Rational(3, 2), Integer(2), Integer(10))] assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \ [(0, 0), (2, -sqrt(2)), (2, sqrt(2))] assert solve_poly_system([x*y - 2*y, 2*y**2 - x**3], x, y) == \ [(0, 0), (2, -2), (2, 2)] assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \ [(-I*sqrt(S.Half), -S.Half), (I*sqrt(S.Half), -S.Half)] f_1 = x**2 + y + z - 1 f_2 = x + y**2 + z - 1 f_3 = x + y + z**2 - 1 a, b = sqrt(2) - 1, -sqrt(2) - 1 assert solve_poly_system([f_1, f_2, f_3], x, y, z) == \ [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)] solution = [(1, -1), (1, 1)] assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution assert solve_poly_system([x**2 - y**2, x - 1]) == solution assert solve_poly_system( [x + x*y - 3, y + x*y - 4], x, y) == [(-3, -2), (1, 2)] pytest.raises(NotImplementedError, lambda: solve_poly_system([x**3 - y**3], x, y)) pytest.raises(PolynomialError, lambda: solve_poly_system([1/x], x))
def test_sign(): assert mrv_sign(Integer(0), x) == 0 assert mrv_sign(Integer(3), x) == 1 assert mrv_sign(Integer(-5), x) == -1 assert mrv_sign(log(x), x) == 1 assert mrv_sign(exp(-x), x) == 1 assert mrv_sign(exp(x), x) == 1 assert mrv_sign(-exp(x), x) == -1 assert mrv_sign(3 - 1 / x, x) == 1 assert mrv_sign(-3 - 1 / x, x) == -1 assert mrv_sign(sin(1 / x), x) == 1 assert mrv_sign(x**2, x) == 1 assert mrv_sign(x**5, x) == 1 assert mrv_sign(x, x) == 1 assert mrv_sign(-x, x) == -1 y = Symbol("y", positive=True) assert mrv_sign(y, x) == 1 assert mrv_sign(-y, x) == -1 assert mrv_sign(y * x, x) == 1 assert mrv_sign(-y * x, x) == -1
def test_sympify_strict(): x = Symbol('x') f = Function('f') # positive sympify assert sympify(x, strict=True) is x assert sympify(f, strict=True) is f assert sympify(1, strict=True) == Integer(1) assert sympify(0.5, strict=True) == Float("0.5") assert sympify(1 + 1j, strict=True) == 1.0 + I*1.0 class A: def _diofant_(self): return Integer(5) a = A() assert sympify(a, strict=True) == Integer(5) # negative sympify pytest.raises(SympifyError, lambda: sympify('1', strict=True)) pytest.raises(SympifyError, lambda: sympify([1, 2, 3], strict=True))