I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,23 @@
from sympy.series import approximants
from sympy.core.symbol import symbols
from sympy.functions.combinatorial.factorials import binomial
from sympy.functions.combinatorial.numbers import (fibonacci, lucas)
def test_approximants():
x, t = symbols("x,t")
g = [lucas(k) for k in range(16)]
assert list(approximants(g)) == (
[2, -4/(x - 2), (5*x - 2)/(3*x - 1), (x - 2)/(x**2 + x - 1)] )
g = [lucas(k)+fibonacci(k+2) for k in range(16)]
assert list(approximants(g)) == (
[3, -3/(x - 1), (3*x - 3)/(2*x - 1), -3/(x**2 + x - 1)] )
g = [lucas(k)**2 for k in range(16)]
assert list(approximants(g)) == (
[4, -16/(x - 4), (35*x - 4)/(9*x - 1), (37*x - 28)/(13*x**2 + 11*x - 7),
(50*x**2 + 63*x - 52)/(37*x**2 + 19*x - 13),
(-x**2 - 7*x + 4)/(x**3 - 2*x**2 - 2*x + 1)] )
p = [sum(binomial(k,i)*x**i for i in range(k+1)) for k in range(16)]
y = approximants(p, t, simplify=True)
assert next(y) == 1
assert next(y) == -1/(t*(x + 1) - 1)

View File

@ -0,0 +1,56 @@
from sympy.core.function import PoleError
from sympy.core.numbers import oo
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 sqrt
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.series.order import O
from sympy.abc import x
from sympy.testing.pytest import raises
def test_simple():
# Gruntz' theses pp. 91 to 96
# 6.6
e = sin(1/x + exp(-x)) - sin(1/x)
assert e.aseries(x) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
e = exp(x) * (exp(1/x + exp(-x)) - exp(1/x))
assert e.aseries(x, n=4) == 1/(6*x**3) + 1/(2*x**2) + 1/x + 1 + O(x**(-4), (x, oo))
e = exp(exp(x) / (1 - 1/x))
assert e.aseries(x) == exp(exp(x) / (1 - 1/x))
# The implementation of bound in aseries is incorrect currently. This test
# should be commented out when that is fixed.
# assert e.aseries(x, bound=3) == exp(exp(x) / x**2)*exp(exp(x) / x)*exp(-exp(x) + exp(x)/(1 - 1/x) - \
# exp(x) / x - exp(x) / x**2) * exp(exp(x))
e = exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))
assert e.aseries(x, n=4) == (-1/(2*x**3) + 1/x + 1 + O(x**(-4), (x, oo)))*exp(-exp(x))
e3 = lambda x:exp(exp(exp(x)))
e = e3(x)/e3(x - 1/e3(x))
assert e.aseries(x, n=3) == 1 + exp(x + exp(x))*exp(-exp(exp(x)))\
+ ((-exp(x)/2 - S.Half)*exp(x + exp(x))\
+ exp(2*x + 2*exp(x))/2)*exp(-2*exp(exp(x))) + O(exp(-3*exp(exp(x))), (x, oo))
e = exp(exp(x)) * (exp(sin(1/x + 1/exp(exp(x)))) - exp(sin(1/x)))
assert e.aseries(x, n=4) == -1/(2*x**3) + 1/x + 1 + O(x**(-4), (x, oo))
n = Symbol('n', integer=True)
e = (sqrt(n)*log(n)**2*exp(sqrt(log(n))*log(log(n))**2*exp(sqrt(log(log(n)))*log(log(log(n)))**3)))/n
assert e.aseries(n) == \
exp(exp(sqrt(log(log(n)))*log(log(log(n)))**3)*sqrt(log(n))*log(log(n))**2)*log(n)**2/sqrt(n)
def test_hierarchical():
e = sin(1/x + exp(-x))
assert e.aseries(x, n=3, hir=True) == -exp(-2*x)*sin(1/x)/2 + \
exp(-x)*cos(1/x) + sin(1/x) + O(exp(-3*x), (x, oo))
e = sin(x) * cos(exp(-x))
assert e.aseries(x, hir=True) == exp(-4*x)*sin(x)/24 - \
exp(-2*x)*sin(x)/2 + sin(x) + O(exp(-6*x), (x, oo))
raises(PoleError, lambda: e.aseries(x))

View File

@ -0,0 +1,143 @@
from sympy.core.numbers import (Rational, oo, pi)
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 (root, sqrt)
from sympy.functions.elementary.trigonometric import (asin, cos, sin, tan)
from sympy.polys.rationaltools import together
from sympy.series.limits import limit
# Numbers listed with the tests refer to problem numbers in the book
# "Anti-demidovich, problemas resueltos, Ed. URSS"
x = Symbol("x")
def test_leadterm():
assert (3 + 2*x**(log(3)/log(2) - 1)).leadterm(x) == (3, 0)
def root3(x):
return root(x, 3)
def root4(x):
return root(x, 4)
def test_Limits_simple_0():
assert limit((2**(x + 1) + 3**(x + 1))/(2**x + 3**x), x, oo) == 3 # 175
def test_Limits_simple_1():
assert limit((x + 1)*(x + 2)*(x + 3)/x**3, x, oo) == 1 # 172
assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0 # 179
assert limit((2*x - 3)*(3*x + 5)*(4*x - 6)/(3*x**3 + x - 1), x, oo) == 8 # Primjer 1
assert limit(x/root3(x**3 + 10), x, oo) == 1 # Primjer 2
assert limit((x + 1)**2/(x**2 + 1), x, oo) == 1 # 181
def test_Limits_simple_2():
assert limit(1000*x/(x**2 - 1), x, oo) == 0 # 182
assert limit((x**2 - 5*x + 1)/(3*x + 7), x, oo) is oo # 183
assert limit((2*x**2 - x + 3)/(x**3 - 8*x + 5), x, oo) == 0 # 184
assert limit((2*x**2 - 3*x - 4)/sqrt(x**4 + 1), x, oo) == 2 # 186
assert limit((2*x + 3)/(x + root3(x)), x, oo) == 2 # 187
assert limit(x**2/(10 + x*sqrt(x)), x, oo) is oo # 188
assert limit(root3(x**2 + 1)/(x + 1), x, oo) == 0 # 189
assert limit(sqrt(x)/sqrt(x + sqrt(x + sqrt(x))), x, oo) == 1 # 190
def test_Limits_simple_3a():
a = Symbol('a')
#issue 3513
assert together(limit((x**2 - (a + 1)*x + a)/(x**3 - a**3), x, a)) == \
(a - 1)/(3*a**2) # 196
def test_Limits_simple_3b():
h = Symbol("h")
assert limit(((x + h)**3 - x**3)/h, h, 0) == 3*x**2 # 197
assert limit((1/(1 - x) - 3/(1 - x**3)), x, 1) == -1 # 198
assert limit((sqrt(1 + x) - 1)/(root3(1 + x) - 1), x, 0) == Rational(3)/2 # Primer 4
assert limit((sqrt(x) - 1)/(x - 1), x, 1) == Rational(1)/2 # 199
assert limit((sqrt(x) - 8)/(root3(x) - 4), x, 64) == 3 # 200
assert limit((root3(x) - 1)/(root4(x) - 1), x, 1) == Rational(4)/3 # 201
assert limit(
(root3(x**2) - 2*root3(x) + 1)/(x - 1)**2, x, 1) == Rational(1)/9 # 202
def test_Limits_simple_4a():
a = Symbol('a')
assert limit((sqrt(x) - sqrt(a))/(x - a), x, a) == 1/(2*sqrt(a)) # Primer 5
assert limit((sqrt(x) - 1)/(root3(x) - 1), x, 1) == Rational(3, 2) # 205
assert limit((sqrt(1 + x) - sqrt(1 - x))/x, x, 0) == 1 # 207
assert limit(sqrt(x**2 - 5*x + 6) - x, x, oo) == Rational(-5, 2) # 213
def test_limits_simple_4aa():
assert limit(x*(sqrt(x**2 + 1) - x), x, oo) == Rational(1)/2 # 214
def test_Limits_simple_4b():
#issue 3511
assert limit(x - root3(x**3 - 1), x, oo) == 0 # 215
def test_Limits_simple_4c():
assert limit(log(1 + exp(x))/x, x, -oo) == 0 # 267a
assert limit(log(1 + exp(x))/x, x, oo) == 1 # 267b
def test_bounded():
assert limit(sin(x)/x, x, oo) == 0 # 216b
assert limit(x*sin(1/x), x, 0) == 0 # 227a
def test_f1a():
#issue 3508:
assert limit((sin(2*x)/x)**(1 + x), x, 0) == 2 # Primer 7
def test_f1a2():
#issue 3509:
assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2) # Primer 9
def test_f1b():
m = Symbol("m")
n = Symbol("n")
h = Symbol("h")
a = Symbol("a")
assert limit(sin(x)/x, x, 2) == sin(2)/2 # 216a
assert limit(sin(3*x)/x, x, 0) == 3 # 217
assert limit(sin(5*x)/sin(2*x), x, 0) == Rational(5, 2) # 218
assert limit(sin(pi*x)/sin(3*pi*x), x, 0) == Rational(1, 3) # 219
assert limit(x*sin(pi/x), x, oo) == pi # 220
assert limit((1 - cos(x))/x**2, x, 0) == S.Half # 221
assert limit(x*sin(1/x), x, oo) == 1 # 227b
assert limit((cos(m*x) - cos(n*x))/x**2, x, 0) == -m**2/2 + n**2/2 # 232
assert limit((tan(x) - sin(x))/x**3, x, 0) == S.Half # 233
assert limit((x - sin(2*x))/(x + sin(3*x)), x, 0) == -Rational(1, 4) # 237
assert limit((1 - sqrt(cos(x)))/x**2, x, 0) == Rational(1, 4) # 239
assert limit((sqrt(1 + sin(x)) - sqrt(1 - sin(x)))/x, x, 0) == 1 # 240
assert limit((1 + h/x)**x, x, oo) == exp(h) # Primer 9
assert limit((sin(x) - sin(a))/(x - a), x, a) == cos(a) # 222, *176
assert limit((cos(x) - cos(a))/(x - a), x, a) == -sin(a) # 223
assert limit((sin(x + h) - sin(x))/h, h, 0) == cos(x) # 225
def test_f2a():
assert limit(((x + 1)/(2*x + 1))**(x**2), x, oo) == 0 # Primer 8
def test_f2():
assert limit((sqrt(
cos(x)) - root3(cos(x)))/(sin(x)**2), x, 0) == -Rational(1, 12) # *184
def test_f3():
a = Symbol('a')
#issue 3504
assert limit(asin(a*x)/x, x, 0) == a

View File

