How can a spline be created if only the points and the coefficients are known? I'm using scipy.interpolate.BSpline here, but am open to other standard packages as well. So basically I want to be able to give someone just those short arrays of coefficients for them to be able to recreate the fit to the data. See the failed red-dashed curve below.
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import BSpline, LSQUnivariateSpline
x = np.linspace(0, 10, 50) # x-data
y = np.exp(-(x-5)**2/4) # y-data
# define the knot positions
t = [1, 2, 4, 5, 6, 8, 9]
# get spline fit
s1 = LSQUnivariateSpline(x, y, t)
x2 = np.linspace(0, 10, 200) # new x-grid
y2 = s1(x2) # evaluate spline on that new grid
# FAILED: try to construct BSpline using the knots and coefficients
k = s1.get_knots()
c = s1.get_coeffs()
s2 = BSpline(t,c,2)
# plotting
plt.plot(x, y, label='original')
plt.plot(t, s1(t),'o', label='knots')
plt.plot(x2, y2, '--', label='spline 1')
plt.plot(x2, s2(x2), 'r:', label='spline 2')
plt.legend()

The fine print under
get_knotssays:That means, to get a usable knot array from
get_knots, one should addkcopies of the left boundary knot at the beginning of array, andkcopies of the right boundary knot at the end. Herekis the degree of the spline, which is usually 3 (you asked forLSQUnivariateSplineof default degree, so that's 3). So:Now, the spline s2 is the same as s1:
Equivalently,
kn = 4*[x[0]] + t + 4*[x[-1]]would work: yourtlist contains only interior knots, sox[0]andx[-1]are added, and then each repeatedktimes more.The mathematical reason for the repetition is that B-splines need some room to get built, due to their inductive definition which requires
(k-1)-degree splines to exist around every interval in which we define thekth degree spline.