Overview

SpectralKit — Module

SpectralKit.jl

lifecycle build codecov Documentation Documentation DOI Aqua QA

Building blocks of spectral methods for Julia. Currently includes Chebyshev polynomials on univariate and Smolyak (multivariate) grids, with domain transformations to semi-infinite and infinite domains.

Introduction

Mostly useful for algorithms along the lines of

Boyd, John P. Chebyshev and Fourier spectral methods. 2001.

The aim is to provide simple, well-tested, robust, and fast building blocks for spectral algorithms, which can be easily combined into algorithms.

At the moment, the package API is experimental and subject to change.

Help

Asking for help in issues is fine, you can also ping me as @Tamas_Papp on the Discourse forum

Bibliography

  • Boyd, J. P. (2001). Chebyshev and fourier spectral methods. Courier Corporation.

  • Xu, K. (2016). The chebyshev points of the first kind. Applied Numerical Mathematics, 102, 17–30.

source

This page introduces the concepts of the package through univariate examples.

Concepts

For the purposes of this package,

  1. A basis is a finite family of functions, defined over a given domain.

  2. Each family has a canonical domain: for example, Chebyshev polynomials are defined over $[-1,1]$. This is mostly hidden from the user, allowing the specification of custom domains using transformations.

  3. The number of basis functions is the dimension.

  4. Each basis has an associated grid recommended for collocation methods.

  5. The dimension is determined by the level and the grid kind.

  6. The grid also has a level, which defaults to the level of the basis: default grid has as many points as the dimension of the basis.

  7. Levels allow nesting gridpoints. Increasing the level (or the grid level) of the basis adds more grid points to existing ones.

Univariate bases

Let's demonstrate these concepts with a concrete example, by constructing a basis of Chebyshev polynomials, transformed to the interval $[0, 1]$ (using a linear mapping), with level = 2.

julia> using SpectralKit;
julia> basis = UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0, 1), 2)# dimension: 7 UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0, 1), 2)

The default printing methods shows the dimension, but one can of query it too using the API:

julia> dimension(basis)7

The grid is an iterable calculated on demand, so that grid does not allocate:

julia> grid(basis)7-element SpectralKit.ChebyshevGrid{Float64, Interior, BoundedLinear{Int64, ErrorOutsideDomain}}:
 0.5
 0.1464466094067262
 0.8535533905932737
 0.03806023374435663
 0.3086582838174551
 0.6913417161825449
 0.9619397662556434

To get the number of points in a grid, just use grid(length(basis)).

For univariate bases, levels are integers starting from 0. One could use a denser grid, eg for the a least squares approximation, by also specifying a level for the grid:

julia> basis_ls = UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0, 1), 1, 2)# dimension: 3, grid length: 7
UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0, 1), 1, 2)

Increasing the level adds more points. Grids can be calculated with types other than Float64 (the default), here we use Float16 for more compact printing:

julia> [grid(Float16, UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0, 1), level))
        for level in 0:3]4-element Vector{SpectralKit.ChebyshevGrid{Float16, Interior, BoundedLinear{Int64, ErrorOutsideDomain}}}:
 [0.5]
 [0.5, 0.1465, 0.8535]
 [0.5, 0.1465, 0.8535, 0.0381, 0.3086, 0.6914, 0.962]
 [0.5, 0.1465, 0.8535, 0.0381, 0.3086, 0.6914, 0.962, 0.00952, 0.0842, 0.2222, 0.4023, 0.5977, 0.778, 0.916, 0.99]

Grid kinds

Currently, two kinds of grids are supported: Endpoints and Interior.

Endpoints() grids are Chebyshev nodes of the second kind, also known as Chebyshev–Lobatto nodes or Chebyshev extrema. They are the extrema of Chebyshev polynomials, and nest with each level adding 2, 2, 4, 8, … gridpoints:

julia> using SpectralKit
julia> [grid(Float16, UnivariateBasis(Chebyshev(), Endpoints(), BoundedLinear(-1, 1), level)) for level in 0:3]4-element Vector{SpectralKit.ChebyshevGrid{Float16, Endpoints, BoundedLinear{Int64, ErrorOutsideDomain}}}: [0.0] [0.0, -1.0, 1.0] [0.0, -1.0, 1.0, -0.707, 0.707] [0.0, -1.0, 1.0, -0.707, 0.707, -0.924, -0.3828, 0.3828, 0.924]

Interior() grids are these with -1 and 1 dropped:

julia> [grid(Float16, UnivariateBasis(Chebyshev(), Endpoints(), BoundedLinear(-1, 1), level))
        for level in 0:3]4-element Vector{SpectralKit.ChebyshevGrid{Float16, Endpoints, BoundedLinear{Int64, ErrorOutsideDomain}}}:
 [0.0]
 [0.0, -1.0, 1.0]
 [0.0, -1.0, 1.0, -0.707, 0.707]
 [0.0, -1.0, 1.0, -0.707, 0.707, -0.924, -0.3828, 0.3828, 0.924]

