def test_sine_transform(): f = Function("f") # Test unevaluated form assert sine_transform(f(t), t, w) == SineTransform(f(t), t, w) assert inverse_sine_transform( f(w), w, t) == InverseSineTransform(f(w), w, t) assert sine_transform(1/sqrt(t), t, w) == 1/sqrt(w) assert inverse_sine_transform(1/sqrt(w), w, t) == 1/sqrt(t) assert sine_transform( (1/sqrt(t))**3, t, w) == sqrt(w)*gamma(Rational(1, 4))/(2*gamma(Rational(5, 4))) assert sine_transform(t**(-a), t, w) == 2**( -a + Rational(1, 2))*w**(a - 1)*gamma(-a/2 + 1)/gamma((a + 1)/2) assert inverse_sine_transform(2**(-a + Rational(1, 2))*w**(a - 1)*gamma(-a/2 + 1)/gamma(a/2 + Rational(1, 2)), w, t) == t**(-a) assert sine_transform( exp(-a*t), t, w) == sqrt(2)*w/(sqrt(pi)*(a**2 + w**2)) assert inverse_sine_transform( sqrt(2)*w/(sqrt(pi)*(a**2 + w**2)), w, t) == exp(-a*t) assert sine_transform( log(t)/t, t, w) == -sqrt(2)*sqrt(pi)*(log(w**2) + 2*EulerGamma)/4 assert sine_transform( t*exp(-a*t**2), t, w) == sqrt(2)*w*exp(-w**2/(4*a))/(4*a**Rational(3, 2)) assert inverse_sine_transform( sqrt(2)*w*exp(-w**2/(4*a))/(4*a**Rational(3, 2)), w, t) == t*exp(-a*t**2)
def test_diofantissue_75(): assert gruntz(abs(log(x)), x) == oo assert gruntz(tan(abs(pi/2 + 1/x))/acosh(pi/2 + 1/x), x) == -oo assert gruntz(tan(abs(pi/2 - 1/x))/acosh(pi/2 - 1/x), x) == +oo assert gruntz(abs(log(2 + 1/x)) - log(2 + 1/x), x) == 0 assert gruntz(abs(log(2 - 1/x)) - log(2 - 1/x), x) == 0
def test_residue_reduce(): a = Poly(2*t**2 - t - x**2, t) d = Poly(t**3 - x**2*t, t) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1/x, t)], 'Tfuncs': [log]}) assert residue_reduce(a, d, DE, z, invert=False) == \ ([(Poly(z**2 - Rational(1, 4), z), Poly((1 + 3*x*z - 6*z**2 - 2*x**2 + 4*x**2*z**2)*t - x*z + x**2 + 2*x**2*z**2 - 2*z*x**3, t))], False) assert residue_reduce(a, d, DE, z, invert=True) == \ ([(Poly(z**2 - Rational(1, 4), z), Poly(t + 2*x*z, t))], False) assert residue_reduce(Poly(-2/x, t), Poly(t**2 - 1, t,), DE, z, invert=False) == \ ([(Poly(z**2 - 1, z), Poly(-2*z*t/x - 2/x, t))], True) ans = residue_reduce(Poly(-2/x, t), Poly(t**2 - 1, t), DE, z, invert=True) assert ans == ([(Poly(z**2 - 1, z), Poly(t + z, t))], True) assert residue_reduce_to_basic(ans[0], DE, z) == -log(-1 + log(x)) + log(1 + log(x)) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-t**2 - t/x - (1 - nu**2/x**2), t)]}) # TODO: Skip or make faster assert residue_reduce(Poly((-2*nu**2 - x**4)/(2*x**2)*t - (1 + x**2)/x, t), Poly(t**2 + 1 + x**2/2, t), DE, z) == \ ([(Poly(z + Rational(1, 2), z, domain='QQ'), Poly(t**2 + 1 + x**2/2, t, domain='EX'))], True) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 + t**2, t)]}) assert residue_reduce(Poly(-2*x*t + 1 - x**2, t), Poly(t**2 + 2*x*t + 1 + x**2, t), DE, z) == \ ([(Poly(z**2 + Rational(1, 4), z), Poly(t + x + 2*z, t))], True) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) assert residue_reduce(Poly(t, t), Poly(t + sqrt(2), t), DE, z) == \ ([(Poly(z - 1, z), Poly(t + sqrt(2), t))], True)
def test_lognormal(): mean = Symbol('mu', real=True) std = Symbol('sigma', positive=True, real=True) X = LogNormal('x', mean, std) # The diofant integrator can't do this too well # assert E(X) == exp(mean+std**2/2) # assert variance(X) == (exp(std**2)-1) * exp(2*mean + std**2) # Right now, only density function and sampling works # Test sampling: Only e^mean in sample std of 0 for i in range(3): X = LogNormal('x', i, 0) assert sample(X) == N(exp(i)) # The diofant integrator can't do this too well # assert E(X) == mu = Symbol("mu", extended_real=True) sigma = Symbol("sigma", positive=True) X = LogNormal('x', mu, sigma) assert density(X)(x) == (sqrt(2)*exp(-(-mu + log(x))**2 / (2*sigma**2))/(2*x*sqrt(pi)*sigma)) X = LogNormal('x', 0, 1) # Mean 0, standard deviation 1 assert density(X)(x) == sqrt(2)*exp(-log(x)**2/2)/(2*x*sqrt(pi))
def test_DifferentialExtension_all_attrs(): # Test 'unimportant' attributes DE = DifferentialExtension(exp(x)*log(x), x, dummy=False, handle_first='exp') assert DE.f == exp(x)*log(x) assert DE.newf == t0*t1 assert DE.x == x assert DE.cases == ['base', 'exp', 'primitive'] assert DE.case == 'primitive' assert DE.level == -1 assert DE.t == t1 == DE.T[DE.level] assert DE.d == Poly(1/x, t1) == DE.D[DE.level] pytest.raises(ValueError, lambda: DE.increment_level()) DE.decrement_level() assert DE.level == -2 assert DE.t == t0 == DE.T[DE.level] assert DE.d == Poly(t0, t0) == DE.D[DE.level] assert DE.case == 'exp' DE.decrement_level() assert DE.level == -3 assert DE.t == x == DE.T[DE.level] == DE.x assert DE.d == Poly(1, x) == DE.D[DE.level] assert DE.case == 'base' pytest.raises(ValueError, lambda: DE.decrement_level()) DE.increment_level() DE.increment_level() assert DE.level == -1 assert DE.t == t1 == DE.T[DE.level] assert DE.d == Poly(1/x, t1) == DE.D[DE.level] assert DE.case == 'primitive'
def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) assert a.evalf() == 3 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336801) assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(1000) assert ceiling(x).evalf(subs={x: 3}) == 3 assert ceiling(x).evalf(subs={x: 3*I}) == 3*I assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2 + 3*I # issue sympy/sympy#10323 l = 1206577996382235787095214 y = ceiling(sqrt(l)) assert y == 1098443442506 assert y**2 >= l def check(x): c, f = ceiling(sqrt(x)), floor(sqrt(x)) assert (c - 1)**2 < x and c**2 >= x assert (f + 1)**2 > x and f**2 <= x check(2**30 + 1) check(2**100 + 1) check(2**112 + 2)
def test_ratsimp(): f, g = 1/x + 1/y, (x + y)/(x*y) assert f != g and ratsimp(f) == g f, g = 1/(1 + 1/x), 1 - 1/(x + 1) assert f != g and ratsimp(f) == g f, g = x/(x + y) + y/(x + y), 1 assert f != g and ratsimp(f) == g f, g = -x - y - y**2/(x + y) + x**2/(x + y), -2*y assert f != g and ratsimp(f) == g f = (a*c*x*y + a*c*z - b*d*x*y - b*d*z - b*t*x*y - b*t*x - b*t*z + e*x)/(x*y + z) G = [a*c - b*d - b*t + (-b*t*x + e*x)/(x*y + z), a*c - b*d - b*t - ( b*t*x - e*x)/(x*y + z)] assert f != g and ratsimp(f) in G A = sqrt(pi) B = log(erf(x) - 1) C = log(erf(x) + 1) D = 8 - 8*erf(x) f = A*B/D - A*C/D + A*C*erf(x)/D - A*B*erf(x)/D + 2*A/D assert ratsimp(f) == A*B/8 - A*C/8 - A/(4*erf(x) - 4)
def test_ei(): pos = Symbol('p', positive=True) neg = Symbol('n', negative=True) assert Ei(0) == -oo assert Ei(+oo) == oo assert Ei(-oo) == 0 assert Ei(-pos) == Ei(polar_lift(-1)*pos) - I*pi assert Ei(neg) == Ei(polar_lift(neg)) - I*pi assert tn_branch(Ei) assert mytd(Ei(x), exp(x)/x, x) assert mytn(Ei(x), Ei(x).rewrite(uppergamma), -uppergamma(0, x*polar_lift(-1)) - I*pi, x) assert mytn(Ei(x), Ei(x).rewrite(expint), -expint(1, x*polar_lift(-1)) - I*pi, x) assert Ei(x).rewrite(expint).rewrite(Ei) == Ei(x) assert Ei(x*exp_polar(2*I*pi)) == Ei(x) + 2*I*pi assert Ei(x*exp_polar(-2*I*pi)) == Ei(x) - 2*I*pi assert mytn(Ei(x), Ei(x).rewrite(Shi), Chi(x) + Shi(x), x) assert mytn(Ei(x*polar_lift(I)), Ei(x*polar_lift(I)).rewrite(Si), Ci(x) + I*Si(x) + I*pi/2, x) assert Ei(log(x)).rewrite(li) == li(x) assert Ei(2*log(x)).rewrite(li) == li(x**2) assert Ei(x).series(x) == (EulerGamma + log(x) + x + x**2/4 + x**3/18 + x**4/96 + x**5/600 + O(x**6)) assert Ei(1 + x).series(x) == (Ei(1) + E*x + E*x**3/6 - E*x**4/12 + 3*E*x**5/40 + O(x**6)) pytest.raises(ArgumentIndexError, lambda: Ei(x).fdiff(2))
def test_asinh(): assert asinh(x) == asinh(x) assert asinh(-x) == -asinh(x) assert asinh(-2) == -asinh(2) assert asinh(nan) == nan assert asinh( 0) == 0 assert asinh(+1) == log(sqrt(2) + 1) assert asinh(-1) == log(sqrt(2) - 1) assert asinh(I) == pi*I/2 assert asinh(-I) == -pi*I/2 assert asinh(I/2) == pi*I/6 assert asinh(-I/2) == -pi*I/6 assert asinh(oo) == oo assert asinh(-oo) == -oo assert asinh(I*oo) == oo assert asinh(-I*oo) == -oo assert asinh(zoo) == zoo assert asinh(I*(sqrt(3) - 1)/2**Rational(3, 2)) == pi*I/12 assert asinh(-I*(sqrt(3) - 1)/2**Rational(3, 2)) == -pi*I/12 assert asinh(I*(sqrt(5) - 1)/4) == pi*I/10 assert asinh(-I*(sqrt(5) - 1)/4) == -pi*I/10 assert asinh(I*(sqrt(5) + 1)/4) == 3*pi*I/10 assert asinh(-I*(sqrt(5) + 1)/4) == -3*pi*I/10 pytest.raises(ArgumentIndexError, lambda: asinh(x).fdiff(2))
def test_messy(): assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True) assert laplace_transform(Shi(x), x, s) == (acoth(s)/s, 1, True) # where should the logs be simplified? assert laplace_transform(Chi(x), x, s) == \ ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True) # TODO maybe simplify the inequalities? assert laplace_transform(besselj(a, x), x, s)[1:] == \ (0, And(Integer(0) < re(a/2) + Rational(1, 2), Integer(0) < re(a/2) + 1)) # NOTE s < 0 can be done, but argument reduction is not good enough yet assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \ (Piecewise((0, 4*abs(pi**2*s**2) > 1), (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0) # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons) # - folding could be better assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \ log(1 + sqrt(2)) assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \ log(Rational(1, 2) + sqrt(2)/2) assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \ Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
def test_sympyissue_5673(): e = LambertW(-1) assert e.is_comparable is False assert e.is_positive is not True e2 = 1 - 1/(1 - exp(-1000)) assert e2.is_positive is not True e3 = -2 + exp(exp(LambertW(log(2)))*LambertW(log(2))) assert e3.is_nonzero is not True
def test_seriesbug2c(): w = Symbol("w", extended_real=True) # more complicated case, but sin(x)~x, so the result is the same as in (1) e = (sin(2*w)/w)**(1 + w) assert e.series(w, 0, 1) == 2 + O(w) assert e.series(w, 0, 3) == 2 + 2*w*log(2) + \ w**2*(-Rational(4, 3) + log(2)**2) + O(w**3) assert e.series(w, 0, 2).subs({w: 0}) == 2
def test_evalf_slow_series(): assert NS(Sum((-1)**n / n, (n, 1, oo)), 15) == NS(-log(2), 15) assert NS(Sum((-1)**n / n, (n, 1, oo)), 50) == NS(-log(2), 50) assert NS(Sum(1/n**2, (n, 1, oo)), 15, strict=False) == NS(pi**2/6, 15) assert NS(Sum(1/n**2, (n, 1, oo)), 100, strict=False) == NS(pi**2/6, 100) assert NS(Sum(1/n**2, (n, 1, oo)), 500, strict=False) == NS(pi**2/6, 500) assert NS(Sum((-1)**n / (2*n + 1)**3, (n, 0, oo)), 15) == NS(pi**3/32, 15) assert NS(Sum((-1)**n / (2*n + 1)**3, (n, 0, oo)), 50) == NS(pi**3/32, 50)
def test_sympyissue_11526(): df = diff(1/(a*log((x - b)/(x - c))), x) res = -1/(-a*c + a*b) assert limit(df, x, oo) == res assert limit(simplify(df), x, oo) == res e = log((1/x - b)/(1/x - c)) assert e.as_leading_term(x) == x*(c - b)
def test_sympyissue_6559(): assert (-12*x + y).subs({-x: 1}) == 12 + y # though this involves cse it generated a failure in Mul._eval_subs x0, x1 = symbols('x0 x1') e = -log(-12*sqrt(2) + 17)/24 - log(-2*sqrt(2) + 3)/12 + sqrt(2)/3 # XXX modify cse so x1 is eliminated and x0 = -sqrt(2)? assert cse(e) == ( [(x0, sqrt(2))], [x0/3 - log(-12*x0 + 17)/24 - log(-2*x0 + 3)/12])
def test_expint(): assert mytn(expint(x, y), expint(x, y).rewrite(uppergamma), y**(x - 1)*uppergamma(1 - x, y), x) assert mytd( expint(x, y), -y**(x - 1)*meijerg([], [1, 1], [0, 0, 1 - x], [], y), x) assert mytd(expint(x, y), -expint(x - 1, y), y) assert mytn(expint(1, x), expint(1, x).rewrite(Ei), -Ei(x*polar_lift(-1)) + I*pi, x) assert expint(-4, x) == exp(-x)/x + 4*exp(-x)/x**2 + 12*exp(-x)/x**3 \ + 24*exp(-x)/x**4 + 24*exp(-x)/x**5 assert expint(-Rational(3, 2), x) == \ exp(-x)/x + 3*exp(-x)/(2*x**2) - 3*sqrt(pi)*erf(sqrt(x))/(4*x**Rational(5, 2)) \ + 3*sqrt(pi)/(4*x**Rational(5, 2)) assert tn_branch(expint, 1) assert tn_branch(expint, 2) assert tn_branch(expint, 3) assert tn_branch(expint, 1.7) assert tn_branch(expint, pi) assert expint(y, x*exp_polar(2*I*pi)) == \ x**(y - 1)*(exp(2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x) assert expint(y, x*exp_polar(-2*I*pi)) == \ x**(y - 1)*(exp(-2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x) assert expint(2, x*exp_polar(2*I*pi)) == 2*I*pi*x + expint(2, x) assert expint(2, x*exp_polar(-2*I*pi)) == -2*I*pi*x + expint(2, x) assert (expint(n, x*exp_polar(2*I*pi)) == expint(n, x*exp_polar(2*I*pi), evaluate=False)) assert expint(1, x).rewrite(Ei).rewrite(expint) == expint(1, x) assert (expint(2, x, evaluate=False).rewrite(Shi) == expint(2, x, evaluate=False)) assert mytn(E1(x), E1(x).rewrite(Shi), Shi(x) - Chi(x), x) assert mytn(E1(polar_lift(I)*x), E1(polar_lift(I)*x).rewrite(Si), -Ci(x) + I*Si(x) - I*pi/2, x) assert mytn(expint(2, x), expint(2, x).rewrite(Ei).rewrite(expint), -x*E1(x) + exp(-x), x) assert mytn(expint(3, x), expint(3, x).rewrite(Ei).rewrite(expint), x**2*E1(x)/2 + (1 - x)*exp(-x)/2, x) assert expint(Rational(3, 2), z).nseries(z, n=10) == \ 2 + 2*z - z**2/3 + z**3/15 - z**4/84 + z**5/540 - \ 2*sqrt(pi)*sqrt(z) + O(z**6) assert E1(z).series(z) == -EulerGamma - log(z) + z - \ z**2/4 + z**3/18 - z**4/96 + z**5/600 + O(z**6) assert expint(4, z).series(z) == Rational(1, 3) - z/2 + z**2/2 + \ z**3*(log(z)/6 - Rational(11, 36) + EulerGamma/6) - z**4/24 + \ z**5/240 + O(z**6) assert (expint(x, x).series(x, x0=1, n=2) == expint(1, 1) + (x - 1)*(-meijerg(((), (1, 1)), ((0, 0, 0), ()), 1) - 1/E) + O((x - 1)**2, (x, 1))) pytest.raises(ArgumentIndexError, lambda: expint(x, y).fdiff(3))
def test_log_product(): i, j = symbols('i,j', positive=True, integer=True) x, y = symbols('x,y', positive=True) assert simplify(log(Product(x**i, (i, 1, n)))) == Sum(i*log(x), (i, 1, n)) assert simplify(log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))) == \ log(Product(x**i*y**j, (i, 1, n), (j, 1, m))) expr = log(Product(-2, (n, 0, 4))) assert simplify(expr) == expr
def test_exp_log(): x = Symbol("x", extended_real=True) assert log(exp(x)) == x assert exp(log(x)) == x assert log(x).inverse() == exp y = Symbol("y", polar=True) assert log(exp_polar(z)) == z assert exp(log(y)) == y
def test_gruntz_eval_special_slow(): assert gruntz(gamma(x + 1)/sqrt(2*pi) - exp(-x)*(x**(x + Rational(1, 2)) + x**(x - Rational(1, 2))/12), x) == oo assert gruntz(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x) == 0 assert gruntz(exp(gamma(x - exp(-x))*exp(1/x)) - exp(gamma(x)), x) == oo assert gruntz( (Ei(x - exp(-exp(x))) - Ei(x))*exp(-x)*exp(exp(x))*x, x) == -1 assert gruntz( exp((log(2) + 1)*x) * (zeta(x + exp(-x)) - zeta(x)), x) == -log(2)
def test_diofantissue_74(): assert gruntz(sign(log(1 + 1/x)), x) == +1 assert gruntz(sign(log(1 - 1/x)), x) == -1 assert gruntz(sign(sin(+1/x)), x) == +1 assert gruntz(sign(sin(-1/x)), x) == -1 assert gruntz(sign(tan(+1/x)), x) == +1 assert gruntz(sign(tan(-1/x)), x) == -1 assert gruntz(sign(cos(pi/2 + 1/x)), x) == -1 assert gruntz(sign(cos(pi/2 - 1/x)), x) == +1
def test_benini(): alpha = Symbol("alpha", positive=True) b = Symbol("beta", positive=True) sigma = Symbol("sigma", positive=True) X = Benini('x', alpha, b, sigma) assert density(X)(x) == ((alpha/x + 2*b*log(x/sigma)/x) * exp(-alpha*log(x/sigma) - b*log(x/sigma)**2)) assert X.pspace.domain.set == Interval(sigma, oo, False, True)
def test_basic1(): assert limit(x, x, oo) == oo assert limit(x, x, -oo) == -oo assert limit(-x, x, oo) == -oo assert limit(x**2, x, -oo) == oo assert limit(-x**2, x, oo) == -oo assert limit(x*log(x), x, 0, dir="+") == 0 assert limit(1/x, x, oo) == 0 assert limit(exp(x), x, oo) == oo assert limit(-exp(x), x, oo) == -oo assert limit(exp(x)/x, x, oo) == oo assert limit(1/x - exp(-x), x, oo) == 0 assert limit(x + 1/x, x, oo) == oo assert limit(x - x**2, x, oo) == -oo assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1 assert limit((1 + x)**oo, x, 0) == oo assert limit((1 + x)**oo, x, 0, dir='-') == 0 assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo assert limit(y/x/log(x), x, 0) == -oo*sign(y) assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo limit(Sum(1/x, (x, 1, y)) - log(y), y, oo) limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo) assert limit(gamma(1/x + 3), x, oo) == 2 assert limit(nan, x, -oo) == nan assert limit(O(2)*x, x, nan) == nan assert limit(sin(O(x)), x, 0) == 0 assert limit(1/(x - 1), x, 1, dir="+") == oo assert limit(1/(x - 1), x, 1, dir="-") == -oo assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo assert limit(1/(5 - x)**3, x, 5, dir="-") == oo assert limit(1/sin(x), x, pi, dir="+") == -oo assert limit(1/sin(x), x, pi, dir="-") == oo assert limit(1/cos(x), x, pi/2, dir="+") == -oo assert limit(1/cos(x), x, pi/2, dir="-") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="+") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="-") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="+") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="-") == oo # approaching 0 # from dir="+" assert limit(1 + 1/x, x, 0) == oo # from dir='-' # Add assert limit(1 + 1/x, x, 0, dir='-') == -oo # Pow assert limit(x**(-2), x, 0, dir='-') == oo assert limit(x**(-3), x, 0, dir='-') == -oo assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I assert limit(x**2, x, 0, dir='-') == 0 assert limit(sqrt(x), x, 0, dir='-') == 0 assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi)) assert limit((1 + cos(x))**oo, x, 0) == oo assert limit(x**2, x, 0, dir='real') == 0 assert limit(exp(x), x, 0, dir='real') == 1 pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
def test_gruntz_other(): assert gruntz(sqrt(log(x + 1)) - sqrt(log(x)), x) == 0 # p12, 2.5 y = Symbol('y') assert gruntz(((1 + 1/x)**y - 1)*x, x) == y # p12, 2.6 n = Symbol('n', integer=True) assert gruntz(x**n/exp(x), x) == 0 # p14, 2.9 assert gruntz((1 + 1/x)*x - 1/log(1 + 1/x), x) == Rational(1, 2) # p15, 2.10 m = Symbol('m', integer=True) assert gruntz((root(1 + 1/x, n) - 1)/(root(1 + 1/x, m) - 1), x) == m/n # p13, 2.7
def test_function_subs(): f = Function("f") S = Sum(x*f(y), (x, 0, oo), (y, 0, oo)) assert S.subs({f(y): y}) == Sum(x*y, (x, 0, oo), (y, 0, oo)) assert S.subs({f(x): x}) == S pytest.raises(ValueError, lambda: S.subs({f(y): x + y})) S = Sum(x*log(y), (x, 0, oo), (y, 0, oo)) assert S.subs({log(y): y}) == S S = Sum(x*f(y), (x, 0, oo), (y, 0, oo)) assert S.subs({f(y): y}) == Sum(x*y, (x, 0, oo), (y, 0, oo))
def test_function_non_commutative(): x = Symbol('x', commutative=False) assert f(x).is_commutative is False assert sin(x).is_commutative is False assert exp(x).is_commutative is False assert log(x).is_commutative is False assert f(x).is_complex is False assert sin(x).is_complex is False assert exp(x).is_complex is False assert log(x).is_complex is False
def test_bug5(): e = (-log(w) + log(1 + w*log(x)))**(-2)*w**(-2)*((-log(w) + log(1 + x*w))*(-log(w) + log(1 + w*log(x)))*w - x*(-log(w) + log(1 + w*log(x)))*w) assert e.nseries(w, n=1, logx=l) == (x/l + 1)/w + O(1, w) assert e.nseries(w, n=2, logx=l) == x*log(x)/l**2 + log(x)/l - \ x/l + (1 + x/l)/w + O(w)
def test_abs(): assert limit(abs(x), x, 0) == 0 assert limit(abs(sin(x)), x, 0) == 0 assert limit(abs(cos(x)), x, 0) == 1 assert limit(abs(sin(x + 1)), x, 0) == sin(1) # sympy/sympy#12398 assert limit(abs(log(n)/n**3), n, oo) == 0 expr = abs(log(n)/n**3) expr2 = expr.subs({n: n + 1}) assert limit(n*(expr/expr2 - 1), n, oo) == 3
def test_heurisch_wrapper(): f = 1/(y + x) assert heurisch_wrapper(f, x) == log(x + y) f = 1/(y - x) assert heurisch_wrapper(f, x) == -log(x - y) f = 1/((y - x)*(y + x)) assert heurisch_wrapper(f, x) == \ Piecewise((1/x, Eq(y, 0)), (log(x + y)/2/y - log(x - y)/2/y, True)) # issue sympy/sympy#6926 f = sqrt(x**2/((y - x)*(y + x))) assert heurisch_wrapper(f, x) == x*sqrt(x**2)*sqrt(1/(-x**2 + y**2)) \ - y**2*sqrt(x**2)*sqrt(1/(-x**2 + y**2))/x
def test_pmint_besselj(): # TODO: in both cases heurisch() gives None. Wrong besselj() derivative? f = besselj(nu + 1, x)/besselj(nu, x) g = nu*log(x) - log(besselj(nu, x)) assert simplify(heurisch(f, x) - g) == 0 f = (nu*besselj(nu, x) - x*besselj(nu + 1, x))/x g = besselj(nu, x) assert simplify(heurisch(f, x) - g) == 0
def test_evalf_euler_maclaurin(): assert NS(Sum(1/k**k, (k, 1, oo)), 15) == '1.29128599706266' assert NS(Sum(1/k**k, (k, 1, oo)), 50) == '1.2912859970626635404072825905956005414986193682745' assert NS(Sum(1/k - log(1 + 1/k), (k, 1, oo)), 15) == NS(EulerGamma, 15) assert NS(Sum(1/k - log(1 + 1/k), (k, 1, oo)), 50) == NS(EulerGamma, 50) assert NS(Sum(log(k)/k**2, (k, 1, oo)), 15) == '0.937548254315844' assert NS(Sum(log(k)/k**2, (k, 1, oo)), 50) == '0.93754825431584375370257409456786497789786028861483' assert NS(Sum(1/k, (k, 1000000, 2000000)), 15) == '0.693147930560008' assert NS(Sum(1/k, (k, 1000000, 2000000)), 50) == '0.69314793056000780941723211364567656807940638436025'
def test_sympyissue_9536(): a = Symbol('a', real=True) assert FiniteSet(log(a)).intersection(S.Reals) == Intersection( S.Reals, FiniteSet(log(a)))
def test_laplace_transform(): LT = laplace_transform a, b, c, = symbols('a b c', positive=True) f = Function("f") # Test unevaluated form assert laplace_transform(f(t), t, w) == LaplaceTransform(f(t), t, w) assert inverse_laplace_transform(f(w), w, t, plane=0) == InverseLaplaceTransform( f(w), w, t, 0) # test a bug spos = symbols('s', positive=True) assert LT(exp(t), t, spos)[:2] == (1 / (spos - 1), 1) # basic tests from wikipedia assert LT((t - a)**b*exp(-c*(t - a))*Heaviside(t - a), t, s) == \ ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1) * gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1 / s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a * s) / s, 0, True) assert LT(1 - exp(-a * t), t, s) == (a / (s * (a + s)), 0, True) assert LT((exp(2*t) - 1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[:2] == (1 / (s - 1), 1) assert LT(exp(2 * t), t, s)[:2] == (1 / (s - 2), 2) assert LT(exp(a * t), t, s)[:2] == (1 / (s - a), a) assert LT(log(t / a), t, s) == ((log(a * s) + EulerGamma) / s / -1, 0, True) assert LT(erf(t), t, s) == ((-erf(s / 2) + 1) * exp(s**2 / 4) / s, 0, True) assert LT(sin(a * t), t, s) == (a / (a**2 + s**2), 0, True) assert LT(cos(a * t), t, s) == (s / (a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a * t) * sin(b * t), t, s) == (b / (b**2 + (a + s)**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ ((a + s)/(b**2 + (a + s)**2), -a, True) assert LT(besselj(0, t), t, s) == (1 / sqrt(1 + s**2), 0, True) assert LT(besselj(1, t), t, s) == (1 - 1 / sqrt(1 + 1 / s**2), 0, True) # TODO general order works, but is a *mess* # TODO besseli also works, but is an even greater mess # test a bug in conditions processing # TODO the auxiliary condition should be recognised/simplified assert LT(exp(t) * cos(t), t, s)[:-1] in [ ((s - 1) / (s**2 - 2 * s + 2), -oo), ((s - 1) / ((s - 1)**2 + 1), -oo), ] # Fresnel functions assert laplace_transform(fresnels(t), t, s) == \ ((-sin(s**2/(2*pi))*fresnels(s/pi) + sin(s**2/(2*pi))/2 - cos(s**2/(2*pi))*fresnelc(s/pi) + cos(s**2/(2*pi))/2)/s, 0, True) assert laplace_transform( fresnelc(t), t, s) == ((sin(s**2 / (2 * pi)) * fresnelc(s / pi) / s - cos(s**2 / (2 * pi)) * fresnels(s / pi) / s + sqrt(2) * cos(s**2 / (2 * pi) + pi / 4) / (2 * s), 0, True)) assert LT(Matrix([[exp(t), t*exp(-t)], [t*exp(-t), exp(t)]]), t, s) ==\ Matrix([ [(1/(s - 1), 1, True), ((s + 1)**(-2), 0, True)], [((s + 1)**(-2), 0, True), (1/(s - 1), 1, True)] ])
def test_inverse_mellin_transform(): IMT = inverse_mellin_transform assert IMT(gamma(s), s, x, (0, oo)) == exp(-x) assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x) assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \ (x**2 + 1)*Heaviside(1 - x)/(4*x) # test passing "None" assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \ -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x) assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \ (-x/2 + 1/(2*x))*Heaviside(-x + 1) # test expansion of sums assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x # test factorisation of polys r = symbols('r', extended_real=True) assert (IMT(1 / (s**2 + 1), s, exp(-x), (None, oo)).subs({ x: r }).rewrite(sin).simplify() == sin(r) * Heaviside(1 - exp(-r))) # test multiplicative substitution _a, _b = symbols('a b', positive=True) assert IMT(_b**(-s / _a) * factorial(s / _a) / s, s, x, (0, oo)) == exp(-_b * x**_a) assert IMT(factorial(_a / _b + s / _b) / (_a + s), s, x, (-_a, oo)) == x**_a * exp(-x**_b) def simp_pows(expr): return simplify(powsimp(expand_mul(expr, deep=False), force=True)).replace(exp_polar, exp) # Now test the inverses of all direct transforms tested above # Section 8.4.2 nu = symbols('nu', real=True) assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1) assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \ == (1 - x)**(beta - 1)*Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s), s, x, (-oo, None))) \ == (x - 1)**(beta - 1)*Heaviside(x - 1) assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \ == (1/(x + 1))**rho assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c) * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi, s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \ == (x**c - d**c)/(x - d) assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s) * gamma(-c/2 - s)/gamma(1 - c - s), s, x, (0, -re(c)/2))) == \ (1 + sqrt(x + 1))**c assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s) / gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \ b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) + b**2 + x)/(b**2 + x) assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s) / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \ b**c*(sqrt(1 + x/b**2) + 1)**c # Section 8.4.5 assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x) assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \ log(x)**3*Heaviside(x - 1) assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1) assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1) assert IMT(pi / (s * sin(2 * pi * s)), s, x, (-Rational(1, 2), 0)) == log(sqrt(x) + 1) assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x) # TODO def mysimp(expr): return expand(powsimp(logcombine(expr, force=True), force=True, deep=True), force=True).replace(exp_polar, exp) assert mysimp(mysimp(IMT(pi / (s * tan(pi * s)), s, x, (-1, 0)))) in [ log(1 - x) * Heaviside(1 - x) + log(x - 1) * Heaviside(x - 1), log(x) * Heaviside(x - 1) + log(1 - 1 / x) * Heaviside(x - 1) + log(-x + 1) * Heaviside(-x + 1) ] # test passing cot assert mysimp(IMT(pi * cot(pi * s) / s, s, x, (0, 1))) in [ log(1 / x - 1) * Heaviside(1 - x) + log(1 - 1 / x) * Heaviside(x - 1), -log(x) * Heaviside(-x + 1) + log(1 - 1 / x) * Heaviside(x - 1) + log(-x + 1) * Heaviside(-x + 1), ] # 8.4.14 assert IMT(-gamma(s + Rational(1, 2))/(sqrt(pi)*s), s, x, (-Rational(1, 2), 0)) == \ erf(sqrt(x)) # 8.4.19 assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, Rational(3, 4)))) \ == besselj(a, 2*sqrt(x)) assert simplify(IMT(2**a*gamma(Rational(1, 2) - 2*s)*gamma(s + (a + 1)/2) / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)), s, x, (-(re(a) + 1)/2, Rational(1, 4)))) == \ sin(sqrt(x))*besselj(a, sqrt(x)) assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(Rational(1, 2) - 2*s) / (gamma(Rational(1, 2) - s - a/2)*gamma(1 - 2*s + a)), s, x, (-re(a)/2, Rational(1, 4)))) == \ cos(sqrt(x))*besselj(a, sqrt(x)) # TODO this comes out as an amazing mess, but simplifies nicely assert simplify(IMT(gamma(a + s)*gamma(Rational(1, 2) - s) / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), s, x, (-re(a), Rational(1, 2)))) == \ besselj(a, sqrt(x))**2 assert simplify(IMT(gamma(s)*gamma(Rational(1, 2) - s) / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)), s, x, (0, Rational(1, 2)))) == \ besselj(-a, sqrt(x))*besselj(a, sqrt(x)) assert simplify(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) * gamma(a/2 + b/2 - s + 1)), s, x, (-(re(a) + re(b))/2, Rational(1, 2)))) == \ besselj(a, sqrt(x))*besselj(b, sqrt(x)) # Section 8.4.20 # TODO this can be further simplified! assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)), s, x, (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), Rational(1, 2)))) == \ besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) - besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b) # TODO more # for coverage assert IMT(pi / cos(pi * s), s, x, (0, Rational(1, 2))) == sqrt(x) / (x + 1)
def test_polylog_expansion(): assert myexpand(polylog(1, z), -log(1 + exp_polar(-I * pi) * z)) assert myexpand(polylog(0, z), z / (1 - z)) assert myexpand(polylog(-1, z), z**2 / (1 - z)**2 + z / (1 - z)) assert myexpand(polylog(-5, z), None)
def test_mellin_transform(): MT = mellin_transform bpos = symbols('b', positive=True) # 8.4.2 assert MT(x**nu*Heaviside(x - 1), x, s) == \ (-1/(nu + s), (-oo, -re(nu)), True) assert MT(x**nu*Heaviside(1 - x), x, s) == \ (1/(nu + s), (-re(nu), oo), True) assert MT((1 - x)**(beta - 1)*Heaviside(1 - x), x, s) == \ (gamma(beta)*gamma(s)/gamma(beta + s), (0, oo), re(-beta) < 0) assert MT((x - 1)**(beta - 1)*Heaviside(x - 1), x, s) == \ (gamma(beta)*gamma(1 - beta - s)/gamma(1 - s), (-oo, -re(beta) + 1), re(-beta) < 0) assert MT((1 + x)**(-rho), x, s) == \ (gamma(s)*gamma(rho - s)/gamma(rho), (0, re(rho)), True) # TODO also the conditions should be simplified assert MT(abs(1 - x)**(-rho), x, s) == (2 * sin(pi * rho / 2) * gamma(1 - rho) * cos(pi * (rho / 2 - s)) * gamma(s) * gamma(rho - s) / pi, (0, re(rho)), And(re(rho) - 1 < 0, re(rho) < 1)) mt = MT((1 - x)**(beta - 1) * Heaviside(1 - x) + a * (x - 1)**(beta - 1) * Heaviside(x - 1), x, s) assert mt[1], mt[2] == ((0, -re(beta) + 1), True) assert MT((x**a - b**a)/(x - b), x, s)[0] == \ pi*b**(a + s - 1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s))) assert MT((x**a - bpos**a)/(x - bpos), x, s) == \ (pi*bpos**(a + s - 1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s))), (Max(-re(a), 0), Min(1 - re(a), 1)), True) expr = (sqrt(x + b**2) + b)**a assert MT(expr.subs({b: bpos}), x, s) == \ (-a*(2*bpos)**(a + 2*s)*gamma(s)*gamma(-a - 2*s)/gamma(-a - s + 1), (0, -re(a)/2), True) expr = (sqrt(x + b**2) + b)**a / sqrt(x + b**2) assert MT(expr.subs({b: bpos}), x, s) == \ (2**(a + 2*s)*bpos**(a + 2*s - 1)*gamma(s) * gamma(1 - a - 2*s)/gamma(1 - a - s), (0, -re(a)/2 + Rational(1, 2)), True) # 8.4.2 assert MT(exp(-x), x, s) == (gamma(s), (0, oo), True) assert MT(exp(-1 / x), x, s) == (gamma(-s), (-oo, 0), True) # 8.4.5 assert MT(log(x)**4 * Heaviside(1 - x), x, s) == (24 / s**5, (0, oo), True) assert MT(log(x)**3 * Heaviside(x - 1), x, s) == (6 / s**4, (-oo, 0), True) assert MT(log(x + 1), x, s) == (pi / (s * sin(pi * s)), (-1, 0), True) assert MT(log(1 / x + 1), x, s) == (pi / (s * sin(pi * s)), (0, 1), True) assert MT(log(abs(1 - x)), x, s) == (pi / (s * tan(pi * s)), (-1, 0), True) assert MT(log(abs(1 - 1 / x)), x, s) == (pi / (s * tan(pi * s)), (0, 1), True) # TODO we cannot currently do these (needs summation of 3F2(-1)) # this also implies that they cannot be written as a single g-function # (although this is possible) mt = MT(log(x) / (x + 1), x, s) assert mt[1:] == ((0, 1), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) mt = MT(log(x)**2 / (x + 1), x, s) assert mt[1:] == ((0, 1), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) mt = MT(log(x) / (x + 1)**2, x, s) assert mt[1:] == ((0, 2), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) # 8.4.14 assert MT(erf(sqrt(x)), x, s) == \ (-gamma(s + Rational(1, 2))/(sqrt(pi)*s), (-Rational(1, 2), 0), True)
def test__zetas(): assert _zetas(1/x).nseries(x) == \ 1 + x**log(7) + x**log(6) + x**log(5) + x**log(4) + x**log(3) + x**log(2) + O(x**log(6))
def test_RootSum(): r = RootSum(x**3 + x + 3, Lambda(y, log(y * z))) assert mathematica_code(r) == ('RootSum[Function[{x}, x^3 + x + 3], ' 'Function[{y}, Log[y*z]]]')
def test_issue_3934(): assert limit((1 + x**log(3))**(1 / x), x, 0) == 1 assert limit((5**(1 / x) + 3**(1 / x))**x, x, 0) == 5
def test_sympyissue_4992(): # Nonelementary integral. Requires hypergeometric/Meijer-G handling. assert not integrate(log(x) * x**(k - 1) * exp(-x) / gamma(k), (x, 0, oo)).has(Integral)
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_issue_4090(): assert limit(1 / (x + 3), x, 2) == Rational(1, 5) assert limit(1 / (x + pi), x, 2) == Integer(1) / (2 + pi) assert limit(log(x) / (x**2 + 3), x, 2) == log(2) / 7 assert limit(log(x) / (x**2 + pi), x, 2) == log(2) / (4 + pi)
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_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 vlog2(x, base=S.Exp1, **kwargs): return log(x, base)
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(2)): w}) != e
def test_ceiling(): x = Symbol('x') i = Symbol('i', imaginary=True) y = Symbol('y', extended_real=True) k, n = symbols('k,n', integer=True) assert ceiling(nan) == nan assert ceiling(oo) == oo assert ceiling(-oo) == -oo assert ceiling(0) == 0 assert ceiling(1) == 1 assert ceiling(-1) == -1 assert ceiling(E) == 3 assert ceiling(-E) == -2 assert ceiling(2*E) == 6 assert ceiling(-2*E) == -5 assert ceiling(pi) == 4 assert ceiling(-pi) == -3 assert ceiling(Rational(1, 2)) == 1 assert ceiling(-Rational(1, 2)) == 0 assert ceiling(Rational(7, 3)) == 3 assert ceiling(-Rational(7, 3)) == -2 assert ceiling(Float(17.0)) == 17 assert ceiling(-Float(17.0)) == -17 assert ceiling(Float(7.69)) == 8 assert ceiling(-Float(7.69)) == -7 assert ceiling(I) == I assert ceiling(-I) == -I e = ceiling(i) assert e.func is ceiling and e.args[0] == i assert ceiling(oo*I) == oo*I assert ceiling(-oo*I) == -oo*I assert ceiling(2*I) == 2*I assert ceiling(-2*I) == -2*I assert ceiling(I/2) == I assert ceiling(-I/2) == 0 assert ceiling(E + 17) == 20 assert ceiling(pi + 2) == 6 assert ceiling(E + pi) == ceiling(E + pi) assert ceiling(I + pi) == ceiling(I + pi) assert ceiling(ceiling(pi)) == 4 assert ceiling(ceiling(y)) == ceiling(y) assert ceiling(ceiling(x)) == ceiling(ceiling(x)) assert ceiling(x) == ceiling(x) assert ceiling(2*x) == ceiling(2*x) assert ceiling(k*x) == ceiling(k*x) assert ceiling(k) == k assert ceiling(2*k) == 2*k assert ceiling(k*n) == k*n assert ceiling(k/2) == ceiling(k/2) assert ceiling(x + y) == ceiling(x + y) assert ceiling(x + 3) == ceiling(x + 3) assert ceiling(x + k) == ceiling(x + k) assert ceiling(y + 3) == ceiling(y) + 3 assert ceiling(y + k) == ceiling(y) + k assert ceiling(3 + pi + y*I) == 7 + ceiling(y)*I assert ceiling(k + n) == k + n assert ceiling(x*I) == ceiling(x*I) assert ceiling(k*I) == k*I assert ceiling(Rational(23, 10) - E*I) == 3 - 2*I assert ceiling(sin(1)) == 1 assert ceiling(sin(-1)) == 0 assert ceiling(exp(2)) == 8 assert ceiling(-log(8)/log(2)) != -2 assert int(ceiling(-log(8)/log(2)).evalf(chop=True)) == -3 assert ceiling(factorial(50)/exp(1)) == \ 11188719610782480504630258070757734324011354208865721592720336801 assert (ceiling(y) >= y) is S.true assert (ceiling(y) < y) is S.false assert (ceiling(x) >= x).is_Relational # x could be non-real assert (ceiling(x) < x).is_Relational assert (ceiling(x) >= y).is_Relational # arg is not same as rhs assert (ceiling(x) < y).is_Relational
def test_piecewise(): # Test canonization assert Piecewise((x, x < 1), (0, True)) == Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, True), (1, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, False), (-1, 1 > 2)) == \ Piecewise((x, x < 1)) assert Piecewise((x, x < 1), (0, x < 1), (0, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, x < 2), (0, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (x, x < 2), (0, True)) == \ Piecewise((x, Or(x < 1, x < 2)), (0, True)) assert Piecewise((x, x < 1), (x, x < 2), (x, True)) == x assert Piecewise((x, True)) == x pytest.raises(TypeError, lambda: Piecewise(x)) pytest.raises(TypeError, lambda: Piecewise((x, x**2))) assert Piecewise((0, Eq(z, 0, evaluate=False)), (1, True)) == 1 # Test subs p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >= 0)) p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >= 0)) assert p.subs({x: x**2}) == p_x2 assert p.subs({x: -5}) == -1 assert p.subs({x: -1}) == 1 assert p.subs({x: 1}) == log(1) # More subs tests p2 = Piecewise((1, x < pi), (-1, x < 2 * pi), (0, x > 2 * pi)) p3 = Piecewise((1, Eq(x, 0)), (1 / x, True)) p4 = Piecewise((1, Eq(x, 0)), (2, 1 / x > 2)) assert p2.subs({x: 2}) == 1 assert p2.subs({x: 4}) == -1 assert p2.subs({x: 10}) == 0 assert p3.subs({x: 0.0}) == 1 assert p4.subs({x: 0.0}) == 1 f, g, h = symbols('f,g,h', cls=Function) pf = Piecewise((f(x), x < -1), (f(x) + h(x) + 2, x <= 1)) pg = Piecewise((g(x), x < -1), (g(x) + h(x) + 2, x <= 1)) assert pg.subs({g: f}) == pf assert Piecewise((1, Eq(x, 0)), (0, True)).subs({x: 0}) == 1 assert Piecewise((1, Eq(x, 0)), (0, True)).subs({x: 1}) == 0 assert Piecewise((1, Eq(x, y)), (0, True)).subs({x: y}) == 1 assert Piecewise((1, Eq(x, z)), (0, True)).subs({x: z}) == 1 assert Piecewise((1, Eq(exp(x), cos(z))), (0, True)).subs({x: z}) == \ Piecewise((1, Eq(exp(z), cos(z))), (0, True)) assert Piecewise((1, Eq(x, y * (y + 1))), (0, True)).subs({ x: y**2 + y }).simplify() == 1 p5 = Piecewise((0, Eq(cos(x) + y, 0)), (1, True)) assert p5.subs({y: 0}) == Piecewise((0, Eq(cos(x), 0)), (1, True)) # Test evalf assert p.evalf() == p assert p.evalf(subs={x: -2}) == -1 assert p.evalf(subs={x: -1}) == 1 assert p.evalf(subs={x: 1}) == log(1) # Test doit f_int = Piecewise((Integral(x, (x, 0, 1)), x < 1)) assert f_int.doit() == Piecewise((1.0 / 2.0, x < 1)) # Test differentiation f = x fp = x * p dp = Piecewise((0, x < -1), (2 * x, x < 0), (1 / x, x >= 0)) fp_dx = x * dp + p assert diff(p, x) == dp assert diff(f * p, x) == fp_dx # Test simple arithmetic assert x * p == fp assert x * p + p == p + x * p assert p + f == f + p assert p + dp == dp + p assert p - dp == -(dp - p) # Test power dp2 = Piecewise((0, x < -1), (4 * x**2, x < 0), (1 / x**2, x >= 0)) assert dp**2 == dp2 # Test _eval_interval f1 = x * y + 2 f2 = x * y**2 + 3 peval = Piecewise((f1, x < 0), (f2, x > 0)) peval_interval = (f1.subs({x: 0}) - f1.subs({x: -1}) + f2.subs({x: 1}) - f2.subs({x: 0})) assert peval._eval_interval(x, 0, 0) == 0 assert peval._eval_interval(x, -1, 1) == peval_interval peval2 = Piecewise((f1, x < 0), (f2, True)) assert peval2._eval_interval(x, 0, 0) == 0 assert peval2._eval_interval(x, 1, -1) == -peval_interval assert peval2._eval_interval(x, -1, -2) == (f1.subs({x: -2}) - f1.subs({x: -1})) assert peval2._eval_interval(x, -1, 1) == peval_interval assert peval2._eval_interval(x, None, 0) == peval2.subs({x: 0}) assert peval2._eval_interval(x, -1, None) == -peval2.subs({x: -1}) # Test integration p_int = Piecewise((-x, x < -1), (x**3 / 3.0, x < 0), (-x + x * log(x), x >= 0)) assert integrate(p, x) == p_int p = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x)) assert integrate(p, (x, -2, 2)) == 5.0 / 6.0 assert integrate(p, (x, 2, -2)) == -5.0 / 6.0 p = Piecewise((0, x < 0), (1, x < 1), (0, x < 2), (1, x < 3), (0, True)) assert integrate(p, (x, -oo, oo)) == 2 p = Piecewise((x, x < -10), (x**2, x <= -1), (x, 1 < x)) pytest.raises(ValueError, lambda: integrate(p, (x, -2, 2))) # Test commutativity assert p.is_commutative is True
def test_dirichlet_eta_eval(): assert dirichlet_eta(0) == Rational(1, 2) assert dirichlet_eta(-1) == Rational(1, 4) assert dirichlet_eta(1) == log(2) assert dirichlet_eta(2) == pi**2 / 12 assert dirichlet_eta(4) == pi**4 * Rational(7, 720)
def test_floor(): x = Symbol('x') i = Symbol('i', imaginary=True) y = Symbol('y', extended_real=True) r = Symbol('r', real=True) k, n = symbols('k,n', integer=True) assert floor(y).is_extended_real assert floor(x).is_extended_real is None assert floor(r).is_finite assert floor(y).is_finite is None assert floor(r).is_integer assert floor(y).is_integer is None assert floor(nan) == nan assert floor(oo) == oo assert floor(-oo) == -oo assert floor(0) == 0 assert floor(1) == 1 assert floor(-1) == -1 assert floor(E) == 2 assert floor(-E) == -3 assert floor(2*E) == 5 assert floor(-2*E) == -6 assert floor(pi) == 3 assert floor(-pi) == -4 assert floor(Rational(1, 2)) == 0 assert floor(-Rational(1, 2)) == -1 assert floor(Rational(7, 3)) == 2 assert floor(-Rational(7, 3)) == -3 assert floor(Float(17.0)) == 17 assert floor(-Float(17.0)) == -17 assert floor(Float(7.69)) == 7 assert floor(-Float(7.69)) == -8 assert floor(I) == I assert floor(-I) == -I e = floor(i) assert e.func is floor and e.args[0] == i assert floor(oo*I) == oo*I assert floor(-oo*I) == -oo*I assert floor(2*I) == 2*I assert floor(-2*I) == -2*I assert floor(I/2) == 0 assert floor(-I/2) == -I assert floor(E + 17) == 19 assert floor(pi + 2) == 5 assert floor(E + pi) == floor(E + pi) assert floor(I + pi) == floor(I + pi) assert floor(floor(pi)) == 3 assert floor(floor(y)) == floor(y) assert floor(floor(x)) == floor(floor(x)) assert floor(x) == floor(x) assert floor(2*x) == floor(2*x) assert floor(k*x) == floor(k*x) assert floor(k) == k assert floor(2*k) == 2*k assert floor(k*n) == k*n assert floor(k/2) == floor(k/2) assert floor(x + y) == floor(x + y) assert floor(x + 3) == floor(x + 3) assert floor(x + k) == floor(x + k) assert floor(y + 3) == floor(y) + 3 assert floor(y + k) == floor(y) + k assert floor(3 + I*y + pi) == 6 + floor(y)*I assert floor(k + n) == k + n assert floor(x*I) == floor(x*I) assert floor(k*I) == k*I assert floor(Rational(23, 10) - E*I) == 2 - 3*I assert floor(sin(1)) == 0 assert floor(sin(-1)) == -1 assert floor(exp(2)) == 7 assert floor(log(8)/log(2)) != 2 assert int(floor(log(8)/log(2)).evalf(chop=True)) == 3 assert floor(factorial(50)/exp(1)) == \ 11188719610782480504630258070757734324011354208865721592720336800 assert (floor(y) <= y) is S.true assert (floor(y) > y) is S.false assert (floor(x) <= x).is_Relational # x could be non-real assert (floor(x) > x).is_Relational assert (floor(x) <= y).is_Relational # arg is not same as rhs assert (floor(x) > y).is_Relational assert floor(x).as_leading_term(x) == floor(x)
def test_logcombine_complex_coeff(): i = Integral((sin(x**2) + cos(x**3))/x, x) assert logcombine(i, force=True) == i assert logcombine(i + 2*log(x), force=True) == i + log(x**2)
def test_issue_5740(): assert limit(log(x) * z - log(2 * x) * y, x, 0) == oo * sign(y - z)
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_bug(): assert residue(2**(z)*(s + z)*(1 - s - z)/z**2, z, 0) == \ 1 + s*log(2) - s**2*log(2) - 2*s
def test_separatevars_advanced_factor(): x, y = symbols('x,y') assert (separatevars(1 + log(x)*log(y) + log(x) + log(y)) == (log(x) + 1)*(log(y) + 1)) assert (separatevars(1 + x - log(z) - x*log(z) - exp(y)*log(z) - x*exp(y)*log(z) + x*exp(y) + exp(y)) == -((exp(y) + 1) * (x + 1)*(log(z) - 1))) x, y = symbols('x,y', positive=True) assert (separatevars(1 + log(x**log(y)) + log(x*y)) == (log(x) + 1)*(log(y) + 1))
def test_issue_8061(): assert limit(4**(acos(1 / (1 + x**2))**2) / log(1 + x, 4), x, 0) == oo
def test_logcombine_1(): z, w = symbols('z,w', positive=True) b = Symbol('b', real=True) assert logcombine(log(x) + 2*log(y)) == log(x) + 2*log(y) assert logcombine(log(x) + 2*log(y), force=True) == log(x*y**2) assert logcombine(a*log(w) + log(z)) == a*log(w) + log(z) assert logcombine(b*log(z) + b*log(x)) == log(z**b) + b*log(x) assert logcombine(b*log(z) - log(w)) == log(z**b/w) assert logcombine(log(x)*log(z)) == log(x)*log(z) assert logcombine(log(w)*log(x)) == log(w)*log(x) assert logcombine(cos(-2*log(z) + b*log(w))) in [cos(log(w**b/z**2)), cos(log(z**2/w**b))] assert logcombine(log(log(x) - log(y)) - log(z), force=True) == \ log(log(x/y)/z) assert logcombine((2 + I)*log(x), force=True) == (2 + I)*log(x) assert logcombine((x**2 + log(x) - log(y))/(x*y), force=True) == \ (x**2 + log(x/y))/(x*y) # the following could also give log(z*x**log(y**2)), what we # are testing is that a canonical result is obtained assert logcombine(log(x)*2*log(y) + log(z), force=True) == \ log(z*y**log(x**2)) assert logcombine((x*y + sqrt(x**4 + y**4) + log(x) - log(y))/(pi*x**Rational(2, 3) * sqrt(y)**3), force=True) == ( x*y + sqrt(x**4 + y**4) + log(x/y))/(pi*x**Rational(2, 3) * y**Rational(3, 2)) assert logcombine(gamma(-log(x/y))*acos(-log(x/y)), force=True) == \ acos(-log(x/y))*gamma(-log(x/y)) assert logcombine(2*log(z)*log(w)*log(x) + log(z) + log(w)) == \ log(z**log(w**2))*log(x) + log(w*z) assert logcombine(3*log(w) + 3*log(z)) == log(w**3*z**3) assert logcombine(x*(y + 1) + log(2) + log(3)) == x*(y + 1) + log(6) assert logcombine((x + y)*log(w) + (-x - y)*log(3)) == (x + y)*log(w/3)
def test_diofantissue_296(): e = log(exp(1 / x) / Float(2) + exp(-1 / x) / 2) * x**2 assert e.limit(x, oo) == 0.5
def test_li(): z = Symbol("z") zr = Symbol("z", extended_real=True) zp = Symbol("z", positive=True) zn = Symbol("z", negative=True) assert li(0) == 0 assert li(1) == -oo assert li(oo) == oo assert isinstance(li(z), li) assert diff(li(z), z) == 1/log(z) assert conjugate(li(z)) == li(conjugate(z)) assert conjugate(li(-zr)) == li(-zr) assert conjugate(li(-zp)) == conjugate(li(-zp)) assert conjugate(li(zn)) == conjugate(li(zn)) assert li(z).rewrite(Li) == Li(z) + li(2) assert li(z).rewrite(Ei) == Ei(log(z)) assert li(z).rewrite(uppergamma) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - expint(1, -log(z))) assert li(z).rewrite(Si) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Ci) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Shi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(Chi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(hyper) == (log(z)*hyper((1, 1), (2, 2), log(z)) - log(1/log(z))/2 + log(log(z))/2 + EulerGamma) assert li(z).rewrite(meijerg) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - meijerg(((), (1,)), ((0, 0), ()), -log(z)))
def test_Function(): assert mathematica_code(f(x, y, z)) == 'f[x, y, z]' assert mathematica_code(sin(x)**cos(x)) == 'Sin[x]^Cos[x]' assert mathematica_code(sign(x)) == 'Sign[x]' assert mathematica_code(atanh(x), user_functions={'atanh': 'ArcTanh'}) == 'ArcTanh[x]' assert (mathematica_code(meijerg( ((1, 1), (3, 4)), ((1, ), ()), x)) == 'MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]') assert (mathematica_code(hyper( (1, 2, 3), (3, 4), x)) == 'HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]') assert mathematica_code(Min(x, y)) == 'Min[x, y]' assert mathematica_code(Max(x, y)) == 'Max[x, y]' assert mathematica_code(Max(x, 2)) == 'Max[2, x]' # issue sympy/sympy#15344 assert mathematica_code(binomial(x, y)) == 'Binomial[x, y]' assert mathematica_code(log(x)) == 'Log[x]' assert mathematica_code(tan(x)) == 'Tan[x]' assert mathematica_code(cot(x)) == 'Cot[x]' assert mathematica_code(asin(x)) == 'ArcSin[x]' assert mathematica_code(acos(x)) == 'ArcCos[x]' assert mathematica_code(atan(x)) == 'ArcTan[x]' assert mathematica_code(acot(x)) == 'ArcCot[x]' assert mathematica_code(sinh(x)) == 'Sinh[x]' assert mathematica_code(cosh(x)) == 'Cosh[x]' assert mathematica_code(tanh(x)) == 'Tanh[x]' assert mathematica_code(coth(x)) == 'Coth[x]' assert mathematica_code(asinh(x)) == 'ArcSinh[x]' assert mathematica_code(acosh(x)) == 'ArcCosh[x]' assert mathematica_code(atanh(x)) == 'ArcTanh[x]' assert mathematica_code(acoth(x)) == 'ArcCoth[x]' assert mathematica_code(sech(x)) == 'Sech[x]' assert mathematica_code(csch(x)) == 'Csch[x]' assert mathematica_code(erf(x)) == 'Erf[x]' assert mathematica_code(erfi(x)) == 'Erfi[x]' assert mathematica_code(erfc(x)) == 'Erfc[x]' assert mathematica_code(conjugate(x)) == 'Conjugate[x]' assert mathematica_code(re(x)) == 'Re[x]' assert mathematica_code(im(x)) == 'Im[x]' assert mathematica_code(polygamma(x, y)) == 'PolyGamma[x, y]' assert mathematica_code(factorial(x)) == 'Factorial[x]' assert mathematica_code(factorial2(x)) == 'Factorial2[x]' assert mathematica_code(rf(x, y)) == 'Pochhammer[x, y]' assert mathematica_code(gamma(x)) == 'Gamma[x]' assert mathematica_code(zeta(x)) == 'Zeta[x]' assert mathematica_code(Heaviside(x)) == 'UnitStep[x]' assert mathematica_code(fibonacci(x)) == 'Fibonacci[x]' assert mathematica_code(polylog(x, y)) == 'PolyLog[x, y]' assert mathematica_code(loggamma(x)) == 'LogGamma[x]' assert mathematica_code(uppergamma(x, y)) == 'Gamma[x, y]' class MyFunc1(Function): @classmethod def eval(cls, x): pass class MyFunc2(Function): @classmethod def eval(cls, x, y): pass pytest.raises( ValueError, lambda: mathematica_code(MyFunc1(x), user_functions={'MyFunc1': ['Myfunc1']})) assert mathematica_code(MyFunc1(x), user_functions={'MyFunc1': 'Myfunc1'}) == 'Myfunc1[x]' assert mathematica_code( MyFunc2(x, y), user_functions={'MyFunc2': [(lambda *x: False, 'Myfunc2')]}) == 'MyFunc2[x, y]'
def vlog1(x, base=None, evaluate=True): return log(x, base, evaluate=evaluate)