def test_Dummy_from_Symbol(): # should not get the full dictionary of assumptions n = Symbol('n', integer=True) d = n.as_dummy() assert srepr(d) == "Dummy('n', integer=True)"
def test_im(): a, b = symbols('a,b', extended_real=True) r = Symbol('r', extended_real=True) i = Symbol('i', imaginary=True) assert im(nan) == nan assert im(oo * I) == oo assert im(-oo * I) == -oo assert im(0) == 0 assert im(1) == 0 assert im(-1) == 0 assert im(E * I) == E assert im(-E * I) == -E assert im(x) == im(x) assert im(x * I) == re(x) assert im(r * I) == r assert im(r) == 0 assert im(i * I) == 0 assert im(i) == -I * i assert im(x + y) == im(x + y) assert im(x + r) == im(x) assert im(x + r * I) == im(x) + r assert im(im(x) * I) == im(x) assert im(2 + I) == 1 assert im(x + I) == im(x) + 1 assert im(x + y * I) == im(x) + re(y) assert im(x + r * I) == im(x) + r assert im(log(2 * I)) == pi / 2 assert im((2 + I)**2).expand(complex=True) == 4 assert im(conjugate(x)) == -im(x) assert conjugate(im(x)) == im(x) assert im(x).as_real_imag() == (im(x), 0) assert im(i * r * x).diff(r) == im(i * x) assert im(i * r * x).diff(i) == -I * re(r * x) assert im(sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * sin( arg(a + I * b) / 2) assert im(a * (2 + b * I)) == a * b assert im((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*sin(arg(a + I*b)/2)/2 assert im(x).rewrite(re) == x - re(x) assert (x + im(y)).rewrite(im, re) == x + y - re(y) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False
def test_issue_3871(): z = Symbol("z", positive=True) f = -1 / z * exp(-z * x) assert limit(f, x, oo) == 0 assert f.limit(x, oo) == 0
def test_Abs(): pytest.raises(TypeError, lambda: Abs(Interval(2, 3))) # issue 8717 x, y = symbols('x,y') assert sign(sign(x)) == sign(x) assert sign(x * y).func is sign assert Abs(0) == 0 assert Abs(1) == 1 assert Abs(-1) == 1 assert Abs(I) == 1 assert Abs(-I) == 1 assert Abs(nan) == nan assert Abs(I * pi) == pi assert Abs(-I * pi) == pi assert Abs(I * x) == Abs(x) assert Abs(-I * x) == Abs(x) assert Abs(-2 * x) == 2 * Abs(x) assert Abs(-2.0 * x) == 2.0 * Abs(x) assert Abs(2 * pi * x * y) == 2 * pi * Abs(x * y) assert Abs(conjugate(x)) == Abs(x) assert conjugate(Abs(x)) == Abs(x) a = Symbol('a', positive=True) assert Abs(2 * pi * x * a) == 2 * pi * a * Abs(x) assert Abs(2 * pi * I * x * a) == 2 * pi * a * Abs(x) x = Symbol('x', extended_real=True) n = Symbol('n', integer=True) assert Abs((-1)**n) == 1 assert x**(2 * n) == Abs(x)**(2 * n) assert Abs(x).diff(x) == sign(x) assert abs(x) == Abs(x) # Python built-in assert Abs(x)**3 == x**2 * Abs(x) assert Abs(x)**4 == x**4 assert (Abs(x)**(3 * n)).args == (Abs(x), 3 * n ) # leave symbolic odd unchanged assert (1 / Abs(x)).args == (Abs(x), -1) assert 1 / Abs(x)**3 == 1 / (x**2 * Abs(x)) assert Abs(x)**-3 == Abs(x) / (x**4) assert Abs(x**3) == x**2 * Abs(x) x = Symbol('x', imaginary=True) assert Abs(x).diff(x) == -sign(x) eq = -sqrt(10 + 6 * sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3 * sqrt(3)) # if there is a fast way to know when you can and when you cannot prove an # expression like this is zero then the equality to zero is ok assert abs(eq).func is Abs or abs(eq) == 0 # but sometimes it's hard to do this so it's better not to load # abs down with tests that will be very slow q = 1 + sqrt(2) - 2 * sqrt(3) + 1331 * sqrt(6) p = expand(q**3)**Rational(1, 3) d = p - q assert abs(d).func is Abs or abs(d) == 0 assert Abs(4 * exp(pi * I / 4)) == 4 assert Abs(3**(2 + I)) == 9 assert Abs((-3)**(1 - I)) == 3 * exp(pi) assert Abs(oo) is oo assert Abs(-oo) is oo assert Abs(oo + I) is oo assert Abs(oo + I * oo) is oo a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False
def test_arg_rewrite(): assert arg(1 + I) == atan2(1, 1) x = Symbol('x', extended_real=True) y = Symbol('y', extended_real=True) assert arg(x + I * y).rewrite(atan2) == atan2(y, x)
def test_bug(): x1 = Symbol('x1') x2 = Symbol('x2') y = x1 * x2 assert y.subs(x1, Float(3.0)) == Float(3.0) * x2
def test_sign(): assert sign(1.2) == 1 assert sign(-1.2) == -1 assert sign(3 * I) == I assert sign(-3 * I) == -I assert sign(0) == 0 assert sign(nan) == nan assert sign(2 + 2 * I).doit() == sqrt(2) * (2 + 2 * I) / 4 assert sign(2 + 3 * I).simplify() == sign(2 + 3 * I) assert sign(2 + 2 * I).simplify() == sign(1 + I) assert sign(im(sqrt(1 - sqrt(3)))) == 1 assert sign(sqrt(1 - sqrt(3))) == I x = Symbol('x') assert sign(x).is_finite is True assert sign(x).is_complex is True assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).doit() == sign(x) assert sign(1.2 * x) == sign(x) assert sign(2 * x) == sign(x) assert sign(I * x) == I * sign(x) assert sign(-2 * I * x) == -I * sign(x) assert sign(conjugate(x)) == conjugate(sign(x)) p = Symbol('p', positive=True) n = Symbol('n', negative=True) m = Symbol('m', negative=True) assert sign(2 * p * x) == sign(x) assert sign(n * x) == -sign(x) assert sign(n * m * x) == sign(x) x = Symbol('x', imaginary=True) xn = Symbol('xn', imaginary=True, nonzero=True) assert sign(x).is_imaginary is True assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).diff(x) == 2 * DiracDelta(-I * x) assert sign(xn).doit() == xn / Abs(xn) assert conjugate(sign(x)) == -sign(x) x = Symbol('x', extended_real=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is None assert sign(x).diff(x) == 2 * DiracDelta(x) assert sign(x).doit() == sign(x) assert conjugate(sign(x)) == sign(x) assert sign(sin(x)).nseries(x) == 1 x = Symbol('x', nonzero=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = Symbol('x', positive=True) assert sign(x).is_imaginary is False assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = 0 assert sign(x).is_imaginary is True assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is True assert sign(x).doit() == 0 assert sign(Abs(x)) == 0 assert Abs(sign(x)) == 0 nz = Symbol('nz', nonzero=True, integer=True) assert sign(nz).is_imaginary is False assert sign(nz).is_integer is True assert sign(nz).is_extended_real is True assert sign(nz).is_zero is False assert sign(nz)**2 == 1 assert (sign(nz)**3).args == (sign(nz), 3) assert sign(Symbol('x', nonnegative=True)).is_nonnegative assert sign(Symbol('x', nonnegative=True)).is_nonpositive is None assert sign(Symbol('x', nonpositive=True)).is_nonnegative is None assert sign(Symbol('x', nonpositive=True)).is_nonpositive assert sign(Symbol('x', extended_real=True)).is_nonnegative is None assert sign(Symbol('x', extended_real=True)).is_nonpositive is None assert sign(Symbol('x', extended_real=True, zero=False)).is_nonpositive is None x, y = Symbol('x', extended_real=True), Symbol('y') assert sign(x).rewrite(Piecewise) == \ Piecewise((1, x > 0), (-1, x < 0), (0, True)) assert sign(y).rewrite(Piecewise) == sign(y) assert sign(x).rewrite(Heaviside) == 2 * Heaviside(x) - 1 assert sign(y).rewrite(Heaviside) == sign(y) # evaluate what can be evaluated assert sign(exp_polar(I * pi) * pi) is S.NegativeOne eq = -sqrt(10 + 6 * sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3 * sqrt(3)) # if there is a fast way to know when and when you cannot prove an # expression like this is zero then the equality to zero is ok assert sign(eq).func is sign or sign(eq) == 0 # but sometimes it's hard to do this so it's better not to load # abs down with tests that will be very slow q = 1 + sqrt(2) - 2 * sqrt(3) + 1331 * sqrt(6) p = expand(q**3)**Rational(1, 3) d = p - q assert sign(d).func is sign or sign(d) == 0
def eq3(): r = Symbol("r") e = Rmn.dd(2, 2) e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r)))
def eq4(): r = Symbol("r") e = Rmn.dd(3, 3) e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r))) pprint(dsolve(e, lam(r), 'best'))
def pprint_Rmn_dd(i, j): pprint(Eq(Symbol('R_%i%i' % (i, j)), Rmn.dd(i, j)))
def eq2(): r = Symbol("r") e = Rmn.dd(1, 1) C = Symbol("CC") e = e.subs(nu(r), -lam(r)) pprint(dsolve(e, lam(r)))
def pprint_Gamma_udd(i, k, l): pprint(Eq(Symbol('Gamma^%i_%i%i' % (i, k, l)), Gamma.udd(i, k, l)))
def ud(self, mu, nu): r = 0 for lam in [0, 1, 2, 3]: r += self.g.uu(mu, lam) * self.dd(lam, nu) return r.expand() def curvature(Rmn): return Rmn.ud(0, 0) + Rmn.ud(1, 1) + Rmn.ud(2, 2) + Rmn.ud(3, 3) nu = Function("nu") lam = Function("lambda") t = Symbol("t") r = Symbol("r") theta = Symbol(r"theta") phi = Symbol(r"phi") # general, spherically symmetric metric gdd = Matrix(((-exp(nu(r)), 0, 0, 0), (0, exp(lam(r)), 0, 0), (0, 0, r**2, 0), (0, 0, 0, r**2 * sin(theta)**2))) g = MT(gdd) X = (t, r, theta, phi) Gamma = G(g, X) Rmn = Ricci(Riemann(Gamma, X), X) def pprint_Gamma_udd(i, k, l): pprint(Eq(Symbol('Gamma^%i_%i%i' % (i, k, l)), Gamma.udd(i, k, l)))
import pytest from diofant import (symbols, expand, expand_func, nan, oo, Float, conjugate, diff, re, im, Abs, O, exp_polar, polar_lift, limit, Symbol, I, integrate, S, sqrt, sin, cos, sinh, cosh, Integer, exp, log, pi, EulerGamma, erf, erfc, erfi, erf2, erfinv, Rational, erfcinv, erf2inv, gamma, uppergamma, Ei, expint, E1, li, Li, Si, Ci, Shi, Chi, fresnels, fresnelc, hyper, meijerg) from diofant.functions.special.error_functions import _erfs, _eis from diofant.core.function import ArgumentIndexError x, y, z = symbols('x,y,z') w = Symbol("w", extended_real=True) n = Symbol("n", integer=True) def test_erf(): assert erf(nan) == nan assert erf(oo) == 1 assert erf(-oo) == -1 assert erf(0) == 0 assert erf(I*oo) == oo*I assert erf(-I*oo) == -oo*I assert erf(-2) == -erf(2) assert erf(-x*y) == -erf(x*y) assert erf(-x - y) == -erf(x + y)
def test_2arg_hack(): N = Symbol('N', commutative=False) ans = Mul(2, y + 1, evaluate=False) assert (2 * x * (y + 1)).subs(x, 1, hack2=True) == ans assert (2 * (y + 1 + N)).subs(N, 0, hack2=True) == ans
def test_Mod1_behavior(): from diofant import Symbol, simplify, lowergamma n = Symbol('n', integer=True) # Note: this should not hang. assert simplify(hyperexpand(meijerg([1], [], [n + 1], [0], z))) == \ lowergamma(n + 1, z)
def test_diofantissue_124(): n = Symbol('n', integer=True) assert exp(n * x).subs({exp(x): x}) == x**n
def test_deriv_sub_bug3(): y = Symbol('y') f = Function('f') pat = Derivative(f(x), x, x) assert pat.subs(y, y**2) == Derivative(f(x), x, x) assert pat.subs(y, y**2) != Derivative(f(x), x)
def test_RootOf_issue_10092(): x = Symbol('x', real=True) eq = x**3 - 17 * x**2 + 81 * x - 118 r = RootOf(eq, 0) assert (x < r).subs(x, r) is S.false
def test_issue_3742(): y = Symbol('y') e = sqrt(x) * exp(y) assert e.subs(sqrt(x), 1) == exp(y)
def test_re(): a, b = symbols('a,b', extended_real=True) r = Symbol('r', extended_real=True) i = Symbol('i', imaginary=True) assert re(nan) == nan assert re(oo) == oo assert re(-oo) == -oo assert re(0) == 0 assert re(1) == 1 assert re(-1) == -1 assert re(E) == E assert re(-E) == -E assert re(x) == re(x) assert re(x * I) == -im(x) assert re(r * I) == 0 assert re(r) == r assert re(i * I) == I * i assert re(i) == 0 assert re(x + y) == re(x + y) assert re(x + r) == re(x) + r assert re(re(x)) == re(x) assert re(2 + I) == 2 assert re(x + I) == re(x) assert re(x + y * I) == re(x) - im(y) assert re(x + r * I) == re(x) assert re(log(2 * I)) == log(2) assert re((2 + I)**2).expand(complex=True) == 3 assert re(conjugate(x)) == re(x) assert conjugate(re(x)) == re(x) assert re(x).as_real_imag() == (re(x), 0) assert re(i * r * x).diff(r) == re(i * x) assert re(i * r * x).diff(i) == I * r * im(x) assert re(sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * cos( arg(a + I * b) / 2) assert re(a * (2 + b * I)) == 2 * a assert re((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2) assert re(x).rewrite(im) == x - im(x) assert (x + re(y)).rewrite(re, im) == x + y - im(y) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False
def test_subs_issue_4009(): assert (I * Symbol('a')).subs(1, 2) == I * Symbol('a')
def test_abs(): # this tests that abs calls Abs; don't rename to # test_Abs since that test is already above a = Symbol('a', positive=True) assert abs(I * (1 + a)**2) == (1 + a)**2
def test_derivative_subs3(): x = Symbol('x') dex = Derivative(exp(x), x) assert Derivative(dex, x).subs(dex, exp(x)) == dex assert dex.subs(exp(x), dex) == Derivative(exp(x), x, x)
def test_issue_4754_derivative_conjugate(): x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert (f(x).conjugate()).diff(x) == (f(x).diff(x)).conjugate() assert (f(y).conjugate()).diff(y) == -(f(y).diff(y)).conjugate()
def test_issue_4680(): N = Symbol('N') assert N.subs(dict(N=3)) == 3
def test_heuristic(): x = Symbol("x", extended_real=True) assert heuristics(sin(1 / x) + atan(x), x, 0, '+') == sin(oo) assert heuristics(log(2 + sqrt(atan(x)) * sin(1 / x)), x, 0, '+') == log(2)
def test_logexppow(): # no eval() x = Symbol('x', extended_real=True) w = Symbol('w') e = (3**(1 + x) + 2**(1 + x)) / (3**x + 2**x) assert e.subs(2**x, w) != e assert e.subs(exp(x * log(Rational(2))), w) != e
def test_order_oo(): x = Symbol('x', positive=True, finite=True) assert O(x) * oo != O(1, x) assert limit(oo / (x**2 - 4), x, oo) == oo
def test_Symbol_no_special_commutative_treatment(): sT(Symbol('x'), "Symbol('x')") sT(Symbol('x', commutative=False), "Symbol('x', commutative=False)") sT(Symbol('x', commutative=0), "Symbol('x', commutative=False)") sT(Symbol('x', commutative=True), "Symbol('x', commutative=True)") sT(Symbol('x', commutative=1), "Symbol('x', commutative=True)")