Skip to contents

The calculus vignette covers Taylor and Laurent series with series(). This vignette covers the other series objects of SymPy: sequences, limits of sequences, Fourier series, formal power series with an explicit formula for the coefficients, holonomic functions, and rational approximation.

library(symcompR)
x <- Symbol("x"); z <- Symbol("z"); k <- Symbol("k")
n <- Symbol("n", integer = TRUE)

Sequences

SeqFormula() defines a sequence by a formula, SeqPer() a periodic sequence. sequence_terms() lists the first terms.

s <- SeqFormula(n^2, list(n, 0, oo))
s
#> SeqFormula(n^2, (n, 0, oo))
sequence_terms(s, 5)
#> [[1]]
#> 0
#> 
#> [[2]]
#> 1
#> 
#> [[3]]
#> 4
#> 
#> [[4]]
#> 9
#> 
#> [[5]]
#> 16
sequence_terms(SeqPer(list(1, 2, 3)), 7)
#> [[1]]
#> 1
#> 
#> [[2]]
#> 2
#> 
#> [[3]]
#> 3
#> 
#> [[4]]
#> 1
#> 
#> [[5]]
#> 2
#> 
#> [[6]]
#> 3
#> 
#> [[7]]
#> 1

Sequences add and multiply term by term:

SeqFormula(n^2, list(n, 0, oo)) + SeqFormula(n^3, list(n, 0, oo))
#> SeqFormula(n^3 + n^2, (n, 0, oo))
sequence_terms(SeqFormula(n^2, list(n, 0, oo)) + SeqFormula(n, list(n, 0, oo)), 5)
#> [[1]]
#> 0
#> 
#> [[2]]
#> 2
#> 
#> [[3]]
#> 6
#> 
#> [[4]]
#> 12
#> 
#> [[5]]
#> 20
alt <- SeqFormula(n, list(n, 0, oo)) * SeqPer(list(1, -1), list(n, 0, oo))
sequence_terms(alt, 6)
#> [[1]]
#> 0
#> 
#> [[2]]
#> -1
#> 
#> [[3]]
#> 2
#> 
#> [[4]]
#> -3
#> 
#> [[5]]
#> 4
#> 
#> [[6]]
#> -5

SeqAdd() and SeqMul() are the classes behind + and *, and EmptySequence is the neutral sequence with no terms.

sq_seq <- SeqFormula(n^2, list(n, 0, oo)); pp <- SeqPer(list(1, 2), list(n, 0, oo))
unlist(lapply(sequence_terms(SeqAdd(sq_seq, pp), 5), sstr))
#> [1] "1"  "3"  "5"  "11" "17"
unlist(lapply(sequence_terms(SeqMul(sq_seq, pp), 5), sstr))
#> [1] "0"  "2"  "4"  "18" "16"
EmptySequence
#> EmptySequence
SeqAdd(sq_seq, EmptySequence)
#> SeqFormula(n^2, (n, 0, oo))

find_linear_recurrence() guesses a linear recurrence from the first terms. For the Fibonacci numbers it finds a_n = a_{n-1} + a_{n-2}, and with gfvar it also returns the generating function.

fib <- sym_sequence(fibonacci(n))
find_linear_recurrence(fib, 10)
#> [[1]]
#> 1
#> 
#> [[2]]
#> 1
find_linear_recurrence(fib, 10, gfvar = x)
#> [[1]]
#> [[1]][[1]]
#> 1
#> 
#> [[1]][[2]]
#> 1
#> 
#> 
#> [[2]]
#> -x/(x^2 + x - 1)

Limits of sequences

limit_seq() computes the limit of a sequence as n \to \infty. It handles oscillating sequences (returning the range of accumulation points) and sequences defined by sums.

limit_seq(n / (n + 1), n)
#> 1
limit_seq((-1)^n, n)
#> AccumBounds(-1, 1)
limit_seq(harmonic(n) / log(n), n)
#> 1
limit_seq((5^n + 3^n) / (3^n + 5^n * n), n)
#> 0
limit_seq(Sum(1 / k, list(k, 1, n)) / n, n)
#> 0

dominant() finds the dominant term of a sum as n \to \infty, and difference_delta() is the forward difference operator.

dominant(5 * n^3 + 4 * n^2 + n + 1, n)
#> 5*n^3
dominant(2^n + n^5, n)
#> 2^n
difference_delta(n^2, n)
#> -n^2 + (n + 1)^2
difference_delta(n * (n + 1), n, 2)
#> 4*n + 6

hypersimp() returns the ratio of consecutive terms when it is a rational function, which shows that a term is hypergeometric.

hypersimp(sym_factorial(k), k)
#> k + 1
hypersimp(sym_binomial(n, k), k)
#> (-k + n)/(k + 1)
hypersimilar(sym_factorial(k), sym_factorial(k + 3), k)
#> [1] TRUE

Fourier series

fourier_series(f, list(x, a, b)) computes the Fourier series of f on the interval [a, b]; truncate(s, n) returns its first n nonzero terms.