@ -0,0 +1,618 @@
from sympy.concrete.summations import Sum
from sympy.core.add import Add
from sympy.core.function import (Derivative, Function)
from sympy.core.mul import Mul
from sympy.core.numbers import (I, Rational, oo, pi)
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.functions.combinatorial.factorials import factorial
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.hyperbolic import (acosh, asech)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sin)
from sympy.functions.special.bessel import airyai
from sympy.functions.special.error_functions import erf
from sympy.functions.special.gamma_functions import gamma
from sympy.integrals.integrals import integrate
from sympy.series.formal import fps
from sympy.series.order import O
from sympy.series.formal import (rational_algorithm, FormalPowerSeries,
FormalPowerSeriesProduct, FormalPowerSeriesCompose,
FormalPowerSeriesInverse, simpleDE,
rational_independent, exp_re, hyper_re)
from sympy.testing.pytest import raises, XFAIL, slow
x, y, z = symbols('x y z')
n, m, k = symbols('n m k', integer=True)
f, r = Function('f'), Function('r')
def test_rational_algorithm():
f = 1 / ((x - 1)**2 * (x - 2))
assert rational_algorithm(f, x, k) == \
(-2**(-k - 1) + 1 - (factorial(k + 1) / factorial(k)), 0, 0)
f = (1 + x + x**2 + x**3) / ((x - 1) * (x - 2))
assert rational_algorithm(f, x, k) == \
(-15*2**(-k - 1) + 4, x + 4, 0)
f = z / (y*m - m*x - y*x + x**2)
assert rational_algorithm(f, x, k) == \
(((-y**(-k - 1)*z) / (y - m)) + ((m**(-k - 1)*z) / (y - m)), 0, 0)
f = x / (1 - x - x**2)
assert rational_algorithm(f, x, k) is None
assert rational_algorithm(f, x, k, full=True) == \
(((Rational(-1, 2) + sqrt(5)/2)**(-k - 1) *
(-sqrt(5)/10 + S.Half)) +
((-sqrt(5)/2 - S.Half)**(-k - 1) *
(sqrt(5)/10 + S.Half)), 0, 0)
f = 1 / (x**2 + 2*x + 2)
assert rational_algorithm(f, x, k) is None
assert rational_algorithm(f, x, k, full=True) == \
((I*(-1 + I)**(-k - 1)) / 2 - (I*(-1 - I)**(-k - 1)) / 2, 0, 0)
f = log(1 + x)
assert rational_algorithm(f, x, k) == \
(-(-1)**(-k) / k, 0, 1)
f = atan(x)
assert rational_algorithm(f, x, k) is None
assert rational_algorithm(f, x, k, full=True) == \
(((I*I**(-k)) / 2 - (I*(-I)**(-k)) / 2) / k, 0, 1)
f = x*atan(x) - log(1 + x**2) / 2
assert rational_algorithm(f, x, k) is None
assert rational_algorithm(f, x, k, full=True) == \
(((I*I**(-k + 1)) / 2 - (I*(-I)**(-k + 1)) / 2) /
(k*(k - 1)), 0, 2)
f = log((1 + x) / (1 - x)) / 2 - atan(x)
assert rational_algorithm(f, x, k) is None
assert rational_algorithm(f, x, k, full=True) == \
((-(-1)**(-k) / 2 - (I*I**(-k)) / 2 + (I*(-I)**(-k)) / 2 +
S.Half) / k, 0, 1)
assert rational_algorithm(cos(x), x, k) is None
def test_rational_independent():
ri = rational_independent
assert ri([], x) == []
assert ri([cos(x), sin(x)], x) == [cos(x), sin(x)]
assert ri([x**2, sin(x), x*sin(x), x**3], x) == \
[x**3 + x**2, x*sin(x) + sin(x)]
assert ri([S.One, x*log(x), log(x), sin(x)/x, cos(x), sin(x), x], x) == \
[x + 1, x*log(x) + log(x), sin(x)/x + sin(x), cos(x)]
def test_simpleDE():
# Tests just the first valid DE
for DE in simpleDE(exp(x), x, f):
assert DE == (-f(x) + Derivative(f(x), x), 1)
break
for DE in simpleDE(sin(x), x, f):
assert DE == (f(x) + Derivative(f(x), x, x), 2)
break
for DE in simpleDE(log(1 + x), x, f):
assert DE == ((x + 1)*Derivative(f(x), x, 2) + Derivative(f(x), x), 2)
break
for DE in simpleDE(asin(x), x, f):
assert DE == (x*Derivative(f(x), x) + (x**2 - 1)*Derivative(f(x), x, x),
2)
break
for DE in simpleDE(exp(x)*sin(x), x, f):
assert DE == (2*f(x) - 2*Derivative(f(x)) + Derivative(f(x), x, x), 2)
break
for DE in simpleDE(((1 + x)/(1 - x))**n, x, f):
assert DE == (2*n*f(x) + (x**2 - 1)*Derivative(f(x), x), 1)
break
for DE in simpleDE(airyai(x), x, f):
assert DE == (-x*f(x) + Derivative(f(x), x, x), 2)
break
def test_exp_re():
d = -f(x) + Derivative(f(x), x)
assert exp_re(d, r, k) == -r(k) + r(k + 1)
d = f(x) + Derivative(f(x), x, x)
assert exp_re(d, r, k) == r(k) + r(k + 2)
d = f(x) + Derivative(f(x), x) + Derivative(f(x), x, x)
assert exp_re(d, r, k) == r(k) + r(k + 1) + r(k + 2)
d = Derivative(f(x), x) + Derivative(f(x), x, x)
assert exp_re(d, r, k) == r(k) + r(k + 1)
d = Derivative(f(x), x, 3) + Derivative(f(x), x, 4) + Derivative(f(x))
assert exp_re(d, r, k) == r(k) + r(k + 2) + r(k + 3)
def test_hyper_re():
d = f(x) + Derivative(f(x), x, x)
assert hyper_re(d, r, k) == r(k) + (k+1)*(k+2)*r(k + 2)
d = -x*f(x) + Derivative(f(x), x, x)
assert hyper_re(d, r, k) == (k + 2)*(k + 3)*r(k + 3) - r(k)
d = 2*f(x) - 2*Derivative(f(x), x) + Derivative(f(x), x, x)
assert hyper_re(d, r, k) == \
(-2*k - 2)*r(k + 1) + (k + 1)*(k + 2)*r(k + 2) + 2*r(k)
d = 2*n*f(x) + (x**2 - 1)*Derivative(f(x), x)
assert hyper_re(d, r, k) == \
k*r(k) + 2*n*r(k + 1) + (-k - 2)*r(k + 2)
d = (x**10 + 4)*Derivative(f(x), x) + x*(x**10 - 1)*Derivative(f(x), x, x)
assert hyper_re(d, r, k) == \
(k*(k - 1) + k)*r(k) + (4*k - (k + 9)*(k + 10) + 40)*r(k + 10)
d = ((x**2 - 1)*Derivative(f(x), x, 3) + 3*x*Derivative(f(x), x, x) +
Derivative(f(x), x))
assert hyper_re(d, r, k) == \
((k*(k - 2)*(k - 1) + 3*k*(k - 1) + k)*r(k) +
(-k*(k + 1)*(k + 2))*r(k + 2))
def test_fps():
assert fps(1) == 1
assert fps(2, x) == 2
assert fps(2, x, dir='+') == 2
assert fps(2, x, dir='-') == 2
assert fps(1/x + 1/x**2) == 1/x + 1/x**2
assert fps(log(1 + x), hyper=False, rational=False) == log(1 + x)
f = fps(x**2 + x + 1)
assert isinstance(f, FormalPowerSeries)
assert f.function == x**2 + x + 1
assert f[0] == 1
assert f[2] == x**2
assert f.truncate(4) == x**2 + x + 1 + O(x**4)
assert f.polynomial() == x**2 + x + 1
f = fps(log(1 + x))
assert isinstance(f, FormalPowerSeries)
assert f.function == log(1 + x)
assert f.subs(x, y) == f
assert f[:5] == [0, x, -x**2/2, x**3/3, -x**4/4]
assert f.as_leading_term(x) == x
assert f.polynomial(6) == x - x**2/2 + x**3/3 - x**4/4 + x**5/5
k = f.ak.variables[0]
assert f.infinite == Sum((-(-1)**(-k)*x**k)/k, (k, 1, oo))
ft, s = f.truncate(n=None), f[:5]
for i, t in enumerate(ft):
if i == 5:
break
assert s[i] == t
f = sin(x).fps(x)
assert isinstance(f, FormalPowerSeries)
assert f.truncate() == x - x**3/6 + x**5/120 + O(x**6)
raises(NotImplementedError, lambda: fps(y*x))
raises(ValueError, lambda: fps(x, dir=0))
@slow
def test_fps__rational():
assert fps(1/x) == (1/x)
assert fps((x**2 + x + 1) / x**3, dir=-1) == (x**2 + x + 1) / x**3
f = 1 / ((x - 1)**2 * (x - 2))
assert fps(f, x).truncate() == \
(Rational(-1, 2) - x*Rational(5, 4) - 17*x**2/8 - 49*x**3/16 - 129*x**4/32 -
321*x**5/64 + O(x**6))
f = (1 + x + x**2 + x**3) / ((x - 1) * (x - 2))
assert fps(f, x).truncate() == \
(S.Half + x*Rational(5, 4) + 17*x**2/8 + 49*x**3/16 + 113*x**4/32 +
241*x**5/64 + O(x**6))
f = x / (1 - x - x**2)
assert fps(f, x, full=True).truncate() == \
x + x**2 + 2*x**3 + 3*x**4 + 5*x**5 + O(x**6)
f = 1 / (x**2 + 2*x + 2)
assert fps(f, x, full=True).truncate() == \
S.Half - x/2 + x**2/4 - x**4/8 + x**5/8 + O(x**6)
f = log(1 + x)
assert fps(f, x).truncate() == \
x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
assert fps(f, x, dir=1).truncate() == fps(f, x, dir=-1).truncate()
assert fps(f, x, 2).truncate() == \
(log(3) - Rational(2, 3) - (x - 2)**2/18 + (x - 2)**3/81 -
(x - 2)**4/324 + (x - 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2)))
assert fps(f, x, 2, dir=-1).truncate() == \
(log(3) - Rational(2, 3) - (-x + 2)**2/18 - (-x + 2)**3/81 -
(-x + 2)**4/324 - (-x + 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2)))
f = atan(x)
assert fps(f, x, full=True).truncate() == x - x**3/3 + x**5/5 + O(x**6)
assert fps(f, x, full=True, dir=1).truncate() == \
fps(f, x, full=True, dir=-1).truncate()
assert fps(f, x, 2, full=True).truncate() == \
(atan(2) - Rational(2, 5) - 2*(x - 2)**2/25 + 11*(x - 2)**3/375 -
6*(x - 2)**4/625 + 41*(x - 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2)))
assert fps(f, x, 2, full=True, dir=-1).truncate() == \
(atan(2) - Rational(2, 5) - 2*(-x + 2)**2/25 - 11*(-x + 2)**3/375 -
6*(-x + 2)**4/625 - 41*(-x + 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2)))
f = x*atan(x) - log(1 + x**2) / 2
assert fps(f, x, full=True).truncate() == x**2/2 - x**4/12 + O(x**6)
f = log((1 + x) / (1 - x)) / 2 - atan(x)
assert fps(f, x, full=True).truncate(n=10) == 2*x**3/3 + 2*x**7/7 + O(x**10)
@slow
def test_fps__hyper():
f = sin(x)
assert fps(f, x).truncate() == x - x**3/6 + x**5/120 + O(x**6)
f = cos(x)
assert fps(f, x).truncate() == 1 - x**2/2 + x**4/24 + O(x**6)
f = exp(x)
assert fps(f, x).truncate() == \
1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + O(x**6)
f = atan(x)
assert fps(f, x).truncate() == x - x**3/3 + x**5/5 + O(x**6)
f = exp(acos(x))
assert fps(f, x).truncate() == \
(exp(pi/2) - x*exp(pi/2) + x**2*exp(pi/2)/2 - x**3*exp(pi/2)/3 +
5*x**4*exp(pi/2)/24 - x**5*exp(pi/2)/6 + O(x**6))
f = exp(acosh(x))
assert fps(f, x).truncate() == I + x - I*x**2/2 - I*x**4/8 + O(x**6)
f = atan(1/x)
assert fps(f, x).truncate() == pi/2 - x + x**3/3 - x**5/5 + O(x**6)
f = x*atan(x) - log(1 + x**2) / 2
assert fps(f, x, rational=False).truncate() == x**2/2 - x**4/12 + O(x**6)
f = log(1 + x)
assert fps(f, x, rational=False).truncate() == \
x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
f = airyai(x**2)
assert fps(f, x).truncate() == \
(3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) -
3**Rational(2, 3)*x**2/(3*gamma(Rational(1, 3))) + O(x**6))
f = exp(x)*sin(x)
assert fps(f, x).truncate() == x + x**2 + x**3/3 - x**5/30 + O(x**6)
f = exp(x)*sin(x)/x
assert fps(f, x).truncate() == 1 + x + x**2/3 - x**4/30 - x**5/90 + O(x**6)
f = sin(x) * cos(x)
assert fps(f, x).truncate() == x - 2*x**3/3 + 2*x**5/15 + O(x**6)
def test_fps_shift():
f = x**-5*sin(x)
assert fps(f, x).truncate() == \
1/x**4 - 1/(6*x**2) + Rational(1, 120) - x**2/5040 + x**4/362880 + O(x**6)
f = x**2*atan(x)
assert fps(f, x, rational=False).truncate() == \
x**3 - x**5/3 + O(x**6)
f = cos(sqrt(x))*x
assert fps(f, x).truncate() == \
x - x**2/2 + x**3/24 - x**4/720 + x**5/40320 + O(x**6)
f = x**2*cos(sqrt(x))
assert fps(f, x).truncate() == \
x**2 - x**3/2 + x**4/24 - x**5/720 + O(x**6)
def test_fps__Add_expr():
f = x*atan(x) - log(1 + x**2) / 2
assert fps(f, x).truncate() == x**2/2 - x**4/12 + O(x**6)
f = sin(x) + cos(x) - exp(x) + log(1 + x)
assert fps(f, x).truncate() == x - 3*x**2/2 - x**4/4 + x**5/5 + O(x**6)
f = 1/x + sin(x)
assert fps(f, x).truncate() == 1/x + x - x**3/6 + x**5/120 + O(x**6)
f = sin(x) - cos(x) + 1/(x - 1)
assert fps(f, x).truncate() == \
-2 - x**2/2 - 7*x**3/6 - 25*x**4/24 - 119*x**5/120 + O(x**6)
def test_fps__asymptotic():
f = exp(x)
assert fps(f, x, oo) == f
assert fps(f, x, -oo).truncate() == O(1/x**6, (x, oo))
f = erf(x)
assert fps(f, x, oo).truncate() == 1 + O(1/x**6, (x, oo))
assert fps(f, x, -oo).truncate() == -1 + O(1/x**6, (x, oo))
f = atan(x)
assert fps(f, x, oo, full=True).truncate() == \
-1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2 + O(1/x**6, (x, oo))
assert fps(f, x, -oo, full=True).truncate() == \
-1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2 + O(1/x**6, (x, oo))
f = log(1 + x)
assert fps(f, x, oo) != \
(-1/(5*x**5) - 1/(4*x**4) + 1/(3*x**3) - 1/(2*x**2) + 1/x - log(1/x) +
O(1/x**6, (x, oo)))
assert fps(f, x, -oo) != \
(-1/(5*x**5) - 1/(4*x**4) + 1/(3*x**3) - 1/(2*x**2) + 1/x + I*pi -
log(-1/x) + O(1/x**6, (x, oo)))
def test_fps__fractional():
f = sin(sqrt(x)) / x
assert fps(f, x).truncate() == \
(1/sqrt(x) - sqrt(x)/6 + x**Rational(3, 2)/120 -
x**Rational(5, 2)/5040 + x**Rational(7, 2)/362880 -
x**Rational(9, 2)/39916800 + x**Rational(11, 2)/6227020800 + O(x**6))
f = sin(sqrt(x)) * x
assert fps(f, x).truncate() == \
(x**Rational(3, 2) - x**Rational(5, 2)/6 + x**Rational(7, 2)/120 -
x**Rational(9, 2)/5040 + x**Rational(11, 2)/362880 + O(x**6))
f = atan(sqrt(x)) / x**2
assert fps(f, x).truncate() == \
(x**Rational(-3, 2) - x**Rational(-1, 2)/3 + x**S.Half/5 -
x**Rational(3, 2)/7 + x**Rational(5, 2)/9 - x**Rational(7, 2)/11 +
x**Rational(9, 2)/13 - x**Rational(11, 2)/15 + O(x**6))
f = exp(sqrt(x))
assert fps(f, x).truncate().expand() == \
(1 + x/2 + x**2/24 + x**3/720 + x**4/40320 + x**5/3628800 + sqrt(x) +
x**Rational(3, 2)/6 + x**Rational(5, 2)/120 + x**Rational(7, 2)/5040 +
x**Rational(9, 2)/362880 + x**Rational(11, 2)/39916800 + O(x**6))
f = exp(sqrt(x))*x
assert fps(f, x).truncate().expand() == \
(x + x**2/2 + x**3/24 + x**4/720 + x**5/40320 + x**Rational(3, 2) +
x**Rational(5, 2)/6 + x**Rational(7, 2)/120 + x**Rational(9, 2)/5040 +
x**Rational(11, 2)/362880 + O(x**6))
def test_fps__logarithmic_singularity():
f = log(1 + 1/x)
assert fps(f, x) != \
-log(x) + x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
assert fps(f, x, rational=False) != \
-log(x) + x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
@XFAIL
def test_fps__logarithmic_singularity_fail():
f = asech(x) # Algorithms for computing limits probably needs improvemnts
assert fps(f, x) == log(2) - log(x) - x**2/4 - 3*x**4/64 + O(x**6)
def test_fps_symbolic():
f = x**n*sin(x**2)
assert fps(f, x).truncate(8) == x**(n + 2) - x**(n + 6)/6 + O(x**(n + 8), x)
f = x**n*log(1 + x)
fp = fps(f, x)
k = fp.ak.variables[0]
assert fp.infinite == \
Sum((-(-1)**(-k)*x**(k + n))/k, (k, 1, oo))
f = (x - 2)**n*log(1 + x)
assert fps(f, x, 2).truncate() == \
((x - 2)**n*log(3) + (x - 2)**(n + 1)/3 - (x - 2)**(n + 2)/18 + (x - 2)**(n + 3)/81 -
(x - 2)**(n + 4)/324 + (x - 2)**(n + 5)/1215 + O((x - 2)**(n + 6), (x, 2)))
f = x**(n - 2)*cos(x)
assert fps(f, x).truncate() == \
(x**(n - 2) - x**n/2 + x**(n + 2)/24 + O(x**(n + 4), x))
f = x**(n - 2)*sin(x) + x**n*exp(x)
assert fps(f, x).truncate() == \
(x**(n - 1) + x**(n + 1) + x**(n + 2)/2 + x**n +
x**(n + 4)/24 + x**(n + 5)/60 + O(x**(n + 6), x))
f = x**n*atan(x)
assert fps(f, x, oo).truncate() == \
(-x**(n - 5)/5 + x**(n - 3)/3 + x**n*(pi/2 - 1/x) +
O((1/x)**(-n)/x**6, (x, oo)))
f = x**(n/2)*cos(x)
assert fps(f, x).truncate() == \
x**(n/2) - x**(n/2 + 2)/2 + x**(n/2 + 4)/24 + O(x**(n/2 + 6), x)
f = x**(n + m)*sin(x)
assert fps(f, x).truncate() == \
x**(m + n + 1) - x**(m + n + 3)/6 + x**(m + n + 5)/120 + O(x**(m + n + 6), x)
def test_fps__slow():
f = x*exp(x)*sin(2*x) # TODO: rsolve needs improvement
assert fps(f, x).truncate() == 2*x**2 + 2*x**3 - x**4/3 - x**5 + O(x**6)
def test_fps__operations():
f1, f2 = fps(sin(x)), fps(cos(x))
fsum = f1 + f2
assert fsum.function == sin(x) + cos(x)
assert fsum.truncate() == \
1 + x - x**2/2 - x**3/6 + x**4/24 + x**5/120 + O(x**6)
fsum = f1 + 1
assert fsum.function == sin(x) + 1
assert fsum.truncate() == 1 + x - x**3/6 + x**5/120 + O(x**6)
fsum = 1 + f2
assert fsum.function == cos(x) + 1
assert fsum.truncate() == 2 - x**2/2 + x**4/24 + O(x**6)
assert (f1 + x) == Add(f1, x)
assert -f2.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
assert (f1 - f1) is S.Zero
fsub = f1 - f2
assert fsub.function == sin(x) - cos(x)
assert fsub.truncate() == \
-1 + x + x**2/2 - x**3/6 - x**4/24 + x**5/120 + O(x**6)
fsub = f1 - 1
assert fsub.function == sin(x) - 1
assert fsub.truncate() == -1 + x - x**3/6 + x**5/120 + O(x**6)
fsub = 1 - f2
assert fsub.function == -cos(x) + 1
assert fsub.truncate() == x**2/2 - x**4/24 + O(x**6)
raises(ValueError, lambda: f1 + fps(exp(x), dir=-1))
raises(ValueError, lambda: f1 + fps(exp(x), x0=1))
fm = f1 * 3
assert fm.function == 3*sin(x)
assert fm.truncate() == 3*x - x**3/2 + x**5/40 + O(x**6)
fm = 3 * f2
assert fm.function == 3*cos(x)
assert fm.truncate() == 3 - 3*x**2/2 + x**4/8 + O(x**6)
assert (f1 * f2) == Mul(f1, f2)
assert (f1 * x) == Mul(f1, x)
fd = f1.diff()
assert fd.function == cos(x)
assert fd.truncate() == 1 - x**2/2 + x**4/24 + O(x**6)
fd = f2.diff()
assert fd.function == -sin(x)
assert fd.truncate() == -x + x**3/6 - x**5/120 + O(x**6)
fd = f2.diff().diff()
assert fd.function == -cos(x)
assert fd.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
f3 = fps(exp(sqrt(x)))
fd = f3.diff()
assert fd.truncate().expand() == \
(1/(2*sqrt(x)) + S.Half + x/12 + x**2/240 + x**3/10080 + x**4/725760 +
x**5/79833600 + sqrt(x)/4 + x**Rational(3, 2)/48 + x**Rational(5, 2)/1440 +
x**Rational(7, 2)/80640 + x**Rational(9, 2)/7257600 + x**Rational(11, 2)/958003200 +
O(x**6))
assert f1.integrate((x, 0, 1)) == -cos(1) + 1
assert integrate(f1, (x, 0, 1)) == -cos(1) + 1
fi = integrate(f1, x)
assert fi.function == -cos(x)
assert fi.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
fi = f2.integrate(x)
assert fi.function == sin(x)
assert fi.truncate() == x - x**3/6 + x**5/120 + O(x**6)
def test_fps__product():
f1, f2, f3 = fps(sin(x)), fps(exp(x)), fps(cos(x))
raises(ValueError, lambda: f1.product(exp(x), x))
raises(ValueError, lambda: f1.product(fps(exp(x), dir=-1), x, 4))
raises(ValueError, lambda: f1.product(fps(exp(x), x0=1), x, 4))
raises(ValueError, lambda: f1.product(fps(exp(y)), x, 4))
fprod = f1.product(f2, x)
assert isinstance(fprod, FormalPowerSeriesProduct)
assert isinstance(fprod.ffps, FormalPowerSeries)
assert isinstance(fprod.gfps, FormalPowerSeries)
assert fprod.f == sin(x)
assert fprod.g == exp(x)
assert fprod.function == sin(x) * exp(x)
assert fprod._eval_terms(4) == x + x**2 + x**3/3
assert fprod.truncate(4) == x + x**2 + x**3/3 + O(x**4)
assert fprod.polynomial(4) == x + x**2 + x**3/3
raises(NotImplementedError, lambda: fprod._eval_term(5))
raises(NotImplementedError, lambda: fprod.infinite)
raises(NotImplementedError, lambda: fprod._eval_derivative(x))
raises(NotImplementedError, lambda: fprod.integrate(x))
assert f1.product(f3, x)._eval_terms(4) == x - 2*x**3/3
assert f1.product(f3, x).truncate(4) == x - 2*x**3/3 + O(x**4)
def test_fps__compose():
f1, f2, f3 = fps(exp(x)), fps(sin(x)), fps(cos(x))
raises(ValueError, lambda: f1.compose(sin(x), x))
raises(ValueError, lambda: f1.compose(fps(sin(x), dir=-1), x, 4))
raises(ValueError, lambda: f1.compose(fps(sin(x), x0=1), x, 4))
raises(ValueError, lambda: f1.compose(fps(sin(y)), x, 4))
raises(ValueError, lambda: f1.compose(f3, x))
raises(ValueError, lambda: f2.compose(f3, x))
fcomp = f1.compose(f2, x)
assert isinstance(fcomp, FormalPowerSeriesCompose)
assert isinstance(fcomp.ffps, FormalPowerSeries)
assert isinstance(fcomp.gfps, FormalPowerSeries)
assert fcomp.f == exp(x)
assert fcomp.g == sin(x)
assert fcomp.function == exp(sin(x))
assert fcomp._eval_terms(6) == 1 + x + x**2/2 - x**4/8 - x**5/15
assert fcomp.truncate() == 1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6)
assert fcomp.truncate(5) == 1 + x + x**2/2 - x**4/8 + O(x**5)
raises(NotImplementedError, lambda: fcomp._eval_term(5))
raises(NotImplementedError, lambda: fcomp.infinite)
raises(NotImplementedError, lambda: fcomp._eval_derivative(x))
raises(NotImplementedError, lambda: fcomp.integrate(x))
assert f1.compose(f2, x).truncate(4) == 1 + x + x**2/2 + O(x**4)
assert f1.compose(f2, x).truncate(8) == \
1 + x + x**2/2 - x**4/8 - x**5/15 - x**6/240 + x**7/90 + O(x**8)
assert f1.compose(f2, x).truncate(6) == \
1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6)
assert f2.compose(f2, x).truncate(4) == x - x**3/3 + O(x**4)
assert f2.compose(f2, x).truncate(8) == x - x**3/3 + x**5/10 - 8*x**7/315 + O(x**8)
assert f2.compose(f2, x).truncate(6) == x - x**3/3 + x**5/10 + O(x**6)
def test_fps__inverse():
f1, f2, f3 = fps(sin(x)), fps(exp(x)), fps(cos(x))
raises(ValueError, lambda: f1.inverse(x))
finv = f2.inverse(x)
assert isinstance(finv, FormalPowerSeriesInverse)
assert isinstance(finv.ffps, FormalPowerSeries)
raises(ValueError, lambda: finv.gfps)
assert finv.f == exp(x)
assert finv.function == exp(-x)
assert finv._eval_terms(5) == 1 - x + x**2/2 - x**3/6 + x**4/24
assert finv.truncate() == 1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + O(x**6)
assert finv.truncate(5) == 1 - x + x**2/2 - x**3/6 + x**4/24 + O(x**5)
raises(NotImplementedError, lambda: finv._eval_term(5))
raises(ValueError, lambda: finv.g)
raises(NotImplementedError, lambda: finv.infinite)
raises(NotImplementedError, lambda: finv._eval_derivative(x))
raises(NotImplementedError, lambda: finv.integrate(x))
assert f2.inverse(x).truncate(8) == \
1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + x**6/720 - x**7/5040 + O(x**8)
assert f3.inverse(x).truncate() == 1 + x**2/2 + 5*x**4/24 + O(x**6)
assert f3.inverse(x).truncate(8) == 1 + x**2/2 + 5*x**4/24 + 61*x**6/720 + O(x**8)

