Spline Calculus
How to apply integration and differentiation to one-dimensional polynomial splines.
Area Under the Curve
Let a piecewise polynomial one-dimensional periodic spline of nonnegative integer degree \(n\) and positive integer period \(K\) be \(f:{\mathbb{R}}\rightarrow{\mathbb{R}},x\mapsto f(x)=\sum_{k\in{\mathbb{Z}}}\,c[{k\bmod K}]\,\beta^{n}(x-\delta x-k),\) where \({\mathbf{c}}=\left(c[k]\right)_{k=0}^{K-1}\in{\mathbb{R}}^{K}\) is its vector of spline coefficients and where \(\delta x\) is its delay. The synthesis function of the spline \(f\) is the B-spline \(\beta^{n}.\) We want to compute the quantity
with integration bounds \(a\in{\mathbb{R}}\) and \(b\in{\mathbb{R}}.\)
We provide now a few lines of code that create a random spline of specified period, degree, and delay. The integration bounds can be set freely and the returned integral is computed exactly, without approximation.
Jupyter Lab notebook
Anti-Gradient
Consider the spline \(f\) above. While it is periodic, its generic integral \(\int_{0}^{x}\,f(t)\,{\mathrm{d}}t\) is not necessarily periodic in terms of \(x.\) Indeed, the integral \(\int_{0}^{K+x}\,f(t)\,{\mathrm{d}}t\) can be segmented in two parts as \(\int_{0}^{K}\,f(t)\,{\mathrm{d}}t+\int_{K}^{K+x}\,f(t)\,{\mathrm{d}}t.\) It is then equal to \(K\,E\{f\}+\int_{0}^{x}\,f(t)\,{\mathrm{d}}t,\) which matches \(\int_{0}^{x}\,f(t)\,{\mathrm{d}}t\) only when the mean of \(f\) vanishes.
The anti-gradient of \(f\) is defined as the function
It turns out to be a periodic spline of degree \(n+1.\) As such, it can be handled by the splinekit library. Its most relevant property is that the gradient of the anti-gradient of a spline is the spline itself, up to an additive constant, so that \(\dot{S}_{f}=\left(f-E\{f\}\right).\)
We provide now a few lines of code that first create a random spline of specified period, degree, and delay, and then show its anti-gradient, along with the gradient of this anti-gradient.
Jupyter Lab notebook
Gradient
Consider the \(K\)-periodic spline
with \(n\in{\mathbb{N}}+1\) a positive degree. The spline gradient is its first derivative
Because B-splines of positive degree satisfy the relation \(\dot{\beta}^{n}(x)=\left(\beta^{n-1}(x+\frac{1}{2})-\beta^{n-1}(x-\frac{1}{2})\right)\) for all \(x\in{\mathbb{R}},\) the gradient of a spline is again a spline, albeit one whose degree is smaller and whose delay is offset by \(\frac{1}{2}.\) As such, the gradient can be handled by the splinekit library.
The centered finite difference of a function \(f\) is parameterized by a step \(h\in{\mathbb{R}}\setminus\{0\}\) and is defined as the quantity \(\frac{1}{2\,h}\,\left(f(x+h)-f(x-h)\right).\) At those arguments \(x\in{\mathbb{R}}\) where the function is differentiable, one has that \(\dot{f}(x)=\lim_{h\rightarrow0}\frac{1}{2\,h}\,\left(f(x+h)-f(x-h)\right).\)
We provide now a few lines of code that first create a random spline of specified period, degree, and delay, and then show its true gradient, along with the approximation of the gradient provided by centered finite differences of rational step \(h=\frac{1}{m},\) with \(m\in{\mathbb{N}}+1\) a positive integer.
Jupyter Lab notebook
Derivative of Positive Integer Order
Given the spline \(f\) as above, we want now to compute its derivative of order \(m,\) with \(m\in{\mathbb{N}}+1.\) This derivative will be a true function whenever the degree of the spline is large enough, with \(n\in{\mathbb{N}}+m;\) more precisely, it is going to be a spline of degree \(\left(n-m\right).\)
We propose two approaches to compute the derivative. The first one is useful to evaluate \(\frac{{\mathrm{d}}^{m}f(x)}{{\mathrm{d}}x^{m}}\) at a single given argument \(x,\) at a low computational cost, but the computational efficiency is lost if this approach is to be repeated for several arguments; the second approach is useful to establish the spline derivative \({\mathrm{D}}^{m}\{f\}:{\mathbb{R}}\rightarrow{\mathbb{R}}\) as an instance of the PeriodicSpline1D class. This second approach is the one that is implemented in the splinekit library.
Derivative at a Single Argument
In the first method, we choose to proceed with the linear-algebra equivalent representation of the spline as
There, the partial vector of coefficients \({\mathbf{c}}_{r}\in{\mathbb{R}}^{n+1}\) is defined as \({\mathbf{c}}_{r}=\left(c[{\left(k-r\right)\bmod K}]\right)_{k=0}^{n},\) where \(r=\left\lceil\xi(x)\right\rceil\in{\mathbb{Z}}\) with \(\xi(x)=\left(\frac{n-1}{2}-x-\delta x\right)\in{\mathbb{R}}.\) The spline evaluation matrix \({\mathbf{W}}^{n}\in{\mathbb{R}}^{\left(n+1\right)\times\left(n+1\right)}\) depends on \(n\) only. Finally, the Vandermonde vector \({\mathbf{v}}^{n}(\chi(x))\in{\mathbb{R}}^{n+1}\) is computed as \({\mathbf{v}}^{n}(\chi(x))=\left(1,\left(\chi^{k}(x)\right)_{k=1}^{n}\right),\) with \(\chi(x)=\left(r-\xi(x)\right)\in[0,1).\)
With these definitions, the derivative of order \(m\) is
where \({\mathbf{\Lambda}}_{m}^{n}={\mathbf{diag}}(\left(\frac{k!}{\left(k-m\right)!}\right)_{k=0}^{n}).\) This allows one to precompute \({\mathbf{W}}^{n}\,{\mathbf{\Lambda}}_{m}^{n}\) since it depends neither on the delay of the spline nor on the spline coefficients. Furthermore, the fact that there are leading zeros in the vector \(\left(\left(0\right)_{k=0}^{m-1},{\mathbf{v}}^{n-m}(\chi(x))\right)\in{\mathbb{R}}^{n+1}\) gives the opportunity of additional computational savings.
Derivative as a Function
The gradient of a polynomial B-spline of positive integer degree \(n\in{\mathbb{N}}+1\) turns out to be the function \(\dot{\beta}^{n}:{\mathbb{R}}\rightarrow{\mathbb{R}},x\mapsto\left(\beta^{n-1}(x+\frac{1}{2})-\beta^{n-1}(x-\frac{1}{2})\right).\) This recursive relation between a B-spline of degree \(n\) and shifted B-splines of degree \(\left(n-1\right)\) can be taken advantage of to express derivatives of higher order, for instance \(\ddot{\beta}^{n}\) or \(\dddot{\beta}^{n}.\) After some algebraic manipulations, it leads one to express the derivative of order \(m\) of a \(K\)-periodic spline as
where the periodic finite-difference operator \(\Delta_{K}^{m},\) applied to the vector \({\mathbf{c}}=\left(c[k]\right)_{k=0}^{K-1}\) of the spline coefficients, is defined by
By identification of terms, it can be seen that the derivative of a periodic spline is indeed a periodic spline of period \(K,\) of degree \(\left(n-m)\right),\) of delay \(\left(\delta x-\frac{m}{2}\right),\) and of spline coefficients \(\left(\Delta_{K}^{m}\{{\mathbf{c}}\}[k]\right)_{k=0}^{K-1}.\)
Jupyter Notebook
We provide now a few lines of code that create a random spline of specified period, degree, and delay, and show its derivative of arbitrary order.
Jupyter Lab notebook
Derivatives and Fourier Coefficients
Suppose we have at our disposal the spline \(g={\mathrm{D}}^{m}\{f\}\) that results from the \(m\)-th order differentiation of the spline \(f.\) On one hand, we know that the function splinekit.PeriodicSpline1D.fourier_coeff provides us with a direct access to the Fourier coefficients \(F\{g\}.\) On the other hand, the theory asserts that they are related to \(F\{f\}\) as
We provide now a few lines of code that create a random spline of specified period, degree, and delay, and illustrate the practical validity of the mathematical relation between the Fourier coefficients, for arbitrary orders of differentiation.
Jupyter Lab notebook