00001
00002 #ifndef _POLYINTERP_H_
00003 #define _POLYINTERP_H_
00004
00005 #include <stdio.h>
00006
00025
00026
00032 class polyinterp {
00033
00034 private:
00035 double fourPointTable[4][4];
00036 double twoPointTable[4][4];
00037
00050 void lagrange( double x, int N, double c[] )
00051 {
00052 double num, denom;
00053
00054 for (int i = 0; i < N; i++) {
00055 num = 1;
00056 denom = 1;
00057 for (int k = 0; k < N; k++) {
00058 if (i != k) {
00059 num = num * (x - k);
00060 denom = denom * (i - k);
00061 }
00062 }
00063 c[i] = num / denom;
00064 }
00065 }
00066
00067
00073 void fillTable( const int N, double table[4][4] )
00074 {
00075 double x;
00076 double n = N;
00077 int i = 0;
00078
00079 for (x = 0.5; x < n; x = x + 1.0) {
00080 lagrange( x, N, table[i] );
00081 i++;
00082 }
00083 }
00084
00085
00089 void printTable( double table[4][4], int N)
00090 {
00091 printf("%d-point interpolation table:\n", N);
00092 double x = 0.5;
00093 for (int i = 0; i < N; i++) {
00094 printf("%4.2f: ", x);
00095 for (int j = 0; j < N; j++) {
00096 printf("%6.4f", table[i][j] );
00097 if (j < N-1)
00098 printf(", ");
00099 }
00100 printf("\n");
00101 x = x + 1.0;
00102 }
00103 }
00104
00105
00110 void printTables()
00111 {
00112 printTable( fourPointTable, 4 );
00113 printTable( twoPointTable, 2 );
00114 printf("\n");
00115 }
00116
00117
00127 void getCoef( double x, int n, double c[])
00128 {
00129 int j = (int)x;
00130
00131 if (j < 0 || j >= n) {
00132 printf("poly::getCoef: n = %d, bad x value = %f\n", n, x);
00133 }
00134
00135 if (n == 2) {
00136 c[2] = 0.0;
00137 c[3] = 0.0;
00138 }
00139 else if (n != 4) {
00140 printf("poly::getCoef: bad value for N\n");
00141 return;
00142 }
00143
00144 for (int i = 0; i < n; i++) {
00145 if (n == 4) {
00146 c[i] = fourPointTable[j][i];
00147 }
00148 else {
00149 c[i] = twoPointTable[j][i];
00150 }
00151 }
00152
00153 }
00154
00155
00156 public:
00157
00163 polyinterp()
00164 {
00165
00166
00167 fillTable( 4, fourPointTable );
00168
00169
00170
00171 fillTable( 2, twoPointTable );
00172 }
00173
00174
00187 double interpPoint( double x, int N, double d[])
00188 {
00189 double c[4];
00190 double point = 0;
00191
00192 int n = 4;
00193 if (N < 4)
00194 n = N;
00195
00196 getCoef( x, n, c );
00197
00198 if (n == 4) {
00199 point = c[0]*d[0] + c[1]*d[1] + c[2]*d[2] + c[3]*d[3];
00200 }
00201 else if (n == 2) {
00202 point = c[0]*d[0] + c[1]*d[1];
00203 }
00204
00205 return point;
00206 }
00207
00208 };
00209
00210
00211 #endif