View File

@ -0,0 +1,165 @@
from sympy.core.add import Add
from sympy.core.numbers import (Rational, oo, pi)
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.piecewise import Piecewise
from sympy.functions.elementary.trigonometric import (cos, sin, sinc, tan)
from sympy.series.fourier import fourier_series
from sympy.series.fourier import FourierSeries
from sympy.testing.pytest import raises
from functools import lru_cache
x, y, z = symbols('x y z')
# Don't declare these during import because they are slow
@lru_cache()
def _get_examples():
fo = fourier_series(x, (x, -pi, pi))
fe = fourier_series(x**2, (-pi, pi))
fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))
return fo, fe, fp
def test_FourierSeries():
fo, fe, fp = _get_examples()
assert fourier_series(1, (-pi, pi)) == 1
assert (Piecewise((0, x < 0), (pi, True)).
fourier_series((x, -pi, pi)).truncate()) == fp.truncate()
assert isinstance(fo, FourierSeries)
assert fo.function == x
assert fo.x == x
assert fo.period == (-pi, pi)
assert fo.term(3) == 2*sin(3*x) / 3
assert fe.term(3) == -4*cos(3*x) / 9
assert fp.term(3) == 2*sin(3*x) / 3
assert fo.as_leading_term(x) == 2*sin(x)
assert fe.as_leading_term(x) == pi**2 / 3
assert fp.as_leading_term(x) == pi / 2
assert fo.truncate() == 2*sin(x) - sin(2*x) + (2*sin(3*x) / 3)
assert fe.truncate() == -4*cos(x) + cos(2*x) + pi**2 / 3
assert fp.truncate() == 2*sin(x) + (2*sin(3*x) / 3) + pi / 2
fot = fo.truncate(n=None)
s = [0, 2*sin(x), -sin(2*x)]
for i, t in enumerate(fot):
if i == 3:
break
assert s[i] == t
def _check_iter(f, i):
for ind, t in enumerate(f):
assert t == f[ind]
if ind == i:
break
_check_iter(fo, 3)
_check_iter(fe, 3)
_check_iter(fp, 3)
assert fo.subs(x, x**2) == fo
raises(ValueError, lambda: fourier_series(x, (0, 1, 2)))
raises(ValueError, lambda: fourier_series(x, (x, 0, oo)))
raises(ValueError, lambda: fourier_series(x*y, (0, oo)))
def test_FourierSeries_2():
p = Piecewise((0, x < 0), (x, True))
f = fourier_series(p, (x, -2, 2))
assert f.term(3) == (2*sin(3*pi*x / 2) / (3*pi) -
4*cos(3*pi*x / 2) / (9*pi**2))
assert f.truncate() == (2*sin(pi*x / 2) / pi - sin(pi*x) / pi -
4*cos(pi*x / 2) / pi**2 + S.Half)
def test_square_wave():
"""Test if fourier_series approximates discontinuous function correctly."""
square_wave = Piecewise((1, x < pi), (-1, True))
s = fourier_series(square_wave, (x, 0, 2*pi))
assert s.truncate(3) == 4 / pi * sin(x) + 4 / (3 * pi) * sin(3 * x) + \
4 / (5 * pi) * sin(5 * x)
assert s.sigma_approximation(4) == 4 / pi * sin(x) * sinc(pi / 4) + \
4 / (3 * pi) * sin(3 * x) * sinc(3 * pi / 4)
def test_sawtooth_wave():
s = fourier_series(x, (x, 0, pi))
assert s.truncate(4) == \
pi/2 - sin(2*x) - sin(4*x)/2 - sin(6*x)/3
s = fourier_series(x, (x, 0, 1))
assert s.truncate(4) == \
S.Half - sin(2*pi*x)/pi - sin(4*pi*x)/(2*pi) - sin(6*pi*x)/(3*pi)
def test_FourierSeries__operations():
fo, fe, fp = _get_examples()
fes = fe.scale(-1).shift(pi**2)
assert fes.truncate() == 4*cos(x) - cos(2*x) + 2*pi**2 / 3
assert fp.shift(-pi/2).truncate() == (2*sin(x) + (2*sin(3*x) / 3) +
(2*sin(5*x) / 5))
fos = fo.scale(3)
assert fos.truncate() == 6*sin(x) - 3*sin(2*x) + 2*sin(3*x)
fx = fe.scalex(2).shiftx(1)
assert fx.truncate() == -4*cos(2*x + 2) + cos(4*x + 4) + pi**2 / 3
fl = fe.scalex(3).shift(-pi).scalex(2).shiftx(1).scale(4)
assert fl.truncate() == (-16*cos(6*x + 6) + 4*cos(12*x + 12) -
4*pi + 4*pi**2 / 3)
raises(ValueError, lambda: fo.shift(x))
raises(ValueError, lambda: fo.shiftx(sin(x)))
raises(ValueError, lambda: fo.scale(x*y))
raises(ValueError, lambda: fo.scalex(x**2))
def test_FourierSeries__neg():
fo, fe, fp = _get_examples()
assert (-fo).truncate() == -2*sin(x) + sin(2*x) - (2*sin(3*x) / 3)
assert (-fe).truncate() == +4*cos(x) - cos(2*x) - pi**2 / 3
def test_FourierSeries__add__sub():
fo, fe, fp = _get_examples()
assert fo + fo == fo.scale(2)
assert fo - fo == 0
assert -fe - fe == fe.scale(-2)
assert (fo + fe).truncate() == 2*sin(x) - sin(2*x) - 4*cos(x) + cos(2*x) \
+ pi**2 / 3
assert (fo - fe).truncate() == 2*sin(x) - sin(2*x) + 4*cos(x) - cos(2*x) \
- pi**2 / 3
assert isinstance(fo + 1, Add)
raises(ValueError, lambda: fo + fourier_series(x, (x, 0, 2)))
def test_FourierSeries_finite():
assert fourier_series(sin(x)).truncate(1) == sin(x)
# assert type(fourier_series(sin(x)*log(x))).truncate() == FourierSeries
# assert type(fourier_series(sin(x**2+6))).truncate() == FourierSeries
assert fourier_series(sin(x)*log(y)*exp(z),(x,pi,-pi)).truncate() == sin(x)*log(y)*exp(z)
assert fourier_series(sin(x)**6).truncate(oo) == -15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 \
+ Rational(5, 16)
assert fourier_series(sin(x) ** 6).truncate() == -15 * cos(2 * x) / 32 + 3 * cos(4 * x) / 16 \
+ Rational(5, 16)
assert fourier_series(sin(4*x+3) + cos(3*x+4)).truncate(oo) == -sin(4)*sin(3*x) + sin(4*x)*cos(3) \
+ cos(4)*cos(3*x) + sin(3)*cos(4*x)
assert fourier_series(sin(x)+cos(x)*tan(x)).truncate(oo) == 2*sin(x)
assert fourier_series(cos(pi*x), (x, -1, 1)).truncate(oo) == cos(pi*x)
assert fourier_series(cos(3*pi*x + 4) - sin(4*pi*x)*log(pi*y), (x, -1, 1)).truncate(oo) == -log(pi*y)*sin(4*pi*x)\
- sin(4)*sin(3*pi*x) + cos(4)*cos(3*pi*x)

View File

@ -0,0 +1,482 @@
from sympy.core import EulerGamma
from sympy.core.numbers import (E, I, Integer, Rational, oo, pi)
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 sqrt
from sympy.functions.elementary.trigonometric import (acot, atan, cos, sin)
from sympy.functions.elementary.complexes import sign as _sign
from sympy.functions.special.error_functions import (Ei, erf)
from sympy.functions.special.gamma_functions import (digamma, gamma, loggamma)
from sympy.functions.special.zeta_functions import zeta
from sympy.polys.polytools import cancel
from sympy.functions.elementary.hyperbolic import cosh, coth, sinh, tanh
from sympy.series.gruntz import compare, mrv, rewrite, mrv_leadterm, gruntz, \
sign
from sympy.testing.pytest import XFAIL, skip, slow
"""
This test suite is testing the limit algorithm using the bottom up approach.
See the documentation in limits2.py. The algorithm itself is highly recursive
by nature, so "compare" is logically the lowest part of the algorithm, yet in
some sense it's the most complex part, because it needs to calculate a limit
to return the result.
Nevertheless, the rest of the algorithm depends on compare working correctly.
"""
x = Symbol('x', real=True)
m = Symbol('m', real=True)
runslow = False
def _sskip():
if not runslow:
skip("slow")
@slow
def test_gruntz_evaluation():
# Gruntz' thesis pp. 122 to 123
# 8.1
assert gruntz(exp(x)*(exp(1/x - exp(-x)) - exp(1/x)), x, oo) == -1
# 8.2
assert gruntz(exp(x)*(exp(1/x + exp(-x) + exp(-x**2))
- exp(1/x - exp(-exp(x)))), x, oo) == 1
# 8.3
assert gruntz(exp(exp(x - exp(-x))/(1 - 1/x)) - exp(exp(x)), x, oo) is oo
# 8.5
assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(exp(x))), x, oo) is oo
# 8.6
assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(x))))),
x, oo) is oo
# 8.7
assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(exp(x)))))),
x, oo) == 1
# 8.8
assert gruntz(exp(exp(x)) / exp(exp(x - exp(-exp(exp(x))))), x, oo) == 1
# 8.9
assert gruntz(log(x)**2 * exp(sqrt(log(x))*(log(log(x)))**2
* exp(sqrt(log(log(x))) * (log(log(log(x))))**3)) / sqrt(x),
x, oo) == 0
# 8.10
assert gruntz((x*log(x)*(log(x*exp(x) - x**2))**2)
/ (log(log(x**2 + 2*exp(exp(3*x**3*log(x)))))), x, oo) == Rational(1, 3)
# 8.11
assert gruntz((exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1)))) - exp(x))/x,
x, oo) == -exp(2)
# 8.12
assert gruntz((3**x + 5**x)**(1/x), x, oo) == 5
# 8.13
assert gruntz(x/log(x**(log(x**(log(2)/log(x))))), x, oo) is oo
# 8.14
assert gruntz(exp(exp(2*log(x**5 + x)*log(log(x))))
/ exp(exp(10*log(x)*log(log(x)))), x, oo) is oo
# 8.15
assert gruntz(exp(exp(Rational(5, 2)*x**Rational(-5, 7) + Rational(21, 8)*x**Rational(6, 11)
+ 2*x**(-8) + Rational(54, 17)*x**Rational(49, 45)))**8
/ log(log(-log(Rational(4, 3)*x**Rational(-5, 14))))**Rational(7, 6), x, oo) is oo
# 8.16
assert gruntz((exp(4*x*exp(-x)/(1/exp(x) + 1/exp(2*x**2/(x + 1)))) - exp(x))
/ exp(x)**4, x, oo) == 1
# 8.17
assert gruntz(exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1))))/exp(x), x, oo) \
== 1
# 8.19
assert gruntz(log(x)*(log(log(x) + log(log(x))) - log(log(x)))
/ (log(log(x) + log(log(log(x))))), x, oo) == 1
# 8.20
assert gruntz(exp((log(log(x + exp(log(x)*log(log(x))))))
/ (log(log(log(exp(x) + x + log(x)))))), x, oo) == E
# Another
assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(x)), x, oo) is oo
def test_gruntz_evaluation_slow():
_sskip()
# 8.4
assert gruntz(exp(exp(exp(x)/(1 - 1/x)))
- exp(exp(exp(x)/(1 - 1/x - log(x)**(-log(x))))), x, oo) is -oo
# 8.18
assert gruntz((exp(exp(-x/(1 + exp(-x))))*exp(-x/(1 + exp(-x/(1 + exp(-x)))))
*exp(exp(-x + exp(-x/(1 + exp(-x))))))
/ (exp(-x/(1 + exp(-x))))**2 - exp(x) + x, x, oo) == 2
@slow
def test_gruntz_eval_special():
# Gruntz, p. 126
assert gruntz(exp(x)*(sin(1/x + exp(-x)) - sin(1/x + exp(-x**2))), x, oo) == 1
assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2),
x, oo) == -2/sqrt(pi)
assert gruntz(exp(exp(x)) * (exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))),
x, oo) == 1
assert gruntz(exp(x)*(gamma(x + exp(-x)) - gamma(x)), x, oo) is oo
assert gruntz(exp(exp(digamma(digamma(x))))/x, x, oo) == exp(Rational(-1, 2))
assert gruntz(exp(exp(digamma(log(x))))/x, x, oo) == exp(Rational(-1, 2))
assert gruntz(digamma(digamma(digamma(x))), x, oo) is oo
assert gruntz(loggamma(loggamma(x)), x, oo) is oo
assert gruntz(((gamma(x + 1/gamma(x)) - gamma(x))/log(x) - cos(1/x))
* x*log(x), x, oo) == Rational(-1, 2)
assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x, oo) \
== S.Half
assert gruntz((gamma(x + 1/gamma(x)) - gamma(x)) / log(x), x, oo) == 1
def test_gruntz_eval_special_slow():
_sskip()
assert gruntz(gamma(x + 1)/sqrt(2*pi)
- exp(-x)*(x**(x + S.Half) + x**(x - S.Half)/12), x, oo) is oo
assert gruntz(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x, oo) == 0
@XFAIL
def test_grunts_eval_special_slow_sometimes_fail():
_sskip()
# XXX This sometimes fails!!!
assert gruntz(exp(gamma(x - exp(-x))*exp(1/x)) - exp(gamma(x)), x, oo) is oo
def test_gruntz_Ei():
assert gruntz((Ei(x - exp(-exp(x))) - Ei(x)) *exp(-x)*exp(exp(x))*x, x, oo) == -1
@XFAIL
def test_gruntz_eval_special_fail():
# TODO zeta function series
assert gruntz(
exp((log(2) + 1)*x) * (zeta(x + exp(-x)) - zeta(x)), x, oo) == -log(2)
# TODO 8.35 - 8.37 (bessel, max-min)
def test_gruntz_hyperbolic():
assert gruntz(cosh(x), x, oo) is oo
assert gruntz(cosh(x), x, -oo) is oo
assert gruntz(sinh(x), x, oo) is oo
assert gruntz(sinh(x), x, -oo) is -oo
assert gruntz(2*cosh(x)*exp(x), x, oo) is oo
assert gruntz(2*cosh(x)*exp(x), x, -oo) == 1
assert gruntz(2*sinh(x)*exp(x), x, oo) is oo
assert gruntz(2*sinh(x)*exp(x), x, -oo) == -1
assert gruntz(tanh(x), x, oo) == 1
assert gruntz(tanh(x), x, -oo) == -1
assert gruntz(coth(x), x, oo) == 1
assert gruntz(coth(x), x, -oo) == -1
def test_compare1():
assert compare(2, x, x) == "<"
assert compare(x, exp(x), x) == "<"
assert compare(exp(x), exp(x**2), x) == "<"
assert compare(exp(x**2), exp(exp(x)), x) == "<"
assert compare(1, exp(exp(x)), x) == "<"
assert compare(x, 2, x) == ">"
assert compare(exp(x), x, x) == ">"
assert compare(exp(x**2), exp(x), x) == ">"
assert compare(exp(exp(x)), exp(x**2), x) == ">"
assert compare(exp(exp(x)), 1, x) == ">"
assert compare(2, 3, x) == "="
assert compare(3, -5, x) == "="
assert compare(2, -5, x) == "="
assert compare(x, x**2, x) == "="
assert compare(x**2, x**3, x) == "="
assert compare(x**3, 1/x, x) == "="
assert compare(1/x, x**m, x) == "="
assert compare(x**m, -x, x) == "="
assert compare(exp(x), exp(-x), x) == "="
assert compare(exp(-x), exp(2*x), x) == "="
assert compare(exp(2*x), exp(x)**2, x) == "="
assert compare(exp(x)**2, exp(x + exp(-x)), x) == "="
assert compare(exp(x), exp(x + exp(-x)), x) == "="
assert compare(exp(x**2), 1/exp(x**2), x) == "="
def test_compare2():
assert compare(exp(x), x**5, x) == ">"
assert compare(exp(x**2), exp(x)**2, x) == ">"
assert compare(exp(x), exp(x + exp(-x)), x) == "="
assert compare(exp(x + exp(-x)), exp(x), x) == "="
assert compare(exp(x + exp(-x)), exp(-x), x) == "="
assert compare(exp(-x), x, x) == ">"
assert compare(x, exp(-x), x) == "<"
assert compare(exp(x + 1/x), x, x) == ">"
assert compare(exp(-exp(x)), exp(x), x) == ">"
assert compare(exp(exp(-exp(x)) + x), exp(-exp(x)), x) == "<"
def test_compare3():
assert compare(exp(exp(x)), exp(x + exp(-exp(x))), x) == ">"
def test_sign1():
assert sign(Rational(0), x) == 0
assert sign(Rational(3), x) == 1
assert sign(Rational(-5), x) == -1
assert sign(log(x), x) == 1
assert sign(exp(-x), x) == 1
assert sign(exp(x), x) == 1
assert sign(-exp(x), x) == -1
assert sign(3 - 1/x, x) == 1
assert sign(-3 - 1/x, x) == -1
assert sign(sin(1/x), x) == 1
assert sign((x**Integer(2)), x) == 1
assert sign(x**2, x) == 1
assert sign(x**5, x) == 1
def test_sign2():
assert sign(x, x) == 1
assert sign(-x, x) == -1
y = Symbol("y", positive=True)
assert sign(y, x) == 1
assert sign(-y, x) == -1
assert sign(y*x, x) == 1
assert sign(-y*x, x) == -1
def mmrv(a, b):
return set(mrv(a, b)[0].keys())
def test_mrv1():
assert mmrv(x, x) == {x}
assert mmrv(x + 1/x, x) == {x}
assert mmrv(x**2, x) == {x}
assert mmrv(log(x), x) == {x}
assert mmrv(exp(x), x) == {exp(x)}
assert mmrv(exp(-x), x) == {exp(-x)}
assert mmrv(exp(x**2), x) == {exp(x**2)}
assert mmrv(-exp(1/x), x) == {x}
assert mmrv(exp(x + 1/x), x) == {exp(x + 1/x)}
def test_mrv2a():
assert mmrv(exp(x + exp(-exp(x))), x) == {exp(-exp(x))}
assert mmrv(exp(x + exp(-x)), x) == {exp(x + exp(-x)), exp(-x)}
assert mmrv(exp(1/x + exp(-x)), x) == {exp(-x)}
#sometimes infinite recursion due to log(exp(x**2)) not simplifying
def test_mrv2b():
assert mmrv(exp(x + exp(-x**2)), x) == {exp(-x**2)}
#sometimes infinite recursion due to log(exp(x**2)) not simplifying
def test_mrv2c():
assert mmrv(
exp(-x + 1/x**2) - exp(x + 1/x), x) == {exp(x + 1/x), exp(1/x**2 - x)}
#sometimes infinite recursion due to log(exp(x**2)) not simplifying
def test_mrv3():
assert mmrv(exp(x**2) + x*exp(x) + log(x)**x/x, x) == {exp(x**2)}
assert mmrv(
exp(x)*(exp(1/x + exp(-x)) - exp(1/x)), x) == {exp(x), exp(-x)}
assert mmrv(log(
x**2 + 2*exp(exp(3*x**3*log(x)))), x) == {exp(exp(3*x**3*log(x)))}
assert mmrv(log(x - log(x))/log(x), x) == {x}
assert mmrv(
(exp(1/x - exp(-x)) - exp(1/x))*exp(x), x) == {exp(x), exp(-x)}
assert mmrv(
1/exp(-x + exp(-x)) - exp(x), x) == {exp(x), exp(-x), exp(x - exp(-x))}
assert mmrv(log(log(x*exp(x*exp(x)) + 1)), x) == {exp(x*exp(x))}
assert mmrv(exp(exp(log(log(x) + 1/x))), x) == {x}
def test_mrv4():
ln = log
assert mmrv((ln(ln(x) + ln(ln(x))) - ln(ln(x)))/ln(ln(x) + ln(ln(ln(x))))*ln(x),
x) == {x}
assert mmrv(log(log(x*exp(x*exp(x)) + 1)) - exp(exp(log(log(x) + 1/x))), x) == \
{exp(x*exp(x))}
def mrewrite(a, b, c):
return rewrite(a[1], a[0], b, c)
def test_rewrite1():
e = exp(x)
assert mrewrite(mrv(e, x), x, m) == (1/m, -x)
e = exp(x**2)
assert mrewrite(mrv(e, x), x, m) == (1/m, -x**2)
e = exp(x + 1/x)
assert mrewrite(mrv(e, x), x, m) == (1/m, -x - 1/x)
e = 1/exp(-x + exp(-x)) - exp(x)
assert mrewrite(mrv(e, x), x, m) == (1/(m*exp(m)) - 1/m, -x)
def test_rewrite2():
e = exp(x)*log(log(exp(x)))
assert mmrv(e, x) == {exp(x)}
assert mrewrite(mrv(e, x), x, m) == (1/m*log(x), -x)
#sometimes infinite recursion due to log(exp(x**2)) not simplifying
def test_rewrite3():
e = exp(-x + 1/x**2) - exp(x + 1/x)
#both of these are correct and should be equivalent:
assert mrewrite(mrv(e, x), x, m) in [(-1/m + m*exp(
1/x + 1/x**2), -x - 1/x), (m - 1/m*exp(1/x + x**(-2)), x**(-2) - x)]
def test_mrv_leadterm1():
assert mrv_leadterm(-exp(1/x), x) == (-1, 0)
assert mrv_leadterm(1/exp(-x + exp(-x)) - exp(x), x) == (-1, 0)
assert mrv_leadterm(
(exp(1/x - exp(-x)) - exp(1/x))*exp(x), x) == (-exp(1/x), 0)
def test_mrv_leadterm2():
#Gruntz: p51, 3.25
assert mrv_leadterm((log(exp(x) + x) - x)/log(exp(x) + log(x))*exp(x), x) == \
(1, 0)
def test_mrv_leadterm3():
#Gruntz: p56, 3.27
assert mmrv(exp(-x + exp(-x)*exp(-x*log(x))), x) == {exp(-x - x*log(x))}
assert mrv_leadterm(exp(-x + exp(-x)*exp(-x*log(x))), x) == (exp(-x), 0)
def test_limit1():
assert gruntz(x, x, oo) is oo
assert gruntz(x, x, -oo) is -oo
assert gruntz(-x, x, oo) is -oo
assert gruntz(x**2, x, -oo) is oo
assert gruntz(-x**2, x, oo) is -oo
assert gruntz(x*log(x), x, 0, dir="+") == 0
assert gruntz(1/x, x, oo) == 0
assert gruntz(exp(x), x, oo) is oo
assert gruntz(-exp(x), x, oo) is -oo
assert gruntz(exp(x)/x, x, oo) is oo
assert gruntz(1/x - exp(-x), x, oo) == 0
assert gruntz(x + 1/x, x, oo) is oo
def test_limit2():
assert gruntz(x**x, x, 0, dir="+") == 1
assert gruntz((exp(x) - 1)/x, x, 0) == 1
assert gruntz(1 + 1/x, x, oo) == 1
assert gruntz(-exp(1/x), x, oo) == -1
assert gruntz(x + exp(-x), x, oo) is oo
assert gruntz(x + exp(-x**2), x, oo) is oo
assert gruntz(x + exp(-exp(x)), x, oo) is oo
assert gruntz(13 + 1/x - exp(-x), x, oo) == 13
def test_limit3():
a = Symbol('a')
assert gruntz(x - log(1 + exp(x)), x, oo) == 0
assert gruntz(x - log(a + exp(x)), x, oo) == 0
assert gruntz(exp(x)/(1 + exp(x)), x, oo) == 1
assert gruntz(exp(x)/(a + exp(x)), x, oo) == 1
def test_limit4():
#issue 3463
assert gruntz((3**x + 5**x)**(1/x), x, oo) == 5
#issue 3463
assert gruntz((3**(1/x) + 5**(1/x))**x, x, 0) == 5
@XFAIL
def test_MrvTestCase_page47_ex3_21():
h = exp(-x/(1 + exp(-x)))
expr = exp(h)*exp(-x/(1 + h))*exp(exp(-x + h))/h**2 - exp(x) + x
assert mmrv(expr, x) == {1/h, exp(-x), exp(x), exp(x - h), exp(x/(1 + h))}
def test_gruntz_I():
y = Symbol("y")
assert gruntz(I*x, x, oo) == I*oo
assert gruntz(y*I*x, x, oo) == y*I*oo
assert gruntz(y*3*I*x, x, oo) == y*I*oo
assert gruntz(y*3*sin(I)*x, x, oo).simplify().rewrite(_sign) == _sign(y)*I*oo
def test_issue_4814():
assert gruntz((x + 1)**(1/log(x + 1)), x, oo) == E
def test_intractable():
assert gruntz(1/gamma(x), x, oo) == 0
assert gruntz(1/loggamma(x), x, oo) == 0
assert gruntz(gamma(x)/loggamma(x), x, oo) is oo
assert gruntz(exp(gamma(x))/gamma(x), x, oo) is oo
assert gruntz(gamma(x), x, 3) == 2
assert gruntz(gamma(Rational(1, 7) + 1/x), x, oo) == gamma(Rational(1, 7))
assert gruntz(log(x**x)/log(gamma(x)), x, oo) == 1
assert gruntz(log(gamma(gamma(x)))/exp(x), x, oo) is oo
def test_aseries_trig():
assert cancel(gruntz(1/log(atan(x)), x, oo)
- 1/(log(pi) + log(S.Half))) == 0
assert gruntz(1/acot(x), x, -oo) is -oo
def test_exp_log_series():
assert gruntz(x/log(log(x*exp(x))), x, oo) is oo
def test_issue_3644():
assert gruntz(((x**7 + x + 1)/(2**x + x**2))**(-1/x), x, oo) == 2
def test_issue_6843():
n = Symbol('n', integer=True, positive=True)
r = (n + 1)*x**(n + 1)/(x**(n + 1) - 1) - x/(x - 1)
assert gruntz(r, x, 1).simplify() == n/2
def test_issue_4190():
assert gruntz(x - gamma(1/x), x, oo) == S.EulerGamma
@XFAIL
def test_issue_5172():
n = Symbol('n')
r = Symbol('r', positive=True)
c = Symbol('c')
p = Symbol('p', positive=True)
m = Symbol('m', negative=True)
expr = ((2*n*(n - r + 1)/(n + r*(n - r + 1)))**c + \
(r - 1)*(n*(n - r + 2)/(n + r*(n - r + 1)))**c - n)/(n**c - n)
expr = expr.subs(c, c + 1)
assert gruntz(expr.subs(c, m), n, oo) == 1
# fail:
assert gruntz(expr.subs(c, p), n, oo).simplify() == \
(2**(p + 1) + r - 1)/(r + 1)**(p + 1)
def test_issue_4109():
assert gruntz(1/gamma(x), x, 0) == 0
assert gruntz(x*gamma(x), x, 0) == 1
def test_issue_6682():
assert gruntz(exp(2*Ei(-x))/x**2, x, 0) == exp(2*EulerGamma)
def test_issue_7096():
from sympy.functions import sign
assert gruntz(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi))
def test_issue_24210_25885():
eq = exp(x)/(1+1/x)**x**2
ans = sqrt(E)
assert gruntz(eq, x, oo) == ans
assert gruntz(1/eq, x, oo) == 1/ans

