hermite  0.0.1
cubic_impl.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <vector>
10 
11 namespace hermite {
25 inline void spline(const double t[], const double y[], const int n,
26  const double yp1, const double ypn, double y2[]) {
27  int i, k;
28  double p, qn, sig, un;
29  std::vector<double> u;
30  u.resize(n + 2);
31 
32  if (yp1 > 0.99e30) {
33  // y2[1] = u[1] = 0.0;
34  y2[0] = u[0] = 0.0;
35  } else {
36  y2[0] = -0.5;
37  u[0] = (3.0 / (t[1] - t[0])) * ((y[1] - y[0]) / (t[1] - t[0]) - yp1);
38  }
39 
40  for (i = 1; i <= n - 2; i++) {
41  sig = (t[i] - t[i - 1]) / (t[i + 1] - t[i - 1]);
42  p = sig * y2[i - 1] + 2.0;
43  y2[i] = (sig - 1.0) / p;
44  u[i] = (y[i + 1] - y[i]) / (t[i + 1] - t[i]) -
45  (y[i] - y[i - 1]) / (t[i] - t[i - 1]);
46  u[i] = (6.0 * u[i] / (t[i + 1] - t[i - 1]) - sig * u[i - 1]) / p;
47  }
48 
49  if (ypn > 0.99e30) {
50  qn = un = 0.0;
51  } else {
52  qn = 0.5;
53  un = (3.0 / (t[n - 1] - t[n - 2])) *
54  (ypn - (y[n - 1] - y[n - 2]) / (t[n - 1] - t[n - 2]));
55  }
56  y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
57 
58  for (k = n - 2; k >= 0; k--) {
59  y2[k] = y2[k] * y2[k + 1] + u[k];
60  }
61 }
62 
78 inline bool splpos(const double xa[], const double ya[], const double y2a[],
79  const int n, const double x, double *y) {
80  int klo, khi, k;
81  double h, b, a;
82 
83  klo = 0;
84  khi = n - 1;
85  while (khi - klo > 1) {
86  k = (khi + klo) >> 1;
87  if (xa[k] > x) {
88  khi = k;
89  } else {
90  klo = k;
91  }
92  }
93 
94  h = xa[khi] - xa[klo];
95  if (h == 0.0) {
96  return false;
97  }
98 
99  a = (xa[khi] - x) / h;
100  b = (x - xa[klo]) / h;
101  *y =
102  a * ya[klo] + b * ya[khi] +
103  ((a * a * a - a) * y2a[klo] + (b * b * b - b) * y2a[khi]) * (h * h) / 6.0;
104  return true;
105 }
106 
122 inline bool splvel(const double xa[], const double ya[], const double y2a[],
123  const int n, const double x, double *y) {
124  int klo, khi, k;
125  double h, b, a;
126 
127  klo = 0;
128  khi = n - 1;
129  while (khi - klo > 1) {
130  k = (khi + klo) >> 1;
131  if (xa[k] > x) {
132  khi = k;
133  } else {
134  klo = k;
135  }
136  }
137 
138  h = xa[khi] - xa[klo];
139  if (h == 0.0) {
140  return false;
141  }
142 
143  a = (xa[khi] - x) / h;
144  b = (x - xa[klo]) / h;
145  *y = (-1 / h) * ya[klo] + (1 / h) * ya[khi] +
146  ((3 * a * a - 1) * y2a[klo] * (-1 / h) +
147  (3 * b * b - 1) * y2a[khi] * (1 / h)) *
148  (h * h) / 6.0;
149  return true;
150 }
151 
167 inline bool splacc(const double xa[], const double ya[], const double y2a[],
168  const int n, const double x, double *y) {
169  int klo, khi, k;
170  double h, b, a;
171 
172  klo = 0;
173  khi = n - 1;
174  while (khi - klo > 1) {
175  k = (khi + klo) >> 1;
176  if (xa[k] > x) {
177  khi = k;
178  } else {
179  klo = k;
180  }
181  }
182 
183  h = xa[khi] - xa[klo];
184  if (h == 0.0) {
185  return false;
186  }
187 
188  (void)ya;
189 
190  a = (xa[khi] - x) / h;
191  b = (x - xa[klo]) / h;
192  *y = y2a[klo] * a + y2a[khi] * b;
193  return true;
194 }
195 } // namespace hermite
bool splacc(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates acceleration of spline.
Definition: cubic_impl.hpp:167
bool splvel(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates velocity of spline.
Definition: cubic_impl.hpp:122
void spline(const double t[], const double y[], const int n, const double yp1, const double ypn, double y2[])
Calculates second derivatives at tabulated points.
Definition: cubic_impl.hpp:25
bool splpos(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates position of spline.
Definition: cubic_impl.hpp:78