18 template <
class T,
class VECTOR>
19 T
interpolate(
const T&
x,
const VECTOR& ys,
const T& x0,
const T& x1)
24 const size_t N = ys.size();
25 if (
x <= x0)
return ys[0];
26 if (
x >= x1)
return ys[N - 1];
27 const T Ax = (x1 - x0) / T(N);
28 const size_t i = int((
x - x0) / Ax);
29 if (i >= N - 1)
return ys[N - 1];
30 const T Ay = ys[i + 1] - ys[i];
31 return ys[i] + (
x - (x0 + i * Ax)) * Ay / Ax;
35 template <
typename NUMTYPE,
class VECTORLIKE>
37 const NUMTYPE
t,
const VECTORLIKE&
x,
const VECTORLIKE&
y,
bool wrap2pi)
45 for (
unsigned int i = 0; i < 3; i++) h[i] =
x[i + 1] -
x[i];
47 double k = 1 / (4 * h[0] * h[1] + 4 * h[0] * h[2] + 3 * h[1] * h[1] +
49 double a11 = 2 * (h[1] + h[2]) * k;
50 double a12 = -h[1] * k;
51 double a22 = 2 * (h[0] + h[1]) * k;
53 double y0, y1, y2, y3;
91 double b1 = (y2 - y1) / h[1] - (y1 - y0) / h[0];
92 double b2 = (y3 - y2) / h[2] - (y2 - y1) / h[1];
95 double z1 = 6 * (a11 *
b1 + a12 *
b2);
96 double z2 = 6 * (a12 *
b1 + a22 *
b2);
101 res = (z1 * pow((
t -
x[0]), 3) + z0 * pow((
x[1] -
t), 3)) / (6 * h[0]) +
102 (y1 / h[0] - h[0] / 6 * z1) * (
t -
x[0]) +
103 (y0 / h[0] - h[0] / 6 * z0) * (
x[1] -
t);
107 res = (z2 * pow((
t -
x[1]), 3) + z1 * pow((
x[2] -
t), 3)) /
109 (y2 / h[1] - h[1] / 6 * z2) * (
t -
x[1]) +
110 (y1 / h[1] - h[1] / 6 * z1) * (
x[2] -
t);
112 res = (z3 * pow((
t -
x[2]), 3) + z2 * pow((
x[3] -
t), 3)) /
114 (y3 / h[2] - h[2] / 6 * z3) * (
t -
x[2]) +
115 (y2 / h[2] - h[2] / 6 * z2) * (
x[3] -
t);
120 template <
typename NUMTYPE,
class VECTORLIKE,
int NUM_POINTS>
122 const NUMTYPE
t,
const VECTORLIKE&
x,
const VECTORLIKE&
y,
bool wrap2pi)
130 const size_t N =
x.size();
133 const NUMTYPE x_min =
x.minimum();
134 Eigen::Matrix<NUMTYPE, 2, NUM_POINTS> Xt;
136 for (
size_t i = 0; i < N; i++)
138 Xt.set_unsafe(0, i, 1);
139 Xt.set_unsafe(1, i,
x[i] - x_min);
142 const auto B = ((Xt * Xt.transpose()).
inv().eval() * Xt *
y).eval();
145 NUMTYPE ret = B[0] + B[1] * (
t - x_min);
157 class VECTORLIKE1,
class VECTORLIKE2,
class VECTORLIKE3,
int NUM_POINTS>
159 const VECTORLIKE1& ts, VECTORLIKE2& outs,
const VECTORLIKE3&
x,
160 const VECTORLIKE3&
y,
bool wrap2pi)
168 const size_t N =
x.size();
171 using NUM =
typename VECTORLIKE3::value_type;
172 const NUM x_min =
x.minimum();
173 Eigen::Matrix<NUM, 2, NUM_POINTS> Xt;
175 for (
size_t i = 0; i < N; i++)
177 Xt.set_unsafe(0, i, 1);
178 Xt.set_unsafe(1, i,
x[i] - x_min);
181 const auto B = ((Xt * Xt.transpose()).
inv().eval() * Xt *
y).eval();
184 const size_t tsN = size_t(ts.size());
187 for (
size_t k = 0; k < tsN; k++)
188 outs[k] = B[0] + B[1] * (ts[k] - x_min);
190 for (
size_t k = 0; k < tsN; k++)