gsl_poly__dd.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* interpolation/interp_poly.c
  2. *
  3. * Copyright (C) 2001 DAN, HO-JIN
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Modified for standalone use in polynomial directory, B.Gough 2001 */
  20. #include "gsl__config.h"
  21. #include "gsl_errno.h"
  22. #include "gsl_poly.h"
  23. int
  24. gsl_poly_dd_init (double dd[], const double xa[], const double ya[],
  25. size_t size)
  26. {
  27. size_t i, j;
  28. /* Newton's divided differences */
  29. dd[0] = ya[0];
  30. for (j = size - 1; j >= 1; j--)
  31. {
  32. dd[j] = (ya[j] - ya[j - 1]) / (xa[j] - xa[j - 1]);
  33. }
  34. for (i = 2; i < size; i++)
  35. {
  36. for (j = size - 1; j >= i; j--)
  37. {
  38. dd[j] = (dd[j] - dd[j - 1]) / (xa[j] - xa[j - i]);
  39. }
  40. }
  41. return GSL_SUCCESS;
  42. }
  43. #ifndef HIDE_INLINE_STATIC
  44. double
  45. gsl_poly_dd_eval (const double dd[], const double xa[], const size_t size, const double x)
  46. {
  47. size_t i;
  48. double y = dd[size - 1];
  49. for (i = size - 1; i--;)
  50. {
  51. y = dd[i] + (x - xa[i]) * y;
  52. }
  53. return y;
  54. }
  55. #endif
  56. int
  57. gsl_poly_dd_taylor (double c[], double xp,
  58. const double dd[], const double xa[], size_t size,
  59. double w[])
  60. {
  61. size_t i, j;
  62. for (i = 0; i < size; i++)
  63. {
  64. c[i] = 0.0;
  65. w[i] = 0.0;
  66. }
  67. w[size - 1] = 1.0;
  68. c[0] = dd[0];
  69. for (i = size - 1; i > 0 && i--;)
  70. {
  71. w[i] = -w[i + 1] * (xa[size - 2 - i] - xp);
  72. for (j = i + 1; j < size - 1; j++)
  73. {
  74. w[j] = w[j] - w[j + 1] * (xa[size - 2 - i] - xp);
  75. }
  76. for (j = i; j < size; j++)
  77. {
  78. c[j - i] += w[j] * dd[size - i - 1];
  79. }
  80. }
  81. return GSL_SUCCESS;
  82. }