source: trunk/src/CubicSplineInterpolator1D.tcc @ 2733

Last change on this file since 2733 was 2733, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: Yes CAS-4770

Ready for Test: Yes

Interface Changes: Yes/No?

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Redefined Interpolator1D and derived classes as template class.


File size: 2.9 KB
Line 
1//
2// C++ Implementation: CubicSplineInterpolator1D
3//
4// Description:
5//
6//
7// Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2012
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#include <assert.h>
13
14#include <iostream>
15using namespace std;
16
17#include "CubicSplineInterpolator1D.h"
18
19namespace asap {
20
21template <class T, class U>
22CubicSplineInterpolator1D<T, U>::CubicSplineInterpolator1D()
23  : Interpolator1D<T, U>(),
24    y2_(0),
25    ny2_(0),
26    reusable_(false)
27{}
28
29template <class T, class U>
30CubicSplineInterpolator1D<T, U>::~CubicSplineInterpolator1D()
31{
32  if (y2_)
33    delete[] y2_;
34}
35
36template <class T, class U>
37void CubicSplineInterpolator1D<T, U>::setData(T *x, U *y, unsigned int n)
38{
39  Interpolator1D<T, U>::setData(x, y, n);
40  reusable_ = false;
41}
42
43template <class T, class U>
44void CubicSplineInterpolator1D<T, U>::setY(U *y, unsigned int n)
45{
46  Interpolator1D<T, U>::setY(y, n);
47  reusable_ = false;
48}
49
50template <class T, class U>
51U CubicSplineInterpolator1D<T, U>::interpolate(T x)
52{
53  assert(this->isready());
54  if (this->n_ == 1)
55    return this->y_[0];
56
57  unsigned int i = this->locator_->locate(x);
58
59  // do not perform extrapolation
60  if (i == 0) {
61    return this->y_[i];
62  }
63  else if (i == this->n_) {
64    return this->y_[i-1];
65  }
66
67  // determine second derivative of each point
68  if (!reusable_) {
69    evaly2();
70    reusable_ = true;
71  }
72
73  // cubic spline interpolation
74  float y = dospline(x, i);
75  return y;
76}
77
78template <class T, class U>
79void CubicSplineInterpolator1D<T, U>::evaly2()
80{
81  if (this->n_ > ny2_) {
82    if (y2_)
83      delete[] y2_;
84    y2_ = new U[this->n_];
85    ny2_ = this->n_;
86  }
87
88  U *u = new U[ny2_-1];
89
90  // Natural cubic spline.
91  y2_[0] = 0.0;
92  y2_[ny2_-1] = 0.0;
93  u[0] = 0.0;
94
95  // Solve tridiagonal system.
96  // Here, tridiagonal matrix is decomposed to triangular matrix
97  // u stores upper triangular components while y2_ stores
98  // right-hand side vector.
99  T a1 = this->x_[1] - this->x_[0];
100  T a2, bi;
101  for (unsigned int i = 1; i < ny2_ - 1; i++) {
102    a2 = this->x_[i+1] - this->x_[i];
103    bi = 1.0 / (this->x_[i+1] - this->x_[i-1]);
104    y2_[i] = 3.0 * bi * ((this->y_[i+1] - this->y_[i]) / a2
105                         - (this->y_[i] - this->y_[i-1]) / a1
106                         - y2_[i-1] * 0.5 * a1);
107    a1 = 1.0 / (1.0 - u[i-1] * 0.5 * a1 * bi);
108    y2_[i] *= a1;
109    u[i] = 0.5 * a2 * bi * a1;
110    a1 = a2;
111  }
112 
113  // Then, solve the system by backsubstitution and store solution
114  // vector to y2_.
115  for (int k = ny2_ - 2; k >= 0; k--)
116    y2_[k] -= u[k] * y2_[k+1];
117
118  delete[] u;
119}
120
121template <class T, class U>
122U CubicSplineInterpolator1D<T, U>::dospline(T x, unsigned int i)
123{
124  unsigned int j = i - 1;
125  T h = this->x_[i] - this->x_[j];
126  T a = (this->x_[i] - x) / h;
127  T b = (x - this->x_[j]) / h;
128  U y = a * this->y_[j] + b * this->y_[i] +
129    ((a * a * a - a) * y2_[j] + (b * b * b - b) * y2_[i]) * (h * h) / 6.0;
130  return y;
131}
132
133}
Note: See TracBrowser for help on using the repository browser.