Changeset 2730 for trunk/src/CubicSplineInterpolator1D.cpp
- Timestamp:
- 01/16/13 16:00:28 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/CubicSplineInterpolator1D.cpp
r2727 r2730 32 32 } 33 33 34 void CubicSplineInterpolator1D::setData(double *x, float *y, unsigned int n) 35 { 36 Interpolator1D::setData(x, y, n); 37 reusable_ = false; 38 } 39 34 40 void CubicSplineInterpolator1D::setY(float *y, unsigned int n) 35 41 { … … 56 62 // determine second derivative of each point 57 63 if (!reusable_) { 58 spline();64 evaly2(); 59 65 reusable_ = true; 60 66 } 61 67 62 68 // cubic spline interpolation 63 float y = splint(x, i);69 float y = dospline(x, i); 64 70 return y; 65 71 } 66 72 67 void CubicSplineInterpolator1D:: spline()73 void CubicSplineInterpolator1D::evaly2() 68 74 { 69 75 if (n_ > ny2_) { … … 75 81 76 82 float *u = new float[ny2_-1]; 83 84 // Natural cubic spline. 77 85 y2_[0] = 0.0; 86 y2_[ny2_-1] = 0.0; 78 87 u[0] = 0.0; 79 88 89 // Solve tridiagonal system. 90 // Here, tridiagonal matrix is decomposed to triangular matrix 91 // u stores upper triangular components while y2_ stores 92 // right-hand side vector. 93 double a1 = x_[1] - x_[0]; 94 double a2, bi; 80 95 for (unsigned int i = 1; i < ny2_ - 1; i++) { 81 double sig = (x_[i] - x_[i-1]) / (x_[i+1] - x_[i-1]); 82 double p = sig * y2_[i-1] + 2.0; 83 y2_[i] = (sig - 1.0) / p; 84 u[i] = (y_[i+1] - y_[i]) / (x_[i+1] - x_[i]) 85 - (y_[i] - y_[i-1]) / (x_[i] - x_[i-1]); 86 u[i] = (6.0 * u[i] / (x_[i+1] - x_[i-1]) - sig * u[i-1]) / p; 96 a2 = x_[i+1] - x_[i]; 97 bi = 1.0 / (x_[i+1] - x_[i-1]); 98 y2_[i] = 3.0 * bi * ((y_[i+1] - y_[i]) / a2 - (y_[i] - y_[i-1]) / a1 99 - y2_[i-1] * 0.5 * a1); 100 a1 = 1.0 / (1.0 - u[i-1] * 0.5 * a1 * bi); 101 y2_[i] *= a1; 102 u[i] = 0.5 * a2 * bi * a1; 103 a1 = a2; 87 104 } 88 89 y2_[ny2_-1] = 0.0;90 105 106 // Then, solve the system by backsubstitution and store solution 107 // vector to y2_. 91 108 for (int k = ny2_ - 2; k >= 0; k--) 92 y2_[k] = y2_[k] * y2_[k+1] + u[k];109 y2_[k] -= u[k] * y2_[k+1]; 93 110 94 111 delete[] u; 95 112 } 96 113 97 float CubicSplineInterpolator1D:: splint(double x, unsigned int i)114 float CubicSplineInterpolator1D::dospline(double x, unsigned int i) 98 115 { 99 116 unsigned int j = i - 1;
Note: See TracChangeset
for help on using the changeset viewer.