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>
33 return (value -
x1) / (
x2 -
x1);
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>
194 :
x(
x),
a(
a), b(b), c(c), d(d) {}
201 return a +
h * (b +
h * (c +
h * d));
209 return b +
h * (c * 2 +
h * d * 3);
221 template<
typename Dataset1,
typename Dataset2>
230 if(x.size() !=
y.size()) {
244 for (
size_t i = 0; i <
n_nodes; ++i) {
246 if(x[i + 1] <= x[i]) {
265 for (
size_t i = 0; i <
n_nodes; ++i)
266 h[i] = x[i + 1] - x[i];
283 for (
size_t i = 1; i <
n_nodes; ++i) {
285 (
y[i + 1] -
y[i]) /
h[i] -
286 (
y[i] -
y[i - 1]) /
h[i - 1]
305 for (
size_t i = 1; i <
n_nodes; ++i)
306 diag[i] = 2.0 * (
h[i - 1] +
h[i]);
309 for (
size_t i = 1; i <
n_nodes; ++i) {
316 for (
int i =
int(
n_nodes) - 1; i >= 0; --i) {
327 std::vector<spline_node> nodes (
n_nodes);
328 for (
size_t i = 0; i <
n_nodes; ++i) {
332 const real b = (
y[i + 1] -
y[i]) /
h[i] -
h[i] * (2.0 *
m[i] +
m[i + 1]) / 6.0;
333 const real c =
m[i] * 0.5;
334 const real d = (
m[i + 1] -
m[i]) / (6.0 *
h[i]);
349 template <
typename DataPo
ints = std::vector<vec2>>
359 inline real operator[](
size_t i)
const {
return points[i][
j]; }
360 inline size_t size()
const {
return points.size(); }
381 template<
typename DataPo
ints = std::vector<vec2>>
391 template<
typename Dataset1,
typename Dataset2>
408 for (
int i =
int(
nodes.size()) - 1; i > 0; --i)
421 for (
unsigned int i =
nodes.size() - 1; i > 0; --i)
423 return nodes[i].deriv(x);
426 return nodes[0].deriv(x);
432 return nodes.begin();
representing a ratio between two objects, like a fraction or a rational polynomial.
Definition ratio.h:25
A natural cubic spline interpolation class.
Definition splines.h:369
spline(const Dataset1 &X, const Dataset2 &Y)
Construct the natural cubic spline interpolation from the sets of x[i] and y[i] data points.
Definition splines.h:392
real operator()(real x) const
Evaluate the natural cubic spline interpolation at a given point.
Definition splines.h:406
real deriv(real x) const
Evaluate the derivative of the natural cubic spline interpolation at a given point.
Definition splines.h:419
auto end()
Get an iterator to one plus the last spline element.
Definition splines.h:437
auto begin()
Get an iterator to the first spline element.
Definition splines.h:431
spline(const DataPoints &p)
Construct the natural cubic spline interpolation from a vector of coordinate pairs.
Definition splines.h:382
spline & operator=(const spline &other)
Copy from another spline.
Definition splines.h:398
std::vector< spline_node > nodes
The computed nodes of the natural cubic spline interpolation over the points.
Definition splines.h:374
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition vec.h:313
vec< Type, N > normalized() const
Return the normalized vector (v / |v|)
Definition vec.h:421
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
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:207
real smootherstep(real x1, real x2, real interp)
Smootherstep interpolation.
Definition splines.h:134
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
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
vec< real, N > bezier_quadratic(const vec< real, N > &P0, const vec< real, N > &P1, const vec< real, N > &P2, real t)
Quadratic Bezier curve.
Definition splines.h:154
real invlerp(real x1, real x2, real value)
Inverse linear interpolation.
Definition splines.h:32
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ InvalidArgument
Invalid argument.
@ OutOfDomain
Argument out of domain.
@ ImpossibleOperation
Mathematically impossible operation.
@ DivByZero
Division by zero.
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:246
vec< real, N > bezier_cubic(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:267
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
std::vector< spline_node > splines_cubic(const Dataset1 &x, const Dataset2 &y)
Compute the cubic splines interpolation of a set of data points using an optimized Thomas algorithm,...
Definition splines.h:222
real clamp(real x, real a, real b)
Clamp x between a and b.
Definition real_analysis.h:355
A cubic splines node for a given x interval.
Definition splines.h:180
spline_node()
Default constructor.
Definition splines.h:190
real operator()(real X) const
Evaluate the interpolating cubic spline (no check on the input value is performed!...
Definition splines.h:199
real deriv(real X) const
Evaluate the derivative of the interpolating cubic spline (no check on the input value is performed)
Definition splines.h:207
real x
Upper extreme of the interpolation interval .
Definition splines.h:183
real a
Coefficients of the interpolating cubic spline .
Definition splines.h:187
spline_node(real x, real a, real b, real c, real d)
Construct from and polynomial coefficients.
Definition splines.h:193