1#include <NDEVR/Angle.h>
2#include <NDEVR/Buffer.h>
3#include <NDEVR/Vertex.h>
4#include <NDEVR/BandMatrix.h>
5#include <NDEVR/SplineBoundaryType.h>
8 template<
class t_po
int_type>
38 ,
fltp08 right_value = 0.0)
47 for (
uint04 i = 0; i <
Y.size(); i++)
54 ,
bool make_monotonic =
false
58 ,
fltp08 right_value = 0.0)
75 lib_assert(x.
size() >= 4,
"not-a-knot with 3 points has multiple solutions");
81 for (
uint04 i = 0; i < n - 1; i++)
91 for (
uint04 i = 0; i < n - 1; i++)
114 for (
uint04 i = 1; i < n - 1; i++)
116 A(i, i - 1) = 1.0 / 3.0 * (x[i] - x[i - 1]);
117 A(i, i) = 2.0 / 3.0 * (x[i + 1] - x[i - 1]);
118 A(i, i + 1) = 1.0 / 3.0 * (x[i + 1] - x[i]);
119 rhs[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]);
133 A(0, 0) = 2.0 * (x[1] - x[0]);
134 A(0, 1) = 1.0 * (x[1] - x[0]);
135 rhs[0] = 3.0 * ((y[1] - y[0]) / (x[1] - x[0]) -
m_left_value);
141 A(0, 0) = -(x[2] - x[1]);
142 A(0, 1) = x[2] - x[0];
143 A(0, 2) = -(x[1] - x[0]);
153 A(n - 1, n - 1) = 2.0;
154 A(n - 1, n - 2) = 0.0;
162 A(n - 1, n - 1) = 2.0 * (x[n - 1] - x[n - 2]);
163 A(n - 1, n - 2) = 1.0 * (x[n - 1] - x[n - 2]);
164 rhs[n - 1] = 3.0 * (
m_right_value - (y[n - 1] - y[n - 2]) / (x[n - 1] - x[n - 2]));
170 A(n - 1, n - 3) = -(x[n - 1] - x[n - 2]);
171 A(n - 1, n - 2) = x[n - 1] - x[n - 3];
172 A(n - 1, n - 1) = -(x[n - 2] - x[n - 3]);
181 m_c =
A.luSolve(rhs);
186 for (
uint04 i = 0; i < n - 1; i++)
188 m_d[i] = 1.0 / 3.0 * (
m_c[i + 1] -
m_c[i]) / (x[i + 1] - x[i]);
189 m_b[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i])
190 - 1.0 / 3.0 * (2.0 *
m_c[i] +
m_c[i + 1]) * (x[i + 1] - x[i]);
194 fltp08 h = x[n - 1] - x[n - 2];
197 m_b[n - 1] = 3.0 *
m_d[n - 2] * h * h + 2.0 *
m_c[n - 2] * h +
m_b[n - 2];
211 for (
uint04 i = 1; i < n - 1; i++)
215 m_b[i] = -h / (hl * (hl + h)) *
m_y[i - 1] + (h - hl) / (hl * h) *
m_y[i]
216 + hl / (h * (hl + h)) *
m_y[i + 1];
234 + h0 * h0 / (h1 * h1) * (
m_b[1] +
m_b[2] - 2.0 * (
m_y[2] -
m_y[1]) / h1);
256 m_b[n - 1] = -
m_b[n - 2] + 2.0 * (
m_y[n - 1] -
m_y[n - 2]) / h1 + h1 * h1 / (h0 * h0)
257 * (
m_b[n - 3] +
m_b[n - 2] - 2.0 * (
m_y[n - 2] -
m_y[n - 3]) / h0);
259 m_c[n - 1] = (
m_b[n - 2] + 2.0 *
m_b[n - 1]) / h1 - 3.0 * (
m_y[n - 1] -
m_y[n - 2]) / (h1 * h1);
285 bool modified =
false;
289 for (
uint04 i = 0; i < n; i++)
302 for (
uint04 i = 0; i < n - 1; i++)
305 for (
uint01 n = 0; n < t_point_type::NumberOfDimensions(); n++)
308 if (avg == 0.0 && (
m_b[i][n] != 0.0 ||
m_b[i + 1][n] != 0.0)) {
313 else if ((
m_b[i][n] >= 0.0 &&
m_b[i + 1][n] >= 0.0 && avg > 0.0) ||
314 (
m_b[i][n] <= 0.0 &&
m_b[i + 1][n] <= 0.0 && avg < 0.0))
323 m_b[i][n] *= (3.0 / r);
324 m_b[i + 1][n] *= (3.0 / r);
356 t_point_type interpol;
362 else if (x >
m_x[n - 1])
365 interpol = (
m_c[n - 1] * h +
m_b[n - 1]) * h +
m_y[n - 1];
370 interpol = ((
m_d[idx] * h +
m_c[idx]) * h +
m_b[idx]) * h +
m_y[idx];
382 t_point_type interpol;
389 interpol = 2.0 *
m_c0 * h +
m_b[0];
392 interpol = 2.0 *
m_c0;
399 else if (x >
m_x[n - 1])
405 interpol = 2.0 *
m_c[n - 1] * h +
m_b[n - 1];
408 interpol = 2.0 *
m_c[n - 1];
421 interpol = (3.0 *
m_d[idx] * h + 2.0 *
m_c[idx]) * h +
m_b[idx];
424 interpol = 6.0 *
m_d[idx] * h + 2.0 *
m_c[idx];
427 interpol = 6.0 *
m_d[idx];
444 if (ignore_extrapolation ==
false)
456 for (
uint04 i = 0; i < n - 1; i++)
463 if ((-eps <= root[j]) && (root[j] <
m_x[i + 1] -
m_x[i]))
465 t_point_type new_root =
m_x[i] + root[j];
466 if (x.
size() > 0 && x.
last() + eps > new_root)
479 if (!ignore_extrapolation)
485 x.
add(
m_x[n - 1] + root[j]);
496 return 2.2204460492503131e-16;
529 , t_point_type b, t_point_type c
530 ,
int newton_iter = 0)
535 const t_point_type p = 0.5 * b / c;
536 const t_point_type q = a / c;
537 const t_point_type discr = p * p - q;
539 const t_point_type discr_err = (6.0 * (p * p) + 3.0 *
abs(q) +
abs(discr)) * eps;
542 if (
abs(discr) <= discr_err)
548 else if (discr < 0.0) {
554 for (
uint01 n = 0; n < t_point_type::NumberOfDimensions(); n++)
556 x[0][n] = -p[n] -
sqrt(discr[n]);
557 x[1][n] = -p[n] +
sqrt(discr[n]);
564 for (
int k = 0; k < newton_iter; k++)
566 t_point_type f = (c * x[i] + b) * x[i] + a;
567 t_point_type f1 = 2.0 * c * x[i] + b;
585 , t_point_type b, t_point_type c
586 , t_point_type d,
int newton_iter)
const
600 const t_point_type p = -(1.0 / 3.0) * b + (1.0 / 9.0) * (c * c);
601 const t_point_type r = 2.0 * (c * c) - 9.0 * b;
602 const t_point_type q = -0.5 * a - (1.0 / 54.0) * (c * r);
603 const t_point_type discr = p * p * p - q * q;
605 t_point_type p_err = eps * ((3.0 / 3.0) *
abs(b)
606 + (4.0 / 9.0) * (c * c)
608 t_point_type r_err = eps * (6.0 * (c * c)
611 t_point_type q_err = 0.5 *
abs(a) * eps
612 + (1.0 / 54.0) *
abs(c) * (r_err +
abs(r) * 3.0 * eps)
614 t_point_type discr_err = (p * p) * (3.0 * p_err
615 +
abs(p) * 2.0 * eps)
616 +
abs(q) * (2.0 * q_err
622 if (
abs(discr) <= discr_err)
638 else if (discr > Vertex < 3, fltp08>(0))
642 for (
uint01 n = 0; n < t_point_type::NumberOfDimensions(); n++)
644 fltp08 ac = (1.0 / 3.0) * acos(q[n] / (p[n] *
sqrt(p[n])));
646 z[0][n] = sq *
cos(ac);
647 z[1][n] = sq *
cos(ac - 2.0 * PI<fltp08>() / 3.0);
648 z[2][n] = sq *
cos(ac - 4.0 * PI<fltp08>() / 3.0);
651 else if (discr < 0.0)
655 for (
uint01 n = 0; n < t_point_type::NumberOfDimensions(); n++)
657 fltp08 sgnq = (q[n] >= 0 ? 1 : -1);
659 fltp08 C = sgnq * pow(basis, 1.0 / 3.0);
660 z[0][n] =
C + p[n] /
C;
666 z[i] -= (1.0 / 3.0) * c;
668 for (
int k = 0; k < newton_iter; k++)
670 t_point_type f = ((z[i] + c) * z[i] + b) * z[i] + a;
671 t_point_type f1 = (3.0 * z[i] + 2.0 * c) * z[i] + b;
681 lib_assert(z.
size() > 0,
"cubic should always have at least one root");
682 t_point_type xmin =
abs(z[0]);
686 if (xmin >
abs(z[i]))
717 for (
uint04 i = 0; i < n - 1; i++)
721 m_c[i] = (3.0 * (
m_y[i + 1] -
m_y[i]) / h - (2.0 *
m_b[i] +
m_b[i + 1])) / h;
723 m_d[i] = ((
m_b[i + 1] -
m_b[i]) / (3.0 * h) - 2.0 / 3.0 *
m_c[i]) / h;
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
Band Matrix solver.
Definition BandMatrix.hpp:13
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
void add(t_type &&object)
Definition Buffer.hpp:199
constexpr t_index_type size() const
Definition Buffer.hpp:1461
decltype(auto) last()
Definition Buffer.hpp:977
decltype(auto) end()
Definition Buffer.hpp:746
void setSize(t_index_type new_size)
Definition Buffer.hpp:1413
decltype(auto) begin()
Definition Buffer.hpp:504
void resize(t_index_type new_size)
Definition Buffer.hpp:1423
Spline(const Buffer< t_point_type > &Y, SplineType type=SplineType::e_cspline, SplineBoundaryType left=SplineBoundaryType::e_second_deriv, fltp08 left_value=0.0, SplineBoundaryType right=SplineBoundaryType::e_second_deriv, fltp08 right_value=0.0)
Definition Spline.hpp:33
static fltp08 getEPS()
Definition Spline.hpp:494
SplineBoundaryType m_right
Definition Spline.hpp:23
fltp08 m_left_value
Definition Spline.hpp:24
Buffer< t_point_type > solveCubic(t_point_type a, t_point_type b, t_point_type c, t_point_type d, int newton_iter) const
Definition Spline.hpp:584
Buffer< t_point_type > m_c
Definition Spline.hpp:18
t_point_type deriv(uint04 order, fltp08 x) const
Definition Spline.hpp:375
fltp08 m_right_value
Definition Spline.hpp:25
void setCoeffsFromB()
Definition Spline.hpp:706
SplineType m_type
Definition Spline.hpp:21
t_point_type operator()(fltp08 x) const
Definition Spline.hpp:345
static Buffer< t_point_type > solveQuadratic(t_point_type a, t_point_type b, t_point_type c, int newton_iter=0)
Definition Spline.hpp:528
bool makeMonotonic()
Definition Spline.hpp:280
void setPoints(const Buffer< fltp08 > &x, const Buffer< t_point_type > &y, SplineType type)
Definition Spline.hpp:70
Spline(const Buffer< fltp08 > &X, const Buffer< t_point_type > &Y, SplineType type=SplineType::e_cspline, bool make_monotonic=false, SplineBoundaryType left=SplineBoundaryType::e_second_deriv, fltp08 left_value=0.0, SplineBoundaryType right=SplineBoundaryType::e_second_deriv, fltp08 right_value=0.0)
Definition Spline.hpp:51
SplineBoundaryType m_left
Definition Spline.hpp:22
Buffer< t_point_type > m_d
Definition Spline.hpp:19
void setBoundary(SplineBoundaryType left, fltp08 left_value, SplineBoundaryType right, fltp08 right_value)
Definition Spline.hpp:697
Spline()
Definition Spline.hpp:30
Buffer< t_point_type > m_y
Definition Spline.hpp:13
uint04 find_closest(fltp08 x) const
Definition Spline.hpp:339
Buffer< t_point_type > solve(t_point_type y, bool ignore_extrapolation) const
Definition Spline.hpp:437
bool m_made_monotonic
Definition Spline.hpp:26
Buffer< t_point_type > m_b
Definition Spline.hpp:17
static Buffer< t_point_type > solveLinear(t_point_type a, t_point_type b)
Definition Spline.hpp:500
t_point_type m_c0
Definition Spline.hpp:20
Buffer< fltp08 > m_x
Definition Spline.hpp:12
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:116
SplineType
Definition SplineEnums.h:7
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type cos(const Angle< t_type > &angle)
Definition AngleFunctions.h:154
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1309
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Definition AngleFunctions.h:750
@ A
Definition BaseValues.hpp:201
@ Y
Definition BaseValues.hpp:202
@ X
Definition BaseValues.hpp:200
@ C
Definition BaseValues.hpp:205
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
SplineBoundaryType
Definition SplineEnums.h:15
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67