Debian Codegraph @

gui/XAlice/libsrc/splint.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .IDENT splint.c
30 .MODULE subroutines -- sprebin.exe
31 .LANGUAGE C
32 .AUTHOR Cristian Levin - ESO La Silla
33 .PURPOSE This file contains two routines for making spline
34 interpolation:
35 - using Hermite polynomials.
36 - using natural-cubic spline.
37 The first one is currently used for rebinning.
38 .KEYWORDS rebin, spline interpolation.
39 .VERSION 1.0 1-Apr-1991 Implementation
40
41 090420 last modif
42 ------------------------------------------------------------*/
43
44 #include <stdio.h>
45 #include <math.h>
46
47 /***************************************************************
48 *
49 * splint(): spline interpolation based on Hermite polynomials.
50 *
51 ***************************************************************/
52
53 float splint( xp, X, Y, n, istart )
54 float xp; /* x-value to interpolate */
55 float *X; /* x-array [0..n-1] */
56 float *Y; /* y-array [0..n-1] */
57 int n;
58 int *istart; /* initial index */
59 {
60 float yp1, yp2, yp;
61 float xpi, xpi1, l1, l2, lp1, lp2;
62 int i;
63
64 if ( xp < X[0] || xp > X[n-1] )
65 return(0.0);
66
67 for ( i = *istart; i < n && xp >= X[i]; i++ )
68 ;
69
70 *istart = i;
71 i--;
72
73 lp1 = 1.0 / (X[i] - X[i+1]);
74 lp2 = -lp1;
75
76 if ( i == 0 )
77 yp1 = (Y[1] - Y[0]) / (X[1] - X[0]);
78 else
79 yp1 = (Y[i+1] - Y[i-1]) / (X[i+1] - X[i-1]);
80
81 if ( i >= n - 2 )
82 yp2 = (Y[n-1] - Y[n-2]) / (X[n-1] - X[n-2]);
83 else
84 yp2 = (Y[i+2] - Y[i]) / (X[i+2] - X[i]);
85
86 xpi1 = xp - X[i+1];
87 xpi = xp - X[i];
88 l1 = xpi1*lp1;
89 l2 = xpi*lp2;
90
91 yp = Y[i]*(1 - 2.0*lp1*xpi)*l1*l1 +
92 Y[i+1]*(1 - 2.0*lp2*xpi1)*l2*l2 +
93 yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;
94 return((float)yp);
95 }
96
97 /***************************************************************
98 *
99 * splint2(): natural cubic-spline interpolation.
100 *
101 ***************************************************************/
102
103 float splint2( xp, x, y, y2, n, kstart )
104 float xp; /* x-value to interpolate */
105 float *x; /* x-array [1..n] */
106 float *y; /* y-array [1..n] */
107 float *y2; /* y2-array [1..n] (2-nd derivatives) */
108 int n;
109 int *kstart; /* initial index */
110 {
111 int klo, khi, k;
112 float a, b, h, yp;
113
114 void nrerror();
115
116
117 klo = *kstart;
118 khi = n;
119
120 if ( xp < x[1] || xp > x[n] )
121 return(0.0);
122 else if ( xp == x[1] )
123 return(y[1]);
124
125 for ( k = klo; k < n && xp > x[k]; k++ )
126 ;
127
128 klo = *kstart = k-1;
129 khi = k;
130
131 h = x[khi] - x[klo];
132 if ( h == 0.0 )
133 nrerror( "Error in spline interpolation" );
134
135 a = (x[khi] - xp) / h;
136 b = (xp - x[klo]) / h;
137
138 yp = a*y[klo] + b*y[khi] + ((a*a*a - a)*y2[klo] + (b*b*b - b)*y2[khi])*
139 (h*h) / 6.0;
140
141 return(yp);
142 }