ABACUS/src/FITTING/polint_cx.cc

51 lines
1.1 KiB
C++

/**********************************************************
This software is part of J.-S. Caux's ABACUS library.
Copyright (c) J.-S. Caux.
-----------------------------------------------------------
File: polint_cx.cc
Purpose: Polynomial interpolation
***********************************************************/
#include "ABACUS.h"
using namespace std;
void ABACUS::polint(Vect_CX& xa, Vect_CX& ya, const complex<DP> x, complex<DP>& y, complex<DP>& dy)
{
// Polynomial interpolation/extrapolation, NR page 113.
int i, m, ns=0;
DP dif, dift;
complex<DP> den, ho, hp, w;
int n = xa.size();
Vect_CX c(n), d(n);
dif = abs(x-xa[0]);
for (i = 0; i < n; i++) {
if ((dift = abs(x-xa[i])) < dif) {
ns = i;
dif = dift;
}
c[i] = ya[i];
d[i] = ya[i];
}
y = ya[ns--];
for (m = 1; m < n; m++) {
for (i = 0; i < n-m; i++) {
ho = xa[i] - x;
hp = xa[i+m] - x;
w = c[i+1] - d[i];
if ((den = ho-hp) == 0.0) ABACUSerror("Error in routine polint_cx.");
den = w/den;
d[i] = hp * den;
c[i] = ho * den;
}
y += (dy = (2 * (ns + 1) < (n-m) ? c[ns + 1] : d[ns--]));
}
}