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_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(S.NaN, x, -oo) == S.NaN assert limit(O(2)*x, x, S.NaN) == S.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, (2*pi)**Rational(1, 3), dir="+") == oo assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), 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_sympyissue_6167_6151(): n = pi**1000 i = int(n) assert sign(n - i) == 1 assert abs(n - i) == n - i eps = pi**-1500 big = pi**1000 one = cos(x)**2 + sin(x)**2 e = big*one - big + eps assert sign(simplify(e)) == 1 for xi in (111, 11, 1, Rational(1, 10)): assert sign(e.subs({x: xi})) == 1
def test_sympyissue_5183(): # using list(...) so py.test can recalculate values tests = list( itertools.product( [x, -x], [-1, 1], [2, 3, Rational(1, 2), Rational(2, 3)], ['-', '+'])) results = (oo, oo, -oo, oo, -oo * I, oo, -oo * sign(cbrt(-1)), oo, 0, 0, 0, 0, 0, 0, 0, 0, oo, oo, oo, -oo, oo, -oo * I, oo, -oo * sign(cbrt(-1)), 0, 0, 0, 0, 0, 0, 0, 0) assert len(tests) == len(results) for i, (args, res) in enumerate(zip(tests, results)): y, s, e, d = args eq = y**(s * e) assert limit(eq, x, 0, dir=d) == res
def test_sympyissue_5183(): # using list(...) so py.test can recalculate values tests = list(itertools.product([x, -x], [-1, 1], [2, 3, Rational(1, 2), Rational(2, 3)], ['-', '+'])) results = (oo, oo, -oo, oo, -oo*I, oo, -oo*sign(cbrt(-1)), oo, 0, 0, 0, 0, 0, 0, 0, 0, oo, oo, oo, -oo, oo, -oo*I, oo, -oo*sign(cbrt(-1)), 0, 0, 0, 0, 0, 0, 0, 0) assert len(tests) == len(results) for i, (args, res) in enumerate(zip(tests, results)): y, s, e, d = args eq = y**(s*e) assert limit(eq, x, 0, dir=d) == res
def test_nsimplify(): assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5))/2 assert nsimplify((1 + sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2 assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == Rational(1, 2) - sqrt(3)*I/2 assert nsimplify(sin(3*pi/5, evaluate=False)) == sqrt(sqrt(5)/8 + Rational(5, 8)) assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2*atan('1/4')*I)) == Rational(49, 17) + 8*I/17 assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == cbrt(2) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).evalf(), rational=True) == Rational(109861228866811, 100000000000000) assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).evalf(3), [pi, log(2)]) == \ -pi/4 - log(2) + Rational(7, 4) assert nsimplify(x/7.0) == x/7 assert nsimplify(pi/1e2) == pi/100 assert nsimplify(pi/1e2, rational=False) == pi/100.0 assert nsimplify(pi/1e-7) == 10000000*pi assert not nsimplify( factor(-3.0*z**2*(z**2)**(-2.5) + 3*(z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == 33 assert nsimplify(33.33, tolerance=10, rational=True) == 30 assert nsimplify(37.76, tolerance=10, rational=True) == 40 assert nsimplify(-203.1) == -Rational(2031, 10) assert nsimplify(+.2, tolerance=0) == Rational(+1, 5) assert nsimplify(-.2, tolerance=0) == Rational(-1, 5) assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == -Rational(1111, 5000) # issue sympy/sympy#7211, PR sympy/sympy#4112 assert nsimplify(Float(2e-8)) == Rational(1, 50000000) # issue sympy/sympy#7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue sympy/sympy#10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i)*oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(Sum(1/n**2, (n, 1, oo)), [pi]) == pi**2/6
def test_singularities(): assert singularities(1, x) == set() assert singularities(x**2, x) == set() assert singularities(x / (x**2 + 3 * x + 2), x) == {-2, -1} assert singularities(1 / (1 + x), x) == {-1} assert singularities(1 / (1 + x)**2, x) == {-1} assert singularities(sign(x), x) == {0}
def test_abs(): assert abs(x).nseries(x, n=4) == x assert abs(-x).nseries(x, n=4) == x assert abs(x + 1).nseries(x, n=4) == x + 1 assert abs(sin(x)).nseries(x, n=4) == x - Rational(1, 6)*x**3 + O(x**5) assert abs(sin(-x)).nseries(x, n=4) == x - Rational(1, 6)*x**3 + O(x**5) assert abs(x - a).series(x, 1) == (x - a)*sign(1 - a)
def test_issue_74(): assert limit(sign(log(1 + 1/x)), x, oo) == +1 assert limit(sign(log(1 - 1/x)), x, oo) == -1 assert limit(sign(sin(+1/x)), x, oo) == +1 assert limit(sign(sin(-1/x)), x, oo) == -1 assert limit(sign(tan(+1/x)), x, oo) == +1 assert limit(sign(tan(-1/x)), x, oo) == -1 assert limit(sign(cos(pi/2 + 1/x)), x, oo) == -1 assert limit(sign(cos(pi/2 - 1/x)), x, oo) == +1
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_limit(): assert gruntz(x, x) == oo assert gruntz(-x, x) == -oo assert gruntz(-x, x) == -oo assert gruntz((-x)**2, x) == oo assert gruntz(-x**2, x) == -oo assert gruntz((1/x)*log(1/x), x) == 0 # Gruntz: p15, 2.11 assert gruntz(1/x, x) == 0 assert gruntz(exp(x), x) == oo assert gruntz(-exp(x), x) == -oo assert gruntz(exp(x)/x, x) == oo assert gruntz(1/x - exp(-x), x) == 0 assert gruntz(x + 1/x, x) == oo assert gruntz((1/x)**(1/x), x) == 1 # Gruntz: p15, 2.11 assert gruntz((exp(1/x) - 1)*x, x) == 1 assert gruntz(1 + 1/x, x) == 1 assert gruntz(-exp(1/x), x) == -1 assert gruntz(x + exp(-x), x) == oo assert gruntz(x + exp(-x**2), x) == oo assert gruntz(x + exp(-exp(x)), x) == oo assert gruntz(13 + 1/x - exp(-x), x) == 13 a = Symbol('a') assert gruntz(x - log(1 + exp(x)), x) == 0 assert gruntz(x - log(a + exp(x)), x) == 0 assert gruntz(exp(x)/(1 + exp(x)), x) == 1 assert gruntz(exp(x)/(a + exp(x)), x) == 1 assert gruntz((3**x + 5**x)**(1/x), x) == 5 # issue sympy/sympy#3463 assert gruntz(Ei(x + exp(-x))*exp(-x)*x, x) == 1 assert gruntz(1/li(x), x) == 0 assert gruntz(1/Li(x), x) == 0 # issue diofant/diofant#56 assert gruntz((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x) == oo # issue sympy/sympy#9471 assert gruntz((((27**(log(x, 3))))/x**3), x) == 1 assert gruntz((((27**(log(x, 3) + 1)))/x**3), x) == 27 # issue sympy/sympy#9449 y = Symbol('y') assert gruntz(x*(abs(1/x + y) - abs(y - 1/x))/2, x) == sign(y) # issue sympy/sympy#8481 assert gruntz(m**x * exp(-m) / factorial(x), x) == 0 # issue sympy/sympy#4187 assert gruntz(exp(1/x)*log(1/x) - Ei(1/x), x) == -EulerGamma assert gruntz(exp(x)*log(x) - Ei(x), x) == oo # issue sympy/sympy#10382 assert gruntz(fibonacci(x + 1)/fibonacci(x), x) == GoldenRatio assert gruntz(zeta(x), x) == 1 assert gruntz(zeta(m)*zeta(x), x) == zeta(m)
def test_sign_sympyissue_6167(): n = pi**1000 i = int(n) assert (n - i).round() == 1 # doesn't hang assert sign(n - i) == 1 # perhaps it's not possible to get the sign right when # only 1 digit is being requested for this situtation; # 2 digits works assert (n - x).evalf(1, subs={x: i}, maxn=400) > 0 assert (n - x).evalf(2, subs={x: i}, maxn=400) > 0
def test_abs(): assert abs(x).series(x, n=4) == x assert abs(-x).series(x, n=4) == x assert abs(x + 1).series(x, n=4) == x + 1 assert abs(sin(x)).series(x, n=4) == x - x**3/6 + O(x**4) assert abs(sin(-x)).series(x, n=4) == x - x**3/6 + O(x**4) assert abs(x - a).series(x, 1) == (x - a)/sign(1 - a) # issue sympy/sympy#5183 assert abs(x + x**2).series(n=1) == O(x) assert abs(x + x**2).series(n=2) == x + O(x**2)
def test_Abs_rewrite(): x = Symbol('x', extended_real=True) a = Abs(x).rewrite(Heaviside).expand() assert a == x*Heaviside(x) - x*Heaviside(-x) for i in [-2, -1, 0, 1, 2]: assert a.subs({x: i}) == abs(i) y = Symbol('y') assert Abs(y).rewrite(Heaviside) == Abs(y) x, y = Symbol('x', extended_real=True), Symbol('y') assert Abs(x).rewrite(Piecewise) == Piecewise((x, x >= 0), (-x, True)) assert Abs(y).rewrite(Piecewise) == Abs(y) assert Abs(y).rewrite(sign) == y/sign(y)
def test_conjugate(): a = Symbol("a", extended_real=True) b = Symbol("b", extended_real=True) c = Symbol("c", imaginary=True) d = Symbol("d", imaginary=True) x = Symbol('x') z = a + I*b + c + I*d zc = a - I*b - c + I*d assert conjugate(z) == zc assert conjugate(exp(z)) == exp(zc) assert conjugate(exp(I*x)) == exp(-I*conjugate(x)) assert conjugate(z**5) == zc**5 assert conjugate(abs(x)) == abs(x) assert conjugate(sign(z)) == sign(zc) assert conjugate(sin(z)) == sin(zc) assert conjugate(cos(z)) == cos(zc) assert conjugate(tan(z)) == tan(zc) assert conjugate(cot(z)) == cot(zc) assert conjugate(sinh(z)) == sinh(zc) assert conjugate(cosh(z)) == cosh(zc) assert conjugate(tanh(z)) == tanh(zc) assert conjugate(coth(z)) == coth(zc)
def test_minimize(): assert minimize(1) == (1, {}) assert minimize((x - 2)**2) == (0, {x: 2}) assert minimize((x - 2)**2, x) == (0, {x: 2}) assert minimize(1 / x, x) == (-oo, {x: 0}) assert minimize(2 * x**2 - 4 * x + 5, x) == (3, {x: 1}) assert minimize([2 * x**2 - 4 * x + 5, x > 0], x) == (3, {x: 1}) assert minimize([2 * x**2 - 4 * x + 5, x > 1], x) is None assert minimize([2 * x**2 - 4 * x + 5, x >= 2], x) == (5, {x: 2}) assert minimize([2 * x**2 - 4 * x + 5, x**2 <= 0], x) == (5, {x: 0}) assert minimize([x**2 - 1, (x - 1) * (x - 2) * (x - 3) * (4 - x) >= 0]) == (0, { x: 1 }) assert minimize([x**2, (x + 2) * (x - 1) * (1 - x) >= 0]) == (1, {x: 1}) assert minimize(sign(x), x) == (-1, {x: 0})
def test_Abs(): pytest.raises(TypeError, lambda: Abs(Interval(2, 3))) # issue sympy/sympy#8717 x, y = symbols('x,y') assert sign(sign(x)) == sign(x) assert isinstance(sign(x*y), 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 = cos(1)**2 + sin(1)**2 - 1 assert Abs(a*x).series(x).simplify() == 0 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).fdiff() == 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) assert Abs(x**pi) == Abs(x**pi, evaluate=False) x = Symbol('x', imaginary=True) assert Abs(x).diff(x) == -sign(x) pytest.raises(ArgumentIndexError, lambda: Abs(z).fdiff(2)) 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) == 0 q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = cbrt(expand(q**3)) d = p - q assert 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 assert abs(sign(z)) == Abs(sign(z), evaluate=False)
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 y = Symbol('y') assert sign(x*y).nseries(x).removeO() == sign(y) 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 Integer(-1) 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) == 0 q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = cbrt(expand(q**3)) d = p - q assert sign(d) == 0 assert abs(sign(z)) == Abs(sign(z), evaluate=False)
def test_sympyissue_8634(): p = Symbol('p', positive=True) assert limit(x**p, x, -oo) == oo*sign((-1)**p)
def test_sympyissue_7096(): assert gruntz((-1/x)**-pi, x) == oo*sign((-1)**(-pi))
def test_sympyissue_5740(): assert limit(log(x)*z - log(2*x)*y, x, 0) == oo*sign(y - z)
def test_sympyissue_7163(): integrate((sign(x - 1) - sign(x - 2))*cos(x), x) # not raises
def test_sympyissue_4099(): assert limit(a/x, x, 0) == oo*sign(a) assert limit(-a/x, x, 0) == -oo*sign(a) assert limit(-a*x, x, oo) == -oo*sign(a) assert limit(a*x, x, oo) == oo*sign(a)
def test_sympyissue_4035(): assert Abs(x).expand(trig=True) == Abs(x) assert sign(x).expand(trig=True) == sign(x) assert arg(x).expand(trig=True) == arg(x)
def test_sympyissue_5436(): # also issue sympy/sympy#13312 (but see diofant/diofant#425!) assert limit(exp(x*y), x, oo) == exp(oo*sign(y)) assert limit(exp(-x*y), x, oo) == exp(-oo*sign(y))
def test_sympyissue_13312(): assert integrate(exp(-a*t), (t, b, oo)) == Piecewise((-b + oo, Eq(a, 0)), (-exp(-oo*sign(a))/a + exp(-a*b)/a, True))
def test_log_sign(): assert sign(log(2)) == 1
def test_I(): y = Symbol("y") assert gruntz(I*x, x) == I*oo assert gruntz(y*I*x, x) == sign(y)*I*oo assert gruntz(y*3*I*x, x) == sign(y)*I*oo assert gruntz(y*3*sin(I)*x, x).simplify() == sign(y)*I*oo
def test_sympyissue_4212(): assert not isinstance(integrate(sign(x), x), Integral)
def test_singularities(): assert singularities(x**2, x) == set() assert singularities(x/(x**2 + 3*x + 2), x) == {-2, -1} assert singularities(1/(1 + x), x) == {-1} assert singularities(sign(x), x) == {0}
def test_fresnel(): assert fresnels(0) == 0 assert fresnels(+oo) == Rational(+1, 2) assert fresnels(-oo) == Rational(-1, 2) assert fresnels(z) == fresnels(z) assert fresnels(-z) == -fresnels(z) assert fresnels(I*z) == -I*fresnels(z) assert fresnels(-I*z) == I*fresnels(z) assert conjugate(fresnels(z)) == fresnels(conjugate(z)) assert fresnels(z).diff(z) == sin(pi*z**2/2) assert fresnels(z).rewrite(erf) == (1 + I)/4 * ( erf((1 + I)/2*sqrt(pi)*z) - I*erf((1 - I)/2*sqrt(pi)*z)) assert fresnels(z).rewrite(hyper) == \ pi*z**3/6 * hyper([Rational(3, 4)], [Rational(3, 2), Rational(7, 4)], -pi**2*z**4/16) assert fresnels(z).series(z, n=15) == \ pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15) assert fresnels(y/z).limit(z, 0) == fresnels(oo*sign(y)) assert fresnels(x).taylor_term(-1, z) == 0 assert fresnels(x).taylor_term(1, z, *(pi*z**3/6,)) == -pi**3*z**7/336 assert fresnels(x).taylor_term(1, z) == -pi**3*z**7/336 assert fresnels(w).is_extended_real is True assert fresnels(z).is_extended_real is None assert fresnels(z).as_real_imag() == \ ((fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 + fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2, I*(fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) - fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) * re(z)*Abs(im(z))/(2*im(z)*Abs(re(z))))) assert fresnels(z).as_real_imag(deep=False) == fresnels(z).as_real_imag() assert fresnels(w).as_real_imag() == (fresnels(w), 0) assert fresnels(w).as_real_imag(deep=False) == fresnels(w).as_real_imag() assert (fresnels(I, evaluate=False).as_real_imag() == (0, -erf(sqrt(pi)/2 + I*sqrt(pi)/2)/4 + I*(-erf(sqrt(pi)/2 + I*sqrt(pi)/2) + erf(sqrt(pi)/2 - I*sqrt(pi)/2))/4 - erf(sqrt(pi)/2 - I*sqrt(pi)/2)/4)) assert fresnels(2 + 3*I).as_real_imag() == ( fresnels(2 + 3*I)/2 + fresnels(2 - 3*I)/2, I*(fresnels(2 - 3*I) - fresnels(2 + 3*I))/2 ) assert expand_func(integrate(fresnels(z), z)) == \ z*fresnels(z) + cos(pi*z**2/2)/pi assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(9, 4) * \ meijerg(((), (1,)), ((Rational(3, 4),), (Rational(1, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(3, 4)*(z**2)**Rational(3, 4)) assert fresnelc(0) == 0 assert fresnelc(+oo) == Rational(+1, 2) assert fresnelc(-oo) == Rational(-1, 2) assert fresnelc(z) == fresnelc(z) assert fresnelc(-z) == -fresnelc(z) assert fresnelc(I*z) == I*fresnelc(z) assert fresnelc(-I*z) == -I*fresnelc(z) assert conjugate(fresnelc(z)) == fresnelc(conjugate(z)) assert fresnelc(z).diff(z) == cos(pi*z**2/2) pytest.raises(ArgumentIndexError, lambda: fresnels(z).fdiff(2)) pytest.raises(ArgumentIndexError, lambda: fresnelc(z).fdiff(2)) assert fresnelc(z).rewrite(erf) == (1 - I)/4 * ( erf((1 + I)/2*sqrt(pi)*z) + I*erf((1 - I)/2*sqrt(pi)*z)) assert fresnelc(z).rewrite(hyper) == \ z * hyper([Rational(1, 4)], [Rational(1, 2), Rational(5, 4)], -pi**2*z**4/16) assert fresnelc(x).taylor_term(-1, z) == 0 assert fresnelc(x).taylor_term(1, z, *(z,)) == -pi**2*z**5/40 assert fresnelc(x).taylor_term(1, z) == -pi**2*z**5/40 assert fresnelc(z).series(z, n=15) == \ z - pi**2*z**5/40 + pi**4*z**9/3456 - pi**6*z**13/599040 + O(z**15) assert fresnelc(y/z).limit(z, 0) == fresnelc(oo*sign(y)) # issue sympy/sympy#6510 assert fresnels(z).series(z, oo) == \ (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + \ (3/(pi**3*z**5) - 1/(pi*z) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + Rational(1, 2) assert fresnelc(z).series(z, oo) == \ (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + \ (-3/(pi**3*z**5) + 1/(pi*z) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + Rational(1, 2) assert fresnels(1/z).series(z) == \ (-z**3/pi**2 + O(z**6))*sin(pi/(2*z**2)) + (-z/pi + 3*z**5/pi**3 + O(z**6))*cos(pi/(2*z**2)) + Rational(1, 2) assert fresnelc(1/z).series(z) == \ (-z**3/pi**2 + O(z**6))*cos(pi/(2*z**2)) + (z/pi - 3*z**5/pi**3 + O(z**6))*sin(pi/(2*z**2)) + Rational(1, 2) assert fresnelc(w).is_extended_real is True assert fresnelc(z).as_real_imag() == \ ((fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 + fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2, I*(fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) - fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) * re(z)*Abs(im(z))/(2*im(z)*Abs(re(z))))) assert fresnelc(2 + 3*I).as_real_imag() == ( fresnelc(2 - 3*I)/2 + fresnelc(2 + 3*I)/2, I*(fresnelc(2 - 3*I) - fresnelc(2 + 3*I))/2 ) assert expand_func(integrate(fresnelc(z), z)) == \ z*fresnelc(z) - sin(pi*z**2/2)/pi assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(3, 4) * \ meijerg(((), (1,)), ((Rational(1, 4),), (Rational(3, 4), 0)), -pi**2*z**4/16)/(2*root(-z, 4)*root(z**2, 4)) verify_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z) verify_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z) verify_numerically(fresnels(z), fresnels(z).rewrite(hyper), z) verify_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z) verify_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z) verify_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z) verify_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z) verify_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z)