View File

@ -0,0 +1,23 @@
from sympy.series.kauers import finite_diff
from sympy.series.kauers import finite_diff_kauers
from sympy.abc import x, y, z, m, n, w
from sympy.core.numbers import pi
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.concrete.summations import Sum
def test_finite_diff():
assert finite_diff(x**2 + 2*x + 1, x) == 2*x + 3
assert finite_diff(y**3 + 2*y**2 + 3*y + 5, y) == 3*y**2 + 7*y + 6
assert finite_diff(z**2 - 2*z + 3, z) == 2*z - 1
assert finite_diff(w**2 + 3*w - 2, w) == 2*w + 4
assert finite_diff(sin(x), x, pi/6) == -sin(x) + sin(x + pi/6)
assert finite_diff(cos(y), y, pi/3) == -cos(y) + cos(y + pi/3)
assert finite_diff(x**2 - 2*x + 3, x, 2) == 4*x
assert finite_diff(n**2 - 2*n + 3, n, 3) == 6*n + 3
def test_finite_diff_kauers():
assert finite_diff_kauers(Sum(x**2, (x, 1, n))) == (n + 1)**2
assert finite_diff_kauers(Sum(y, (y, 1, m))) == (m + 1)
assert finite_diff_kauers(Sum((x*y), (x, 1, m), (y, 1, n))) == (m + 1)*(n + 1)
assert finite_diff_kauers(Sum((x*y**2), (x, 1, m), (y, 1, n))) == (n + 1)**2*(m + 1)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,177 @@
from sympy.concrete.summations import Sum
from sympy.core.add import Add
from sympy.core.numbers import (I, Rational, oo, pi)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.combinatorial.factorials import (binomial, factorial, subfactorial)
from sympy.functions.combinatorial.numbers import (fibonacci, harmonic)
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.functions.special.gamma_functions import gamma
from sympy.series.limitseq import limit_seq
from sympy.series.limitseq import difference_delta as dd
from sympy.testing.pytest import raises, XFAIL
from sympy.calculus.accumulationbounds import AccumulationBounds
n, m, k = symbols('n m k', integer=True)
def test_difference_delta():
e = n*(n + 1)
e2 = e * k
assert dd(e) == 2*n + 2
assert dd(e2, n, 2) == k*(4*n + 6)
raises(ValueError, lambda: dd(e2))
raises(ValueError, lambda: dd(e2, n, oo))
def test_difference_delta__Sum():
e = Sum(1/k, (k, 1, n))
assert dd(e, n) == 1/(n + 1)
assert dd(e, n, 5) == Add(*[1/(i + n + 1) for i in range(5)])
e = Sum(1/k, (k, 1, 3*n))
assert dd(e, n) == Add(*[1/(i + 3*n + 1) for i in range(3)])
e = n * Sum(1/k, (k, 1, n))
assert dd(e, n) == 1 + Sum(1/k, (k, 1, n))
e = Sum(1/k, (k, 1, n), (m, 1, n))
assert dd(e, n) == harmonic(n)
def test_difference_delta__Add():
e = n + n*(n + 1)
assert dd(e, n) == 2*n + 3
assert dd(e, n, 2) == 4*n + 8
e = n + Sum(1/k, (k, 1, n))
assert dd(e, n) == 1 + 1/(n + 1)
assert dd(e, n, 5) == 5 + Add(*[1/(i + n + 1) for i in range(5)])
def test_difference_delta__Pow():
e = 4**n
assert dd(e, n) == 3*4**n
assert dd(e, n, 2) == 15*4**n
e = 4**(2*n)
assert dd(e, n) == 15*4**(2*n)
assert dd(e, n, 2) == 255*4**(2*n)
e = n**4
assert dd(e, n) == (n + 1)**4 - n**4
e = n**n
assert dd(e, n) == (n + 1)**(n + 1) - n**n
def test_limit_seq():
e = binomial(2*n, n) / Sum(binomial(2*k, k), (k, 1, n))
assert limit_seq(e) == S(3) / 4
assert limit_seq(e, m) == e
e = (5*n**3 + 3*n**2 + 4) / (3*n**3 + 4*n - 5)
assert limit_seq(e, n) == S(5) / 3
e = (harmonic(n) * Sum(harmonic(k), (k, 1, n))) / (n * harmonic(2*n)**2)
assert limit_seq(e, n) == 1
e = Sum(k**2 * Sum(2**m/m, (m, 1, k)), (k, 1, n)) / (2**n*n)
assert limit_seq(e, n) == 4
e = (Sum(binomial(3*k, k) * binomial(5*k, k), (k, 1, n)) /
(binomial(3*n, n) * binomial(5*n, n)))
assert limit_seq(e, n) == S(84375) / 83351
e = Sum(harmonic(k)**2/k, (k, 1, 2*n)) / harmonic(n)**3
assert limit_seq(e, n) == S.One / 3
raises(ValueError, lambda: limit_seq(e * m))
def test_alternating_sign():
assert limit_seq((-1)**n/n**2, n) == 0
assert limit_seq((-2)**(n+1)/(n + 3**n), n) == 0
assert limit_seq((2*n + (-1)**n)/(n + 1), n) == 2
assert limit_seq(sin(pi*n), n) == 0
assert limit_seq(cos(2*pi*n), n) == 1
assert limit_seq((S.NegativeOne/5)**n, n) == 0
assert limit_seq((Rational(-1, 5))**n, n) == 0
assert limit_seq((I/3)**n, n) == 0
assert limit_seq(sqrt(n)*(I/2)**n, n) == 0
assert limit_seq(n**7*(I/3)**n, n) == 0
assert limit_seq(n/(n + 1) + (I/2)**n, n) == 1
def test_accum_bounds():
assert limit_seq((-1)**n, n) == AccumulationBounds(-1, 1)
assert limit_seq(cos(pi*n), n) == AccumulationBounds(-1, 1)
assert limit_seq(sin(pi*n/2)**2, n) == AccumulationBounds(0, 1)
assert limit_seq(2*(-3)**n/(n + 3**n), n) == AccumulationBounds(-2, 2)
assert limit_seq(3*n/(n + 1) + 2*(-1)**n, n) == AccumulationBounds(1, 5)
def test_limitseq_sum():
from sympy.abc import x, y, z
assert limit_seq(Sum(1/x, (x, 1, y)) - log(y), y) == S.EulerGamma
assert limit_seq(Sum(1/x, (x, 1, y)) - 1/y, y) is S.Infinity
assert (limit_seq(binomial(2*x, x) / Sum(binomial(2*y, y), (y, 1, x)), x) ==
S(3) / 4)
assert (limit_seq(Sum(y**2 * Sum(2**z/z, (z, 1, y)), (y, 1, x)) /
(2**x*x), x) == 4)
def test_issue_9308():
assert limit_seq(subfactorial(n)/factorial(n), n) == exp(-1)
def test_issue_10382():
n = Symbol('n', integer=True)
assert limit_seq(fibonacci(n+1)/fibonacci(n), n).together() == S.GoldenRatio
def test_issue_11672():
assert limit_seq(Rational(-1, 2)**n, n) == 0
def test_issue_14196():
k, n = symbols('k, n', positive=True)
m = Symbol('m')
assert limit_seq(Sum(m**k, (m, 1, n)).doit()/(n**(k + 1)), n) == 1/(k + 1)
def test_issue_16735():
assert limit_seq(5**n/factorial(n), n) == 0
def test_issue_19868():
assert limit_seq(1/gamma(n + S.One/2), n) == 0
@XFAIL
def test_limit_seq_fail():
# improve Summation algorithm or add ad-hoc criteria
e = (harmonic(n)**3 * Sum(1/harmonic(k), (k, 1, n)) /
(n * Sum(harmonic(k)/k, (k, 1, n))))
assert limit_seq(e, n) == 2
# No unique dominant term
e = (Sum(2**k * binomial(2*k, k) / k**2, (k, 1, n)) /
(Sum(2**k/k*2, (k, 1, n)) * Sum(binomial(2*k, k), (k, 1, n))))
assert limit_seq(e, n) == S(3) / 7
# Simplifications of summations needs to be improved.
e = n**3*Sum(2**k/k**2, (k, 1, n))**2 / (2**n * Sum(2**k/k, (k, 1, n)))
assert limit_seq(e, n) == 2
e = (harmonic(n) * Sum(2**k/k, (k, 1, n)) /
(n * Sum(2**k*harmonic(k)/k**2, (k, 1, n))))
assert limit_seq(e, n) == 1
e = (Sum(2**k*factorial(k) / k**2, (k, 1, 2*n)) /
(Sum(4**k/k**2, (k, 1, n)) * Sum(factorial(k), (k, 1, 2*n))))
assert limit_seq(e, n) == S(3) / 16

View File

@ -0,0 +1,65 @@
from sympy.core.numbers import E
from sympy.core.singleton import S
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.hyperbolic import tanh
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.series.order import Order
from sympy.abc import x, y
def test_sin():
e = sin(x).lseries(x)
assert next(e) == x
assert next(e) == -x**3/6
assert next(e) == x**5/120
def test_cos():
e = cos(x).lseries(x)
assert next(e) == 1
assert next(e) == -x**2/2
assert next(e) == x**4/24
def test_exp():
e = exp(x).lseries(x)
assert next(e) == 1
assert next(e) == x
assert next(e) == x**2/2
assert next(e) == x**3/6
def test_exp2():
e = exp(cos(x)).lseries(x)
assert next(e) == E
assert next(e) == -E*x**2/2
assert next(e) == E*x**4/6
assert next(e) == -31*E*x**6/720
def test_simple():
assert list(x.lseries()) == [x]
assert list(S.One.lseries(x)) == [1]
assert not next((x/(x + y)).lseries(y)).has(Order)
def test_issue_5183():
s = (x + 1/x).lseries()
assert list(s) == [1/x, x]
assert next((x + x**2).lseries()) == x
assert next(((1 + x)**7).lseries(x)) == 1
assert next((sin(x + y)).series(x, n=3).lseries(y)) == x
# it would be nice if all terms were grouped, but in the
# following case that would mean that all the terms would have
# to be known since, for example, every term has a constant in it.
s = ((1 + x)**7).series(x, 1, n=None)
assert [next(s) for i in range(2)] == [128, -448 + 448*x]
def test_issue_6999():
s = tanh(x).lseries(x, 1)
assert next(s) == tanh(1)
assert next(s) == x - (x - 1)*tanh(1)**2 - 1
assert next(s) == -(x - 1)**2*tanh(1) + (x - 1)**2*tanh(1)**3
assert next(s) == -(x - 1)**3*tanh(1)**4 - (x - 1)**3/3 + \
4*(x - 1)**3*tanh(1)**2/3

View File