Domain transformations

The domain transformations are BoundedLinear, SemiInfRational, and InfRational, with the latter transforming to semi-infinite regions, such as $(0, ∞)$, and the real line, respectively. The latter two have parameters that influence the scale:

julia> [grid(Float16, UnivariateBasis(Chebyshev(), Interior(), InfRational(; scale), 2))
        for scale in [0.5, 1.0, 2.0]]3-element Vector{SpectralKit.ChebyshevGrid{Float16, Interior, InfRational{Float64}}}:
 [0.0, -0.5, 0.5, -1.207, -0.207, 0.207, 1.207]
 [0.0, -1.0, 1.0, -2.414, -0.414, 0.414, 2.414]
 [0.0, -2.0, 2.0, -4.83, -0.828, 0.828, 4.83]

and location

julia> [grid(Float16, UnivariateBasis(Chebyshev(), Interior(),
                                      SemiInfRational(; endpoint), 2))
        for endpoint in 0:2]3-element Vector{SpectralKit.ChebyshevGrid{Float16, Interior, SemiInfRational{Int64}}}:
 [1.0, 0.1716, 5.83, 0.03958, 0.4463, 2.24, 25.25]
 [2.0, 1.172, 6.83, 1.04, 1.446, 3.24, 26.25]
 [3.0, 2.172, 7.83, 2.04, 2.445, 4.242, 27.25]

Linear combinations

There are two ways to evaluate a combination of basis functions: either use linear_combination with a basis and a vector of coefficients, which gives a callable inside the domain, or to add the input as the third argument.

julia> using SpectralKit, Miter
julia> basis = UnivariateBasis(Chebyshev(), Interior(), InfRational(), 2)# dimension: 7 UnivariateBasis(Chebyshev(), Interior(), InfRational(; center = 0.0, scale = 1.0), 2)
julia> θ = [1/x^2 for x in 1:7]7-element Vector{Float64}: 1.0 0.25 0.1111111111111111 0.0625 0.04 0.027777777777777776 0.02040816326530612
julia> linear_combination(basis, θ, 0.5)1.0119199674384034
let f = linear_combination(basis, θ), g = grid(basis)
    Plot(Lines((x, f(x)) for x in range(extrema(g)...; length = 500)),
         Scatter(MarkSymbol(:o), ((x, f(x)) for x in g)))
end
Example block output

basis_at provides an iterable of the basis functions evaluated at a point:

julia> collect(basis_at(basis, 0.3))7-element Vector{Float64}:
  1.0
  0.2873478855663454
 -0.834862385321101
 -0.7671397678881331
  0.39399040484807696
  0.9935643874211797
  0.17700684695091745

Importantly, since basis functions are calculated recursively, this is not indexable as a vector. It serves as a building block for linear_combination.

Domain and values outside it

You can use domain to query the domain of a transformation:

julia> using SpectralKit
julia> b_strict = UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0.0, 3.0), 2)# dimension: 7 UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0.0, 3.0), 2)
julia> domain(b_strict)[0.0,3.0]

Domains support extrema:

julia> extrema(domain(b_strict))(0.0, 3.0)

The default behavior is to error for inputs outside the domain:

julia> linear_combination(b_strict, ones(dimension(b_strict)), 3.0001)ERROR: DomainError with 3.0001:
outside domain

but this can be overridden:

julia> b_tolerant = UnivariateBasis(Chebyshev(), Interior(),
                                    BoundedLinear(0.0, 3.0, NearestInDomain()), 2)# dimension: 7
UnivariateBasis(Chebyshev(), Interior(), BoundedLinear(0.0, 3.0, NearestInDomain()), 2)
julia> linear_combination(b_tolerant, ones(dimension(b_tolerant)), 3.0001)7.0

Transformations that do not cover the entire real line have an option to specify what happens outside the domain. This is for handling floating point error: do not deliberately evaluate bases outside their domain.

Dimensions without transformations

If you don't need grid values or linear combinations, you can specify an arbitrary value, such as 0 or nothing, in place of transformations. This allows the user to obtain the just dimension of the basis or the grid.

julia> using SpectralKit
julia> dimension(UnivariateBasis(Chebyshev(), Interior(), nothing, 2))7

This also works with Smolyak bases, and/or length(grid(basis)).

Changing one aspect of a basis

UnivariateBasis and SmolyakBasis are just containers for their properties. This means that if you just want to change some aspects of a basis, you can use a library like Accessors.jl:

julia> using SpectralKit, Accessors
julia> basis = UnivariateBasis(Chebyshev(), Interior(), (BoundedLinear(1, 2), SemiInfRational()), 2)# dimension: 7 UnivariateBasis(Chebyshev(), Interior(), (BoundedLinear(1, 2), SemiInfRational(endpoint = 0, scale = 1)), 2)
julia> basis2 = @set basis.level = 2# dimension: 7 UnivariateBasis(Chebyshev(), Interior(), (BoundedLinear(1, 2), SemiInfRational(endpoint = 0, scale = 1)), 2)