11 Stars
Updated Last
1 Year Ago
Started In
December 2013


Truncated Power Series for Julia

Build Status

PowerSeries.jl defines Series types that represent truncated power series by their coefficients. You can do arithmetic on Series and apply functions to series just as you would Real or Complex numbers. Here's an example session:

julia> using PowerSeries

# Represent the series 1.0 + 1.0*x - 2.0*x^2
julia> a = series(1.0, 1.0, -2.0)

# Represent the series 1.0 + 0.0*x + 1.0*x^2
julia> b = series(1.0, 0.0, 1.0)

# Series add linearly
julia> a+b

# The output of series operations is truncated to match the input size.
# Represents (1+x-2x^2)(1 + x^2) = 1+x-x^2+o(x^3)
julia> a*b

# Extract the constant term of a series
julia> constant(a)

# Functions with known derivatives can easily be overloaded to operate on
# power series.
# You can generate the taylor series of a function about a point x up to
# e.g. 5th order by computing f(Series(x, 1.0, 0.0, 0.0, 0.0, 0.0))
julia> x = series(0.0, 1.0, 0.0, 0.0, 0.0, 0.0)

julia> sin(x)

julia> log(1 + x)

julia> 1/(1 - x)

# These are numerically equal to their series definitions
julia> series(0.0, 1.0, 0.0, -1.0/6.0, 0.0, 1.0/120)

julia> series(0.0, 1.0, -1.0/2, 1.0/3, -1.0/4, 1.0/5)

# Take the derivative of a series
julia> polyder(a)

# Integrate a series term by term. Note that by convention, the constant term is 0.
julia> polyint(a)

julia> @assert polyder(polyint(a)) == a

# Truncate a series to a series 1 order lower
julia> restrict(a)

# Restricting a first order series returns a real number
julia> restrict(restrict(a))

# polyint, polydir, and restrict are the only operations that change the order of
# a series. Arithmetic on series of different orders is disallowed because
# relevant terms in the lower order series may have been dropped at intermediate
# steps.
julia> series(1.0, 1.0) + series(1.0, 2.0, 3.0)
ERROR: no promotion exists for Series1{Float64} and Series2{Float64}
 in + at promotion.jl:158

# Truncated power series offer one of the best ways to take multiple derivatives
# of generic mathematical functions.
julia> f(x) = exp(-x^2)
f (generic function with 1 method)

julia> f2(x) = polyder(polyder(f(series(x, 1, 0))))
f2 (generic function with 1 method)

julia> f2(2.0)

# Compare to the symbolic second derivative
julia> let x = 2.0; -2exp(-x^2)+4x^2*exp(-x^2); end

# PowerSeries comes with types defined for series up to order 7. By default,
# trying to construct a higher order series is a type error.
julia> series(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
ERROR: no method series(Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64)

# If you want to work with higher order series, you can generate types up
# to a given order with PowerSeries.generate(order)
julia> PowerSeries.generate(9)
julia> series(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

For taking first derivatives of code, see also DualNumbers.jl, and for taking symbolic derivatives, see the differentiate method of Calculus.jl.

Truncated series have performance advantages over symbolic derivatives for either deeply nested functions or high order derivatives.

###Theory of operation Computations of functions of a power series are based on the fundamental theorem of calculus:


Using this relation, it's easy to derive a composition rule for functions that can be applied directly to power series.


This is essentially an extension of the chain rule from infinitesimal calculus to finite step sizes.

Once differentiation and definite integration are defined on series, this relation allows a simple definition of functions of series. For example, the sine and cosine of series are mutually-recursively defined as

sin(x::AbstractSeries) = sin(constant(x)) + polyint(polyder(x)*cos(restrict(x)))
cos(x::AbstractSeries) = cos(constant(x)) - polyint(polyder(x)*sin(restrict(x)))

The general pattern is

f(x::AbstractSeries) = f(constant(x)) + polyint(polyder(x)*f'(restrict(x)))

where f' should be replaced by a known derivative function.