1 /* interpolation/interp.c
3 * Copyright (C) 2007 Brian Gough
4 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_interp.h>
28 #define DISCARD_STATUS(s) if ((s) != GSL_SUCCESS) { GSL_ERROR_VAL("interpolation error", (s), GSL_NAN); }
31 gsl_interp_alloc (const gsl_interp_type * T, size_t size)
35 if (size < T->min_size)
37 GSL_ERROR_NULL ("insufficient number of points for interpolation type",
41 interp = (gsl_interp *) malloc (sizeof(gsl_interp));
45 GSL_ERROR_NULL ("failed to allocate space for interp struct",
52 if (interp->type->alloc == NULL)
58 interp->state = interp->type->alloc(size);
60 if (interp->state == NULL)
63 GSL_ERROR_NULL ("failed to allocate space for interp state", GSL_ENOMEM);
70 gsl_interp_init (gsl_interp * interp, const double x_array[], const double y_array[], size_t size)
74 if (size != interp->size)
76 GSL_ERROR ("data must match size of interpolation object", GSL_EINVAL);
79 for (i = 1; i < size; i++)
81 if (!(x_array[i-1] < x_array[i]))
83 GSL_ERROR ("x values must be monotonically increasing", GSL_EINVAL);
87 interp->xmin = x_array[0];
88 interp->xmax = x_array[size - 1];
91 int status = interp->type->init(interp->state, x_array, y_array, size);
97 gsl_interp_name(const gsl_interp * interp)
99 return interp->type->name;
103 gsl_interp_min_size(const gsl_interp * interp)
105 return interp->type->min_size;
109 gsl_interp_free (gsl_interp * interp)
111 if (interp->type->free)
112 interp->type->free (interp->state);
119 gsl_interp_eval_e (const gsl_interp * interp,
120 const double xa[], const double ya[], double x,
121 gsl_interp_accel * a, double *y)
123 if (x < interp->xmin)
128 else if (x > interp->xmax)
130 *y = ya[interp->size - 1];
134 return interp->type->eval (interp->state, xa, ya, interp->size, x, a, y);
138 gsl_interp_eval (const gsl_interp * interp,
139 const double xa[], const double ya[], double x,
140 gsl_interp_accel * a)
143 int status = interp->type->eval (interp->state, xa, ya, interp->size, x, a, &y);
145 DISCARD_STATUS(status);
152 gsl_interp_eval_deriv_e (const gsl_interp * interp,
153 const double xa[], const double ya[], double x,
154 gsl_interp_accel * a,
157 if (x < interp->xmin)
162 else if (x > interp->xmax)
168 return interp->type->eval_deriv (interp->state, xa, ya, interp->size, x, a, dydx);
172 gsl_interp_eval_deriv (const gsl_interp * interp,
173 const double xa[], const double ya[], double x,
174 gsl_interp_accel * a)
177 int status = interp->type->eval_deriv (interp->state, xa, ya, interp->size, x, a, &dydx);
179 DISCARD_STATUS(status);
186 gsl_interp_eval_deriv2_e (const gsl_interp * interp,
187 const double xa[], const double ya[], double x,
188 gsl_interp_accel * a,
191 if (x < interp->xmin)
196 else if (x > interp->xmax)
202 return interp->type->eval_deriv2 (interp->state, xa, ya, interp->size, x, a, d2);
206 gsl_interp_eval_deriv2 (const gsl_interp * interp,
207 const double xa[], const double ya[], double x,
208 gsl_interp_accel * a)
211 int status = interp->type->eval_deriv2 (interp->state, xa, ya, interp->size, x, a, &d2);
213 DISCARD_STATUS(status);
220 gsl_interp_eval_integ_e (const gsl_interp * interp,
221 const double xa[], const double ya[],
223 gsl_interp_accel * acc,
226 if (a > b || a < interp->xmin || b > interp->xmax)
237 return interp->type->eval_integ (interp->state, xa, ya, interp->size, acc, a, b, result);
242 gsl_interp_eval_integ (const gsl_interp * interp,
243 const double xa[], const double ya[],
245 gsl_interp_accel * acc)
248 int status = interp->type->eval_integ (interp->state, xa, ya, interp->size, acc, a, b, &result);
250 DISCARD_STATUS(status);