Reference🔗
berhoel.splines 🔗
Various Splines.
Modules:
-
akima–Calculate interpolating spline with the akima subspline algorithm.
-
line–Class for representing lines between two points.
-
renner–Class for calculating Renner subsplines for given nodes.
-
tests–
Classes:
-
AkimaSubspline–Akima subsplines interpolator acc. to [^l1], chapter 13.
Classes🔗
berhoel.splines.AkimaSubspline 🔗
AkimaSubspline(
x: ToFloat1D,
y: ToComplexND,
axis: int = 0,
extrapolate: bool | None | Literal["periodic"] = None,
)
Akima subsplines interpolator acc. to 1, chapter 13.
The original definition is found in 2.
Fit piecewise cubic polynomials, given vectors \(x\) and \(y\). The interpolation method by Akima uses a continuously differentiable sub-spline built from piecewise cubic polynomials. The resultant curve passes through the given data points and will appear smooth and natural.
Notes🔗
Use only for precise data, as the fitted curve passes through the given points exactly. This routine is useful for plotting a pleasingly smooth curve through a few given points for purposes of plotting.
Given: \(\left(x_i,y_i\right)\), \(i=0(1)n, n>=4, x_i<x_{i+1}\) for \(i=0(1)n-1\), not periodic or (with \(y_n = y_0\)) periodic.
Let \(m_i = (y_{i+1} - y_i) / (x_{i+1} - x_i)\) be the slopes of the interval \(\left[x_i, x_{i+1}\right)\).
Further slopes are for the not periodic variant:
For the periodic variant:
Examples🔗
If the 3 or more bases one side of the subspline are colinear, a corner will appear in the interpoltion spline.
# mkdocs: render
import numpy as np
from berhoel.splines import AkimaSubspline
import matplotlib.pyplot as plt
x = np.linspace(1, 7, 7)
y = np.array([-1, -1, -1, 0, 1, 1, 1])
xs = np.linspace(min(x), max(x), num=100)
y_akima = AkimaSubspline(x, y)(xs)
fig, ax = plt.subplots()
ax.plot(x, y, "o", label="bases")
ax.plot(xs, y_akima, label="akima")
ax.legend()
fig.show()
The AkimaSubspline implementation allows for periodic splines, in contrast to scipy.interpolate.Akima1DInterpolator.
# mkdocs: render
from berhoel.splines import AkimaSubspline
x = np.linspace(-np.pi, np.pi, 6)
y = np.sin(x)
xs = np.linspace(-np.pi, np.pi, 100) * 1.7
y_akima = AkimaSubspline(x, y, extrapolate="periodic")(xs)
fig, ax = plt.subplots()
ax.plot(x, y, "o", label="bases")
ax.plot(xs, y_akima, label="akima")
ax.plot(xs, np.sin(xs), label="sin")
ax.legend()
fig.show()
References🔗
-
Engeln-Müllges, Gisela, und Fritz Reutter. Numerik-Algorithmen: Entscheidungshilfe zur Auswahl und Nutzung. 8., Neubearb. und erw. Aufl. Düsseldorf: VDI-Verl, 1996., ISBN 3-18-401539-4 ↩
-
Akima, Hiroshi. „A New Method of Interpolation and Smooth Curve Fitting Based on Local Procedures“. Journal of the ACM 17, Nr. 4 (Oktober 1970): 589-602. https://doi.org/10.1145/321607.321609. ↩
Parameters:
-
(x🔗ToFloat1D) –1-D array of monotonically increasing real values.
-
(y🔗ToComplexND) –N-D array of real values. The length of
yalong the interpolation axis must be equal to the length ofx. Use theaxisparameter to select the interpolation axis. -
(axis🔗int, default:0) –Axis in the
yarray corresponding to the x-coordinate values. Defaults toaxis=0. -
(extrapolate🔗bool | None | Literal['periodic'], default:None) –If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If None,
extrapolateis set to False. If “periodic” the starting and finishing slopes are calculate for allowing interpolating periodic functions.
Methods:
-
__call__–Evaluate the piecewise polynomial or its derivative.
Source code in src/berhoel/splines/akima.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
Functions🔗
__call__ 🔗
Evaluate the piecewise polynomial or its derivative.
Parameters:
-
(x🔗NDArray) –Points to evaluate the interpolant at.
-
(nu🔗int, default:0) –Order of derivative to evaluate. Must be non-negative.
-
(extrapolate🔗str | None, default:None) –Extrapolation level
Returns:
-
y(NDArray) –Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.
Notes🔗
Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open, \([a, b)\), except for the last interval which is closed \([a, b]\).
Source code in src/berhoel/splines/akima.py
Functions🔗
berhoel.splines.intervall 🔗
Find the interval number of a decomposition.
This is a generally usable procedure for finding an interval that contains a given value xvalue. Given is a strictly monotonically increasing sequence of values \(x_i, i=0(1)n\). The index \(i_x\) is sought such that \(x_{i_x} <= x_\text{value} < x_{i_x+1}\). If \(x_\text{value} < x_0\) or \(x_\text{value} >= x_{n-1}\), \(i_x\) gets the value \(0\) or \(n-1\).
Parameters:
-
(vals🔗float | NDArray) –value whose interval is being searched
-
(boundaries🔗NDArray) –field with the interval limits
Returns:
-
NDArray–index ix to be searched for
Source code in src/berhoel/splines/__init__.py
berhoel.splines.line 🔗
Class for representing lines between two points.
Requires NumPy and, for the exampes, Michael Haggerty’s Gnuplot module.
Classes:
-
Line–Represent a line.
Classes🔗
berhoel.splines.line.Line 🔗
Line(points: object)
Represent a line.
Methods:
Source code in src/berhoel/splines/line.py
Functions🔗
__call__ 🔗
intersect 🔗
Calculate intersection of line with area.
Parameters:
Source code in src/berhoel/splines/line.py
berhoel.splines.renner 🔗
Class for calculating Renner subsplines for given nodes.
Requires ‘NumPy’ and, for the exampes, Michael Haggerty’s ‘Gnuplot’ module and Konrad Hinsens ‘VRML’ module.
References:🔗
.. [1] Engeln-Müllges, Gisela, und Fritz Reutter. Numerik-Algorithmen:
Entscheidungshilfe zur Auswahl und Nutzung. 8., Neubearb. und erw. Aufl.
Düsseldorf: VDI-Verl, 1996., ISBN 3-18-401539-4
Classes:
-
Renner–Renner splines.
Classes🔗
berhoel.splines.renner.Renner 🔗
Renner splines.
Methods:
-
__call__–Calculate ‘steps’ interpolated points between each two input points.
-
intersect–Return a list of intersections.
Source code in src/berhoel/splines/renner.py
Functions🔗
__call__ 🔗
Calculate ‘steps’ interpolated points between each two input points.
Source code in src/berhoel/splines/renner.py
intersect 🔗
Return a list of intersections.