@ -0,0 +1,557 @@
from sympy.calculus.util import AccumBounds
from sympy.core.function import (Derivative, PoleError)
from sympy.core.numbers import (E, I, Integer, Rational, pi)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.complexes import sign
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.hyperbolic import (acosh, acoth, asinh, atanh, cosh, coth, sinh, tanh)
from sympy.functions.elementary.integers import (ceiling, floor, frac)
from sympy.functions.elementary.miscellaneous import (cbrt, sqrt)
from sympy.functions.elementary.trigonometric import (asin, cos, cot, sin, tan)
from sympy.series.limits import limit
from sympy.series.order import O
from sympy.abc import x, y, z
from sympy.testing.pytest import raises, XFAIL
def test_simple_1():
assert x.nseries(x, n=5) == x
assert y.nseries(x, n=5) == y
assert (1/(x*y)).nseries(y, n=5) == 1/(x*y)
assert Rational(3, 4).nseries(x, n=5) == Rational(3, 4)
assert x.nseries() == x
def test_mul_0():
assert (x*log(x)).nseries(x, n=5) == x*log(x)
def test_mul_1():
assert (x*log(2 + x)).nseries(x, n=5) == x*log(2) + x**2/2 - x**3/8 + \
x**4/24 + O(x**5)
assert (x*log(1 + x)).nseries(
x, n=5) == x**2 - x**3/2 + x**4/3 + O(x**5)
def test_pow_0():
assert (x**2).nseries(x, n=5) == x**2
assert (1/x).nseries(x, n=5) == 1/x
assert (1/x**2).nseries(x, n=5) == 1/x**2
assert (x**Rational(2, 3)).nseries(x, n=5) == (x**Rational(2, 3))
assert (sqrt(x)**3).nseries(x, n=5) == (sqrt(x)**3)
def test_pow_1():
assert ((1 + x)**2).nseries(x, n=5) == x**2 + 2*x + 1
# https://github.com/sympy/sympy/issues/21075
assert ((sqrt(x) + 1)**2).nseries(x) == 2*sqrt(x) + x + 1
assert ((sqrt(x) + cbrt(x))**2).nseries(x) == 2*x**Rational(5, 6)\
+ x**Rational(2, 3) + x
def test_geometric_1():
assert (1/(1 - x)).nseries(x, n=5) == 1 + x + x**2 + x**3 + x**4 + O(x**5)
assert (x/(1 - x)).nseries(x, n=6) == x + x**2 + x**3 + x**4 + x**5 + O(x**6)
assert (x**3/(1 - x)).nseries(x, n=8) == x**3 + x**4 + x**5 + x**6 + \
x**7 + O(x**8)
def test_sqrt_1():
assert sqrt(1 + x).nseries(x, n=5) == 1 + x/2 - x**2/8 + x**3/16 - 5*x**4/128 + O(x**5)
def test_exp_1():
assert exp(x).nseries(x, n=5) == 1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)
assert exp(x).nseries(x, n=12) == 1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + \
x**6/720 + x**7/5040 + x**8/40320 + x**9/362880 + x**10/3628800 + \
x**11/39916800 + O(x**12)
assert exp(1/x).nseries(x, n=5) == exp(1/x)
assert exp(1/(1 + x)).nseries(x, n=4) == \
(E*(1 - x - 13*x**3/6 + 3*x**2/2)).expand() + O(x**4)
assert exp(2 + x).nseries(x, n=5) == \
(exp(2)*(1 + x + x**2/2 + x**3/6 + x**4/24)).expand() + O(x**5)
def test_exp_sqrt_1():
assert exp(1 + sqrt(x)).nseries(x, n=3) == \
(exp(1)*(1 + sqrt(x) + x/2 + sqrt(x)*x/6)).expand() + O(sqrt(x)**3)
def test_power_x_x1():
assert (exp(x*log(x))).nseries(x, n=4) == \
1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
def test_power_x_x2():
assert (x**x).nseries(x, n=4) == \
1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
def test_log_singular1():
assert log(1 + 1/x).nseries(x, n=5) == x - log(x) - x**2/2 + x**3/3 - \
x**4/4 + O(x**5)
def test_log_power1():
e = 1 / (1/x + x ** (log(3)/log(2)))
assert e.nseries(x, n=5) == -x**(log(3)/log(2) + 2) + x + O(x**5)
def test_log_series():
l = Symbol('l')
e = 1/(1 - log(x))
assert e.nseries(x, n=5, logx=l) == 1/(1 - l)
def test_log2():
e = log(-1/x)
assert e.nseries(x, n=5) == -log(x) + log(-1)
def test_log3():
l = Symbol('l')
e = 1/log(-1/x)
assert e.nseries(x, n=4, logx=l) == 1/(-l + log(-1))
def test_series1():
e = sin(x)
assert e.nseries(x, 0, 0) != 0
assert e.nseries(x, 0, 0) == O(1, x)
assert e.nseries(x, 0, 1) == O(x, x)
assert e.nseries(x, 0, 2) == x + O(x**2, x)
assert e.nseries(x, 0, 3) == x + O(x**3, x)
assert e.nseries(x, 0, 4) == x - x**3/6 + O(x**4, x)
e = (exp(x) - 1)/x
assert e.nseries(x, 0, 3) == 1 + x/2 + x**2/6 + O(x**3)
assert x.nseries(x, 0, 2) == x
@XFAIL
def test_series1_failing():
assert x.nseries(x, 0, 0) == O(1, x)
assert x.nseries(x, 0, 1) == O(x, x)
def test_seriesbug1():
assert (1/x).nseries(x, 0, 3) == 1/x
assert (x + 1/x).nseries(x, 0, 3) == x + 1/x
def test_series2x():
assert ((x + 1)**(-2)).nseries(x, 0, 4) == 1 - 2*x + 3*x**2 - 4*x**3 + O(x**4, x)
assert ((x + 1)**(-1)).nseries(x, 0, 4) == 1 - x + x**2 - x**3 + O(x**4, x)
assert ((x + 1)**0).nseries(x, 0, 3) == 1
assert ((x + 1)**1).nseries(x, 0, 3) == 1 + x
assert ((x + 1)**2).nseries(x, 0, 3) == x**2 + 2*x + 1
assert ((x + 1)**3).nseries(x, 0, 3) == 1 + 3*x + 3*x**2 + O(x**3)
assert (1/(1 + x)).nseries(x, 0, 4) == 1 - x + x**2 - x**3 + O(x**4, x)
assert (x + 3/(1 + 2*x)).nseries(x, 0, 4) == 3 - 5*x + 12*x**2 - 24*x**3 + O(x**4, x)
assert ((1/x + 1)**3).nseries(x, 0, 3) == 1 + 3/x + 3/x**2 + x**(-3)
assert (1/(1 + 1/x)).nseries(x, 0, 4) == x - x**2 + x**3 - O(x**4, x)
assert (1/(1 + 1/x**2)).nseries(x, 0, 6) == x**2 - x**4 + O(x**6, x)
def test_bug2(): # 1/log(0)*log(0) problem
w = Symbol("w")
e = (w**(-1) + w**(
-log(3)*log(2)**(-1)))**(-1)*(3*w**(-log(3)*log(2)**(-1)) + 2*w**(-1))
e = e.expand()
assert e.nseries(w, 0, 4).subs(w, 0) == 3
def test_exp():
e = (1 + x)**(1/x)
assert e.nseries(x, n=3) == exp(1) - x*exp(1)/2 + 11*exp(1)*x**2/24 + O(x**3)
def test_exp2():
w = Symbol("w")
e = w**(1 - log(x)/(log(2) + log(x)))
logw = Symbol("logw")
assert e.nseries(
w, 0, 1, logx=logw) == exp(logw*log(2)/(log(x) + log(2)))
def test_bug3():
e = (2/x + 3/x**2)/(1/x + 1/x**2)
assert e.nseries(x, n=3) == 3 - x + x**2 + O(x**3)
def test_generalexponent():
p = 2
e = (2/x + 3/x**p)/(1/x + 1/x**p)
assert e.nseries(x, 0, 3) == 3 - x + x**2 + O(x**3)
p = S.Half
e = (2/x + 3/x**p)/(1/x + 1/x**p)
assert e.nseries(x, 0, 2) == 2 - x + sqrt(x) + x**(S(3)/2) + O(x**2)
e = 1 + sqrt(x)
assert e.nseries(x, 0, 4) == 1 + sqrt(x)
# more complicated example
def test_genexp_x():
e = 1/(1 + sqrt(x))
assert e.nseries(x, 0, 2) == \
1 + x - sqrt(x) - sqrt(x)**3 + O(x**2, x)
# more complicated example
def test_genexp_x2():
p = Rational(3, 2)
e = (2/x + 3/x**p)/(1/x + 1/x**p)
assert e.nseries(x, 0, 3) == 3 + x + x**2 - sqrt(x) - x**(S(3)/2) - x**(S(5)/2) + O(x**3)
def test_seriesbug2():
w = Symbol("w")
#simple case (1):
e = ((2*w)/w)**(1 + w)
assert e.nseries(w, 0, 1) == 2 + O(w, w)
assert e.nseries(w, 0, 1).subs(w, 0) == 2
def test_seriesbug2b():
w = Symbol("w")
#test sin
e = sin(2*w)/w
assert e.nseries(w, 0, 3) == 2 - 4*w**2/3 + O(w**3)
def test_seriesbug2d():
w = Symbol("w", real=True)
e = log(sin(2*w)/w)
assert e.series(w, n=5) == log(2) - 2*w**2/3 - 4*w**4/45 + O(w**5)
def test_seriesbug2c():
w = Symbol("w", 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_expbug4():
x = Symbol("x", real=True)
assert (log(
sin(2*x)/x)*(1 + x)).series(x, 0, 2) == log(2) + x*log(2) + O(x**2, x)
assert exp(
log(sin(2*x)/x)*(1 + x)).series(x, 0, 2) == 2 + 2*x*log(2) + O(x**2)
assert exp(log(2) + O(x)).nseries(x, 0, 2) == 2 + O(x)
assert ((2 + O(x))**(1 + x)).nseries(x, 0, 2) == 2 + O(x)
def test_logbug4():
assert log(2 + O(x)).nseries(x, 0, 2) == log(2) + O(x, x)
def test_expbug5():
assert exp(log(1 + x)/x).nseries(x, n=3) == exp(1) + -exp(1)*x/2 + 11*exp(1)*x**2/24 + O(x**3)
assert exp(O(x)).nseries(x, 0, 2) == 1 + O(x)
def test_sinsinbug():
assert sin(sin(x)).nseries(x, 0, 8) == x - x**3/3 + x**5/10 - 8*x**7/315 + O(x**8)
def test_issue_3258():
a = x/(exp(x) - 1)
assert a.nseries(x, 0, 5) == 1 - x/2 - x**4/720 + x**2/12 + O(x**5)
def test_issue_3204():
x = Symbol("x", nonnegative=True)
f = sin(x**3)**Rational(1, 3)
assert f.nseries(x, 0, 17) == x - x**7/18 - x**13/3240 + O(x**17)
def test_issue_3224():
f = sqrt(1 - sqrt(y))
assert f.nseries(y, 0, 2) == 1 - sqrt(y)/2 - y/8 - sqrt(y)**3/16 + O(y**2)
def test_issue_3463():
w, i = symbols('w,i')
r = log(5)/log(3)
p = w**(-1 + r)
e = 1/x*(-log(w**(1 + r)) + log(w + w**r))
e_ser = -r*log(w)/x + p/x - p**2/(2*x) + O(w)
assert e.nseries(w, n=1) == e_ser
def test_sin():
assert sin(8*x).nseries(x, n=4) == 8*x - 256*x**3/3 + O(x**4)
assert sin(x + y).nseries(x, n=1) == sin(y) + O(x)
assert sin(x + y).nseries(x, n=2) == sin(y) + cos(y)*x + O(x**2)
assert sin(x + y).nseries(x, n=5) == sin(y) + cos(y)*x - sin(y)*x**2/2 - \
cos(y)*x**3/6 + sin(y)*x**4/24 + O(x**5)
def test_issue_3515():
e = sin(8*x)/x
assert e.nseries(x, n=6) == 8 - 256*x**2/3 + 4096*x**4/15 + O(x**6)
def test_issue_3505():
e = sin(x)**(-4)*(sqrt(cos(x))*sin(x)**2 -
cos(x)**Rational(1, 3)*sin(x)**2)
assert e.nseries(x, n=9) == Rational(-1, 12) - 7*x**2/288 - \
43*x**4/10368 - 1123*x**6/2488320 + 377*x**8/29859840 + O(x**9)
def test_issue_3501():
a = Symbol("a")
e = x**(-2)*(x*sin(a + x) - x*sin(a))
assert e.nseries(x, n=6) == cos(a) - sin(a)*x/2 - cos(a)*x**2/6 + \
x**3*sin(a)/24 + x**4*cos(a)/120 - x**5*sin(a)/720 + O(x**6)
e = x**(-2)*(x*cos(a + x) - x*cos(a))
assert e.nseries(x, n=6) == -sin(a) - cos(a)*x/2 + sin(a)*x**2/6 + \
cos(a)*x**3/24 - x**4*sin(a)/120 - x**5*cos(a)/720 + O(x**6)
def test_issue_3502():
e = sin(5*x)/sin(2*x)
assert e.nseries(x, n=2) == Rational(5, 2) + O(x**2)
assert e.nseries(x, n=6) == \
Rational(5, 2) - 35*x**2/4 + 329*x**4/48 + O(x**6)
def test_issue_3503():
e = sin(2 + x)/(2 + x)
assert e.nseries(x, n=2) == sin(2)/2 + x*cos(2)/2 - x*sin(2)/4 + O(x**2)
def test_issue_3506():
e = (x + sin(3*x))**(-2)*(x*(x + sin(3*x)) - (x + sin(3*x))*sin(2*x))
assert e.nseries(x, n=7) == \
Rational(-1, 4) + 5*x**2/96 + 91*x**4/768 + 11117*x**6/129024 + O(x**7)
def test_issue_3508():
x = Symbol("x", real=True)
assert log(sin(x)).series(x, n=5) == log(x) - x**2/6 - x**4/180 + O(x**5)
e = -log(x) + x*(-log(x) + log(sin(2*x))) + log(sin(2*x))
assert e.series(x, n=5) == \
log(2) + log(2)*x - 2*x**2/3 - 2*x**3/3 - 4*x**4/45 + O(x**5)
def test_issue_3507():
e = x**(-4)*(x**2 - x**2*sqrt(cos(x)))
assert e.nseries(x, n=9) == \
Rational(1, 4) + x**2/96 + 19*x**4/5760 + 559*x**6/645120 + 29161*x**8/116121600 + O(x**9)
def test_issue_3639():
assert sin(cos(x)).nseries(x, n=5) == \
sin(1) - x**2*cos(1)/2 - x**4*sin(1)/8 + x**4*cos(1)/24 + O(x**5)
def test_hyperbolic():
assert sinh(x).nseries(x, n=6) == x + x**3/6 + x**5/120 + O(x**6)
assert cosh(x).nseries(x, n=5) == 1 + x**2/2 + x**4/24 + O(x**5)
assert tanh(x).nseries(x, n=6) == x - x**3/3 + 2*x**5/15 + O(x**6)
assert coth(x).nseries(x, n=6) == \
1/x - x**3/45 + x/3 + 2*x**5/945 + O(x**6)
assert asinh(x).nseries(x, n=6) == x - x**3/6 + 3*x**5/40 + O(x**6)
assert acosh(x).nseries(x, n=6) == \
pi*I/2 - I*x - 3*I*x**5/40 - I*x**3/6 + O(x**6)
assert atanh(x).nseries(x, n=6) == x + x**3/3 + x**5/5 + O(x**6)
assert acoth(x).nseries(x, n=6) == -I*pi/2 + x + x**3/3 + x**5/5 + O(x**6)
def test_series2():
w = Symbol("w", real=True)
x = Symbol("x", real=True)
e = w**(-2)*(w*exp(1/x - w) - w*exp(1/x))
assert e.nseries(w, n=4) == -exp(1/x) + w*exp(1/x)/2 - w**2*exp(1/x)/6 + w**3*exp(1/x)/24 + O(w**4)
def test_series3():
w = Symbol("w", real=True)
e = w**(-6)*(w**3*tan(w) - w**3*sin(w))
assert e.nseries(w, n=8) == Integer(1)/2 + w**2/8 + 13*w**4/240 + 529*w**6/24192 + O(w**8)
def test_bug4():
w = Symbol("w")
e = x/(w**4 + x**2*w**4 + 2*x*w**4)*w**4
assert e.nseries(w, n=2).removeO().expand() in [x/(1 + 2*x + x**2),
1/(1 + x/2 + 1/x/2)/2, 1/x/(1 + 2/x + x**(-2))]
def test_bug5():
w = Symbol("w")
l = Symbol('l')
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=0, logx=l) == x/w/l + 1/w + O(1, w)
assert e.nseries(w, n=1, logx=l) == x/w/l + 1/w - x/l + 1/l*log(x) \
+ x*log(x)/l**2 + O(w)
def test_issue_4115():
assert (sin(x)/(1 - cos(x))).nseries(x, n=1) == 2/x + O(x)
assert (sin(x)**2/(1 - cos(x))).nseries(x, n=1) == 2 + O(x)
def test_pole():
raises(PoleError, lambda: sin(1/x).series(x, 0, 5))
raises(PoleError, lambda: sin(1 + 1/x).series(x, 0, 5))
raises(PoleError, lambda: (x*sin(1/x)).series(x, 0, 5))
def test_expsinbug():
assert exp(sin(x)).series(x, 0, 0) == O(1, x)
assert exp(sin(x)).series(x, 0, 1) == 1 + O(x)
assert exp(sin(x)).series(x, 0, 2) == 1 + x + O(x**2)
assert exp(sin(x)).series(x, 0, 3) == 1 + x + x**2/2 + O(x**3)
assert exp(sin(x)).series(x, 0, 4) == 1 + x + x**2/2 + O(x**4)
assert exp(sin(x)).series(x, 0, 5) == 1 + x + x**2/2 - x**4/8 + O(x**5)
def test_floor():
x = Symbol('x')
assert floor(x).series(x) == 0
assert floor(-x).series(x) == -1
assert floor(sin(x)).series(x) == 0
assert floor(sin(-x)).series(x) == -1
assert floor(x**3).series(x) == 0
assert floor(-x**3).series(x) == -1
assert floor(cos(x)).series(x) == 0
assert floor(cos(-x)).series(x) == 0
assert floor(5 + sin(x)).series(x) == 5
assert floor(5 + sin(-x)).series(x) == 4
assert floor(x).series(x, 2) == 2
assert floor(-x).series(x, 2) == -3
x = Symbol('x', negative=True)
assert floor(x + 1.5).series(x) == 1
def test_frac():
assert frac(x).series(x, cdir=1) == x
assert frac(x).series(x, cdir=-1) == 1 + x
assert frac(2*x + 1).series(x, cdir=1) == 2*x
assert frac(2*x + 1).series(x, cdir=-1) == 1 + 2*x
assert frac(x**2).series(x, cdir=1) == x**2
assert frac(x**2).series(x, cdir=-1) == x**2
assert frac(sin(x) + 5).series(x, cdir=1) == x - x**3/6 + x**5/120 + O(x**6)
assert frac(sin(x) + 5).series(x, cdir=-1) == 1 + x - x**3/6 + x**5/120 + O(x**6)
assert frac(sin(x) + S.Half).series(x) == S.Half + x - x**3/6 + x**5/120 + O(x**6)
assert frac(x**8).series(x, cdir=1) == O(x**6)
assert frac(1/x).series(x) == AccumBounds(0, 1) + O(x**6)
def test_ceiling():
assert ceiling(x).series(x) == 1
assert ceiling(-x).series(x) == 0
assert ceiling(sin(x)).series(x) == 1
assert ceiling(sin(-x)).series(x) == 0
assert ceiling(1 - cos(x)).series(x) == 1
assert ceiling(1 - cos(-x)).series(x) == 1
assert ceiling(x).series(x, 2) == 3
assert ceiling(-x).series(x, 2) == -2
def test_abs():
a = Symbol('a')
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**4)
assert abs(sin(-x)).nseries(x, n=4) == x - Rational(1, 6)*x**3 + O(x**4)
assert abs(x - a).nseries(x, 1) == -a*sign(1 - a) + (x - 1)*sign(1 - a) + sign(1 - a)
def test_dir():
assert abs(x).series(x, 0, dir="+") == x
assert abs(x).series(x, 0, dir="-") == -x
assert floor(x + 2).series(x, 0, dir='+') == 2
assert floor(x + 2).series(x, 0, dir='-') == 1
assert floor(x + 2.2).series(x, 0, dir='-') == 2
assert ceiling(x + 2.2).series(x, 0, dir='-') == 3
assert sin(x + y).series(x, 0, dir='-') == sin(x + y).series(x, 0, dir='+')
def test_cdir():
assert abs(x).series(x, 0, cdir=1) == x
assert abs(x).series(x, 0, cdir=-1) == -x
assert floor(x + 2).series(x, 0, cdir=1) == 2
assert floor(x + 2).series(x, 0, cdir=-1) == 1
assert floor(x + 2.2).series(x, 0, cdir=1) == 2
assert ceiling(x + 2.2).series(x, 0, cdir=-1) == 3
assert sin(x + y).series(x, 0, cdir=-1) == sin(x + y).series(x, 0, cdir=1)
def test_issue_3504():
a = Symbol("a")
e = asin(a*x)/x
assert e.series(x, 4, n=2).removeO() == \
(x - 4)*(a/(4*sqrt(-16*a**2 + 1)) - asin(4*a)/16) + asin(4*a)/4
def test_issue_4441():
a, b = symbols('a,b')
f = 1/(1 + a*x)
assert f.series(x, 0, 5) == 1 - a*x + a**2*x**2 - a**3*x**3 + \
a**4*x**4 + O(x**5)
f = 1/(1 + (a + b)*x)
assert f.series(x, 0, 3) == 1 + x*(-a - b)\
+ x**2*(a + b)**2 + O(x**3)
def test_issue_4329():
assert tan(x).series(x, pi/2, n=3).removeO() == \
-pi/6 + x/3 - 1/(x - pi/2)
assert cot(x).series(x, pi, n=3).removeO() == \
-x/3 + pi/3 + 1/(x - pi)
assert limit(tan(x)**tan(2*x), x, pi/4) == exp(-1)
def test_issue_5183():
assert abs(x + x**2).series(n=1) == O(x)
assert abs(x + x**2).series(n=2) == x + O(x**2)
assert ((1 + x)**2).series(x, n=6) == x**2 + 2*x + 1
assert (1 + 1/x).series() == 1 + 1/x
assert Derivative(exp(x).series(), x).doit() == \
1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)
def test_issue_5654():
a = Symbol('a')
assert (1/(x**2+a**2)**2).nseries(x, x0=I*a, n=0) == \
-I/(4*a**3*(-I*a + x)) - 1/(4*a**2*(-I*a + x)**2) + O(1, (x, I*a))
assert (1/(x**2+a**2)**2).nseries(x, x0=I*a, n=1) == 3/(16*a**4) \
-I/(4*a**3*(-I*a + x)) - 1/(4*a**2*(-I*a + x)**2) + O(-I*a + x, (x, I*a))
def test_issue_5925():
sx = sqrt(x + z).series(z, 0, 1)
sxy = sqrt(x + y + z).series(z, 0, 1)
s1, s2 = sx.subs(x, x + y), sxy
assert (s1 - s2).expand().removeO().simplify() == 0
sx = sqrt(x + z).series(z, 0, 1)
sxy = sqrt(x + y + z).series(z, 0, 1)
assert sxy.subs({x:1, y:2}) == sx.subs(x, 3)
def test_exp_2():
assert exp(x**3).nseries(x, 0, 14) == 1 + x**3 + x**6/2 + x**9/6 + x**12/24 + O(x**14)

