10 #define DO_ASSERTS 0 // similar parameter as in some init.h's
20 FTYPE interpn(
int order,
FTYPE x_eval,
FTYPE x1,
FTYPE f1,
FTYPE x2,
FTYPE f2,
FTYPE x3,
FTYPE f3,
FTYPE x4,
FTYPE f4,
FTYPE x5,
FTYPE f5,
FTYPE x6,
FTYPE f6 )
23 #define max_interp_order 6 //should be equal to the number of points input to the function; degree of interpolation is only limited by the number of arguments to the function --
27 FTYPE x[] = { x1, x2, x3, x4, x5, x6};
28 FTYPE f[] = { f1, f2, f3, f4, f5, f6};
33 trifprintf(
"interpn: requested order of interpolation non-positive or too high.\n" );
39 for( i = 1; i < order; i++ ) {
40 for( j = 0; j <
i; j++ ) {
41 if( x[i] - x[j] == 0.0 ) {
42 fprintf( stderr,
"interpn: abscissas of values to be interpolated are to be all different\n" );
49 for( i = 0; i < order; i++ ) {
54 for( j = 1; j < order; j++ ) {
55 x_b = x[(i +
j) % order];
56 delta_f_eval *= (x_eval - x_b) / (x_a - x_b);
60 f_eval += delta_f_eval;
79 value = value*pow(10, precision);
81 fraction = modf(value, &temp);
82 if(fraction>=0.5) temp+=1.0;
83 if(fraction<=-0.5) temp-=1.0;
85 return temp*pow(0.1, precision);
93 FTYPE slope,intercept;
94 FTYPE slope1,slope2,xminusx0;
95 FTYPE f0,f1,f2,x0,x1,x2;
96 FTYPE linslope1,linslope2;
111 else if(i+1>=numpoints){
135 else if(i+1>=numpoints){
166 slope = (f1-f0)/(x1-x0);
168 *answer = slope*(pos-x0) + intercept;
175 slope2 = ((f0-f2)/(x0-x2) - (f2-f1)/(x2-x1))/(x0-x1);
176 slope1 = (f0-f1)/(x0-x1) + (f0-f2)/(x0-x2) - (f2-f1)/(x2-x1);
178 linslope1=(f1-f0)/(x1-x0);
179 linslope2=(f2-f1)/(x2-x1);
182 if(linslope1*linslope2<0.0){
187 else if(pos>=x0 && pos<=x1){
188 if(fabs(pos-x0)<fabs(pos-x1)) *answer=f0;
191 else if(pos>=x1 && pos<=x2){
192 if(fabs(pos-x1)<fabs(pos-x2)) *answer=f1;
197 if(fabs(linslope1)<fabs(linslope2)){
198 slope = (f1-f0)/(x1-x0);
200 *answer = slope*(pos-x0) + intercept;
203 slope = (f2-f1)/(x2-x1);
205 *answer = slope*(pos-x1) + intercept;
213 *answer = slope2*pow(xminusx0,2.0) + slope1*xminusx0 + f0;
218 if(*answer>f0 && *answer>f1 && *answer>f2){
222 else if(f1>f0 && f1>f2){
225 else if(f2>f0 && f2>f1){
229 else if(*answer<f0 && *answer<f1 && *answer<f2){
233 else if(f1<f0 && f1<f2){
236 else if(f2<f0 && f2<f1){
248 slope = log(f1/f0)/log(x1/f0);
249 if(fabs(slope)<1
E-10) *answer=f1;
252 *answer=-exp( slope*log(pos/x0)+log(-f0) );
254 else *answer=exp( slope*log(pos/x0)+log(f0) );