Changeset 2730 for trunk/src/PolynomialInterpolator1D.cpp
- Timestamp:
- 01/16/13 16:00:28 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/PolynomialInterpolator1D.cpp
r2727 r2730 12 12 #include <assert.h> 13 13 #include <math.h> 14 #include <iostream> 15 using namespace std; 16 17 #include <casa/Exceptions/Error.h> 14 18 15 19 #include "PolynomialInterpolator1D.h" … … 43 47 float y; 44 48 if (order_ >= n_ - 1) { 45 y = polint(x, i, 0, n_); 49 // use full region 50 y = dopoly(x, 0, n_); 46 51 } 47 52 else { 53 // use partial region 48 54 int j = i - 1 - order_ / 2; 49 55 unsigned int m = n_ - 1 - order_; 50 56 unsigned int k = (unsigned int)((j > 0) ? j : 0); 51 57 k = ((k > m) ? m : k); 52 y = polint(x, i, k, order_ + 1);58 y = dopoly(x, k, order_ + 1); 53 59 } 54 60 … … 56 62 } 57 63 58 float PolynomialInterpolator1D:: polint(double x, unsigned int loc,59 unsigned int left, unsigned intn)64 float PolynomialInterpolator1D::dopoly(double x, unsigned int left, 65 unsigned int n) 60 66 { 61 int ns = loc - left;62 if (fabs(x - x_[loc]) >= fabs(x - x_[loc-1])) {63 ns--;64 }65 66 67 double *xa = &x_[left]; 67 68 float *ya = &y_[left]; 68 69 69 float y = ya[ns]; 70 71 // c stores C11, C21, C31, ..., C[n-1]1 72 // d stores D11, D21, D31, ..., D[n-1]1 70 // storage for C and D in Neville's algorithm 73 71 float *c = new float[n]; 74 72 float *d = new float[n]; … … 78 76 } 79 77 78 // Neville's algorithm 79 float y = c[0]; 80 80 for (unsigned int m = 1; m < n; m++) { 81 // Evaluate Cm1, Cm2, Cm3, ... Cm[n-m] and Dm1, Dm2, Dm3, ... Dm[n-m]. 82 // Those are stored to c[0], c[1], ..., c[n-m-1] and d[0], d[1], ..., 83 // d[n-m-1]. 81 84 for (unsigned int i = 0; i < n - m; i++) { 82 double ho = xa[i] - x; 83 double hp = xa[i+m] - x; 84 float w = c[i+1] - d[i]; 85 double denom = ho - hp; 86 if (denom == 0.0) { 85 float cd = c[i+1] - d[i]; 86 double dx = xa[i] - xa[i+m]; 87 try { 88 cd /= dx; 89 } 90 catch (...) { 87 91 delete[] c; 88 92 delete[] d; 89 assert(denom != 0.0);93 throw casa::AipsError("x_ has duplicate elements"); 90 94 } 91 denom = w / denom; 92 93 d[i] = hp * denom; 94 c[i] = ho * denom; 95 c[i] = (xa[i] - x) * cd; 96 d[i] = (xa[i+m] - x) * cd; 95 97 } 96 98 97 float dy; 98 if (2 * ns < (int)(n - m)) { 99 dy = c[ns]; 100 } 101 else { 102 dy = d[ns-1]; 103 ns--; 104 } 105 y += dy; 99 // In each step, c[0] holds Cm1 which is a correction between 100 // P12...m and P12...[m+1]. Thus, the following repeated update 101 // corresponds to the route P1 -> P12 -> P123 ->...-> P123...n. 102 y += c[0]; 106 103 } 107 104 108 105 delete[] c; 109 106 delete[] d;
Note: See TracChangeset
for help on using the changeset viewer.