View File

@ -0,0 +1,477 @@
from sympy.core.add import Add
from sympy.core.function import (Function, expand)
from sympy.core.numbers import (I, Rational, nan, oo, pi)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.complexes import (conjugate, transpose)
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.integrals.integrals import Integral
from sympy.series.order import O, Order
from sympy.core.expr import unchanged
from sympy.testing.pytest import raises
from sympy.abc import w, x, y, z
def test_caching_bug():
#needs to be a first test, so that all caches are clean
#cache it
O(w)
#and test that this won't raise an exception
O(w**(-1/x/log(3)*log(5)), w)
def test_free_symbols():
assert Order(1).free_symbols == set()
assert Order(x).free_symbols == {x}
assert Order(1, x).free_symbols == {x}
assert Order(x*y).free_symbols == {x, y}
assert Order(x, x, y).free_symbols == {x, y}
def test_simple_1():
o = Rational(0)
assert Order(2*x) == Order(x)
assert Order(x)*3 == Order(x)
assert -28*Order(x) == Order(x)
assert Order(Order(x)) == Order(x)
assert Order(Order(x), y) == Order(Order(x), x, y)
assert Order(-23) == Order(1)
assert Order(exp(x)) == Order(1, x)
assert Order(exp(1/x)).expr == exp(1/x)
assert Order(x*exp(1/x)).expr == x*exp(1/x)
assert Order(x**(o/3)).expr == x**(o/3)
assert Order(x**(o*Rational(5, 3))).expr == x**(o*Rational(5, 3))
assert Order(x**2 + x + y, x) == O(1, x)
assert Order(x**2 + x + y, y) == O(1, y)
raises(ValueError, lambda: Order(exp(x), x, x))
raises(TypeError, lambda: Order(x, 2 - x))
def test_simple_2():
assert Order(2*x)*x == Order(x**2)
assert Order(2*x)/x == Order(1, x)
assert Order(2*x)*x*exp(1/x) == Order(x**2*exp(1/x))
assert (Order(2*x)*x*exp(1/x)/log(x)**3).expr == x**2*exp(1/x)*log(x)**-3
def test_simple_3():
assert Order(x) + x == Order(x)
assert Order(x) + 2 == 2 + Order(x)
assert Order(x) + x**2 == Order(x)
assert Order(x) + 1/x == 1/x + Order(x)
assert Order(1/x) + 1/x**2 == 1/x**2 + Order(1/x)
assert Order(x) + exp(1/x) == Order(x) + exp(1/x)
def test_simple_4():
assert Order(x)**2 == Order(x**2)
def test_simple_5():
assert Order(x) + Order(x**2) == Order(x)
assert Order(x) + Order(x**-2) == Order(x**-2)
assert Order(x) + Order(1/x) == Order(1/x)
def test_simple_6():
assert Order(x) - Order(x) == Order(x)
assert Order(x) + Order(1) == Order(1)
assert Order(x) + Order(x**2) == Order(x)
assert Order(1/x) + Order(1) == Order(1/x)
assert Order(x) + Order(exp(1/x)) == Order(exp(1/x))
assert Order(x**3) + Order(exp(2/x)) == Order(exp(2/x))
assert Order(x**-3) + Order(exp(2/x)) == Order(exp(2/x))
def test_simple_7():
assert 1 + O(1) == O(1)
assert 2 + O(1) == O(1)
assert x + O(1) == O(1)
assert 1/x + O(1) == 1/x + O(1)
def test_simple_8():
assert O(sqrt(-x)) == O(sqrt(x))
assert O(x**2*sqrt(x)) == O(x**Rational(5, 2))
assert O(x**3*sqrt(-(-x)**3)) == O(x**Rational(9, 2))
assert O(x**Rational(3, 2)*sqrt((-x)**3)) == O(x**3)
assert O(x*(-2*x)**(I/2)) == O(x*(-x)**(I/2))
def test_as_expr_variables():
assert Order(x).as_expr_variables(None) == (x, ((x, 0),))
assert Order(x).as_expr_variables(((x, 0),)) == (x, ((x, 0),))
assert Order(y).as_expr_variables(((x, 0),)) == (y, ((x, 0), (y, 0)))
assert Order(y).as_expr_variables(((x, 0), (y, 0))) == (y, ((x, 0), (y, 0)))
def test_contains_0():
assert Order(1, x).contains(Order(1, x))
assert Order(1, x).contains(Order(1))
assert Order(1).contains(Order(1, x)) is False
def test_contains_1():
assert Order(x).contains(Order(x))
assert Order(x).contains(Order(x**2))
assert not Order(x**2).contains(Order(x))
assert not Order(x).contains(Order(1/x))
assert not Order(1/x).contains(Order(exp(1/x)))
assert not Order(x).contains(Order(exp(1/x)))
assert Order(1/x).contains(Order(x))
assert Order(exp(1/x)).contains(Order(x))
assert Order(exp(1/x)).contains(Order(1/x))
assert Order(exp(1/x)).contains(Order(exp(1/x)))
assert Order(exp(2/x)).contains(Order(exp(1/x)))
assert not Order(exp(1/x)).contains(Order(exp(2/x)))
def test_contains_2():
assert Order(x).contains(Order(y)) is None
assert Order(x).contains(Order(y*x))
assert Order(y*x).contains(Order(x))
assert Order(y).contains(Order(x*y))
assert Order(x).contains(Order(y**2*x))
def test_contains_3():
assert Order(x*y**2).contains(Order(x**2*y)) is None
assert Order(x**2*y).contains(Order(x*y**2)) is None
def test_contains_4():
assert Order(sin(1/x**2)).contains(Order(cos(1/x**2))) is True
assert Order(cos(1/x**2)).contains(Order(sin(1/x**2))) is True
def test_contains():
assert Order(1, x) not in Order(1)
assert Order(1) in Order(1, x)
raises(TypeError, lambda: Order(x*y**2) in Order(x**2*y))
def test_add_1():
assert Order(x + x) == Order(x)
assert Order(3*x - 2*x**2) == Order(x)
assert Order(1 + x) == Order(1, x)
assert Order(1 + 1/x) == Order(1/x)
# TODO : A better output for Order(log(x) + 1/log(x))
# could be Order(log(x)). Currently Order for expressions
# where all arguments would involve a log term would fall
# in this category and outputs for these should be improved.
assert Order(log(x) + 1/log(x)) == Order((log(x)**2 + 1)/log(x))
assert Order(exp(1/x) + x) == Order(exp(1/x))
assert Order(exp(1/x) + 1/x**20) == Order(exp(1/x))
def test_ln_args():
assert O(log(x)) + O(log(2*x)) == O(log(x))
assert O(log(x)) + O(log(x**3)) == O(log(x))
assert O(log(x*y)) + O(log(x) + log(y)) == O(log(x) + log(y), x, y)
def test_multivar_0():
assert Order(x*y).expr == x*y
assert Order(x*y**2).expr == x*y**2
assert Order(x*y, x).expr == x
assert Order(x*y**2, y).expr == y**2
assert Order(x*y*z).expr == x*y*z
assert Order(x/y).expr == x/y
assert Order(x*exp(1/y)).expr == x*exp(1/y)
assert Order(exp(x)*exp(1/y)).expr == exp(x)*exp(1/y)
def test_multivar_0a():
assert Order(exp(1/x)*exp(1/y)).expr == exp(1/x)*exp(1/y)
def test_multivar_1():
assert Order(x + y).expr == x + y
assert Order(x + 2*y).expr == x + y
assert (Order(x + y) + x).expr == (x + y)
assert (Order(x + y) + x**2) == Order(x + y)
assert (Order(x + y) + 1/x) == 1/x + Order(x + y)
assert Order(x**2 + y*x).expr == x**2 + y*x
def test_multivar_2():
assert Order(x**2*y + y**2*x, x, y).expr == x**2*y + y**2*x
def test_multivar_mul_1():
assert Order(x + y)*x == Order(x**2 + y*x, x, y)
def test_multivar_3():
assert (Order(x) + Order(y)).args in [
(Order(x), Order(y)),
(Order(y), Order(x))]
assert Order(x) + Order(y) + Order(x + y) == Order(x + y)
assert (Order(x**2*y) + Order(y**2*x)).args in [
(Order(x*y**2), Order(y*x**2)),
(Order(y*x**2), Order(x*y**2))]
assert (Order(x**2*y) + Order(y*x)) == Order(x*y)
def test_issue_3468():
y = Symbol('y', negative=True)
z = Symbol('z', complex=True)
# check that Order does not modify assumptions about symbols
Order(x)
Order(y)
Order(z)
assert x.is_positive is None
assert y.is_positive is False
assert z.is_positive is None
def test_leading_order():
assert (x + 1 + 1/x**5).extract_leading_order(x) == ((1/x**5, O(1/x**5)),)
assert (1 + 1/x).extract_leading_order(x) == ((1/x, O(1/x)),)
assert (1 + x).extract_leading_order(x) == ((1, O(1, x)),)
assert (1 + x**2).extract_leading_order(x) == ((1, O(1, x)),)
assert (2 + x**2).extract_leading_order(x) == ((2, O(1, x)),)
assert (x + x**2).extract_leading_order(x) == ((x, O(x)),)
def test_leading_order2():
assert set((2 + pi + x**2).extract_leading_order(x)) == {(pi, O(1, x)),
(S(2), O(1, x))}
assert set((2*x + pi*x + x**2).extract_leading_order(x)) == {(2*x, O(x)),
(x*pi, O(x))}
def test_order_leadterm():
assert O(x**2)._eval_as_leading_term(x) == O(x**2)
def test_order_symbols():
e = x*y*sin(x)*Integral(x, (x, 1, 2))
assert O(e) == O(x**2*y, x, y)
assert O(e, x) == O(x**2)
def test_nan():
assert O(nan) is nan
assert not O(x).contains(nan)
def test_O1():
assert O(1, x) * x == O(x)
assert O(1, y) * x == O(1, y)
def test_getn():
# other lines are tested incidentally by the suite
assert O(x).getn() == 1
assert O(x/log(x)).getn() == 1
assert O(x**2/log(x)**2).getn() == 2
assert O(x*log(x)).getn() == 1
raises(NotImplementedError, lambda: (O(x) + O(y)).getn())
def test_diff():
assert O(x**2).diff(x) == O(x)
def test_getO():
assert (x).getO() is None
assert (x).removeO() == x
assert (O(x)).getO() == O(x)
assert (O(x)).removeO() == 0
assert (z + O(x) + O(y)).getO() == O(x) + O(y)
assert (z + O(x) + O(y)).removeO() == z
raises(NotImplementedError, lambda: (O(x) + O(y)).getn())
def test_leading_term():
from sympy.functions.special.gamma_functions import digamma
assert O(1/digamma(1/x)) == O(1/log(x))
def test_eval():
assert Order(x).subs(Order(x), 1) == 1
assert Order(x).subs(x, y) == Order(y)
assert Order(x).subs(y, x) == Order(x)
assert Order(x).subs(x, x + y) == Order(x + y, (x, -y))
assert (O(1)**x).is_Pow
def test_issue_4279():
a, b = symbols('a b')
assert O(a, a, b) + O(1, a, b) == O(1, a, b)
assert O(b, a, b) + O(1, a, b) == O(1, a, b)
assert O(a + b, a, b) + O(1, a, b) == O(1, a, b)
assert O(1, a, b) + O(a, a, b) == O(1, a, b)
assert O(1, a, b) + O(b, a, b) == O(1, a, b)
assert O(1, a, b) + O(a + b, a, b) == O(1, a, b)
def test_issue_4855():
assert 1/O(1) != O(1)
assert 1/O(x) != O(1/x)
assert 1/O(x, (x, oo)) != O(1/x, (x, oo))
f = Function('f')
assert 1/O(f(x)) != O(1/x)
def test_order_conjugate_transpose():
x = Symbol('x', real=True)
y = Symbol('y', imaginary=True)
assert conjugate(Order(x)) == Order(conjugate(x))
assert conjugate(Order(y)) == Order(conjugate(y))
assert conjugate(Order(x**2)) == Order(conjugate(x)**2)
assert conjugate(Order(y**2)) == Order(conjugate(y)**2)
assert transpose(Order(x)) == Order(transpose(x))
assert transpose(Order(y)) == Order(transpose(y))
assert transpose(Order(x**2)) == Order(transpose(x)**2)
assert transpose(Order(y**2)) == Order(transpose(y)**2)
def test_order_noncommutative():
A = Symbol('A', commutative=False)
assert Order(A + A*x, x) == Order(1, x)
assert (A + A*x)*Order(x) == Order(x)
assert (A*x)*Order(x) == Order(x**2, x)
assert expand((1 + Order(x))*A*A*x) == A*A*x + Order(x**2, x)
assert expand((A*A + Order(x))*x) == A*A*x + Order(x**2, x)
assert expand((A + Order(x))*A*x) == A*A*x + Order(x**2, x)
def test_issue_6753():
assert (1 + x**2)**10000*O(x) == O(x)
def test_order_at_infinity():
assert Order(1 + x, (x, oo)) == Order(x, (x, oo))
assert Order(3*x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo))*3 == Order(x, (x, oo))
assert -28*Order(x, (x, oo)) == Order(x, (x, oo))
assert Order(Order(x, (x, oo)), (x, oo)) == Order(x, (x, oo))
assert Order(Order(x, (x, oo)), (y, oo)) == Order(x, (x, oo), (y, oo))
assert Order(3, (x, oo)) == Order(1, (x, oo))
assert Order(x**2 + x + y, (x, oo)) == O(x**2, (x, oo))
assert Order(x**2 + x + y, (y, oo)) == O(y, (y, oo))
assert Order(2*x, (x, oo))*x == Order(x**2, (x, oo))
assert Order(2*x, (x, oo))/x == Order(1, (x, oo))
assert Order(2*x, (x, oo))*x*exp(1/x) == Order(x**2*exp(1/x), (x, oo))
assert Order(2*x, (x, oo))*x*exp(1/x)/log(x)**3 == Order(x**2*exp(1/x)*log(x)**-3, (x, oo))
assert Order(x, (x, oo)) + 1/x == 1/x + Order(x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + 1 == 1 + Order(x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + x == x + Order(x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + x**2 == x**2 + Order(x, (x, oo))
assert Order(1/x, (x, oo)) + 1/x**2 == 1/x**2 + Order(1/x, (x, oo)) == Order(1/x, (x, oo))
assert Order(x, (x, oo)) + exp(1/x) == exp(1/x) + Order(x, (x, oo))
assert Order(x, (x, oo))**2 == Order(x**2, (x, oo))
assert Order(x, (x, oo)) + Order(x**2, (x, oo)) == Order(x**2, (x, oo))
assert Order(x, (x, oo)) + Order(x**-2, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + Order(1/x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) - Order(x, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + Order(1, (x, oo)) == Order(x, (x, oo))
assert Order(x, (x, oo)) + Order(x**2, (x, oo)) == Order(x**2, (x, oo))
assert Order(1/x, (x, oo)) + Order(1, (x, oo)) == Order(1, (x, oo))
assert Order(x, (x, oo)) + Order(exp(1/x), (x, oo)) == Order(x, (x, oo))
assert Order(x**3, (x, oo)) + Order(exp(2/x), (x, oo)) == Order(x**3, (x, oo))
assert Order(x**-3, (x, oo)) + Order(exp(2/x), (x, oo)) == Order(exp(2/x), (x, oo))
# issue 7207
assert Order(exp(x), (x, oo)).expr == Order(2*exp(x), (x, oo)).expr == exp(x)
assert Order(y**x, (x, oo)).expr == Order(2*y**x, (x, oo)).expr == exp(x*log(y))
# issue 19545
assert Order(1/x - 3/(3*x + 2), (x, oo)).expr == x**(-2)
def test_mixing_order_at_zero_and_infinity():
assert (Order(x, (x, 0)) + Order(x, (x, oo))).is_Add
assert Order(x, (x, 0)) + Order(x, (x, oo)) == Order(x, (x, oo)) + Order(x, (x, 0))
assert Order(Order(x, (x, oo))) == Order(x, (x, oo))
# not supported (yet)
raises(NotImplementedError, lambda: Order(x, (x, 0))*Order(x, (x, oo)))
raises(NotImplementedError, lambda: Order(x, (x, oo))*Order(x, (x, 0)))
raises(NotImplementedError, lambda: Order(Order(x, (x, oo)), y))
raises(NotImplementedError, lambda: Order(Order(x), (x, oo)))
def test_order_at_some_point():
assert Order(x, (x, 1)) == Order(1, (x, 1))
assert Order(2*x - 2, (x, 1)) == Order(x - 1, (x, 1))
assert Order(-x + 1, (x, 1)) == Order(x - 1, (x, 1))
assert Order(x - 1, (x, 1))**2 == Order((x - 1)**2, (x, 1))
assert Order(x - 2, (x, 2)) - O(x - 2, (x, 2)) == Order(x - 2, (x, 2))
def test_order_subs_limits():
# issue 3333
assert (1 + Order(x)).subs(x, 1/x) == 1 + Order(1/x, (x, oo))
assert (1 + Order(x)).limit(x, 0) == 1
# issue 5769
assert ((x + Order(x**2))/x).limit(x, 0) == 1
assert Order(x**2).subs(x, y - 1) == Order((y - 1)**2, (y, 1))
assert Order(10*x**2, (x, 2)).subs(x, y - 1) == Order(1, (y, 3))
def test_issue_9351():
assert exp(x).series(x, 10, 1) == exp(10) + Order(x - 10, (x, 10))
def test_issue_9192():
assert O(1)*O(1) == O(1)
assert O(1)**O(1) == O(1)
def test_issue_9910():
assert O(x*log(x) + sin(x), (x, oo)) == O(x*log(x), (x, oo))
def test_performance_of_adding_order():
l = [x**i for i in range(1000)]
l.append(O(x**1001))
assert Add(*l).subs(x,1) == O(1)
def test_issue_14622():
assert (x**(-4) + x**(-3) + x**(-1) + O(x**(-6), (x, oo))).as_numer_denom() == (
x**4 + x**5 + x**7 + O(x**2, (x, oo)), x**8)
assert (x**3 + O(x**2, (x, oo))).is_Add
assert O(x**2, (x, oo)).contains(x**3) is False
assert O(x, (x, oo)).contains(O(x, (x, 0))) is None
assert O(x, (x, 0)).contains(O(x, (x, oo))) is None
raises(NotImplementedError, lambda: O(x**3).contains(x**w))
def test_issue_15539():
assert O(1/x**2 + 1/x**4, (x, -oo)) == O(1/x**2, (x, -oo))
assert O(1/x**4 + exp(x), (x, -oo)) == O(1/x**4, (x, -oo))
assert O(1/x**4 + exp(-x), (x, -oo)) == O(exp(-x), (x, -oo))
assert O(1/x, (x, oo)).subs(x, -x) == O(-1/x, (x, -oo))
def test_issue_18606():
assert unchanged(Order, 0)
def test_issue_22165():
assert O(log(x)).contains(2)
def test_issue_23231():
# This test checks Order for expressions having
# arguments containing variables in exponents/powers.
assert O(x**x + 2**x, (x, oo)) == O(exp(x*log(x)), (x, oo))
assert O(x**x + x**2, (x, oo)) == O(exp(x*log(x)), (x, oo))
assert O(x**x + 1/x**2, (x, oo)) == O(exp(x*log(x)), (x, oo))
assert O(2**x + 3**x , (x, oo)) == O(exp(x*log(3)), (x, oo))
def test_issue_9917():
assert O(x*sin(x) + 1, (x, oo)) == O(x, (x, oo))

View File

@ -0,0 +1,101 @@
from sympy.core.function import Function
from sympy.core.numbers import (I, Rational, pi)
from sympy.core.singleton import S
from sympy.core.symbol import Symbol
from sympy.functions.combinatorial.factorials import factorial
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.hyperbolic import tanh
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (cot, sin, tan)
from sympy.series.residues import residue
from sympy.testing.pytest import XFAIL, raises
from sympy.abc import x, z, a, s, k
def test_basic1():
assert residue(1/x, x, 0) == 1
assert residue(-2/x, x, 0) == -2
assert residue(81/x, x, 0) == 81
assert residue(1/x**2, x, 0) == 0
assert residue(0, x, 0) == 0
assert residue(5, x, 0) == 0
assert residue(x, x, 0) == 0
assert residue(x**2, x, 0) == 0
def test_basic2():
assert residue(1/x, x, 1) == 0
assert residue(-2/x, x, 1) == 0
assert residue(81/x, x, -1) == 0
assert residue(1/x**2, x, 1) == 0
assert residue(0, x, 1) == 0
assert residue(5, x, 1) == 0
assert residue(x, x, 1) == 0
assert residue(x**2, x, 5) == 0
def test_f():
f = Function("f")
assert residue(f(x)/x**5, x, 0) == f(x).diff(x, 4).subs(x, 0)/24
def test_functions():
assert residue(1/sin(x), x, 0) == 1
assert residue(2/sin(x), x, 0) == 2
assert residue(1/sin(x)**2, x, 0) == 0
assert residue(1/sin(x)**5, x, 0) == Rational(3, 8)
def test_expressions():
assert residue(1/(x + 1), x, 0) == 0
assert residue(1/(x + 1), x, -1) == 1
assert residue(1/(x**2 + 1), x, -1) == 0
assert residue(1/(x**2 + 1), x, I) == -I/2
assert residue(1/(x**2 + 1), x, -I) == I/2
assert residue(1/(x**4 + 1), x, 0) == 0
assert residue(1/(x**4 + 1), x, exp(I*pi/4)).equals(-(Rational(1, 4) + I/4)/sqrt(2))
assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/4/a**3
@XFAIL
def test_expressions_failing():
n = Symbol('n', integer=True, positive=True)
assert residue(exp(z)/(z - pi*I/4*a)**n, z, I*pi*a) == \
exp(I*pi*a/4)/factorial(n - 1)
def test_NotImplemented():
raises(NotImplementedError, lambda: residue(exp(1/z), z, 0))
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_issue_5654():
assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/(4*a**3)
assert residue(1/s*1/(z - exp(s)), s, 0) == 1/(z - 1)
assert residue((1 + k)/s*1/(z - exp(s)), s, 0) == k/(z - 1) + 1/(z - 1)
def test_issue_6499():
assert residue(1/(exp(z) - 1), z, 0) == 1
def test_issue_14037():
assert residue(sin(x**50)/x**51, x, 0) == 1
def test_issue_21176():
f = x**2*cot(pi*x)/(x**4 + 1)
assert residue(f, x, -sqrt(2)/2 - sqrt(2)*I/2).cancel().together(deep=True)\
== sqrt(2)*(1 - I)/(8*tan(sqrt(2)*pi*(1 + I)/2))
def test_issue_21177():
r = -sqrt(3)*tanh(sqrt(3)*pi/2)/3
a = residue(cot(pi*x)/((x - 1)*(x - 2) + 1), x, S(3)/2 - sqrt(3)*I/2)
b = residue(cot(pi*x)/(x**2 - 3*x + 3), x, S(3)/2 - sqrt(3)*I/2)
assert a == r
assert (b - a).cancel() == 0

View File

@ -0,0 +1,312 @@
from sympy.core.containers import Tuple
from sympy.core.function import Function
from sympy.core.numbers import oo, Rational
from sympy.core.singleton import S
from sympy.core.symbol import symbols, Symbol
from sympy.functions.combinatorial.numbers import tribonacci, fibonacci
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import cos, sin
from sympy.series import EmptySequence
from sympy.series.sequences import (SeqMul, SeqAdd, SeqPer, SeqFormula,
sequence)
from sympy.sets.sets import Interval
from sympy.tensor.indexed import Indexed, Idx
from sympy.series.sequences import SeqExpr, SeqExprOp, RecursiveSeq
from sympy.testing.pytest import raises, slow
x, y, z = symbols('x y z')
n, m = symbols('n m')
def test_EmptySequence():
assert S.EmptySequence is EmptySequence
assert S.EmptySequence.interval is S.EmptySet
assert S.EmptySequence.length is S.Zero
assert list(S.EmptySequence) == []
def test_SeqExpr():
#SeqExpr is a baseclass and does not take care of
#ensuring all arguments are Basics hence the use of
#Tuple(...) here.
s = SeqExpr(Tuple(1, n, y), Tuple(x, 0, 10))
assert isinstance(s, SeqExpr)
assert s.gen == (1, n, y)
assert s.interval == Interval(0, 10)
assert s.start == 0
assert s.stop == 10
assert s.length == 11
assert s.variables == (x,)
assert SeqExpr(Tuple(1, 2, 3), Tuple(x, 0, oo)).length is oo
def test_SeqPer():
s = SeqPer((1, n, 3), (x, 0, 5))
assert isinstance(s, SeqPer)
assert s.periodical == Tuple(1, n, 3)
assert s.period == 3
assert s.coeff(3) == 1
assert s.free_symbols == {n}
assert list(s) == [1, n, 3, 1, n, 3]
assert s[:] == [1, n, 3, 1, n, 3]
assert SeqPer((1, n, 3), (x, -oo, 0))[0:6] == [1, n, 3, 1, n, 3]
raises(ValueError, lambda: SeqPer((1, 2, 3), (0, 1, 2)))
raises(ValueError, lambda: SeqPer((1, 2, 3), (x, -oo, oo)))
raises(ValueError, lambda: SeqPer(n**2, (0, oo)))
assert SeqPer((n, n**2, n**3), (m, 0, oo))[:6] == \
[n, n**2, n**3, n, n**2, n**3]
assert SeqPer((n, n**2, n**3), (n, 0, oo))[:6] == [0, 1, 8, 3, 16, 125]
assert SeqPer((n, m), (n, 0, oo))[:6] == [0, m, 2, m, 4, m]
def test_SeqFormula():
s = SeqFormula(n**2, (n, 0, 5))
assert isinstance(s, SeqFormula)
assert s.formula == n**2
assert s.coeff(3) == 9
assert list(s) == [i**2 for i in range(6)]
assert s[:] == [i**2 for i in range(6)]
assert SeqFormula(n**2, (n, -oo, 0))[0:6] == [i**2 for i in range(6)]
assert SeqFormula(n**2, (0, oo)) == SeqFormula(n**2, (n, 0, oo))
assert SeqFormula(n**2, (0, m)).subs(m, x) == SeqFormula(n**2, (0, x))
assert SeqFormula(m*n**2, (n, 0, oo)).subs(m, x) == \
SeqFormula(x*n**2, (n, 0, oo))
raises(ValueError, lambda: SeqFormula(n**2, (0, 1, 2)))
raises(ValueError, lambda: SeqFormula(n**2, (n, -oo, oo)))
raises(ValueError, lambda: SeqFormula(m*n**2, (0, oo)))
seq = SeqFormula(x*(y**2 + z), (z, 1, 100))
assert seq.expand() == SeqFormula(x*y**2 + x*z, (z, 1, 100))
seq = SeqFormula(sin(x*(y**2 + z)),(z, 1, 100))
assert seq.expand(trig=True) == SeqFormula(sin(x*y**2)*cos(x*z) + sin(x*z)*cos(x*y**2), (z, 1, 100))
assert seq.expand() == SeqFormula(sin(x*y**2 + x*z), (z, 1, 100))
assert seq.expand(trig=False) == SeqFormula(sin(x*y**2 + x*z), (z, 1, 100))
seq = SeqFormula(exp(x*(y**2 + z)), (z, 1, 100))
assert seq.expand() == SeqFormula(exp(x*y**2)*exp(x*z), (z, 1, 100))
assert seq.expand(power_exp=False) == SeqFormula(exp(x*y**2 + x*z), (z, 1, 100))
assert seq.expand(mul=False, power_exp=False) == SeqFormula(exp(x*(y**2 + z)), (z, 1, 100))
def test_sequence():
form = SeqFormula(n**2, (n, 0, 5))
per = SeqPer((1, 2, 3), (n, 0, 5))
inter = SeqFormula(n**2)
assert sequence(n**2, (n, 0, 5)) == form
assert sequence((1, 2, 3), (n, 0, 5)) == per
assert sequence(n**2) == inter
def test_SeqExprOp():
form = SeqFormula(n**2, (n, 0, 10))
per = SeqPer((1, 2, 3), (m, 5, 10))
s = SeqExprOp(form, per)
assert s.gen == (n**2, (1, 2, 3))
assert s.interval == Interval(5, 10)
assert s.start == 5
assert s.stop == 10
assert s.length == 6
assert s.variables == (n, m)
def test_SeqAdd():
per = SeqPer((1, 2, 3), (n, 0, oo))
form = SeqFormula(n**2)
per_bou = SeqPer((1, 2), (n, 1, 5))
form_bou = SeqFormula(n**2, (6, 10))
form_bou2 = SeqFormula(n**2, (1, 5))
assert SeqAdd() == S.EmptySequence
assert SeqAdd(S.EmptySequence) == S.EmptySequence
assert SeqAdd(per) == per
assert SeqAdd(per, S.EmptySequence) == per
assert SeqAdd(per_bou, form_bou) == S.EmptySequence
s = SeqAdd(per_bou, form_bou2, evaluate=False)
assert s.args == (form_bou2, per_bou)
assert s[:] == [2, 6, 10, 18, 26]
assert list(s) == [2, 6, 10, 18, 26]
assert isinstance(SeqAdd(per, per_bou, evaluate=False), SeqAdd)
s1 = SeqAdd(per, per_bou)
assert isinstance(s1, SeqPer)
assert s1 == SeqPer((2, 4, 4, 3, 3, 5), (n, 1, 5))
s2 = SeqAdd(form, form_bou)
assert isinstance(s2, SeqFormula)
assert s2 == SeqFormula(2*n**2, (6, 10))
assert SeqAdd(form, form_bou, per) == \
SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
assert SeqAdd(form, SeqAdd(form_bou, per)) == \
SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
assert SeqAdd(per, SeqAdd(form, form_bou), evaluate=False) == \
SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
assert SeqAdd(SeqPer((1, 2), (n, 0, oo)), SeqPer((1, 2), (m, 0, oo))) == \
SeqPer((2, 4), (n, 0, oo))
def test_SeqMul():
per = SeqPer((1, 2, 3), (n, 0, oo))
form = SeqFormula(n**2)
per_bou = SeqPer((1, 2), (n, 1, 5))
form_bou = SeqFormula(n**2, (n, 6, 10))
form_bou2 = SeqFormula(n**2, (1, 5))
assert SeqMul() == S.EmptySequence
assert SeqMul(S.EmptySequence) == S.EmptySequence
assert SeqMul(per) == per
assert SeqMul(per, S.EmptySequence) == S.EmptySequence
assert SeqMul(per_bou, form_bou) == S.EmptySequence
s = SeqMul(per_bou, form_bou2, evaluate=False)
assert s.args == (form_bou2, per_bou)
assert s[:] == [1, 8, 9, 32, 25]
assert list(s) == [1, 8, 9, 32, 25]
assert isinstance(SeqMul(per, per_bou, evaluate=False), SeqMul)
s1 = SeqMul(per, per_bou)
assert isinstance(s1, SeqPer)
assert s1 == SeqPer((1, 4, 3, 2, 2, 6), (n, 1, 5))
s2 = SeqMul(form, form_bou)
assert isinstance(s2, SeqFormula)
assert s2 == SeqFormula(n**4, (6, 10))
assert SeqMul(form, form_bou, per) == \
SeqMul(per, SeqFormula(n**4, (6, 10)))
assert SeqMul(form, SeqMul(form_bou, per)) == \
SeqMul(per, SeqFormula(n**4, (6, 10)))
assert SeqMul(per, SeqMul(form, form_bou2,
evaluate=False), evaluate=False) == \
SeqMul(form, per, form_bou2, evaluate=False)
assert SeqMul(SeqPer((1, 2), (n, 0, oo)), SeqPer((1, 2), (n, 0, oo))) == \
SeqPer((1, 4), (n, 0, oo))
def test_add():
per = SeqPer((1, 2), (n, 0, oo))
form = SeqFormula(n**2)
assert per + (SeqPer((2, 3))) == SeqPer((3, 5), (n, 0, oo))
assert form + SeqFormula(n**3) == SeqFormula(n**2 + n**3)
assert per + form == SeqAdd(per, form)
raises(TypeError, lambda: per + n)
raises(TypeError, lambda: n + per)
def test_sub():
per = SeqPer((1, 2), (n, 0, oo))
form = SeqFormula(n**2)
assert per - (SeqPer((2, 3))) == SeqPer((-1, -1), (n, 0, oo))
assert form - (SeqFormula(n**3)) == SeqFormula(n**2 - n**3)
assert per - form == SeqAdd(per, -form)
raises(TypeError, lambda: per - n)
raises(TypeError, lambda: n - per)
def test_mul__coeff_mul():
assert SeqPer((1, 2), (n, 0, oo)).coeff_mul(2) == SeqPer((2, 4), (n, 0, oo))
assert SeqFormula(n**2).coeff_mul(2) == SeqFormula(2*n**2)
assert S.EmptySequence.coeff_mul(100) == S.EmptySequence
assert SeqPer((1, 2), (n, 0, oo)) * (SeqPer((2, 3))) == \
SeqPer((2, 6), (n, 0, oo))
assert SeqFormula(n**2) * SeqFormula(n**3) == SeqFormula(n**5)
assert S.EmptySequence * SeqFormula(n**2) == S.EmptySequence
assert SeqFormula(n**2) * S.EmptySequence == S.EmptySequence
raises(TypeError, lambda: sequence(n**2) * n)
raises(TypeError, lambda: n * sequence(n**2))
def test_neg():
assert -SeqPer((1, -2), (n, 0, oo)) == SeqPer((-1, 2), (n, 0, oo))
assert -SeqFormula(n**2) == SeqFormula(-n**2)
def test_operations():
per = SeqPer((1, 2), (n, 0, oo))
per2 = SeqPer((2, 4), (n, 0, oo))
form = SeqFormula(n**2)
form2 = SeqFormula(n**3)
assert per + form + form2 == SeqAdd(per, form, form2)
assert per + form - form2 == SeqAdd(per, form, -form2)
assert per + form - S.EmptySequence == SeqAdd(per, form)
assert per + per2 + form == SeqAdd(SeqPer((3, 6), (n, 0, oo)), form)
assert S.EmptySequence - per == -per
assert form + form == SeqFormula(2*n**2)
assert per * form * form2 == SeqMul(per, form, form2)
assert form * form == SeqFormula(n**4)
assert form * -form == SeqFormula(-n**4)
assert form * (per + form2) == SeqMul(form, SeqAdd(per, form2))
assert form * (per + per) == SeqMul(form, per2)
assert form.coeff_mul(m) == SeqFormula(m*n**2, (n, 0, oo))
assert per.coeff_mul(m) == SeqPer((m, 2*m), (n, 0, oo))
def test_Idx_limits():
i = symbols('i', cls=Idx)
r = Indexed('r', i)
assert SeqFormula(r, (i, 0, 5))[:] == [r.subs(i, j) for j in range(6)]
assert SeqPer((1, 2), (i, 0, 5))[:] == [1, 2, 1, 2, 1, 2]
@slow
def test_find_linear_recurrence():
assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \
(n, 0, 10)).find_linear_recurrence(11) == [1, 1]
assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \
1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11]
assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \
== [4, -6, 4, -1]
assert sequence(x**n, (n,0,20)).find_linear_recurrence(21) == [x]
assert sequence((1,2,3)).find_linear_recurrence(10, 5) == [0, 0, 1]
assert sequence(((1 + sqrt(5))/2)**n + \
(-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1]
assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \
(n,0,oo)).find_linear_recurrence(10) == [1, 1]
assert sequence((1,2,3,4,6),(n, 0, 4)).find_linear_recurrence(5) == []
assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
== ([], None)
assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
== ([Rational(19, 2), -20, Rational(27, 2)], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2))
assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \
== ([1, 1], -x/(x**2 + x - 1))
assert sequence(tribonacci(n)).find_linear_recurrence(30,gfvar=x) \
== ([1, 1, 1], -x/(x**3 + x**2 + x - 1))
def test_RecursiveSeq():
y = Function('y')
n = Symbol('n')
fib = RecursiveSeq(y(n - 1) + y(n - 2), y(n), n, [0, 1])
assert fib.coeff(3) == 2

View File

@ -0,0 +1,404 @@
from sympy.core.evalf import N
from sympy.core.function import (Derivative, Function, PoleError, Subs)
from sympy.core.numbers import (E, Float, Rational, oo, pi, I)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.exponential import (LambertW, exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (atan, cos, sin)
from sympy.functions.special.gamma_functions import gamma
from sympy.integrals.integrals import Integral, integrate
from sympy.series.order import O
from sympy.series.series import series
from sympy.abc import x, y, n, k
from sympy.testing.pytest import raises
from sympy.core import EulerGamma
def test_sin():
e1 = sin(x).series(x, 0)
e2 = series(sin(x), x, 0)
assert e1 == e2
def test_cos():
e1 = cos(x).series(x, 0)
e2 = series(cos(x), x, 0)
assert e1 == e2
def test_exp():
e1 = exp(x).series(x, 0)
e2 = series(exp(x), x, 0)
assert e1 == e2
def test_exp2():
e1 = exp(cos(x)).series(x, 0)
e2 = series(exp(cos(x)), x, 0)
assert e1 == e2
def test_issue_5223():
assert series(1, x) == 1
assert next(S.Zero.lseries(x)) == 0
assert cos(x).series() == cos(x).series(x)
raises(ValueError, lambda: cos(x + y).series())
raises(ValueError, lambda: x.series(dir=""))
assert (cos(x).series(x, 1) -
cos(x + 1).series(x).subs(x, x - 1)).removeO() == 0
e = cos(x).series(x, 1, n=None)
assert [next(e) for i in range(2)] == [cos(1), -((x - 1)*sin(1))]
e = cos(x).series(x, 1, n=None, dir='-')
assert [next(e) for i in range(2)] == [cos(1), (1 - x)*sin(1)]
# the following test is exact so no need for x -> x - 1 replacement
assert abs(x).series(x, 1, dir='-') == x
assert exp(x).series(x, 1, dir='-', n=3).removeO() == \
E - E*(-x + 1) + E*(-x + 1)**2/2
D = Derivative
assert D(x**2 + x**3*y**2, x, 2, y, 1).series(x).doit() == 12*x*y
assert next(D(cos(x), x).lseries()) == D(1, x)
assert D(
exp(x), x).series(n=3) == D(1, x) + D(x, x) + D(x**2/2, x) + D(x**3/6, x) + O(x**3)
assert Integral(x, (x, 1, 3), (y, 1, x)).series(x) == -4 + 4*x
assert (1 + x + O(x**2)).getn() == 2
assert (1 + x).getn() is None
raises(PoleError, lambda: ((1/sin(x))**oo).series())
logx = Symbol('logx')
assert ((sin(x))**y).nseries(x, n=1, logx=logx) == \
exp(y*logx) + O(x*exp(y*logx), x)
assert sin(1/x).series(x, oo, n=5) == 1/x - 1/(6*x**3) + O(x**(-5), (x, oo))
assert abs(x).series(x, oo, n=5, dir='+') == x
assert abs(x).series(x, -oo, n=5, dir='-') == -x
assert abs(-x).series(x, oo, n=5, dir='+') == x
assert abs(-x).series(x, -oo, n=5, dir='-') == -x
assert exp(x*log(x)).series(n=3) == \
1 + x*log(x) + x**2*log(x)**2/2 + O(x**3*log(x)**3)
# XXX is this right? If not, fix "ngot > n" handling in expr.
p = Symbol('p', positive=True)
assert exp(sqrt(p)**3*log(p)).series(n=3) == \
1 + p**S('3/2')*log(p) + O(p**3*log(p)**3)
assert exp(sin(x)*log(x)).series(n=2) == 1 + x*log(x) + O(x**2*log(x)**2)
def test_issue_6350():
expr = integrate(exp(k*(y**3 - 3*y)), (y, 0, oo), conds='none')
assert expr.series(k, 0, 3) == -(-1)**(S(2)/3)*sqrt(3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(6*pi*k**(S(1)/3)) - \
sqrt(3)*k*gamma(-S(2)/3)*gamma(-S(1)/3)/(6*pi) - \
(-1)**(S(1)/3)*sqrt(3)*k**(S(1)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(6*pi) - \
(-1)**(S(2)/3)*sqrt(3)*k**(S(5)/3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(4*pi) - \
(-1)**(S(1)/3)*sqrt(3)*k**(S(7)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(8*pi) + O(k**3)
def test_issue_11313():
assert Integral(cos(x), x).series(x) == sin(x).series(x)
assert Derivative(sin(x), x).series(x, n=3).doit() == cos(x).series(x, n=3)
assert Derivative(x**3, x).as_leading_term(x) == 3*x**2
assert Derivative(x**3, y).as_leading_term(x) == 0
assert Derivative(sin(x), x).as_leading_term(x) == 1
assert Derivative(cos(x), x).as_leading_term(x) == -x
# This result is equivalent to zero, zero is not return because
# `Expr.series` doesn't currently detect an `x` in its `free_symbol`s.
assert Derivative(1, x).as_leading_term(x) == Derivative(1, x)
assert Derivative(exp(x), x).series(x).doit() == exp(x).series(x)
assert 1 + Integral(exp(x), x).series(x) == exp(x).series(x)
assert Derivative(log(x), x).series(x).doit() == (1/x).series(x)
assert Integral(log(x), x).series(x) == Integral(log(x), x).doit().series(x).removeO()
def test_series_of_Subs():
from sympy.abc import z
subs1 = Subs(sin(x), x, y)
subs2 = Subs(sin(x) * cos(z), x, y)
subs3 = Subs(sin(x * z), (x, z), (y, x))
assert subs1.series(x) == subs1
subs1_series = (Subs(x, x, y) + Subs(-x**3/6, x, y) +
Subs(x**5/120, x, y) + O(y**6))
assert subs1.series() == subs1_series
assert subs1.series(y) == subs1_series
assert subs1.series(z) == subs1
assert subs2.series(z) == (Subs(z**4*sin(x)/24, x, y) +
Subs(-z**2*sin(x)/2, x, y) + Subs(sin(x), x, y) + O(z**6))
assert subs3.series(x).doit() == subs3.doit().series(x)
assert subs3.series(z).doit() == sin(x*y)
raises(ValueError, lambda: Subs(x + 2*y, y, z).series())
assert Subs(x + y, y, z).series(x).doit() == x + z
def test_issue_3978():
f = Function('f')
assert f(x).series(x, 0, 3, dir='-') == \
f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
assert f(x).series(x, 0, 3) == \
f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
assert f(x**2).series(x, 0, 3) == \
f(0) + x**2*Subs(Derivative(f(x), x), x, 0) + O(x**3)
assert f(x**2+1).series(x, 0, 3) == \
f(1) + x**2*Subs(Derivative(f(x), x), x, 1) + O(x**3)
class TestF(Function):
pass
assert TestF(x).series(x, 0, 3) == TestF(0) + \
x*Subs(Derivative(TestF(x), x), x, 0) + \
x**2*Subs(Derivative(TestF(x), x, x), x, 0)/2 + O(x**3)
from sympy.series.acceleration import richardson, shanks
from sympy.concrete.summations import Sum
from sympy.core.numbers import Integer
def test_acceleration():
e = (1 + 1/n)**n
assert round(richardson(e, n, 10, 20).evalf(), 10) == round(E.evalf(), 10)
A = Sum(Integer(-1)**(k + 1) / k, (k, 1, n))
assert round(shanks(A, n, 25).evalf(), 4) == round(log(2).evalf(), 4)
assert round(shanks(A, n, 25, 5).evalf(), 10) == round(log(2).evalf(), 10)
def test_issue_5852():
assert series(1/cos(x/log(x)), x, 0) == 1 + x**2/(2*log(x)**2) + \
5*x**4/(24*log(x)**4) + O(x**6)
def test_issue_4583():
assert cos(1 + x + x**2).series(x, 0, 5) == cos(1) - x*sin(1) + \
x**2*(-sin(1) - cos(1)/2) + x**3*(-cos(1) + sin(1)/6) + \
x**4*(-11*cos(1)/24 + sin(1)/2) + O(x**5)
def test_issue_6318():
eq = (1/x)**Rational(2, 3)
assert (eq + 1).as_leading_term(x) == eq
def test_x_is_base_detection():
eq = (x**2)**Rational(2, 3)
assert eq.series() == x**Rational(4, 3)
def test_issue_7203():
assert series(cos(x), x, pi, 3) == \
-1 + (x - pi)**2/2 + O((x - pi)**3, (x, pi))
def test_exp_product_positive_factors():
a, b = symbols('a, b', positive=True)
x = a * b
assert series(exp(x), x, n=8) == 1 + a*b + a**2*b**2/2 + \
a**3*b**3/6 + a**4*b**4/24 + a**5*b**5/120 + a**6*b**6/720 + \
a**7*b**7/5040 + O(a**8*b**8, a, b)
def test_issue_8805():
assert series(1, n=8) == 1
def test_issue_9549():
y = (x**2 + x + 1) / (x**3 + x**2)
assert series(y, x, oo) == x**(-5) - 1/x**4 + x**(-3) + 1/x + O(x**(-6), (x, oo))
def test_issue_10761():
assert series(1/(x**-2 + x**-3), x, 0) == x**3 - x**4 + x**5 + O(x**6)
def test_issue_12578():
y = (1 - 1/(x/2 - 1/(2*x))**4)**(S(1)/8)
assert y.series(x, 0, n=17) == 1 - 2*x**4 - 8*x**6 - 34*x**8 - 152*x**10 - 714*x**12 - \
3472*x**14 - 17318*x**16 + O(x**17)
def test_issue_12791():
beta = symbols('beta', positive=True)
theta, varphi = symbols('theta varphi', real=True)
expr = (-beta**2*varphi*sin(theta) + beta**2*cos(theta) + \
beta*varphi*sin(theta) - beta*cos(theta) - beta + 1)/(beta*cos(theta) - 1)**2
sol = (0.5/(0.5*cos(theta) - 1.0)**2 - 0.25*cos(theta)/(0.5*cos(theta) - 1.0)**2
+ (beta - 0.5)*(-0.25*varphi*sin(2*theta) - 1.5*cos(theta)
+ 0.25*cos(2*theta) + 1.25)/((0.5*cos(theta) - 1.0)**2*(0.5*cos(theta) - 1.0))
+ 0.25*varphi*sin(theta)/(0.5*cos(theta) - 1.0)**2
+ O((beta - S.Half)**2, (beta, S.Half)))
assert expr.series(beta, 0.5, 2).trigsimp() == sol
def test_issue_14384():
x, a = symbols('x a')
assert series(x**a, x) == x**a
assert series(x**(-2*a), x) == x**(-2*a)
assert series(exp(a*log(x)), x) == exp(a*log(x))
raises(PoleError, lambda: series(x**I, x))
raises(PoleError, lambda: series(x**(I + 1), x))
raises(PoleError, lambda: series(exp(I*log(x)), x))
def test_issue_14885():
assert series(x**Rational(-3, 2)*exp(x), x, 0) == (x**Rational(-3, 2) + 1/sqrt(x) +
sqrt(x)/2 + x**Rational(3, 2)/6 + x**Rational(5, 2)/24 + x**Rational(7, 2)/120 +
x**Rational(9, 2)/720 + x**Rational(11, 2)/5040 + O(x**6))
def test_issue_15539():
assert series(atan(x), x, -oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2
+ O(x**(-6), (x, -oo)))
assert series(atan(x), x, oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2
+ O(x**(-6), (x, oo)))
def test_issue_7259():
assert series(LambertW(x), x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + 125*x**5/24 + O(x**6)
assert series(LambertW(x**2), x, n=8) == x**2 - x**4 + 3*x**6/2 + O(x**8)
assert series(LambertW(sin(x)), x, n=4) == x - x**2 + 4*x**3/3 + O(x**4)
def test_issue_11884():
assert cos(x).series(x, 1, n=1) == cos(1) + O(x - 1, (x, 1))
def test_issue_18008():
y = x*(1 + x*(1 - x))/((1 + x*(1 - x)) - (1 - x)*(1 - x))
assert y.series(x, oo, n=4) == -9/(32*x**3) - 3/(16*x**2) - 1/(8*x) + S(1)/4 + x/2 + \
O(x**(-4), (x, oo))
def test_issue_18842():
f = log(x/(1 - x))
assert f.series(x, 0.491, n=1).removeO().nsimplify() == \
-S(180019443780011)/5000000000000000
def test_issue_19534():
dt = symbols('dt', real=True)
expr = 16*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0)/45 + \
49*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + 0.051640768506639183825*dt + \
dt*(1/2 - sqrt(21)/14) + 1.0)/180 + 49*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + \
2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
1.1854881643947648988*dt + dt*(sqrt(21)/14 + 1/2) + 1.0)/180 + \
dt*(0.66666666666666666667*dt*(2.0*dt + 1.0) + \
6.0173399699313066769*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
4.1117044797036320069*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) - \
7.0189140975801991157*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) + \
0.94010945196161777522*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + \
2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
0.35816132904077632692*dt + 1.0) + 5.5065024887242400038*dt + 1.0)/20 + dt/20 + 1
assert N(expr.series(dt, 0, 8), 20) == (
- Float('0.00092592592592592596126289', precision=70) * dt**7
+ Float('0.0027777777777777783174695', precision=70) * dt**6
+ Float('0.016666666666666656027029', precision=70) * dt**5
+ Float('0.083333333333333300951828', precision=70) * dt**4
+ Float('0.33333333333333337034077', precision=70) * dt**3
+ Float('1.0', precision=70) * dt**2
+ Float('1.0', precision=70) * dt
+ Float('1.0', precision=70)
)
def test_issue_11407():
a, b, c, x = symbols('a b c x')
assert series(sqrt(a + b + c*x), x, 0, 1) == sqrt(a + b) + O(x)
assert series(sqrt(a + b + c + c*x), x, 0, 1) == sqrt(a + b + c) + O(x)
def test_issue_14037():
assert (sin(x**50)/x**51).series(x, n=0) == 1/x + O(1, x)
def test_issue_20551():
expr = (exp(x)/x).series(x, n=None)
terms = [ next(expr) for i in range(3) ]
assert terms == [1/x, 1, x/2]
def test_issue_20697():
p_0, p_1, p_2, p_3, b_0, b_1, b_2 = symbols('p_0 p_1 p_2 p_3 b_0 b_1 b_2')
Q = (p_0 + (p_1 + (p_2 + p_3/y)/y)/y)/(1 + ((p_3/(b_0*y) + (b_0*p_2\
- b_1*p_3)/b_0**2)/y + (b_0**2*p_1 - b_0*b_1*p_2 - p_3*(b_0*b_2\
- b_1**2))/b_0**3)/y)
assert Q.series(y, n=3).ratsimp() == b_2*y**2 + b_1*y + b_0 + O(y**3)
def test_issue_21245():
fi = (1 + sqrt(5))/2
assert (1/(1 - x - x**2)).series(x, 1/fi, 1).factor() == \
(-4812 - 2152*sqrt(5) + 1686*x + 754*sqrt(5)*x\
+ O((x - 2/(1 + sqrt(5)))**2, (x, 2/(1 + sqrt(5)))))/((1 + sqrt(5))\
*(20 + 9*sqrt(5))**2*(x + sqrt(5)*x - 2))
def test_issue_21938():
expr = sin(1/x + exp(-x)) - sin(1/x)
assert expr.series(x, oo) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
def test_issue_23432():
expr = 1/sqrt(1 - x**2)
result = expr.series(x, 0.5)
assert result.is_Add and len(result.args) == 7
def test_issue_23727():
res = series(sqrt(1 - x**2), x, 0.1)
assert res.is_Add == True
def test_issue_24266():
#type1: exp(f(x))
assert (exp(-I*pi*(2*x+1))).series(x, 0, 3) == -1 + 2*I*pi*x + 2*pi**2*x**2 + O(x**3)
assert (exp(-I*pi*(2*x+1))*gamma(1+x)).series(x, 0, 3) == -1 + x*(EulerGamma + 2*I*pi) + \
x**2*(-EulerGamma**2/2 + 23*pi**2/12 - 2*EulerGamma*I*pi) + O(x**3)
#type2: c**f(x)
assert ((2*I)**(-I*pi*(2*x+1))).series(x, 0, 2) == exp(pi**2/2 - I*pi*log(2)) + \
x*(pi**2*exp(pi**2/2 - I*pi*log(2)) - 2*I*pi*exp(pi**2/2 - I*pi*log(2))*log(2)) + O(x**2)
assert ((2)**(-I*pi*(2*x+1))).series(x, 0, 2) == exp(-I*pi*log(2)) - 2*I*pi*x*exp(-I*pi*log(2))*log(2) + O(x**2)
#type3: f(y)**g(x)
assert ((y)**(I*pi*(2*x+1))).series(x, 0, 2) == exp(I*pi*log(y)) + 2*I*pi*x*exp(I*pi*log(y))*log(y) + O(x**2)
assert ((I*y)**(I*pi*(2*x+1))).series(x, 0, 2) == exp(I*pi*log(I*y)) + 2*I*pi*x*exp(I*pi*log(I*y))*log(I*y) + O(x**2)