s <- fourier_series(x^2, list(x, -sym_pi, sym_pi))
truncate(s, 4)
#> -4*cos(x) + cos(2*x) - 4*cos(3*x)/9 + pi^2/3
truncate(fourier_series(x, list(x, -sym_pi, sym_pi)), 3)
#> 2*sin(x) - sin(2*x) + 2*sin(3*x)/3
truncate(fourier_series(sin(x)^2, list(x, -sym_pi, sym_pi)), 3)
#> 1/2 - cos(2*x)/2
truncate(fourier_series(x, list(x, -1, 1)), 3)
#> 2*sin(pi*x)/pi - sin(2*pi*x)/pi + 2*sin(3*pi*x)/(3*pi)

The square wave:

sq <- Piecewise(list(0, Lt(x, 0)), list(1, TRUE))
truncate(fourier_series(sq, list(x, -sym_pi, sym_pi)), 4)
#> 2*sin(x)/pi + 2*sin(3*x)/(3*pi) + 2*sin(5*x)/(5*pi) + 1/2

Series can be shifted and scaled, and sigma_approximation() applies Lanczos sigma factors that reduce the Gibbs phenomenon.

truncate(shift(s, 1), 3)
#> -4*cos(x) + cos(2*x) + 1 + pi^2/3
truncate(scale(s, 2), 3)
#> -8*cos(x) + 2*cos(2*x) + 2*pi^2/3
sigma_approximation(fourier_series(x, list(x, -sym_pi, sym_pi)), 4)
#> 2*sin(x)*sinc(pi/4) - 2*sin(2*x)/pi + 2*sin(3*x)*sinc(3*pi/4)/3
truncate(shiftx(fourier_series(x, list(x, -sym_pi, sym_pi)), 1), 3)
#> 2*sin(x + 1) - sin(2*x + 2) + 2*sin(3*x + 3)/3
truncate(scalex(fourier_series(x, list(x, -sym_pi, sym_pi)), 2), 3)
#> 2*sin(2*x) - sin(4*x) + 2*sin(6*x)/3

Formal power series

fps() computes a formal power series: unlike series(), it finds a formula for the general coefficient, so the series can be expanded to any order.

fps_truncate(fps(exp(x), x))
#> 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120 + O(x^6)
fps_truncate(fps(log(1 + x), x), 4)
#> x - x^2/2 + x^3/3 + O(x^4)
fps_truncate(fps(sin(x), x), 8)
#> x - x^3/6 + x^5/120 - x^7/5040 + O(x^8)
fps_truncate(fps(atan(x), x), 8)
#> x - x^3/3 + x^5/5 - x^7/7 + O(x^8)
fps_truncate(fps(1 / (1 - x), x), 5)
#> 1 + x + x^2 + x^3 + x^4 + O(x^5)
fps_term(fps(exp(x), x), 3)
#> x^3/6
fps_polynomial(fps(exp(x), x), 4)
#> x^3/6 + x^2/2 + x + 1

Formal power series can be integrated, multiplied, composed, and inverted:

fps_truncate(fps_integrate(fps(exp(x), x), x), 4)
#> 1 + x + x^2/2 + x^3/6 + O(x^4)
fps_truncate(fps_product(fps(sin(x), x), fps(cos(x), x), 6), 6)
#> x - 2*x^3/3 + 2*x^5/15 + O(x^6)
fps_truncate(fps_compose(fps(exp(x), x), fps(sin(x), x), 5), 5)
#> 1 + x + x^2/2 - x^4/8 + O(x^5)
fps_truncate(fps_inverse(fps(exp(x), x), 5), 5)
#> 1 - x + x^2/2 - x^3/6 + x^4/24 + O(x^5)

compute_fps() is the engine behind fps(). It returns the coefficient sequence, the sequence of powers, and the independent terms.

cf <- compute_fps(exp(x), x)
cf[[1]]
#> SeqFormula(Piecewise((1/factorial(_k), Eq(Mod(_k, 1), 0)), (0, True)), (_k, 1, oo))
cf[[2]]
#> SeqFormula(x^_k, (_k, 0, oo))

Holonomic functions

Holonomic functions have their own vignette, vignette("holonomic", package = "symcompR").

Rational approximation

approximants() returns the successive Padé approximants of a sequence of coefficients. It returns a list; sstr() prints each element on one line. The Catalan numbers have generating function (1 - \sqrt{1 - 4x}) / (2x), and the approximants converge to it:

vapply(approximants(list(1, 1, 2, 5, 14, 42, 132)), sstr, "")
#> [1] "1"                                                 
#> [2] "-1/(x - 1)"                                        
#> [3] "(x - 1)/(2*x - 1)"                                 
#> [4] "(1 - 2*x)/(x^2 - 3*x + 1)"                         
#> [5] "(x^2 - 3*x + 1)/(3*x^2 - 4*x + 1)"                 
#> [6] "(-3*x^2 + 4*x - 1)/(x^3 - 6*x^2 + 5*x - 1)"        
#> [7] "(x^3 - 6*x^2 + 5*x - 1)/(4*x^3 - 10*x^2 + 6*x - 1)"

