Boundary intervals for splines using CompactBases

I want to implement natural cubic splines in Julia via B-splines (which are a basis for splines). I found CompactBases.jl, which uses B-splines and allows me to evaluate the basis functions at a value x.

Now, let the set of knots be t = \{\zeta_1, \dots, \zeta_K\}. Splines split \mathbb{R} into K+1 segments, (-\infty, \zeta_1], (\zeta_1,\zeta_2],\dots,(\zeta_K, \infty) and use polynomials (here cubic) in each segment. Natural splines make the function linear in the boundary segments, (-\infty, \zeta_1] and (\zeta_K,\infty).

However, when I implement them via CompactBases, the polynomial seems to be of order zero (a constant) rather than one (linear). See the below MWE, where I try to approximate \sin{x} over -6:6 but let t be in -5:5.

Outside -5:5, the approximation is just constant (at zero). Any help would be appreciated.

using Plots
using CompactBases

t = LinearKnotSet(4, -5, 5, 3)
B = BSpline(t)

x = [-6:0.01:6;]
y = sin.(x)
c = B[x,:] \ y

X = B[x,:]
approx = X*c

plot(x,[y, approx])


Could you ellaborate why linear polynomials are expected?

That code deals with B-splines which use cubic polynomials as basis functions (when k=4) . In your example, these can be plotted with:

plot(X, legend=:outerright)


It is not surprising that such package will only work within the compact support defined but you can contact the author at CompactBases.jl github page.

1 Like

Not that they are expected, but I wanted to use the package to build natural splines. For that to be the case, there needs to be an option to choose the degree of the polynomials for the boundary intervals, and I was wondering whether it was possible.

OK, thanks.

Just in case, note that the Interpolations.jl package has natural cubic splines using CubicSplineInterpolation() and also natural cubic B-splines using something like interpolate(x, BSpline(Cubic(Natural(OnGrid())))), whose second derivatives at the two endpoints are zero.

1 Like

Thank you for your suggestion. That would be great, but apparently Interpolations.jl does not allow to evaluate some vlaue x in the basis functions–or more generally the functions comprising the spline–directly (CompactBases.jl does, see my example above when calling B[x,:]).