1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/* interpolation/cspline.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004 Gerard Jungman
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Author: G. Jungman
*/
#include <config.h>
#include <stdlib.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_vector.h>
#include "integ_eval.h"
#include <gsl/gsl_interp.h>
typedef struct
{
double * c;
double * g;
double * diag;
double * offdiag;
} cspline_state_t;
/* common initialization */
static void *
cspline_alloc (size_t size)
{
cspline_state_t * state = (cspline_state_t *) malloc (sizeof (cspline_state_t));
if (state == NULL)
{
GSL_ERROR_NULL("failed to allocate space for state", GSL_ENOMEM);
}
state->c = (double *) malloc (size * sizeof (double));
if (state->c == NULL)
{
free (state);
GSL_ERROR_NULL("failed to allocate space for c", GSL_ENOMEM);
}
state->g = (double *) malloc (size * sizeof (double));
if (state->g == NULL)
{
free (state->c);
free (state);
GSL_ERROR_NULL("failed to allocate space for g", GSL_ENOMEM);
}
state->diag = (double *) malloc (size * sizeof (double));
if (state->diag == NULL)
{
free (state->g);
free (state->c);
free (state);
GSL_ERROR_NULL("failed to allocate space for diag", GSL_ENOMEM);
}
state->offdiag = (double *) malloc (size * sizeof (double));
if (state->offdiag == NULL)
{
free (state->diag);
free (state->g);
free (state->c);
free (state);
GSL_ERROR_NULL("failed to allocate space for offdiag", GSL_ENOMEM);
}
return state;
}
/* natural spline calculation
* see [Engeln-Mullges + Uhlig, p. 254]
*/
static int
cspline_init (void * vstate, const double xa[], const double ya[],
size_t size)
{
cspline_state_t *state = (cspline_state_t *) vstate;
size_t i;
size_t num_points = size;
size_t max_index = num_points - 1; /* Engeln-Mullges + Uhlig "n" */
size_t sys_size = max_index - 1; /* linear system is sys_size x sys_size */
state->c[0] = 0.0;
state->c[max_index] = 0.0;
for (i = 0; i < sys_size; i++)
{
const double h_i = xa[i + 1] - xa[i];
const double h_ip1 = xa[i + 2] - xa[i + 1];
const double ydiff_i = ya[i + 1] - ya[i];
const double ydiff_ip1 = ya[i + 2] - ya[i + 1];
const double g_i = (h_i != 0.0) ? 1.0 / h_i : 0.0;
const double g_ip1 = (h_ip1 != 0.0) ? 1.0 / h_ip1 : 0.0;
state->offdiag[i] = h_ip1;
state->diag[i] = 2.0 * (h_ip1 + h_i);
state->g[i] = 3.0 * (ydiff_ip1 * g_ip1 - ydiff_i * g_i);
}
if (sys_size == 1)
{
state->c[1] = state->g[0] / state->diag[0];
return GSL_SUCCESS;
}
else
{
gsl_vector_view g_vec = gsl_vector_view_array(state->g, sys_size);
gsl_vector_view diag_vec = gsl_vector_view_array(state->diag, sys_size);
gsl_vector_view offdiag_vec = gsl_vector_view_array(state->offdiag, sys_size - 1);
gsl_vector_view solution_vec = gsl_vector_view_array ((state->c) + 1, sys_size);
int status = gsl_linalg_solve_symm_tridiag(&diag_vec.vector,
&offdiag_vec.vector,
&g_vec.vector,
&solution_vec.vector);
return status;
}
}
/* periodic spline calculation
* see [Engeln-Mullges + Uhlig, p. 256]
*/
static int
cspline_init_periodic (void * vstate, const double xa[], const double ya[],
size_t size)
{
cspline_state_t *state = (cspline_state_t *) vstate;
size_t i;
size_t num_points = size;
size_t max_index = num_points - 1; /* Engeln-Mullges + Uhlig "n" */
size_t sys_size = max_index; /* linear system is sys_size x sys_size */
if (sys_size == 2) {
/* solve 2x2 system */
const double h0 = xa[1] - xa[0];
const double h1 = xa[2] - xa[1];
const double A = 2.0*(h0 + h1);
const double B = h0 + h1;
double g[2];
double det;
g[0] = 3.0 * ((ya[2] - ya[1]) / h1 - (ya[1] - ya[0]) / h0);
g[1] = 3.0 * ((ya[1] - ya[2]) / h0 - (ya[2] - ya[1]) / h1);
det = 3.0 * (h0 + h1) * (h0 + h1);
state->c[1] = ( A * g[0] - B * g[1])/det;
state->c[2] = (-B * g[0] + A * g[1])/det;
state->c[0] = state->c[2];
return GSL_SUCCESS;
} else {
for (i = 0; i < sys_size-1; i++) {
const double h_i = xa[i + 1] - xa[i];
const double h_ip1 = xa[i + 2] - xa[i + 1];
const double ydiff_i = ya[i + 1] - ya[i];
const double ydiff_ip1 = ya[i + 2] - ya[i + 1];
const double g_i = (h_i != 0.0) ? 1.0 / h_i : 0.0;
const double g_ip1 = (h_ip1 != 0.0) ? 1.0 / h_ip1 : 0.0;
state->offdiag[i] = h_ip1;
state->diag[i] = 2.0 * (h_ip1 + h_i);
state->g[i] = 3.0 * (ydiff_ip1 * g_ip1 - ydiff_i * g_i);
}
i = sys_size - 1;
{
const double h_i = xa[i + 1] - xa[i];
const double h_ip1 = xa[1] - xa[0];
const double ydiff_i = ya[i + 1] - ya[i];
const double ydiff_ip1 = ya[1] - ya[0];
const double g_i = (h_i != 0.0) ? 1.0 / h_i : 0.0;
const double g_ip1 = (h_ip1 != 0.0) ? 1.0 / h_ip1 : 0.0;
state->offdiag[i] = h_ip1;
state->diag[i] = 2.0 * (h_ip1 + h_i);
state->g[i] = 3.0 * (ydiff_ip1 * g_ip1 - ydiff_i * g_i);
}
{
gsl_vector_view g_vec = gsl_vector_view_array(state->g, sys_size);
gsl_vector_view diag_vec = gsl_vector_view_array(state->diag, sys_size);
gsl_vector_view offdiag_vec = gsl_vector_view_array(state->offdiag, sys_size);
gsl_vector_view solution_vec = gsl_vector_view_array ((state->c) + 1, sys_size);
int status = gsl_linalg_solve_symm_cyc_tridiag(&diag_vec.vector,
&offdiag_vec.vector,
&g_vec.vector,
&solution_vec.vector);
state->c[0] = state->c[max_index];
return status;
}
}
}
static
void
cspline_free (void * vstate)
{
cspline_state_t *state = (cspline_state_t *) vstate;
free (state->c);
free (state->g);
free (state->diag);
free (state->offdiag);
free (state);
}
/* function for common coefficient determination
*/
static inline void
coeff_calc (const double c_array[], double dy, double dx, size_t index,
double * b, double * c, double * d)
{
const double c_i = c_array[index];
const double c_ip1 = c_array[index + 1];
*b = (dy / dx) - dx * (c_ip1 + 2.0 * c_i) / 3.0;
*c = c_i;
*d = (c_ip1 - c_i) / (3.0 * dx);
}
static
int
cspline_eval (const void * vstate,
const double x_array[], const double y_array[], size_t size,
double x,
gsl_interp_accel * a,
double *y)
{
const cspline_state_t *state = (const cspline_state_t *) vstate;
double x_lo, x_hi;
double dx;
size_t index;
if (a != 0)
{
index = gsl_interp_accel_find (a, x_array, size, x);
}
else
{
index = gsl_interp_bsearch (x_array, x, 0, size - 1);
}
/* evaluate */
x_hi = x_array[index + 1];
x_lo = x_array[index];
dx = x_hi - x_lo;
if (dx > 0.0)
{
const double y_lo = y_array[index];
const double y_hi = y_array[index + 1];
const double dy = y_hi - y_lo;
double delx = x - x_lo;
double b_i, c_i, d_i;
coeff_calc(state->c, dy, dx, index, &b_i, &c_i, &d_i);
*y = y_lo + delx * (b_i + delx * (c_i + delx * d_i));
return GSL_SUCCESS;
}
else
{
*y = 0.0;
return GSL_EINVAL;
}
}
static
int
cspline_eval_deriv (const void * vstate,
const double x_array[], const double y_array[], size_t size,
double x,
gsl_interp_accel * a,
double *dydx)
{
const cspline_state_t *state = (const cspline_state_t *) vstate;
double x_lo, x_hi;
double dx;
size_t index;
if (a != 0)
{
index = gsl_interp_accel_find (a, x_array, size, x);
}
else
{
index = gsl_interp_bsearch (x_array, x, 0, size - 1);
}
/* evaluate */
x_hi = x_array[index + 1];
x_lo = x_array[index];
dx = x_hi - x_lo;
if (dx > 0.0)
{
const double y_lo = y_array[index];
const double y_hi = y_array[index + 1];
const double dy = y_hi - y_lo;
double delx = x - x_lo;
double b_i, c_i, d_i;
coeff_calc(state->c, dy, dx, index, &b_i, &c_i, &d_i);
*dydx = b_i + delx * (2.0 * c_i + 3.0 * d_i * delx);
return GSL_SUCCESS;
}
else
{
*dydx = 0.0;
return GSL_EINVAL;
}
}
static
int
cspline_eval_deriv2 (const void * vstate,
const double x_array[], const double y_array[], size_t size,
double x,
gsl_interp_accel * a,
double * y_pp)
{
const cspline_state_t *state = (const cspline_state_t *) vstate;
double x_lo, x_hi;
double dx;
size_t index;
if (a != 0)
{
index = gsl_interp_accel_find (a, x_array, size, x);
}
else
{
index = gsl_interp_bsearch (x_array, x, 0, size - 1);
}
/* evaluate */
x_hi = x_array[index + 1];
x_lo = x_array[index];
dx = x_hi - x_lo;
if (dx > 0.0)
{
const double y_lo = y_array[index];
const double y_hi = y_array[index + 1];
const double dy = y_hi - y_lo;
double delx = x - x_lo;
double b_i, c_i, d_i;
coeff_calc(state->c, dy, dx, index, &b_i, &c_i, &d_i);
*y_pp = 2.0 * c_i + 6.0 * d_i * delx;
return GSL_SUCCESS;
}
else
{
*y_pp = 0.0;
return GSL_EINVAL;
}
}
static
int
cspline_eval_integ (const void * vstate,
const double x_array[], const double y_array[], size_t size,
gsl_interp_accel * acc,
double a, double b,
double * result)
{
const cspline_state_t *state = (const cspline_state_t *) vstate;
size_t i, index_a, index_b;
if (acc != 0)
{
index_a = gsl_interp_accel_find (acc, x_array, size, a);
index_b = gsl_interp_accel_find (acc, x_array, size, b);
}
else
{
index_a = gsl_interp_bsearch (x_array, a, 0, size - 1);
index_b = gsl_interp_bsearch (x_array, b, 0, size - 1);
}
*result = 0.0;
/* interior intervals */
for(i=index_a; i<=index_b; i++) {
const double x_hi = x_array[i + 1];
const double x_lo = x_array[i];
const double y_lo = y_array[i];
const double y_hi = y_array[i + 1];
const double dx = x_hi - x_lo;
const double dy = y_hi - y_lo;
if(dx != 0.0) {
double b_i, c_i, d_i;
coeff_calc(state->c, dy, dx, i, &b_i, &c_i, &d_i);
if (i == index_a || i == index_b)
{
double x1 = (i == index_a) ? a : x_lo;
double x2 = (i == index_b) ? b : x_hi;
*result += integ_eval(y_lo, b_i, c_i, d_i, x_lo, x1, x2);
}
else
{
*result += dx * (y_lo + dx*(0.5*b_i + dx*(c_i/3.0 + 0.25*d_i*dx)));
}
}
else {
*result = 0.0;
return GSL_EINVAL;
}
}
return GSL_SUCCESS;
}
static const gsl_interp_type cspline_type =
{
"cspline",
3,
&cspline_alloc,
&cspline_init,
&cspline_eval,
&cspline_eval_deriv,
&cspline_eval_deriv2,
&cspline_eval_integ,
&cspline_free
};
const gsl_interp_type * gsl_interp_cspline = &cspline_type;
static const gsl_interp_type cspline_periodic_type =
{
"cspline-periodic",
2,
&cspline_alloc,
&cspline_init_periodic,
&cspline_eval,
&cspline_eval_deriv,
&cspline_eval_deriv2,
&cspline_eval_integ,
&cspline_free
};
const gsl_interp_type * gsl_interp_cspline_periodic = &cspline_periodic_type;
|