gsl_interp.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* interpolation/gsl_interp.h
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004 Gerard Jungman
  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. /* Author: G. Jungman
  20. */
  21. #ifndef __GSL_INTERP_H__
  22. #define __GSL_INTERP_H__
  23. #include <stdlib.h>
  24. #include "gsl_types.h"
  25. #undef __BEGIN_DECLS
  26. #undef __END_DECLS
  27. #ifdef __cplusplus
  28. # define __BEGIN_DECLS extern "C" {
  29. # define __END_DECLS }
  30. #else
  31. # define __BEGIN_DECLS /* empty */
  32. # define __END_DECLS /* empty */
  33. #endif
  34. __BEGIN_DECLS
  35. /* evaluation accelerator */
  36. typedef struct {
  37. size_t cache; /* cache of index */
  38. size_t miss_count; /* keep statistics */
  39. size_t hit_count;
  40. }
  41. gsl_interp_accel;
  42. /* interpolation object type */
  43. typedef struct {
  44. const char * name;
  45. unsigned int min_size;
  46. void * (*alloc) (size_t size);
  47. int (*init) (void *, const double xa[], const double ya[], size_t size);
  48. int (*eval) (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y);
  49. int (*eval_deriv) (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y_p);
  50. int (*eval_deriv2) (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y_pp);
  51. int (*eval_integ) (const void *, const double xa[], const double ya[], size_t size, gsl_interp_accel *, double a, double b, double * result);
  52. void (*free) (void *);
  53. } gsl_interp_type;
  54. /* general interpolation object */
  55. typedef struct {
  56. const gsl_interp_type * type;
  57. double xmin;
  58. double xmax;
  59. size_t size;
  60. void * state;
  61. } gsl_interp;
  62. /* available types */
  63. GSL_VAR const gsl_interp_type * gsl_interp_linear;
  64. GSL_VAR const gsl_interp_type * gsl_interp_polynomial;
  65. GSL_VAR const gsl_interp_type * gsl_interp_cspline;
  66. GSL_VAR const gsl_interp_type * gsl_interp_cspline_periodic;
  67. GSL_VAR const gsl_interp_type * gsl_interp_akima;
  68. GSL_VAR const gsl_interp_type * gsl_interp_akima_periodic;
  69. gsl_interp_accel *
  70. gsl_interp_accel_alloc(void);
  71. size_t
  72. gsl_interp_accel_find(gsl_interp_accel * a, const double x_array[], size_t size, double x);
  73. int
  74. gsl_interp_accel_reset (gsl_interp_accel * a);
  75. void
  76. gsl_interp_accel_free(gsl_interp_accel * a);
  77. gsl_interp *
  78. gsl_interp_alloc(const gsl_interp_type * T, size_t n);
  79. int
  80. gsl_interp_init(gsl_interp * obj, const double xa[], const double ya[], size_t size);
  81. const char * gsl_interp_name(const gsl_interp * interp);
  82. unsigned int gsl_interp_min_size(const gsl_interp * interp);
  83. int
  84. gsl_interp_eval_e(const gsl_interp * obj,
  85. const double xa[], const double ya[], double x,
  86. gsl_interp_accel * a, double * y);
  87. double
  88. gsl_interp_eval(const gsl_interp * obj,
  89. const double xa[], const double ya[], double x,
  90. gsl_interp_accel * a);
  91. int
  92. gsl_interp_eval_deriv_e(const gsl_interp * obj,
  93. const double xa[], const double ya[], double x,
  94. gsl_interp_accel * a,
  95. double * d);
  96. double
  97. gsl_interp_eval_deriv(const gsl_interp * obj,
  98. const double xa[], const double ya[], double x,
  99. gsl_interp_accel * a);
  100. int
  101. gsl_interp_eval_deriv2_e(const gsl_interp * obj,
  102. const double xa[], const double ya[], double x,
  103. gsl_interp_accel * a,
  104. double * d2);
  105. double
  106. gsl_interp_eval_deriv2(const gsl_interp * obj,
  107. const double xa[], const double ya[], double x,
  108. gsl_interp_accel * a);
  109. int
  110. gsl_interp_eval_integ_e(const gsl_interp * obj,
  111. const double xa[], const double ya[],
  112. double a, double b,
  113. gsl_interp_accel * acc,
  114. double * result);
  115. double
  116. gsl_interp_eval_integ(const gsl_interp * obj,
  117. const double xa[], const double ya[],
  118. double a, double b,
  119. gsl_interp_accel * acc);
  120. void
  121. gsl_interp_free(gsl_interp * interp);
  122. size_t gsl_interp_bsearch(const double x_array[], double x,
  123. size_t index_lo, size_t index_hi);
  124. #ifdef HAVE_INLINE
  125. extern inline size_t
  126. gsl_interp_bsearch(const double x_array[], double x,
  127. size_t index_lo, size_t index_hi);
  128. extern inline size_t
  129. gsl_interp_bsearch(const double x_array[], double x,
  130. size_t index_lo, size_t index_hi)
  131. {
  132. size_t ilo = index_lo;
  133. size_t ihi = index_hi;
  134. while(ihi > ilo + 1) {
  135. size_t i = (ihi + ilo)/2;
  136. if(x_array[i] > x)
  137. ihi = i;
  138. else
  139. ilo = i;
  140. }
  141. return ilo;
  142. }
  143. #endif
  144. #ifdef HAVE_INLINE
  145. extern inline size_t
  146. gsl_interp_accel_find(gsl_interp_accel * a, const double xa[], size_t len, double x)
  147. {
  148. size_t x_index = a->cache;
  149. if(x < xa[x_index]) {
  150. a->miss_count++;
  151. a->cache = gsl_interp_bsearch(xa, x, 0, x_index);
  152. }
  153. else if(x > xa[x_index + 1]) {
  154. a->miss_count++;
  155. a->cache = gsl_interp_bsearch(xa, x, x_index, len-1);
  156. }
  157. else {
  158. a->hit_count++;
  159. }
  160. return a->cache;
  161. }
  162. #endif /* HAVE_INLINE */
  163. __END_DECLS
  164. #endif /* __GSL_INTERP_H__ */