add read me
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,336 @@
|
||||
from sympy.core.numbers import (E, Rational, oo, pi, zoo)
|
||||
from sympy.core.singleton import S
|
||||
from sympy.core.symbol import Symbol
|
||||
from sympy.functions.elementary.exponential import (exp, log)
|
||||
from sympy.functions.elementary.miscellaneous import (Max, Min, sqrt)
|
||||
from sympy.functions.elementary.trigonometric import (cos, sin, tan)
|
||||
from sympy.calculus.accumulationbounds import AccumBounds
|
||||
from sympy.core import Add, Mul, Pow
|
||||
from sympy.core.expr import unchanged
|
||||
from sympy.testing.pytest import raises, XFAIL
|
||||
from sympy.abc import x
|
||||
|
||||
a = Symbol('a', real=True)
|
||||
B = AccumBounds
|
||||
|
||||
|
||||
def test_AccumBounds():
|
||||
assert B(1, 2).args == (1, 2)
|
||||
assert B(1, 2).delta is S.One
|
||||
assert B(1, 2).mid == Rational(3, 2)
|
||||
assert B(1, 3).is_real == True
|
||||
|
||||
assert B(1, 1) is S.One
|
||||
|
||||
assert B(1, 2) + 1 == B(2, 3)
|
||||
assert 1 + B(1, 2) == B(2, 3)
|
||||
assert B(1, 2) + B(2, 3) == B(3, 5)
|
||||
|
||||
assert -B(1, 2) == B(-2, -1)
|
||||
|
||||
assert B(1, 2) - 1 == B(0, 1)
|
||||
assert 1 - B(1, 2) == B(-1, 0)
|
||||
assert B(2, 3) - B(1, 2) == B(0, 2)
|
||||
|
||||
assert x + B(1, 2) == Add(B(1, 2), x)
|
||||
assert a + B(1, 2) == B(1 + a, 2 + a)
|
||||
assert B(1, 2) - x == Add(B(1, 2), -x)
|
||||
|
||||
assert B(-oo, 1) + oo == B(-oo, oo)
|
||||
assert B(1, oo) + oo is oo
|
||||
assert B(1, oo) - oo == B(-oo, oo)
|
||||
assert (-oo - B(-1, oo)) is -oo
|
||||
assert B(-oo, 1) - oo is -oo
|
||||
|
||||
assert B(1, oo) - oo == B(-oo, oo)
|
||||
assert B(-oo, 1) - (-oo) == B(-oo, oo)
|
||||
assert (oo - B(1, oo)) == B(-oo, oo)
|
||||
assert (-oo - B(1, oo)) is -oo
|
||||
|
||||
assert B(1, 2)/2 == B(S.Half, 1)
|
||||
assert 2/B(2, 3) == B(Rational(2, 3), 1)
|
||||
assert 1/B(-1, 1) == B(-oo, oo)
|
||||
|
||||
assert abs(B(1, 2)) == B(1, 2)
|
||||
assert abs(B(-2, -1)) == B(1, 2)
|
||||
assert abs(B(-2, 1)) == B(0, 2)
|
||||
assert abs(B(-1, 2)) == B(0, 2)
|
||||
c = Symbol('c')
|
||||
raises(ValueError, lambda: B(0, c))
|
||||
raises(ValueError, lambda: B(1, -1))
|
||||
r = Symbol('r', real=True)
|
||||
raises(ValueError, lambda: B(r, r - 1))
|
||||
|
||||
|
||||
def test_AccumBounds_mul():
|
||||
assert B(1, 2)*2 == B(2, 4)
|
||||
assert 2*B(1, 2) == B(2, 4)
|
||||
assert B(1, 2)*B(2, 3) == B(2, 6)
|
||||
assert B(0, 2)*B(2, oo) == B(0, oo)
|
||||
l, r = B(-oo, oo), B(-a, a)
|
||||
assert l*r == B(-oo, oo)
|
||||
assert r*l == B(-oo, oo)
|
||||
l, r = B(1, oo), B(-3, -2)
|
||||
assert l*r == B(-oo, -2)
|
||||
assert r*l == B(-oo, -2)
|
||||
assert B(1, 2)*0 == 0
|
||||
assert B(1, oo)*0 == B(0, oo)
|
||||
assert B(-oo, 1)*0 == B(-oo, 0)
|
||||
assert B(-oo, oo)*0 == B(-oo, oo)
|
||||
|
||||
assert B(1, 2)*x == Mul(B(1, 2), x, evaluate=False)
|
||||
|
||||
assert B(0, 2)*oo == B(0, oo)
|
||||
assert B(-2, 0)*oo == B(-oo, 0)
|
||||
assert B(0, 2)*(-oo) == B(-oo, 0)
|
||||
assert B(-2, 0)*(-oo) == B(0, oo)
|
||||
assert B(-1, 1)*oo == B(-oo, oo)
|
||||
assert B(-1, 1)*(-oo) == B(-oo, oo)
|
||||
assert B(-oo, oo)*oo == B(-oo, oo)
|
||||
|
||||
|
||||
def test_AccumBounds_div():
|
||||
assert B(-1, 3)/B(3, 4) == B(Rational(-1, 3), 1)
|
||||
assert B(-2, 4)/B(-3, 4) == B(-oo, oo)
|
||||
assert B(-3, -2)/B(-4, 0) == B(S.Half, oo)
|
||||
|
||||
# these two tests can have a better answer
|
||||
# after Union of B is improved
|
||||
assert B(-3, -2)/B(-2, 1) == B(-oo, oo)
|
||||
assert B(2, 3)/B(-2, 2) == B(-oo, oo)
|
||||
|
||||
assert B(-3, -2)/B(0, 4) == B(-oo, Rational(-1, 2))
|
||||
assert B(2, 4)/B(-3, 0) == B(-oo, Rational(-2, 3))
|
||||
assert B(2, 4)/B(0, 3) == B(Rational(2, 3), oo)
|
||||
|
||||
assert B(0, 1)/B(0, 1) == B(0, oo)
|
||||
assert B(-1, 0)/B(0, 1) == B(-oo, 0)
|
||||
assert B(-1, 2)/B(-2, 2) == B(-oo, oo)
|
||||
|
||||
assert 1/B(-1, 2) == B(-oo, oo)
|
||||
assert 1/B(0, 2) == B(S.Half, oo)
|
||||
assert (-1)/B(0, 2) == B(-oo, Rational(-1, 2))
|
||||
assert 1/B(-oo, 0) == B(-oo, 0)
|
||||
assert 1/B(-1, 0) == B(-oo, -1)
|
||||
assert (-2)/B(-oo, 0) == B(0, oo)
|
||||
assert 1/B(-oo, -1) == B(-1, 0)
|
||||
|
||||
assert B(1, 2)/a == Mul(B(1, 2), 1/a, evaluate=False)
|
||||
|
||||
assert B(1, 2)/0 == B(1, 2)*zoo
|
||||
assert B(1, oo)/oo == B(0, oo)
|
||||
assert B(1, oo)/(-oo) == B(-oo, 0)
|
||||
assert B(-oo, -1)/oo == B(-oo, 0)
|
||||
assert B(-oo, -1)/(-oo) == B(0, oo)
|
||||
assert B(-oo, oo)/oo == B(-oo, oo)
|
||||
assert B(-oo, oo)/(-oo) == B(-oo, oo)
|
||||
assert B(-1, oo)/oo == B(0, oo)
|
||||
assert B(-1, oo)/(-oo) == B(-oo, 0)
|
||||
assert B(-oo, 1)/oo == B(-oo, 0)
|
||||
assert B(-oo, 1)/(-oo) == B(0, oo)
|
||||
|
||||
|
||||
def test_issue_18795():
|
||||
r = Symbol('r', real=True)
|
||||
a = B(-1,1)
|
||||
c = B(7, oo)
|
||||
b = B(-oo, oo)
|
||||
assert c - tan(r) == B(7-tan(r), oo)
|
||||
assert b + tan(r) == B(-oo, oo)
|
||||
assert (a + r)/a == B(-oo, oo)*B(r - 1, r + 1)
|
||||
assert (b + a)/a == B(-oo, oo)
|
||||
|
||||
|
||||
def test_AccumBounds_func():
|
||||
assert (x**2 + 2*x + 1).subs(x, B(-1, 1)) == B(-1, 4)
|
||||
assert exp(B(0, 1)) == B(1, E)
|
||||
assert exp(B(-oo, oo)) == B(0, oo)
|
||||
assert log(B(3, 6)) == B(log(3), log(6))
|
||||
|
||||
|
||||
@XFAIL
|
||||
def test_AccumBounds_powf():
|
||||
nn = Symbol('nn', nonnegative=True)
|
||||
assert B(1 + nn, 2 + nn)**B(1, 2) == B(1 + nn, (2 + nn)**2)
|
||||
i = Symbol('i', integer=True, negative=True)
|
||||
assert B(1, 2)**i == B(2**i, 1)
|
||||
|
||||
|
||||
def test_AccumBounds_pow():
|
||||
assert B(0, 2)**2 == B(0, 4)
|
||||
assert B(-1, 1)**2 == B(0, 1)
|
||||
assert B(1, 2)**2 == B(1, 4)
|
||||
assert B(-1, 2)**3 == B(-1, 8)
|
||||
assert B(-1, 1)**0 == 1
|
||||
|
||||
assert B(1, 2)**Rational(5, 2) == B(1, 4*sqrt(2))
|
||||
assert B(0, 2)**S.Half == B(0, sqrt(2))
|
||||
|
||||
neg = Symbol('neg', negative=True)
|
||||
assert unchanged(Pow, B(neg, 1), S.Half)
|
||||
nn = Symbol('nn', nonnegative=True)
|
||||
assert B(nn, nn + 1)**S.Half == B(sqrt(nn), sqrt(nn + 1))
|
||||
assert B(nn, nn + 1)**nn == B(nn**nn, (nn + 1)**nn)
|
||||
assert unchanged(Pow, B(nn, nn + 1), x)
|
||||
i = Symbol('i', integer=True)
|
||||
assert B(1, 2)**i == B(Min(1, 2**i), Max(1, 2**i))
|
||||
i = Symbol('i', integer=True, nonnegative=True)
|
||||
assert B(1, 2)**i == B(1, 2**i)
|
||||
assert B(0, 1)**i == B(0**i, 1)
|
||||
|
||||
assert B(1, 5)**(-2) == B(Rational(1, 25), 1)
|
||||
assert B(-1, 3)**(-2) == B(0, oo)
|
||||
assert B(0, 2)**(-3) == B(Rational(1, 8), oo)
|
||||
assert B(-2, 0)**(-3) == B(-oo, -Rational(1, 8))
|
||||
assert B(0, 2)**(-2) == B(Rational(1, 4), oo)
|
||||
assert B(-1, 2)**(-3) == B(-oo, oo)
|
||||
assert B(-3, -2)**(-3) == B(Rational(-1, 8), Rational(-1, 27))
|
||||
assert B(-3, -2)**(-2) == B(Rational(1, 9), Rational(1, 4))
|
||||
assert B(0, oo)**S.Half == B(0, oo)
|
||||
assert B(-oo, 0)**(-2) == B(0, oo)
|
||||
assert B(-2, 0)**(-2) == B(Rational(1, 4), oo)
|
||||
|
||||
assert B(Rational(1, 3), S.Half)**oo is S.Zero
|
||||
assert B(0, S.Half)**oo is S.Zero
|
||||
assert B(S.Half, 1)**oo == B(0, oo)
|
||||
assert B(0, 1)**oo == B(0, oo)
|
||||
assert B(2, 3)**oo is oo
|
||||
assert B(1, 2)**oo == B(0, oo)
|
||||
assert B(S.Half, 3)**oo == B(0, oo)
|
||||
assert B(Rational(-1, 3), Rational(-1, 4))**oo is S.Zero
|
||||
assert B(-1, Rational(-1, 2))**oo is S.NaN
|
||||
assert B(-3, -2)**oo is zoo
|
||||
assert B(-2, -1)**oo is S.NaN
|
||||
assert B(-2, Rational(-1, 2))**oo is S.NaN
|
||||
assert B(Rational(-1, 2), S.Half)**oo is S.Zero
|
||||
assert B(Rational(-1, 2), 1)**oo == B(0, oo)
|
||||
assert B(Rational(-2, 3), 2)**oo == B(0, oo)
|
||||
assert B(-1, 1)**oo == B(-oo, oo)
|
||||
assert B(-1, S.Half)**oo == B(-oo, oo)
|
||||
assert B(-1, 2)**oo == B(-oo, oo)
|
||||
assert B(-2, S.Half)**oo == B(-oo, oo)
|
||||
|
||||
assert B(1, 2)**x == Pow(B(1, 2), x, evaluate=False)
|
||||
|
||||
assert B(2, 3)**(-oo) is S.Zero
|
||||
assert B(0, 2)**(-oo) == B(0, oo)
|
||||
assert B(-1, 2)**(-oo) == B(-oo, oo)
|
||||
|
||||
assert (tan(x)**sin(2*x)).subs(x, B(0, pi/2)) == \
|
||||
Pow(B(-oo, oo), B(0, 1))
|
||||
|
||||
|
||||
def test_AccumBounds_exponent():
|
||||
# base is 0
|
||||
z = 0**B(a, a + S.Half)
|
||||
assert z.subs(a, 0) == B(0, 1)
|
||||
assert z.subs(a, 1) == 0
|
||||
p = z.subs(a, -1)
|
||||
assert p.is_Pow and p.args == (0, B(-1, -S.Half))
|
||||
# base > 0
|
||||
# when base is 1 the type of bounds does not matter
|
||||
assert 1**B(a, a + 1) == 1
|
||||
# otherwise we need to know if 0 is in the bounds
|
||||
assert S.Half**B(-2, 2) == B(S(1)/4, 4)
|
||||
assert 2**B(-2, 2) == B(S(1)/4, 4)
|
||||
|
||||
# +eps may introduce +oo
|
||||
# if there is a negative integer exponent
|
||||
assert B(0, 1)**B(S(1)/2, 1) == B(0, 1)
|
||||
assert B(0, 1)**B(0, 1) == B(0, 1)
|
||||
|
||||
# positive bases have positive bounds
|
||||
assert B(2, 3)**B(-3, -2) == B(S(1)/27, S(1)/4)
|
||||
assert B(2, 3)**B(-3, 2) == B(S(1)/27, 9)
|
||||
|
||||
# bounds generating imaginary parts unevaluated
|
||||
assert unchanged(Pow, B(-1, 1), B(1, 2))
|
||||
assert B(0, S(1)/2)**B(1, oo) == B(0, S(1)/2)
|
||||
assert B(0, 1)**B(1, oo) == B(0, oo)
|
||||
assert B(0, 2)**B(1, oo) == B(0, oo)
|
||||
assert B(0, oo)**B(1, oo) == B(0, oo)
|
||||
assert B(S(1)/2, 1)**B(1, oo) == B(0, oo)
|
||||
assert B(S(1)/2, 1)**B(-oo, -1) == B(0, oo)
|
||||
assert B(S(1)/2, 1)**B(-oo, oo) == B(0, oo)
|
||||
assert B(S(1)/2, 2)**B(1, oo) == B(0, oo)
|
||||
assert B(S(1)/2, 2)**B(-oo, -1) == B(0, oo)
|
||||
assert B(S(1)/2, 2)**B(-oo, oo) == B(0, oo)
|
||||
assert B(S(1)/2, oo)**B(1, oo) == B(0, oo)
|
||||
assert B(S(1)/2, oo)**B(-oo, -1) == B(0, oo)
|
||||
assert B(S(1)/2, oo)**B(-oo, oo) == B(0, oo)
|
||||
assert B(1, 2)**B(1, oo) == B(0, oo)
|
||||
assert B(1, 2)**B(-oo, -1) == B(0, oo)
|
||||
assert B(1, 2)**B(-oo, oo) == B(0, oo)
|
||||
assert B(1, oo)**B(1, oo) == B(0, oo)
|
||||
assert B(1, oo)**B(-oo, -1) == B(0, oo)
|
||||
assert B(1, oo)**B(-oo, oo) == B(0, oo)
|
||||
assert B(2, oo)**B(1, oo) == B(2, oo)
|
||||
assert B(2, oo)**B(-oo, -1) == B(0, S(1)/2)
|
||||
assert B(2, oo)**B(-oo, oo) == B(0, oo)
|
||||
|
||||
|
||||
def test_comparison_AccumBounds():
|
||||
assert (B(1, 3) < 4) == S.true
|
||||
assert (B(1, 3) < -1) == S.false
|
||||
assert (B(1, 3) < 2).rel_op == '<'
|
||||
assert (B(1, 3) <= 2).rel_op == '<='
|
||||
|
||||
assert (B(1, 3) > 4) == S.false
|
||||
assert (B(1, 3) > -1) == S.true
|
||||
assert (B(1, 3) > 2).rel_op == '>'
|
||||
assert (B(1, 3) >= 2).rel_op == '>='
|
||||
|
||||
assert (B(1, 3) < B(4, 6)) == S.true
|
||||
assert (B(1, 3) < B(2, 4)).rel_op == '<'
|
||||
assert (B(1, 3) < B(-2, 0)) == S.false
|
||||
|
||||
assert (B(1, 3) <= B(4, 6)) == S.true
|
||||
assert (B(1, 3) <= B(-2, 0)) == S.false
|
||||
|
||||
assert (B(1, 3) > B(4, 6)) == S.false
|
||||
assert (B(1, 3) > B(-2, 0)) == S.true
|
||||
|
||||
assert (B(1, 3) >= B(4, 6)) == S.false
|
||||
assert (B(1, 3) >= B(-2, 0)) == S.true
|
||||
|
||||
# issue 13499
|
||||
assert (cos(x) > 0).subs(x, oo) == (B(-1, 1) > 0)
|
||||
|
||||
c = Symbol('c')
|
||||
raises(TypeError, lambda: (B(0, 1) < c))
|
||||
raises(TypeError, lambda: (B(0, 1) <= c))
|
||||
raises(TypeError, lambda: (B(0, 1) > c))
|
||||
raises(TypeError, lambda: (B(0, 1) >= c))
|
||||
|
||||
|
||||
def test_contains_AccumBounds():
|
||||
assert (1 in B(1, 2)) == S.true
|
||||
raises(TypeError, lambda: a in B(1, 2))
|
||||
assert 0 in B(-1, 0)
|
||||
raises(TypeError, lambda:
|
||||
(cos(1)**2 + sin(1)**2 - 1) in B(-1, 0))
|
||||
assert (-oo in B(1, oo)) == S.true
|
||||
assert (oo in B(-oo, 0)) == S.true
|
||||
|
||||
# issue 13159
|
||||
assert Mul(0, B(-1, 1)) == Mul(B(-1, 1), 0) == 0
|
||||
import itertools
|
||||
for perm in itertools.permutations([0, B(-1, 1), x]):
|
||||
assert Mul(*perm) == 0
|
||||
|
||||
|
||||
def test_intersection_AccumBounds():
|
||||
assert B(0, 3).intersection(B(1, 2)) == B(1, 2)
|
||||
assert B(0, 3).intersection(B(1, 4)) == B(1, 3)
|
||||
assert B(0, 3).intersection(B(-1, 2)) == B(0, 2)
|
||||
assert B(0, 3).intersection(B(-1, 4)) == B(0, 3)
|
||||
assert B(0, 1).intersection(B(2, 3)) == S.EmptySet
|
||||
raises(TypeError, lambda: B(0, 3).intersection(1))
|
||||
|
||||
|
||||
def test_union_AccumBounds():
|
||||
assert B(0, 3).union(B(1, 2)) == B(0, 3)
|
||||
assert B(0, 3).union(B(1, 4)) == B(0, 4)
|
||||
assert B(0, 3).union(B(-1, 2)) == B(-1, 3)
|
||||
assert B(0, 3).union(B(-1, 4)) == B(-1, 4)
|
||||
raises(TypeError, lambda: B(0, 3).union(1))
|
||||
@@ -0,0 +1,74 @@
|
||||
from sympy.core.function import (Derivative as D, Function)
|
||||
from sympy.core.relational import Eq
|
||||
from sympy.core.symbol import (Symbol, symbols)
|
||||
from sympy.functions.elementary.trigonometric import (cos, sin)
|
||||
from sympy.testing.pytest import raises
|
||||
from sympy.calculus.euler import euler_equations as euler
|
||||
|
||||
|
||||
def test_euler_interface():
|
||||
x = Function('x')
|
||||
y = Symbol('y')
|
||||
t = Symbol('t')
|
||||
raises(TypeError, lambda: euler())
|
||||
raises(TypeError, lambda: euler(D(x(t), t)*y(t), [x(t), y]))
|
||||
raises(ValueError, lambda: euler(D(x(t), t)*x(y), [x(t), x(y)]))
|
||||
raises(TypeError, lambda: euler(D(x(t), t)**2, x(0)))
|
||||
raises(TypeError, lambda: euler(D(x(t), t)*y(t), [t]))
|
||||
assert euler(D(x(t), t)**2/2, {x(t)}) == [Eq(-D(x(t), t, t), 0)]
|
||||
assert euler(D(x(t), t)**2/2, x(t), {t}) == [Eq(-D(x(t), t, t), 0)]
|
||||
|
||||
|
||||
def test_euler_pendulum():
|
||||
x = Function('x')
|
||||
t = Symbol('t')
|
||||
L = D(x(t), t)**2/2 + cos(x(t))
|
||||
assert euler(L, x(t), t) == [Eq(-sin(x(t)) - D(x(t), t, t), 0)]
|
||||
|
||||
|
||||
def test_euler_henonheiles():
|
||||
x = Function('x')
|
||||
y = Function('y')
|
||||
t = Symbol('t')
|
||||
L = sum(D(z(t), t)**2/2 - z(t)**2/2 for z in [x, y])
|
||||
L += -x(t)**2*y(t) + y(t)**3/3
|
||||
assert euler(L, [x(t), y(t)], t) == [Eq(-2*x(t)*y(t) - x(t) -
|
||||
D(x(t), t, t), 0),
|
||||
Eq(-x(t)**2 + y(t)**2 -
|
||||
y(t) - D(y(t), t, t), 0)]
|
||||
|
||||
|
||||
def test_euler_sineg():
|
||||
psi = Function('psi')
|
||||
t = Symbol('t')
|
||||
x = Symbol('x')
|
||||
L = D(psi(t, x), t)**2/2 - D(psi(t, x), x)**2/2 + cos(psi(t, x))
|
||||
assert euler(L, psi(t, x), [t, x]) == [Eq(-sin(psi(t, x)) -
|
||||
D(psi(t, x), t, t) +
|
||||
D(psi(t, x), x, x), 0)]
|
||||
|
||||
|
||||
def test_euler_high_order():
|
||||
# an example from hep-th/0309038
|
||||
m = Symbol('m')
|
||||
k = Symbol('k')
|
||||
x = Function('x')
|
||||
y = Function('y')
|
||||
t = Symbol('t')
|
||||
L = (m*D(x(t), t)**2/2 + m*D(y(t), t)**2/2 -
|
||||
k*D(x(t), t)*D(y(t), t, t) + k*D(y(t), t)*D(x(t), t, t))
|
||||
assert euler(L, [x(t), y(t)]) == [Eq(2*k*D(y(t), t, t, t) -
|
||||
m*D(x(t), t, t), 0),
|
||||
Eq(-2*k*D(x(t), t, t, t) -
|
||||
m*D(y(t), t, t), 0)]
|
||||
|
||||
w = Symbol('w')
|
||||
L = D(x(t, w), t, w)**2/2
|
||||
assert euler(L) == [Eq(D(x(t, w), t, t, w, w), 0)]
|
||||
|
||||
def test_issue_18653():
|
||||
x, y, z = symbols("x y z")
|
||||
f, g, h = symbols("f g h", cls=Function, args=(x, y))
|
||||
f, g, h = f(), g(), h()
|
||||
expr2 = f.diff(x)*h.diff(z)
|
||||
assert euler(expr2, (f,), (x, y)) == []
|
||||
@@ -0,0 +1,164 @@
|
||||
from itertools import product
|
||||
|
||||
from sympy.core.function import (Function, diff)
|
||||
from sympy.core.numbers import Rational
|
||||
from sympy.core.singleton import S
|
||||
from sympy.core.symbol import symbols
|
||||
from sympy.functions.elementary.exponential import exp
|
||||
from sympy.calculus.finite_diff import (
|
||||
apply_finite_diff, differentiate_finite, finite_diff_weights,
|
||||
_as_finite_diff
|
||||
)
|
||||
from sympy.testing.pytest import raises, warns_deprecated_sympy
|
||||
|
||||
|
||||
def test_apply_finite_diff():
|
||||
x, h = symbols('x h')
|
||||
f = Function('f')
|
||||
assert (apply_finite_diff(1, [x-h, x+h], [f(x-h), f(x+h)], x) -
|
||||
(f(x+h)-f(x-h))/(2*h)).simplify() == 0
|
||||
|
||||
assert (apply_finite_diff(1, [5, 6, 7], [f(5), f(6), f(7)], 5) -
|
||||
(Rational(-3, 2)*f(5) + 2*f(6) - S.Half*f(7))).simplify() == 0
|
||||
raises(ValueError, lambda: apply_finite_diff(1, [x, h], [f(x)]))
|
||||
|
||||
|
||||
def test_finite_diff_weights():
|
||||
|
||||
d = finite_diff_weights(1, [5, 6, 7], 5)
|
||||
assert d[1][2] == [Rational(-3, 2), 2, Rational(-1, 2)]
|
||||
|
||||
# Table 1, p. 702 in doi:10.1090/S0025-5718-1988-0935077-0
|
||||
# --------------------------------------------------------
|
||||
xl = [0, 1, -1, 2, -2, 3, -3, 4, -4]
|
||||
|
||||
# d holds all coefficients
|
||||
d = finite_diff_weights(4, xl, S.Zero)
|
||||
|
||||
# Zeroeth derivative
|
||||
for i in range(5):
|
||||
assert d[0][i] == [S.One] + [S.Zero]*8
|
||||
|
||||
# First derivative
|
||||
assert d[1][0] == [S.Zero]*9
|
||||
assert d[1][2] == [S.Zero, S.Half, Rational(-1, 2)] + [S.Zero]*6
|
||||
assert d[1][4] == [S.Zero, Rational(2, 3), Rational(-2, 3), Rational(-1, 12), Rational(1, 12)] + [S.Zero]*4
|
||||
assert d[1][6] == [S.Zero, Rational(3, 4), Rational(-3, 4), Rational(-3, 20), Rational(3, 20),
|
||||
Rational(1, 60), Rational(-1, 60)] + [S.Zero]*2
|
||||
assert d[1][8] == [S.Zero, Rational(4, 5), Rational(-4, 5), Rational(-1, 5), Rational(1, 5),
|
||||
Rational(4, 105), Rational(-4, 105), Rational(-1, 280), Rational(1, 280)]
|
||||
|
||||
# Second derivative
|
||||
for i in range(2):
|
||||
assert d[2][i] == [S.Zero]*9
|
||||
assert d[2][2] == [-S(2), S.One, S.One] + [S.Zero]*6
|
||||
assert d[2][4] == [Rational(-5, 2), Rational(4, 3), Rational(4, 3), Rational(-1, 12), Rational(-1, 12)] + [S.Zero]*4
|
||||
assert d[2][6] == [Rational(-49, 18), Rational(3, 2), Rational(3, 2), Rational(-3, 20), Rational(-3, 20),
|
||||
Rational(1, 90), Rational(1, 90)] + [S.Zero]*2
|
||||
assert d[2][8] == [Rational(-205, 72), Rational(8, 5), Rational(8, 5), Rational(-1, 5), Rational(-1, 5),
|
||||
Rational(8, 315), Rational(8, 315), Rational(-1, 560), Rational(-1, 560)]
|
||||
|
||||
# Third derivative
|
||||
for i in range(3):
|
||||
assert d[3][i] == [S.Zero]*9
|
||||
assert d[3][4] == [S.Zero, -S.One, S.One, S.Half, Rational(-1, 2)] + [S.Zero]*4
|
||||
assert d[3][6] == [S.Zero, Rational(-13, 8), Rational(13, 8), S.One, -S.One,
|
||||
Rational(-1, 8), Rational(1, 8)] + [S.Zero]*2
|
||||
assert d[3][8] == [S.Zero, Rational(-61, 30), Rational(61, 30), Rational(169, 120), Rational(-169, 120),
|
||||
Rational(-3, 10), Rational(3, 10), Rational(7, 240), Rational(-7, 240)]
|
||||
|
||||
# Fourth derivative
|
||||
for i in range(4):
|
||||
assert d[4][i] == [S.Zero]*9
|
||||
assert d[4][4] == [S(6), -S(4), -S(4), S.One, S.One] + [S.Zero]*4
|
||||
assert d[4][6] == [Rational(28, 3), Rational(-13, 2), Rational(-13, 2), S(2), S(2),
|
||||
Rational(-1, 6), Rational(-1, 6)] + [S.Zero]*2
|
||||
assert d[4][8] == [Rational(91, 8), Rational(-122, 15), Rational(-122, 15), Rational(169, 60), Rational(169, 60),
|
||||
Rational(-2, 5), Rational(-2, 5), Rational(7, 240), Rational(7, 240)]
|
||||
|
||||
# Table 2, p. 703 in doi:10.1090/S0025-5718-1988-0935077-0
|
||||
# --------------------------------------------------------
|
||||
xl = [[j/S(2) for j in list(range(-i*2+1, 0, 2))+list(range(1, i*2+1, 2))]
|
||||
for i in range(1, 5)]
|
||||
|
||||
# d holds all coefficients
|
||||
d = [finite_diff_weights({0: 1, 1: 2, 2: 4, 3: 4}[i], xl[i], 0) for
|
||||
i in range(4)]
|
||||
|
||||
# Zeroth derivative
|
||||
assert d[0][0][1] == [S.Half, S.Half]
|
||||
assert d[1][0][3] == [Rational(-1, 16), Rational(9, 16), Rational(9, 16), Rational(-1, 16)]
|
||||
assert d[2][0][5] == [Rational(3, 256), Rational(-25, 256), Rational(75, 128), Rational(75, 128),
|
||||
Rational(-25, 256), Rational(3, 256)]
|
||||
assert d[3][0][7] == [Rational(-5, 2048), Rational(49, 2048), Rational(-245, 2048), Rational(1225, 2048),
|
||||
Rational(1225, 2048), Rational(-245, 2048), Rational(49, 2048), Rational(-5, 2048)]
|
||||
|
||||
# First derivative
|
||||
assert d[0][1][1] == [-S.One, S.One]
|
||||
assert d[1][1][3] == [Rational(1, 24), Rational(-9, 8), Rational(9, 8), Rational(-1, 24)]
|
||||
assert d[2][1][5] == [Rational(-3, 640), Rational(25, 384), Rational(-75, 64),
|
||||
Rational(75, 64), Rational(-25, 384), Rational(3, 640)]
|
||||
assert d[3][1][7] == [Rational(5, 7168), Rational(-49, 5120),
|
||||
Rational(245, 3072), Rational(-1225, 1024),
|
||||
Rational(1225, 1024), Rational(-245, 3072),
|
||||
Rational(49, 5120), Rational(-5, 7168)]
|
||||
|
||||
# Reasonably the rest of the table is also correct... (testing of that
|
||||
# deemed excessive at the moment)
|
||||
raises(ValueError, lambda: finite_diff_weights(-1, [1, 2]))
|
||||
raises(ValueError, lambda: finite_diff_weights(1.2, [1, 2]))
|
||||
x = symbols('x')
|
||||
raises(ValueError, lambda: finite_diff_weights(x, [1, 2]))
|
||||
|
||||
|
||||
def test_as_finite_diff():
|
||||
x = symbols('x')
|
||||
f = Function('f')
|
||||
dx = Function('dx')
|
||||
|
||||
_as_finite_diff(f(x).diff(x), [x-2, x-1, x, x+1, x+2])
|
||||
|
||||
# Use of undefined functions in ``points``
|
||||
df_true = -f(x+dx(x)/2-dx(x+dx(x)/2)/2) / dx(x+dx(x)/2) \
|
||||
+ f(x+dx(x)/2+dx(x+dx(x)/2)/2) / dx(x+dx(x)/2)
|
||||
df_test = diff(f(x), x).as_finite_difference(points=dx(x), x0=x+dx(x)/2)
|
||||
assert (df_test - df_true).simplify() == 0
|
||||
|
||||
|
||||
def test_differentiate_finite():
|
||||
x, y, h = symbols('x y h')
|
||||
f = Function('f')
|
||||
with warns_deprecated_sympy():
|
||||
res0 = differentiate_finite(f(x, y) + exp(42), x, y, evaluate=True)
|
||||
xm, xp, ym, yp = [v + sign*S.Half for v, sign in product([x, y], [-1, 1])]
|
||||
ref0 = f(xm, ym) + f(xp, yp) - f(xm, yp) - f(xp, ym)
|
||||
assert (res0 - ref0).simplify() == 0
|
||||
|
||||
g = Function('g')
|
||||
with warns_deprecated_sympy():
|
||||
res1 = differentiate_finite(f(x)*g(x) + 42, x, evaluate=True)
|
||||
ref1 = (-f(x - S.Half) + f(x + S.Half))*g(x) + \
|
||||
(-g(x - S.Half) + g(x + S.Half))*f(x)
|
||||
assert (res1 - ref1).simplify() == 0
|
||||
|
||||
res2 = differentiate_finite(f(x) + x**3 + 42, x, points=[x-1, x+1])
|
||||
ref2 = (f(x + 1) + (x + 1)**3 - f(x - 1) - (x - 1)**3)/2
|
||||
assert (res2 - ref2).simplify() == 0
|
||||
raises(TypeError, lambda: differentiate_finite(f(x)*g(x), x,
|
||||
pints=[x-1, x+1]))
|
||||
|
||||
res3 = differentiate_finite(f(x)*g(x).diff(x), x)
|
||||
ref3 = (-g(x) + g(x + 1))*f(x + S.Half) - (g(x) - g(x - 1))*f(x - S.Half)
|
||||
assert res3 == ref3
|
||||
|
||||
res4 = differentiate_finite(f(x)*g(x).diff(x).diff(x), x)
|
||||
ref4 = -((g(x - Rational(3, 2)) - 2*g(x - S.Half) + g(x + S.Half))*f(x - S.Half)) \
|
||||
+ (g(x - S.Half) - 2*g(x + S.Half) + g(x + Rational(3, 2)))*f(x + S.Half)
|
||||
assert res4 == ref4
|
||||
|
||||
res5_expr = f(x).diff(x)*g(x).diff(x)
|
||||
res5 = differentiate_finite(res5_expr, points=[x-h, x, x+h])
|
||||
ref5 = (-2*f(x)/h + f(-h + x)/(2*h) + 3*f(h + x)/(2*h))*(-2*g(x)/h + g(-h + x)/(2*h) \
|
||||
+ 3*g(h + x)/(2*h))/(2*h) - (2*f(x)/h - 3*f(-h + x)/(2*h) - \
|
||||
f(h + x)/(2*h))*(2*g(x)/h - 3*g(-h + x)/(2*h) - g(h + x)/(2*h))/(2*h)
|
||||
assert res5 == ref5
|
||||
@@ -0,0 +1,122 @@
|
||||
from sympy.core.numbers import (I, Rational, pi, oo)
|
||||
from sympy.core.singleton import S
|
||||
from sympy.core.symbol import Symbol, Dummy
|
||||
from sympy.core.function import Lambda
|
||||
from sympy.functions.elementary.exponential import (exp, log)
|
||||
from sympy.functions.elementary.trigonometric import sec, csc
|
||||
from sympy.functions.elementary.hyperbolic import (coth, sech,
|
||||
atanh, asech, acoth, acsch)
|
||||
from sympy.functions.elementary.miscellaneous import sqrt
|
||||
from sympy.calculus.singularities import (
|
||||
singularities,
|
||||
is_increasing,
|
||||
is_strictly_increasing,
|
||||
is_decreasing,
|
||||
is_strictly_decreasing,
|
||||
is_monotonic
|
||||
)
|
||||
from sympy.sets import Interval, FiniteSet, Union, ImageSet
|
||||
from sympy.testing.pytest import raises
|
||||
from sympy.abc import x, y
|
||||
|
||||
|
||||
def test_singularities():
|
||||
x = Symbol('x')
|
||||
assert singularities(x**2, x) == S.EmptySet
|
||||
assert singularities(x/(x**2 + 3*x + 2), x) == FiniteSet(-2, -1)
|
||||
assert singularities(1/(x**2 + 1), x) == FiniteSet(I, -I)
|
||||
assert singularities(x/(x**3 + 1), x) == \
|
||||
FiniteSet(-1, (1 - sqrt(3) * I) / 2, (1 + sqrt(3) * I) / 2)
|
||||
assert singularities(1/(y**2 + 2*I*y + 1), y) == \
|
||||
FiniteSet(-I + sqrt(2)*I, -I - sqrt(2)*I)
|
||||
_n = Dummy('n')
|
||||
assert singularities(sech(x), x).dummy_eq(Union(
|
||||
ImageSet(Lambda(_n, 2*_n*I*pi + I*pi/2), S.Integers),
|
||||
ImageSet(Lambda(_n, 2*_n*I*pi + 3*I*pi/2), S.Integers)))
|
||||
assert singularities(coth(x), x).dummy_eq(Union(
|
||||
ImageSet(Lambda(_n, 2*_n*I*pi + I*pi), S.Integers),
|
||||
ImageSet(Lambda(_n, 2*_n*I*pi), S.Integers)))
|
||||
assert singularities(atanh(x), x) == FiniteSet(-1, 1)
|
||||
assert singularities(acoth(x), x) == FiniteSet(-1, 1)
|
||||
assert singularities(asech(x), x) == FiniteSet(0)
|
||||
assert singularities(acsch(x), x) == FiniteSet(0)
|
||||
|
||||
x = Symbol('x', real=True)
|
||||
assert singularities(1/(x**2 + 1), x) == S.EmptySet
|
||||
assert singularities(exp(1/x), x, S.Reals) == FiniteSet(0)
|
||||
assert singularities(exp(1/x), x, Interval(1, 2)) == S.EmptySet
|
||||
assert singularities(log((x - 2)**2), x, Interval(1, 3)) == FiniteSet(2)
|
||||
raises(NotImplementedError, lambda: singularities(x**-oo, x))
|
||||
assert singularities(sec(x), x, Interval(0, 3*pi)) == FiniteSet(
|
||||
pi/2, 3*pi/2, 5*pi/2)
|
||||
assert singularities(csc(x), x, Interval(0, 3*pi)) == FiniteSet(
|
||||
0, pi, 2*pi, 3*pi)
|
||||
|
||||
|
||||
def test_is_increasing():
|
||||
"""Test whether is_increasing returns correct value."""
|
||||
a = Symbol('a', negative=True)
|
||||
|
||||
assert is_increasing(x**3 - 3*x**2 + 4*x, S.Reals)
|
||||
assert is_increasing(-x**2, Interval(-oo, 0))
|
||||
assert not is_increasing(-x**2, Interval(0, oo))
|
||||
assert not is_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval(-2, 3))
|
||||
assert is_increasing(x**2 + y, Interval(1, oo), x)
|
||||
assert is_increasing(-x**2*a, Interval(1, oo), x)
|
||||
assert is_increasing(1)
|
||||
|
||||
assert is_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval(-2, 3)) is False
|
||||
|
||||
|
||||
def test_is_strictly_increasing():
|
||||
"""Test whether is_strictly_increasing returns correct value."""
|
||||
assert is_strictly_increasing(
|
||||
4*x**3 - 6*x**2 - 72*x + 30, Interval.Ropen(-oo, -2))
|
||||
assert is_strictly_increasing(
|
||||
4*x**3 - 6*x**2 - 72*x + 30, Interval.Lopen(3, oo))
|
||||
assert not is_strictly_increasing(
|
||||
4*x**3 - 6*x**2 - 72*x + 30, Interval.open(-2, 3))
|
||||
assert not is_strictly_increasing(-x**2, Interval(0, oo))
|
||||
assert not is_strictly_decreasing(1)
|
||||
|
||||
assert is_strictly_increasing(4*x**3 - 6*x**2 - 72*x + 30, Interval.open(-2, 3)) is False
|
||||
|
||||
|
||||
def test_is_decreasing():
|
||||
"""Test whether is_decreasing returns correct value."""
|
||||
b = Symbol('b', positive=True)
|
||||
|
||||
assert is_decreasing(1/(x**2 - 3*x), Interval.open(Rational(3,2), 3))
|
||||
assert is_decreasing(1/(x**2 - 3*x), Interval.open(1.5, 3))
|
||||
assert is_decreasing(1/(x**2 - 3*x), Interval.Lopen(3, oo))
|
||||
assert not is_decreasing(1/(x**2 - 3*x), Interval.Ropen(-oo, Rational(3, 2)))
|
||||
assert not is_decreasing(-x**2, Interval(-oo, 0))
|
||||
assert not is_decreasing(-x**2*b, Interval(-oo, 0), x)
|
||||
|
||||
|
||||
def test_is_strictly_decreasing():
|
||||
"""Test whether is_strictly_decreasing returns correct value."""
|
||||
assert is_strictly_decreasing(1/(x**2 - 3*x), Interval.Lopen(3, oo))
|
||||
assert not is_strictly_decreasing(
|
||||
1/(x**2 - 3*x), Interval.Ropen(-oo, Rational(3, 2)))
|
||||
assert not is_strictly_decreasing(-x**2, Interval(-oo, 0))
|
||||
assert not is_strictly_decreasing(1)
|
||||
assert is_strictly_decreasing(1/(x**2 - 3*x), Interval.open(Rational(3,2), 3))
|
||||
assert is_strictly_decreasing(1/(x**2 - 3*x), Interval.open(1.5, 3))
|
||||
|
||||
|
||||
def test_is_monotonic():
|
||||
"""Test whether is_monotonic returns correct value."""
|
||||
assert is_monotonic(1/(x**2 - 3*x), Interval.open(Rational(3,2), 3))
|
||||
assert is_monotonic(1/(x**2 - 3*x), Interval.open(1.5, 3))
|
||||
assert is_monotonic(1/(x**2 - 3*x), Interval.Lopen(3, oo))
|
||||
assert is_monotonic(x**3 - 3*x**2 + 4*x, S.Reals)
|
||||
assert not is_monotonic(-x**2, S.Reals)
|
||||
assert is_monotonic(x**2 + y + 1, Interval(1, 2), x)
|
||||
raises(NotImplementedError, lambda: is_monotonic(x**2 + y + 1))
|
||||
|
||||
|
||||
def test_issue_23401():
|
||||
x = Symbol('x')
|
||||
expr = (x + 1)/(-1.0e-3*x**2 + 0.1*x + 0.1)
|
||||
assert is_increasing(expr, Interval(1,2), x)
|
||||
@@ -0,0 +1,392 @@
|
||||
from sympy.core.function import Lambda
|
||||
from sympy.core.numbers import (E, I, Rational, oo, pi)
|
||||
from sympy.core.relational import Eq
|
||||
from sympy.core.singleton import S
|
||||
from sympy.core.symbol import (Dummy, Symbol)
|
||||
from sympy.functions.elementary.complexes import (Abs, re)
|
||||
from sympy.functions.elementary.exponential import (exp, log)
|
||||
from sympy.functions.elementary.integers import frac
|
||||
from sympy.functions.elementary.miscellaneous import sqrt
|
||||
from sympy.functions.elementary.piecewise import Piecewise
|
||||
from sympy.functions.elementary.trigonometric import (
|
||||
cos, cot, csc, sec, sin, tan, asin, acos, atan, acot, asec, acsc)
|
||||
from sympy.functions.elementary.hyperbolic import (sinh, cosh, tanh, coth,
|
||||
sech, csch, asinh, acosh, atanh, acoth, asech, acsch)
|
||||
from sympy.functions.special.gamma_functions import gamma
|
||||
from sympy.functions.special.error_functions import expint
|
||||
from sympy.matrices.expressions.matexpr import MatrixSymbol
|
||||
from sympy.simplify.simplify import simplify
|
||||
from sympy.calculus.util import (function_range, continuous_domain, not_empty_in,
|
||||
periodicity, lcim, is_convex,
|
||||
stationary_points, minimum, maximum)
|
||||
from sympy.sets.sets import (Interval, FiniteSet, Complement, Union)
|
||||
from sympy.sets.fancysets import ImageSet
|
||||
from sympy.sets.conditionset import ConditionSet
|
||||
from sympy.testing.pytest import XFAIL, raises, _both_exp_pow, slow
|
||||
from sympy.abc import x, y
|
||||
|
||||
a = Symbol('a', real=True)
|
||||
|
||||
def test_function_range():
|
||||
assert function_range(sin(x), x, Interval(-pi/2, pi/2)
|
||||
) == Interval(-1, 1)
|
||||
assert function_range(sin(x), x, Interval(0, pi)
|
||||
) == Interval(0, 1)
|
||||
assert function_range(tan(x), x, Interval(0, pi)
|
||||
) == Interval(-oo, oo)
|
||||
assert function_range(tan(x), x, Interval(pi/2, pi)
|
||||
) == Interval(-oo, 0)
|
||||
assert function_range((x + 3)/(x - 2), x, Interval(-5, 5)
|
||||
) == Union(Interval(-oo, Rational(2, 7)), Interval(Rational(8, 3), oo))
|
||||
assert function_range(1/(x**2), x, Interval(-1, 1)
|
||||
) == Interval(1, oo)
|
||||
assert function_range(exp(x), x, Interval(-1, 1)
|
||||
) == Interval(exp(-1), exp(1))
|
||||
assert function_range(log(x) - x, x, S.Reals
|
||||
) == Interval(-oo, -1)
|
||||
assert function_range(sqrt(3*x - 1), x, Interval(0, 2)
|
||||
) == Interval(0, sqrt(5))
|
||||
assert function_range(x*(x - 1) - (x**2 - x), x, S.Reals
|
||||
) == FiniteSet(0)
|
||||
assert function_range(x*(x - 1) - (x**2 - x) + y, x, S.Reals
|
||||
) == FiniteSet(y)
|
||||
assert function_range(sin(x), x, Union(Interval(-5, -3), FiniteSet(4))
|
||||
) == Union(Interval(-sin(3), 1), FiniteSet(sin(4)))
|
||||
assert function_range(cos(x), x, Interval(-oo, -4)
|
||||
) == Interval(-1, 1)
|
||||
assert function_range(cos(x), x, S.EmptySet) == S.EmptySet
|
||||
assert function_range(x/sqrt(x**2+1), x, S.Reals) == Interval.open(-1,1)
|
||||
raises(NotImplementedError, lambda : function_range(
|
||||
exp(x)*(sin(x) - cos(x))/2 - x, x, S.Reals))
|
||||
raises(NotImplementedError, lambda : function_range(
|
||||
sin(x) + x, x, S.Reals)) # issue 13273
|
||||
raises(NotImplementedError, lambda : function_range(
|
||||
log(x), x, S.Integers))
|
||||
raises(NotImplementedError, lambda : function_range(
|
||||
sin(x)/2, x, S.Naturals))
|
||||
|
||||
|
||||
@slow
|
||||
def test_function_range1():
|
||||
assert function_range(tan(x)**2 + tan(3*x)**2 + 1, x, S.Reals) == Interval(1,oo)
|
||||
|
||||
|
||||
def test_continuous_domain():
|
||||
assert continuous_domain(sin(x), x, Interval(0, 2*pi)) == Interval(0, 2*pi)
|
||||
assert continuous_domain(tan(x), x, Interval(0, 2*pi)) == \
|
||||
Union(Interval(0, pi/2, False, True), Interval(pi/2, pi*Rational(3, 2), True, True),
|
||||
Interval(pi*Rational(3, 2), 2*pi, True, False))
|
||||
assert continuous_domain(cot(x), x, Interval(0, 2*pi)) == Union(
|
||||
Interval.open(0, pi), Interval.open(pi, 2*pi))
|
||||
assert continuous_domain((x - 1)/((x - 1)**2), x, S.Reals) == \
|
||||
Union(Interval(-oo, 1, True, True), Interval(1, oo, True, True))
|
||||
assert continuous_domain(log(x) + log(4*x - 1), x, S.Reals) == \
|
||||
Interval(Rational(1, 4), oo, True, True)
|
||||
assert continuous_domain(1/sqrt(x - 3), x, S.Reals) == Interval(3, oo, True, True)
|
||||
assert continuous_domain(1/x - 2, x, S.Reals) == \
|
||||
Union(Interval.open(-oo, 0), Interval.open(0, oo))
|
||||
assert continuous_domain(1/(x**2 - 4) + 2, x, S.Reals) == \
|
||||
Union(Interval.open(-oo, -2), Interval.open(-2, 2), Interval.open(2, oo))
|
||||
assert continuous_domain((x+1)**pi, x, S.Reals) == Interval(-1, oo)
|
||||
assert continuous_domain((x+1)**(pi/2), x, S.Reals) == Interval(-1, oo)
|
||||
assert continuous_domain(x**x, x, S.Reals) == Interval(0, oo)
|
||||
assert continuous_domain((x+1)**log(x**2), x, S.Reals) == Union(
|
||||
Interval.Ropen(-1, 0), Interval.open(0, oo))
|
||||
domain = continuous_domain(log(tan(x)**2 + 1), x, S.Reals)
|
||||
assert not domain.contains(3*pi/2)
|
||||
assert domain.contains(5)
|
||||
d = Symbol('d', even=True, zero=False)
|
||||
assert continuous_domain(x**(1/d), x, S.Reals) == Interval(0, oo)
|
||||
n = Dummy('n')
|
||||
assert continuous_domain(1/sin(x), x, S.Reals).dummy_eq(Complement(
|
||||
S.Reals, Union(ImageSet(Lambda(n, 2*n*pi + pi), S.Integers),
|
||||
ImageSet(Lambda(n, 2*n*pi), S.Integers))))
|
||||
assert continuous_domain(sin(x) + cos(x), x, S.Reals) == S.Reals
|
||||
assert continuous_domain(asin(x), x, S.Reals) == Interval(-1, 1) # issue #21786
|
||||
assert continuous_domain(1/acos(log(x)), x, S.Reals) == Interval.Ropen(exp(-1), E)
|
||||
assert continuous_domain(sinh(x)+cosh(x), x, S.Reals) == S.Reals
|
||||
assert continuous_domain(tanh(x)+sech(x), x, S.Reals) == S.Reals
|
||||
assert continuous_domain(atan(x)+asinh(x), x, S.Reals) == S.Reals
|
||||
assert continuous_domain(acosh(x), x, S.Reals) == Interval(1, oo)
|
||||
assert continuous_domain(atanh(x), x, S.Reals) == Interval.open(-1, 1)
|
||||
assert continuous_domain(atanh(x)+acosh(x), x, S.Reals) == S.EmptySet
|
||||
assert continuous_domain(asech(x), x, S.Reals) == Interval.Lopen(0, 1)
|
||||
assert continuous_domain(acoth(x), x, S.Reals) == Union(
|
||||
Interval.open(-oo, -1), Interval.open(1, oo))
|
||||
assert continuous_domain(asec(x), x, S.Reals) == Union(
|
||||
Interval(-oo, -1), Interval(1, oo))
|
||||
assert continuous_domain(acsc(x), x, S.Reals) == Union(
|
||||
Interval(-oo, -1), Interval(1, oo))
|
||||
for f in (coth, acsch, csch):
|
||||
assert continuous_domain(f(x), x, S.Reals) == Union(
|
||||
Interval.open(-oo, 0), Interval.open(0, oo))
|
||||
assert continuous_domain(acot(x), x, S.Reals).contains(0) == False
|
||||
assert continuous_domain(1/(exp(x) - x), x, S.Reals) == Complement(
|
||||
S.Reals, ConditionSet(x, Eq(-x + exp(x), 0), S.Reals))
|
||||
assert continuous_domain(frac(x**2), x, Interval(-2,-1)) == Union(
|
||||
Interval.open(-2, -sqrt(3)), Interval.open(-sqrt(2), -1),
|
||||
Interval.open(-sqrt(3), -sqrt(2)))
|
||||
assert continuous_domain(frac(x), x, S.Reals) == Complement(
|
||||
S.Reals, S.Integers)
|
||||
raises(NotImplementedError, lambda : continuous_domain(
|
||||
1/(x**2+1), x, S.Complexes))
|
||||
raises(NotImplementedError, lambda : continuous_domain(
|
||||
gamma(x), x, Interval(-5,0)))
|
||||
assert continuous_domain(x + gamma(pi), x, S.Reals) == S.Reals
|
||||
|
||||
|
||||
@XFAIL
|
||||
def test_continuous_domain_acot():
|
||||
acot_cont = Piecewise((pi+acot(x), x<0), (acot(x), True))
|
||||
assert continuous_domain(acot_cont, x, S.Reals) == S.Reals
|
||||
|
||||
@XFAIL
|
||||
def test_continuous_domain_gamma():
|
||||
assert continuous_domain(gamma(x), x, S.Reals).contains(-1) == False
|
||||
|
||||
@XFAIL
|
||||
def test_continuous_domain_neg_power():
|
||||
assert continuous_domain((x-2)**(1-x), x, S.Reals) == Interval.open(2, oo)
|
||||
|
||||
|
||||
def test_not_empty_in():
|
||||
assert not_empty_in(FiniteSet(x, 2*x).intersect(Interval(1, 2, True, False)), x) == \
|
||||
Interval(S.Half, 2, True, False)
|
||||
assert not_empty_in(FiniteSet(x, x**2).intersect(Interval(1, 2)), x) == \
|
||||
Union(Interval(-sqrt(2), -1), Interval(1, 2))
|
||||
assert not_empty_in(FiniteSet(x**2 + x, x).intersect(Interval(2, 4)), x) == \
|
||||
Union(Interval(-sqrt(17)/2 - S.Half, -2),
|
||||
Interval(1, Rational(-1, 2) + sqrt(17)/2), Interval(2, 4))
|
||||
assert not_empty_in(FiniteSet(x/(x - 1)).intersect(S.Reals), x) == \
|
||||
Complement(S.Reals, FiniteSet(1))
|
||||
assert not_empty_in(FiniteSet(a/(a - 1)).intersect(S.Reals), a) == \
|
||||
Complement(S.Reals, FiniteSet(1))
|
||||
assert not_empty_in(FiniteSet((x**2 - 3*x + 2)/(x - 1)).intersect(S.Reals), x) == \
|
||||
Complement(S.Reals, FiniteSet(1))
|
||||
assert not_empty_in(FiniteSet(3, 4, x/(x - 1)).intersect(Interval(2, 3)), x) == \
|
||||
Interval(-oo, oo)
|
||||
assert not_empty_in(FiniteSet(4, x/(x - 1)).intersect(Interval(2, 3)), x) == \
|
||||
Interval(S(3)/2, 2)
|
||||
assert not_empty_in(FiniteSet(x/(x**2 - 1)).intersect(S.Reals), x) == \
|
||||
Complement(S.Reals, FiniteSet(-1, 1))
|
||||
assert not_empty_in(FiniteSet(x, x**2).intersect(Union(Interval(1, 3, True, True),
|
||||
Interval(4, 5))), x) == \
|
||||
Union(Interval(-sqrt(5), -2), Interval(-sqrt(3), -1, True, True),
|
||||
Interval(1, 3, True, True), Interval(4, 5))
|
||||
assert not_empty_in(FiniteSet(1).intersect(Interval(3, 4)), x) == S.EmptySet
|
||||
assert not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x) == \
|
||||
Union(Interval(-2, -1, True, False), Interval(2, oo))
|
||||
raises(ValueError, lambda: not_empty_in(x))
|
||||
raises(ValueError, lambda: not_empty_in(Interval(0, 1), x))
|
||||
raises(NotImplementedError,
|
||||
lambda: not_empty_in(FiniteSet(x).intersect(S.Reals), x, a))
|
||||
|
||||
|
||||
@_both_exp_pow
|
||||
def test_periodicity():
|
||||
assert periodicity(sin(2*x), x) == pi
|
||||
assert periodicity((-2)*tan(4*x), x) == pi/4
|
||||
assert periodicity(sin(x)**2, x) == 2*pi
|
||||
assert periodicity(3**tan(3*x), x) == pi/3
|
||||
assert periodicity(tan(x)*cos(x), x) == 2*pi
|
||||
assert periodicity(sin(x)**(tan(x)), x) == 2*pi
|
||||
assert periodicity(tan(x)*sec(x), x) == 2*pi
|
||||
assert periodicity(sin(2*x)*cos(2*x) - y, x) == pi/2
|
||||
assert periodicity(tan(x) + cot(x), x) == pi
|
||||
assert periodicity(sin(x) - cos(2*x), x) == 2*pi
|
||||
assert periodicity(sin(x) - 1, x) == 2*pi
|
||||
assert periodicity(sin(4*x) + sin(x)*cos(x), x) == pi
|
||||
assert periodicity(exp(sin(x)), x) == 2*pi
|
||||
assert periodicity(log(cot(2*x)) - sin(cos(2*x)), x) == pi
|
||||
assert periodicity(sin(2*x)*exp(tan(x) - csc(2*x)), x) == pi
|
||||
assert periodicity(cos(sec(x) - csc(2*x)), x) == 2*pi
|
||||
assert periodicity(tan(sin(2*x)), x) == pi
|
||||
assert periodicity(2*tan(x)**2, x) == pi
|
||||
assert periodicity(sin(x%4), x) == 4
|
||||
assert periodicity(sin(x)%4, x) == 2*pi
|
||||
assert periodicity(tan((3*x-2)%4), x) == Rational(4, 3)
|
||||
assert periodicity((sqrt(2)*(x+1)+x) % 3, x) == 3 / (sqrt(2)+1)
|
||||
assert periodicity((x**2+1) % x, x) is None
|
||||
assert periodicity(sin(re(x)), x) == 2*pi
|
||||
assert periodicity(sin(x)**2 + cos(x)**2, x) is S.Zero
|
||||
assert periodicity(tan(x), y) is S.Zero
|
||||
assert periodicity(sin(x) + I*cos(x), x) == 2*pi
|
||||
assert periodicity(x - sin(2*y), y) == pi
|
||||
|
||||
assert periodicity(exp(x), x) is None
|
||||
assert periodicity(exp(I*x), x) == 2*pi
|
||||
assert periodicity(exp(I*a), a) == 2*pi
|
||||
assert periodicity(exp(a), a) is None
|
||||
assert periodicity(exp(log(sin(a) + I*cos(2*a)), evaluate=False), a) == 2*pi
|
||||
assert periodicity(exp(log(sin(2*a) + I*cos(a)), evaluate=False), a) == 2*pi
|
||||
assert periodicity(exp(sin(a)), a) == 2*pi
|
||||
assert periodicity(exp(2*I*a), a) == pi
|
||||
assert periodicity(exp(a + I*sin(a)), a) is None
|
||||
assert periodicity(exp(cos(a/2) + sin(a)), a) == 4*pi
|
||||
assert periodicity(log(x), x) is None
|
||||
assert periodicity(exp(x)**sin(x), x) is None
|
||||
assert periodicity(sin(x)**y, y) is None
|
||||
|
||||
assert periodicity(Abs(sin(Abs(sin(x)))), x) == pi
|
||||
assert all(periodicity(Abs(f(x)), x) == pi for f in (
|
||||
cos, sin, sec, csc, tan, cot))
|
||||
assert periodicity(Abs(sin(tan(x))), x) == pi
|
||||
assert periodicity(Abs(sin(sin(x) + tan(x))), x) == 2*pi
|
||||
assert periodicity(sin(x) > S.Half, x) == 2*pi
|
||||
|
||||
assert periodicity(x > 2, x) is None
|
||||
assert periodicity(x**3 - x**2 + 1, x) is None
|
||||
assert periodicity(Abs(x), x) is None
|
||||
assert periodicity(Abs(x**2 - 1), x) is None
|
||||
|
||||
assert periodicity((x**2 + 4)%2, x) is None
|
||||
assert periodicity((E**x)%3, x) is None
|
||||
|
||||
assert periodicity(sin(expint(1, x))/expint(1, x), x) is None
|
||||
# returning `None` for any Piecewise
|
||||
p = Piecewise((0, x < -1), (x**2, x <= 1), (log(x), True))
|
||||
assert periodicity(p, x) is None
|
||||
|
||||
m = MatrixSymbol('m', 3, 3)
|
||||
raises(NotImplementedError, lambda: periodicity(sin(m), m))
|
||||
raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m))
|
||||
raises(NotImplementedError, lambda: periodicity(sin(m), m[0, 0]))
|
||||
raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m[0, 0]))
|
||||
|
||||
|
||||
def test_periodicity_check():
|
||||
assert periodicity(tan(x), x, check=True) == pi
|
||||
assert periodicity(sin(x) + cos(x), x, check=True) == 2*pi
|
||||
assert periodicity(sec(x), x) == 2*pi
|
||||
assert periodicity(sin(x*y), x) == 2*pi/abs(y)
|
||||
assert periodicity(Abs(sec(sec(x))), x) == pi
|
||||
|
||||
|
||||
def test_lcim():
|
||||
assert lcim([S.Half, S(2), S(3)]) == 6
|
||||
assert lcim([pi/2, pi/4, pi]) == pi
|
||||
assert lcim([2*pi, pi/2]) == 2*pi
|
||||
assert lcim([S.One, 2*pi]) is None
|
||||
assert lcim([S(2) + 2*E, E/3 + Rational(1, 3), S.One + E]) == S(2) + 2*E
|
||||
|
||||
|
||||
def test_is_convex():
|
||||
assert is_convex(1/x, x, domain=Interval.open(0, oo)) == True
|
||||
assert is_convex(1/x, x, domain=Interval(-oo, 0)) == False
|
||||
assert is_convex(x**2, x, domain=Interval(0, oo)) == True
|
||||
assert is_convex(1/x**3, x, domain=Interval.Lopen(0, oo)) == True
|
||||
assert is_convex(-1/x**3, x, domain=Interval.Ropen(-oo, 0)) == True
|
||||
assert is_convex(log(x) ,x) == False
|
||||
assert is_convex(x**2+y**2, x, y) == True
|
||||
assert is_convex(cos(x) + cos(y), x) == False
|
||||
assert is_convex(8*x**2 - 2*y**2, x, y) == False
|
||||
|
||||
|
||||
def test_stationary_points():
|
||||
assert stationary_points(sin(x), x, Interval(-pi/2, pi/2)
|
||||
) == {-pi/2, pi/2}
|
||||
assert stationary_points(sin(x), x, Interval.Ropen(0, pi/4)
|
||||
) is S.EmptySet
|
||||
assert stationary_points(tan(x), x,
|
||||
) is S.EmptySet
|
||||
assert stationary_points(sin(x)*cos(x), x, Interval(0, pi)
|
||||
) == {pi/4, pi*Rational(3, 4)}
|
||||
assert stationary_points(sec(x), x, Interval(0, pi)
|
||||
) == {0, pi}
|
||||
assert stationary_points((x+3)*(x-2), x
|
||||
) == FiniteSet(Rational(-1, 2))
|
||||
assert stationary_points((x + 3)/(x - 2), x, Interval(-5, 5)
|
||||
) is S.EmptySet
|
||||
assert stationary_points((x**2+3)/(x-2), x
|
||||
) == {2 - sqrt(7), 2 + sqrt(7)}
|
||||
assert stationary_points((x**2+3)/(x-2), x, Interval(0, 5)
|
||||
) == {2 + sqrt(7)}
|
||||
assert stationary_points(x**4 + x**3 - 5*x**2, x, S.Reals
|
||||
) == FiniteSet(-2, 0, Rational(5, 4))
|
||||
assert stationary_points(exp(x), x
|
||||
) is S.EmptySet
|
||||
assert stationary_points(log(x) - x, x, S.Reals
|
||||
) == {1}
|
||||
assert stationary_points(cos(x), x, Union(Interval(0, 5), Interval(-6, -3))
|
||||
) == {0, -pi, pi}
|
||||
assert stationary_points(y, x, S.Reals
|
||||
) == S.Reals
|
||||
assert stationary_points(y, x, S.EmptySet) == S.EmptySet
|
||||
|
||||
|
||||
def test_maximum():
|
||||
assert maximum(sin(x), x) is S.One
|
||||
assert maximum(sin(x), x, Interval(0, 1)) == sin(1)
|
||||
assert maximum(tan(x), x) is oo
|
||||
assert maximum(tan(x), x, Interval(-pi/4, pi/4)) is S.One
|
||||
assert maximum(sin(x)*cos(x), x, S.Reals) == S.Half
|
||||
assert simplify(maximum(sin(x)*cos(x), x, Interval(pi*Rational(3, 8), pi*Rational(5, 8)))
|
||||
) == sqrt(2)/4
|
||||
assert maximum((x+3)*(x-2), x) is oo
|
||||
assert maximum((x+3)*(x-2), x, Interval(-5, 0)) == S(14)
|
||||
assert maximum((x+3)/(x-2), x, Interval(-5, 0)) == Rational(2, 7)
|
||||
assert simplify(maximum(-x**4-x**3+x**2+10, x)
|
||||
) == 41*sqrt(41)/512 + Rational(5419, 512)
|
||||
assert maximum(exp(x), x, Interval(-oo, 2)) == exp(2)
|
||||
assert maximum(log(x) - x, x, S.Reals) is S.NegativeOne
|
||||
assert maximum(cos(x), x, Union(Interval(0, 5), Interval(-6, -3))
|
||||
) is S.One
|
||||
assert maximum(cos(x)-sin(x), x, S.Reals) == sqrt(2)
|
||||
assert maximum(y, x, S.Reals) == y
|
||||
assert maximum(abs(a**3 + a), a, Interval(0, 2)) == 10
|
||||
assert maximum(abs(60*a**3 + 24*a), a, Interval(0, 2)) == 528
|
||||
assert maximum(abs(12*a*(5*a**2 + 2)), a, Interval(0, 2)) == 528
|
||||
assert maximum(x/sqrt(x**2+1), x, S.Reals) == 1
|
||||
|
||||
raises(ValueError, lambda : maximum(sin(x), x, S.EmptySet))
|
||||
raises(ValueError, lambda : maximum(log(cos(x)), x, S.EmptySet))
|
||||
raises(ValueError, lambda : maximum(1/(x**2 + y**2 + 1), x, S.EmptySet))
|
||||
raises(ValueError, lambda : maximum(sin(x), sin(x)))
|
||||
raises(ValueError, lambda : maximum(sin(x), x*y, S.EmptySet))
|
||||
raises(ValueError, lambda : maximum(sin(x), S.One))
|
||||
|
||||
|
||||
def test_minimum():
|
||||
assert minimum(sin(x), x) is S.NegativeOne
|
||||
assert minimum(sin(x), x, Interval(1, 4)) == sin(4)
|
||||
assert minimum(tan(x), x) is -oo
|
||||
assert minimum(tan(x), x, Interval(-pi/4, pi/4)) is S.NegativeOne
|
||||
assert minimum(sin(x)*cos(x), x, S.Reals) == Rational(-1, 2)
|
||||
assert simplify(minimum(sin(x)*cos(x), x, Interval(pi*Rational(3, 8), pi*Rational(5, 8)))
|
||||
) == -sqrt(2)/4
|
||||
assert minimum((x+3)*(x-2), x) == Rational(-25, 4)
|
||||
assert minimum((x+3)/(x-2), x, Interval(-5, 0)) == Rational(-3, 2)
|
||||
assert minimum(x**4-x**3+x**2+10, x) == S(10)
|
||||
assert minimum(exp(x), x, Interval(-2, oo)) == exp(-2)
|
||||
assert minimum(log(x) - x, x, S.Reals) is -oo
|
||||
assert minimum(cos(x), x, Union(Interval(0, 5), Interval(-6, -3))
|
||||
) is S.NegativeOne
|
||||
assert minimum(cos(x)-sin(x), x, S.Reals) == -sqrt(2)
|
||||
assert minimum(y, x, S.Reals) == y
|
||||
assert minimum(x/sqrt(x**2+1), x, S.Reals) == -1
|
||||
|
||||
raises(ValueError, lambda : minimum(sin(x), x, S.EmptySet))
|
||||
raises(ValueError, lambda : minimum(log(cos(x)), x, S.EmptySet))
|
||||
raises(ValueError, lambda : minimum(1/(x**2 + y**2 + 1), x, S.EmptySet))
|
||||
raises(ValueError, lambda : minimum(sin(x), sin(x)))
|
||||
raises(ValueError, lambda : minimum(sin(x), x*y, S.EmptySet))
|
||||
raises(ValueError, lambda : minimum(sin(x), S.One))
|
||||
|
||||
|
||||
def test_issue_19869():
|
||||
assert (maximum(sqrt(3)*(x - 1)/(3*sqrt(x**2 + 1)), x)
|
||||
) == sqrt(3)/3
|
||||
|
||||
|
||||
def test_issue_16469():
|
||||
f = abs(a)
|
||||
assert function_range(f, a, S.Reals) == Interval(0, oo, False, True)
|
||||
|
||||
|
||||
@_both_exp_pow
|
||||
def test_issue_18747():
|
||||
assert periodicity(exp(pi*I*(x/4 + S.Half/2)), x) == 8
|
||||
|
||||
|
||||
def test_issue_25942():
|
||||
assert (acos(x) > pi/3).as_set() == Interval.Ropen(-1, S(1)/2)
|
||||
Reference in New Issue
Block a user