6#ifndef THEORETICA_SPLINES_H
7#define THEORETICA_SPLINES_H
9#include "../core/constants.h"
10#include "../algebra/algebra_types.h"
23 template<
unsigned int N>
38 template<
unsigned int N>
45 for (
int i = 1; i < N; ++i) {
66 template<
unsigned int N>
75 template<
unsigned int N>
84 template<
unsigned int N>
129 return x * x * (3 - 2 * x);
145 return x * x * x * (x * (x * 6 - 15) + 10);
153 template<
unsigned int N>
163 template<
unsigned int N>
189 template<
unsigned int N>
202 for (
int index =
points.size(); index > 1; --index) {
204 for (
int i = 0; i < index - 1; ++i)
227 :
x(
x),
a(
a), b(b), c(c), d(d) {}
234 return a +
h * (b +
h * (c +
h * d));
242 return b +
h * (c * 2 +
h * d * 3);
250 template<
typename DataPo
ints = std::vector<vec2>>
258 const unsigned int n =
p.size() - 1;
260 std::vector<real>
dx(
n);
261 std::vector<real>
delta(
n);
265 std::vector<real>
alpha(
n + 1);
266 std::vector<real> beta(
n + 1);
267 std::vector<real> gamma(
n + 1);
273 std::vector<real> b(
n);
274 std::vector<real> c(
n);
275 std::vector<real> d(
n);
277 for (
unsigned int i = 0; i <
n; ++i)
278 dx[i] =
p[i + 1][0] -
p[i][0];
280 for (
unsigned int i = 1; i <
n; ++i)
282 ((
p[i + 1][1] -
p[i][1]) /
dx[i])
283 - (
p[i][1] -
p[i - 1][1]) /
dx[i - 1]);
285 for (
unsigned int i = 1; i <
n; ++i) {
286 alpha[i] = 2 * (
p[i + 1][0] -
p[i - 1][0]) -
dx[i - 1] * beta[i - 1];
288 gamma[i] = (
delta[i] -
dx[i] * gamma[i - 1]) /
alpha[i];
298 for (
int i =
n - 1; i >= 0; --i) {
300 c[i] = gamma[i] - beta[i] * c[i + 1];
301 b[i] = (
p[i + 1][1] -
p[i][1]) /
dx[i]
302 -
dx[i] * (c[i + 1] + 2 * c[i]) / 3.0;
303 d[i] = (c[i + 1] - c[i]) / (3.0 *
dx[i]);
306 std::vector<spline_node> nodes(
n);
308 for (
unsigned int i = 0; i <
n; ++i)
318 template<
typename Dataset1,
typename Dataset2>
327 if(x.size() !=
y.size()) {
332 const unsigned int n = x.size() - 1;
334 std::vector<real>
dx(
n);
335 std::vector<real>
delta(
n);
339 std::vector<real>
alpha(
n + 1);
340 std::vector<real> beta(
n + 1);
341 std::vector<real> gamma(
n + 1);
347 std::vector<real> b(
n);
348 std::vector<real> c(
n);
349 std::vector<real> d(
n);
351 for (
unsigned int i = 0; i <
n; ++i)
352 dx[i] = x[i + 1] - x[i];
354 for (
unsigned int i = 1; i <
n; ++i)
356 ((
y[i + 1] -
y[i]) /
dx[i])
357 - (
y[i] -
y[i - 1]) /
dx[i - 1]);
359 for (
unsigned int i = 1; i <
n; ++i) {
360 alpha[i] = 2 * (x[i + 1] - x[i - 1]) -
dx[i - 1] * beta[i - 1];
362 gamma[i] = (
delta[i] -
dx[i] * gamma[i - 1]) /
alpha[i];
372 for (
int i =
n - 1; i >= 0; --i) {
374 c[i] = gamma[i] - beta[i] * c[i + 1];
375 b[i] = (
y[i + 1] -
y[i]) /
dx[i]
376 -
dx[i] * (c[i + 1] + 2 * c[i]) / 3.0;
377 d[i] = (c[i + 1] - c[i]) / (3.0 *
dx[i]);
380 std::vector<spline_node> nodes(
n);
382 for (
unsigned int i = 0; i <
n; ++i)
401 template<
typename DataPo
ints = std::vector<vec2>>
409 template<
typename Dataset1,
typename Dataset2>
426 for (
int i =
int(
nodes.size()) - 1; i > 0; --i)
439 for (
unsigned int i =
nodes.size() - 1; i > 0; --i)
441 return nodes[i].deriv(x);
444 return nodes[0].deriv(x);
450 return nodes.begin();
A natural cubic spline interpolation class.
Definition splines.h:391
spline(const Dataset1 &X, const Dataset2 &Y)
Construct the natural cubic spline interpolation from the sets of X and Y data points.
Definition splines.h:410
real operator()(real x) const
Evaluate the natural cubic spline interpolation at a given point.
Definition splines.h:424
real deriv(real x) const
Evaluate the derivative of the natural cubic spline interpolation at a given point.
Definition splines.h:437
auto end()
Get an iterator to one plus the last spline element.
Definition splines.h:455
auto begin()
Get an iterator to the first spline element.
Definition splines.h:449
spline(const DataPoints &p)
Construct the natural cubic spline interpolation from a vector of coordinate pairs.
Definition splines.h:402
spline & operator=(const spline &other)
Copy from another spline.
Definition splines.h:416
std::vector< spline_node > nodes
The computed nodes of the natural cubic spline interpolation over the points.
Definition splines.h:396
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:219
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:198
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
real smootherstep(real x1, real x2, real interp)
Smootherstep interpolation.
Definition splines.h:134
vec< real, N > nlerp(const vec< real, N > &P1, const vec< real, N > &P2, real interp)
Normalized linear interpolation.
Definition splines.h:76
real remap(real iFrom, real iTo, real oFrom, real oTo, real value)
Remap a value from one range to another.
Definition splines.h:60
real smoothstep(real x1, real x2, real interp)
Smoothstep interpolation.
Definition splines.h:118
real invlerp(real x1, real x2, real value)
Inverse linear interpolation.
Definition splines.h:32
std::vector< spline_node > cubic_splines(DataPoints p)
Compute the cubic splines interpolation of a set of data points.
Definition splines.h:251
real lerp(real x1, real x2, real interp)
Linear interpolation.
Definition splines.h:17
constexpr real E
The Euler mathematical constant (e)
Definition constants.h:237
vec< real, N > bezier(const std::vector< vec< real, N > > &points, real t)
Generic Bezier curve in N dimensions.
Definition splines.h:190
vec< real, N > cubic_bezier(const vec< real, N > &P0, const vec< real, N > &P1, const vec< real, N > &P2, vec< real, N > P3, real t)
Cubic Bezier curve.
Definition splines.h:164
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition dual2_functions.h:223
vec< real, N > slerp(const vec< real, N > &P1, const vec< real, N > &P2, real t)
Spherical interpolation.
Definition splines.h:85
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54
real clamp(real x, real a, real b)
Clamp x between a and b.
Definition real_analysis.h:355
vec< real, N > quadratic_bezier(const vec< real, N > &P0, const vec< real, N > &P1, const vec< real, N > &P2, real t)
Quadratic Bezier curve.
Definition splines.h:154
A cubic splines node for a given x interval.
Definition splines.h:213
spline_node()
Default constructor.
Definition splines.h:223
real operator()(real X) const
Evaluate the interpolating cubic spline (no check on the input value is performed!...
Definition splines.h:232
real deriv(real X) const
Evaluate the derivative of the interpolating cubic spline (no check on the input value is performed)
Definition splines.h:240
real x
Upper extreme of the interpolation interval .
Definition splines.h:216
real a
Coefficients of the interpolating cubic spline .
Definition splines.h:220
spline_node(real x, real a, real b, real c, real d)
Construct from and polynomial coefficients.
Definition splines.h:226