You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polint.cc 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**********************************************************
  2. This software is part of J.-S. Caux's ABACUS library.
  3. Copyright (c) J.-S. Caux.
  4. -----------------------------------------------------------
  5. File: polint.cc
  6. Purpose: Polynomial interpolation
  7. ***********************************************************/
  8. #include "ABACUS.h"
  9. using namespace std;
  10. void ABACUS::polint(Vect_DP& xa, Vect_DP& ya, const DP x, DP& y, DP& dy)
  11. {
  12. // Polynomial interpolation/extrapolation, NR page 113.
  13. int i, m, ns=0;
  14. DP den, dif, dift, ho, hp, w;
  15. int n = xa.size();
  16. Vect_DP c(n), d(n);
  17. dif = fabs(x-xa[0]);
  18. for (i = 0; i < n; i++) {
  19. if ((dift = fabs(x-xa[i])) < dif) {
  20. ns = i;
  21. dif = dift;
  22. }
  23. c[i] = ya[i];
  24. d[i] = ya[i];
  25. }
  26. y = ya[ns--];
  27. for (m = 1; m < n; m++) {
  28. for (i = 0; i < n-m; i++) {
  29. ho = xa[i] - x;
  30. hp = xa[i+m] - x;
  31. w = c[i+1] - d[i];
  32. if ((den = ho-hp) == 0.0) ABACUSerror("Error in routine polint.");
  33. den = w/den;
  34. d[i] = hp * den;
  35. c[i] = ho * den;
  36. }
  37. y += (dy = (2 * (ns + 1) < (n-m) ? c[ns + 1] : d[ns--]));
  38. }
  39. }