Skip to content

Commit 6581596

Browse files
authored
Merge pull request #29 from jamiescottie1/master
Fix CubicSplineInterpolator integration function
2 parents ecaab1c + b619ab7 commit 6581596

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/libInterpolate/Interpolators/_1D/CubicSplineInterpolator.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Real CubicSplineInterpolator<Real>::integral(Real _a, Real _b) const {
155155
* I = (x_2 - x_1) \int_t_a^t_b ( 1 - t )*y_1 + t*y_2 + t*( 1 - t )( a*(1 -
156156
* t) + b*t ) dt
157157
*
158-
* = (x_2 - x_1) [ ( t - t^2/2 )*y_1 + t^2/2*y_2 + a*(t^2 - 2*t^3/3 +
158+
* = (x_2 - x_1) [ ( t - t^2/2 )*y_1 + t^2/2*y_2 + a*(t^2/2 - 2*t^3/3 +
159159
* t^4/4) + b*(t^3/3 - t^4/4) ] |_t_a^t_b
160160
*
161161
* if we integrate over the entire element, i.e. x -> [x1,x2], then we will
@@ -166,7 +166,7 @@ Real CubicSplineInterpolator<Real>::integral(Real _a, Real _b) const {
166166
*
167167
*/
168168

169-
Real x_1, x_2, t;
169+
Real x_1, x_2, t, t2, t3, t4;
170170
Real y_1, y_2;
171171
Real sum = 0;
172172
for (int i = ai; i < bi - 1; i++) {
@@ -198,13 +198,16 @@ Real CubicSplineInterpolator<Real>::integral(Real _a, Real _b) const {
198198
y_1 = Y[bi - 1];
199199
y_2 = Y[bi];
200200
t = (_b - x_1) / (x_2 - x_1);
201+
t2 = t*t;
202+
t3 = t2*t;
203+
t4 = t2*t2;
201204

202205
// adding area between x_1 and _b
203206
sum += static_cast<Real>(
204207
(x_2 - x_1) *
205-
((t - pow(t, 2) / 2) * y_1 + pow(t, 2) / 2. * y_2 +
206-
a[bi - 1] * (pow(t, 2) - 2. * pow(t, 3) / 3. + pow(t, 4) / 4.) +
207-
b[bi - 1] * (pow(t, 3) / 3. - pow(t, 4) / 4.)));
208+
((t - 0.5 * t2) * y_1 + 0.5 * t2 * y_2 +
209+
a[bi - 1] * (0.5 * t2 - 2. * t3 / 3. + 0.25 * t4) +
210+
b[bi - 1] * (t3 / 3. - 0.25 * t4)));
208211

209212
//
210213
// [_a,X(0)]
@@ -217,13 +220,16 @@ Real CubicSplineInterpolator<Real>::integral(Real _a, Real _b) const {
217220
y_1 = Y[ai - 1];
218221
y_2 = Y[ai];
219222
t = (_a - x_1) / (x_2 - x_1);
223+
t2 = t*t;
224+
t3 = t2*t;
225+
t4 = t2*t2;
220226

221227
// subtracting area from x_1 to _a
222228
sum -= static_cast<Real>(
223229
(x_2 - x_1) *
224-
((t - pow(t, 2) / 2) * y_1 + pow(t, 2) / 2. * y_2 +
225-
a[ai - 1] * (pow(t, 2) - 2. * pow(t, 3) / 3. + pow(t, 4) / 4.) +
226-
b[ai - 1] * (pow(t, 3) / 3. - pow(t, 4) / 4.)));
230+
((t - 0.5 * t2) * y_1 + 0.5 * t2 * y_2 +
231+
a[ai - 1] * (0.5 * t2 - 2. * t3 / 3. + 0.25 * t4) +
232+
b[ai - 1] * (t3 / 3. - 0.25 * t4)));
227233

228234
if (ai != bi) // _a and _b are not in the in the same element, need to add
229235
// area of element containing _a

0 commit comments

Comments
 (0)