For the Fibonacci numbers the approximants stop changing once they reach the exact rational generating function:

vapply(approximants(lapply(1:9, fibonacci), x), sstr, "")
#> [1] "1"                 "-1/(x - 1)"        "(x - 1)/(2*x - 1)"
#> [4] "-1/(x^2 + x - 1)"

pade() computes the [m/n] Padé approximant of a function at zero from its Taylor series. pade_approximant(f, x, x0, m, n) is SymPy’s version from sympy.series.approximants; it expands at x0:

pade(cos(x), x, 2, 2)
#> (1 - 5*x^2/12)/(x^2/12 + 1)
pade_approximant(cos(x), x, 0, 2, 2)
#> (1 - 5*x^2/12)/(x^2/12 + 1)

Order terms and other expansions

Order() (also O()) is the order symbol, which absorbs higher order terms. nseries() is the expansion engine behind series(), and aseries() computes asymptotic expansions at infinity.

Order(x^2)
#> O(x^2)
Order(x^2) + x^3
#> O(x^2)
nseries(sin(x), x, 0, 6)
#> x - x^3/6 + x^5/120 + O(x^6)
nseries(exp(x), x, 0, 4)
#> 1 + x + x^2/2 + x^3/6 + O(x^4)
aseries(exp(1 / x + exp(-x)) - exp(1 / x), x, 3)
#> (1/(2*x^2) + 1/x + 1 + O(x^(-3), (x, oo)))*exp(-x)

Residues

residue(1 / (x^2 + 1), x, sym_I)
#> -I/2
residue(exp(z) / z^3, z, 0)
#> 1/2
residue(cot(sym_pi * z), z, 0)
#> 1/pi

Guessing and accelerating sequences

The functions of sympy.concrete.guess find a formula, a linear recurrence or a generating function that fits the first terms of a sequence.

fib <- c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
find_simple_recurrence_vector(fib)
#> [[1]]
#> 1
#> 
#> [[2]]
#> -1
#> 
#> [[3]]
#> -1
find_simple_recurrence(fib)
#> -a(n) - a(n + 1) + a(n + 2)
guess(c(1, 2, 6, 24, 120))
#> [[1]]
#> RisingFactorial(2, i0 - 1)
guess(c(1, 3, 6, 10, 15, 21))
#> [[1]]
#> i0^2/2 + i0/2
guess_generating_function_rational(c(5, 8, 13, 21, 34, 55, 89, 144))
#> (3*x + 5)/(-x^2 - x + 1)
guess_generating_function(c(1, 2, 4, 8, 16, 32))$ogf
#> 1/(1 - 2*x)
rationalize(pi, maxcoeff = 250)
#> 355/113

richardson() and shanks() accelerate the convergence of a sequence toward its limit:

richardson((1 + 1 / k)^k, k, 10, 3)
#> 64760917612327084418953891207076443/23824634392675154069619793920000000
shanks(1 - 1 / k, k, 4)
#> 7/8

finite_diff() is the forward difference of an expression and finite_diff_kauers() the difference of a sum with respect to its upper limit:

m <- Symbol("m")
finite_diff(x^2 + 2 * x + 1, x)
#> 2*x + 3
finite_diff_kauers(Sum(x^2, list(x, 1, m)))
#> (m + 1)^2

pade_approximants_gcdex() lists the Padé approximants of a truncated power series from the steps of the extended Euclidean algorithm, and pade_approximant_gcdex() picks the one with given degrees:

f <- 1 + x + x^2 / 2 + x^3 / 6 + x^4 / 24
lapply(pade_approximants_gcdex(Poly(f, x), 2), function(pq) lapply(pq, as_expr))
#> [[1]]
#> [[1]][[1]]
#> x^2/2 + x + 1
#> 
#> [[1]][[2]]
#> 1
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> 2*x + 4
#> 
#> [[2]][[2]]
#> 4 - 2*x
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> 1
#> 
#> [[3]][[2]]
#> x^2/2 - x + 1
lapply(pade_approximant_gcdex(Poly(f, x), 2), as_expr)
#> [[1]]
#> x^2/4 + 3*x/2 + 3
#> 
#> [[2]]
#> x^2/4 - 3*x/2 + 3
lapply(pade_approximant_gcdex(Poly(f, x), 3, 1), as_expr)
#> [[1]]
#> 4*x^3 + 24*x^2 + 72*x + 96
#> 
#> [[2]]
#> 96 - 24*x

recurrence_memo() and assoc_recurrence_memo() build memoized functions for sequences defined by recurrences, with one or two indices:

fibm <- recurrence_memo(list(0, 1))(function(n, prev) prev[[n]] + prev[[n - 1]])
fibm(30)
#> [1] 832040
binom <- assoc_recurrence_memo(function(i) 1)(
  function(i, j, prev) prev[[i + 1]][[j]] * (i - j + 1) / j)
binom(5, 2)
#> [1] 10