scijit.interpolate.splrep¶
- scijit.interpolate.splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, full_output=0, per=0, quiet=1)¶
Compute the B-spline representation of a 1-D curve.
- Parameters:
- x1-D array_like of float, length m
Abscissae, strictly increasing.
- y1-D array_like of float, length m
Ordinates.
- w1-D array_like of float, optional
Positive weights, length m.
None(the default) means unit weights AND selectss = 0; supplying w selectss = m - sqrt(2*m)instead.- xb, xefloat, optional
Interval the fit is made over.
None(the default) meansx[0]andx[-1]. They may only WIDEN the data interval.- kint, optional
Spline degree, 1 <= k <= 5. Default 3.
- taskint, optional
0 (default) finds the smoothing spline. -1 finds the weighted least-squares spline for the knots given in t. 1 is not supported; see Deviations.
- sfloat, optional
Smoothing factor: the fit satisfies
sum(w[i]*(y[i]-spl(x[i])))**2 <= s.None(the default) resolves as described under w.- t1-D array_like of float, optional
INTERIOR knots for
task=-1, without the boundary repetitions. Supplying it forcestask=-1. t and per are read independently, so the two together give the periodic least-squares fit on those knots.- full_outputbool, optional
Non-zero returns the 4-tuple described below. Must be a compile-time constant inside
@njit, since it selects the return type.- perbool, optional
Non-zero fits a periodic spline through
percur.y[m-1]andw[m-1]are not used. Any non-zero value behaves as 1.- quietbool, optional
Zero also warns on a SUCCESSFUL fit, reporting k, the allocated knot count, m, fp and s. Default 1, which suppresses it. The
ierin {1, 2, 3} warning is issued either way.
- Returns:
- tcktuple of (t, c, k)
Knot vector, coefficients in FITPACK’s padded form (only the first
len(t) - k - 1are meaningful) and the degree.- fpfloat
Weighted sum of squared residuals. Returned only when full_output is non-zero.
- ierint
FITPACK status: -1 interpolating spline, -2 least-squares polynomial, 0 smoothing achieved, 1/2/3 failure, 10 invalid input. Returned only when full_output is non-zero. It is the value FITPACK set, including on the failure paths that raise when full_output is zero.
- msgstr
The exit message for the FITPACK status code ier. Returned only when full_output is non-zero, and reads “An error occurred” for any ier outside the table.
- Raises:
- TypeError
len(w) != m,len(y) != m,koutside 1..5,m <= k,taskoutside -1..1,task == 1,task == -1without t, or a FITPACK failure withieroutside {1, 2, 3, 10}.- ValueError
ier == 10, FITPACK’s invalid-input code, when full_output is zero. Without the raise, the empty t/c evaluate to 0.0 through splev: a wrong number and no error.
- Warns:
- RuntimeWarning
ierin {1, 2, 3} withfull_output=0: the fit is returned and the matching_iermesstext is warned. Withquiet=0, also onier <= 0, reporting the successful fit. Both are issued through anumba.objmodeblock, which runs its body in the interpreter, sowarnings.catch_warningsand-Wsee them from compiled and uncompiled callers alike.
See also
scipy.interpolate.splrepThe scipy routine this mirrors.
Notes
full_output must be a compile-time constant inside
@njit: it selects between a 3-tuple and a 4-tuple return, and numba compiles one return type per specialization. A bool, an int, a string and the default are read while the call compiles; a float, a container and a runtime variable are not, and raiseTypingErrornaming the argument. From Python every object is read by its truthiness.task=1raises. scipy keeps the previous fit in a function-local_curfit_cache, so its owntask=1raisesUnboundLocalErroron every call, including immediately after atask=0call. There is no behaviour to reproduce.A parametric y (2-D, or a list of arrays) is not accepted.
For
task=-1the interior knots are validated before FITPACK sees them, and two codes FITPACK never produces are reported: 30 for knots that are not strictly increasing or not strictly inside(xb, xe), and 50 for knots outside(x[0], x[-1]). Neither is in scipy’s_iermesstable, so both surface asTypeError("An error occurred"). Those two checks run only while the data lies inside[xb, xe]; when it does not,fpchecfails first and reportsier = 10. A NaN knot passes both checks. They belong to the non-periodic route: withpernon-zero the knots go topercur, which reports a bad knot vector asier = 10andValueError("Error on input data").A SMOOTHING FIT MAY WARN THAT IT IS RANK DEFICIENT. With a small s on noisy data, FITPACK’s knot search can place knots so that one B-spline coefficient is not determined by the data at all. The curve still passes through the data as asked, and
fpreports success, but between the data points it carries an arbitrary component. AUserWarningnaming the number of undetermined coefficients is issued when this happens, and a larger s is the fix. scipy issues no warning for it.prange-safe: yes.
Examples
>>> import numpy as np >>> from numba import njit >>> from scijit.interpolate import splrep, splev >>> x = np.linspace(0, 4, 40) >>> y = np.sin(x) >>> @njit ... def fit_and_eval(x, y, q): ... tck = splrep(x, y) ... return splev(q, tck) >>> float(fit_and_eval(x, y, np.array([1.5]))[0]) 0.9974947337577743