TrigPolys
TrigPolys.jl is a package for fast manipulation of trigonometric polynomials.
A trignometric polynomial is defined on $x \in [0,2\pi)$ by
\[p(x) = a_0 + \sum_{k=1}^n a_k \cos(kx) + a_{-k} \sin(kx)\]
The polynomial $p(x)$ can be represented either by $2n+1$ coefficients $a_k$ or by evaluations at $2n+1$ distinct points in the interval $[0,2\pi)$. Each representation is useful in different situations: the coefficient representation is useful for truncating or increasing the degree of a polynomial whereas the evaluation representation is useful for adding and multiplying polynomials.
This package provides the functions evaluate
and interpolate
to convert efficiently between these two representations. These operations are implemented via the Fast Fourier Transform (FFT) provided by the FFTW.jl library.
For example, multiplying two trigonometric polynomials is implemented with the following code:
function Base.:*(p1::TrigPoly, p2::TrigPoly)
n = p1.n + p2.n
interpolate(evaluate(pad_to(p1, n)) .* evaluate(pad_to(p2, n)))
end
Construction and Convertion
TrigPolys.TrigPoly
— Typestruct TrigPoly{T<:AbstractFloat, VT<:AbstractVector{T}}
a0::T # Constant coefficient
ac::VT # cos coefficients
as::VT # sin coefficients
end
Represents a trigonometric polynomial by its coefficients.
TrigPolys.TrigPoly
— Method(p::TrigPoly)(x::Number)
Evaluate p(x)
at a single point. See TrigPolys.evaluate
for faster evaluation for special points on the unit circle.
TrigPolys.TrigPoly
— MethodTrigPoly(u::AbstractVector{T}) where {T<:AbstractFloat}
Constructs a TrigPoly
given a vector of coefficients of length 2n+1
. The first entry of u
is a0
, the next n
entries are ac
and the last n
entries are as
.
TrigPolys.a0
— Functiona0(p::TrigPoly)
Equivalent to p.a0
, used for incorporation into automatic differentiation libraries.
TrigPolys.ac
— Functionac(p::TrigPoly)
Equivalent to p.ac
, used for incorporation into automatic differentiation libraries.
TrigPolys.as
— Functionas(p::TrigPoly)
Equivalent to p.as
, used for incorporation into automatic differentiation libraries.
TrigPolys.n
— Functionn(p::TrigPoly)
Equivalent to p.n
. Returns the length of cosine/sine coefficients, which is length(p.ac) == length(p.as)
.
TrigPolys.degree
— Functiondegree(p::TrigPoly)
Returns the number of coefficients of p
, which is 2*p.n + 1
.
Methods
Base.vec
— MethodBase.vec(p::TrigPoly)
Converts a TrigPoly into a vector, inverse operation of TrigPolys.TrigPoly
Base.:==
— MethodBase.:(==)(p1::TrigPoly, p2::TrigPoly)
Returns true if the coefficients of p1
and p2
are all equal.
Base.:+
— MethodBase.:+(p1::TrigPoly, p2::TrigPoly)
Adds p1
to p2
. The degree of the resulting polynomial will be the maximum degree of p1
and p2
: (p1 + p2).n == max(p1.n, p2.n)
Base.:*
— MethodBase.:*(p1::TrigPoly, p2::TrigPoly)
Multiplies p1
and p2
. The degree of the resulting polynomial will be the sum of degrees of p1
and p2
: (p1 * p2).n == p1.n + p2.n
Base.:/
— MethodBase.:/(p::TrigPoly, a::Number)
Divides p
by a scalar.
Functions
TrigPolys.pad_to
— Functionpad_to(p::TrigPoly{T,VT}, n::Integer) where {T<:AbstractFloat, VT<:AbstractVector{T}}
Increase the length of p.ac
and p.as
to n
, padding with zeros if necessary. n
cannot be smaller than p.n
.
TrigPolys.pad_by
— Functionpad_by(p::TrigPoly, n::Integer)
Increase the length of p.ac
and p.as
by n
, padding with n
zeros.
TrigPolys.random_trig_poly
— Functionrandom_trig_poly(n)
Generate a TrigPoly
with random coefficients.
TrigPolys.basis
— Functionbasis(n, x)
Generate the basis for TrigPoly
expressed as a vector, so that basis(p.n, x)'*vec(p) = p(x)
.
TrigPolys.evaluate
— Functionevaluate(u::AbstractVector)
Evaluates an array representation of p::TrigPoly
, returned by vec(p)
.
evaluate(p::TrigPoly)
Evaluates p
on degree(p)
uniformly-spaced points on the circle. See here for more details.
TrigPolys.evaluateT
— FunctionevaluateT(u::AbstractVector)
Adjoint of evaluate operator on an array representation of p::TrigPoly
, returned by vec(p)
.
evaluateT(p::TrigPoly)
Adjoint of evaluate operator. Returns a vector.
TrigPolys.interpolatev
— Functioninterpolatev(u::Vector)
Inverse of TrigPolys.evaluate(p::AbstractVector)
. Returns a vector.
TrigPolys.interpolate
— Functioninterpolate(u::Vector)
Inverse of TrigPolys.evaluate(p::TrigPoly)
. Returns a TrigPoly
.