1 | #ifndef _theplu_yat_regression_gslinterpolation_ |
---|
2 | #define _theplu_yat_regression_gslinterpolation_ |
---|
3 | |
---|
4 | // $Id: GSLInterpolation.h 1650 2008-12-14 22:13:48Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Jari Häkkinen |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <gsl/gsl_interp.h> |
---|
26 | |
---|
27 | namespace theplu { |
---|
28 | namespace yat { |
---|
29 | namespace utility { |
---|
30 | class VectorBase; |
---|
31 | } |
---|
32 | namespace regression { |
---|
33 | |
---|
34 | /** |
---|
35 | \brief Base class for interfacing GSL interpolation. |
---|
36 | |
---|
37 | The GSL interpolation is described in |
---|
38 | http://www.gnu.org/software/gsl/manual/html_node/Interpolation.html. The |
---|
39 | GSL library provides a variety of interpolation methods, |
---|
40 | including Cubic splines and Akima splines. Interpolations can be |
---|
41 | defined for both normal and periodic boundary |
---|
42 | conditions. Additional functions are available for computing |
---|
43 | derivatives and integrals of interpolating functions. |
---|
44 | |
---|
45 | Given a set of data points \f$ (x_1, y_1) \dots (x_n, y_n) \f$ |
---|
46 | the sub classes compute a continuous interpolating function \f$ |
---|
47 | y(x) \f$ such that \f$ y(x_i) = y_i \f$. The interpolation is |
---|
48 | piecewise smooth, and its behavior at the end-points is |
---|
49 | determined by the type of interpolation used. |
---|
50 | |
---|
51 | Some underlying GSL functions return GSL error codes. The error |
---|
52 | code is stored internally and should be checked for by the caller |
---|
53 | using GSLInterpolation::error_status(void). Refer to the |
---|
54 | gsl_errno.h for the error code listing. |
---|
55 | */ |
---|
56 | class GSLInterpolation |
---|
57 | { |
---|
58 | |
---|
59 | public: |
---|
60 | /** |
---|
61 | \brief Search index. |
---|
62 | |
---|
63 | This function returns the index \f$ i \f$ of the array \a |
---|
64 | x_array such that \f$ x_array[i] <= x < x_array[i+1] \f$. The |
---|
65 | index is searched for in the range |
---|
66 | \f$ [index_lo, index_hi] \f$. |
---|
67 | */ |
---|
68 | size_t bsearch(const double x_array[], double x, size_t index_lo, |
---|
69 | size_t index_hi) const; |
---|
70 | |
---|
71 | /** |
---|
72 | \brief Check the error status |
---|
73 | |
---|
74 | Some underlying GSL functions return GSL error codes. The error |
---|
75 | code is stored internally and should be checked for by the |
---|
76 | caller. Refer to the gsl_errno.h for the error code listing. |
---|
77 | |
---|
78 | \return The current error status. A zero return values |
---|
79 | indicates no errors where encountered. |
---|
80 | */ |
---|
81 | int error_status(void) const; |
---|
82 | |
---|
83 | /** |
---|
84 | \brief Calculate the interpolated value for \a x. |
---|
85 | |
---|
86 | GSL error status is set if evaluation is requested outside the |
---|
87 | range defined by the interpolation algorithm. Use function |
---|
88 | error_status to check for errors. |
---|
89 | |
---|
90 | \return The interpolated value of \f$ y \f$ for a given point |
---|
91 | \a x. |
---|
92 | |
---|
93 | \see error_status(void) |
---|
94 | */ |
---|
95 | double evaluate(double x); |
---|
96 | |
---|
97 | /** |
---|
98 | \brief Calculate the derivative of the interpolated function at |
---|
99 | \a x. |
---|
100 | |
---|
101 | GSL error status is set if evaluation is requested outside the |
---|
102 | range defined by the interpolation algorithm. Use function |
---|
103 | error_status to check for errors. |
---|
104 | |
---|
105 | \return The derivative. |
---|
106 | |
---|
107 | \see error_status(void) |
---|
108 | */ |
---|
109 | double evaluate_derivative(double x); |
---|
110 | |
---|
111 | /** |
---|
112 | \brief Calculate the 2nd derivative of the interpolated |
---|
113 | function at \a x. |
---|
114 | |
---|
115 | GSL error status is set if evaluation is requested outside the |
---|
116 | range defined by the interpolation algorithm. Use function |
---|
117 | error_status to check for errors. |
---|
118 | |
---|
119 | \return The 2nd derivative. |
---|
120 | |
---|
121 | \see error_status(void) |
---|
122 | */ |
---|
123 | double evaluate_derivative2(double x); |
---|
124 | |
---|
125 | /** |
---|
126 | \brief Calculate the numerical integral of the interpolated |
---|
127 | function over the range \f$ [a,b] \f$. |
---|
128 | |
---|
129 | GSL error status is set if evaluation is requested outside the |
---|
130 | range defined by the interpolation algorithm. Use function |
---|
131 | error_status to check for errors. |
---|
132 | |
---|
133 | \return The integral. |
---|
134 | |
---|
135 | \see error_status(void) |
---|
136 | */ |
---|
137 | double evaluate_integral(double a, double b); |
---|
138 | |
---|
139 | /** |
---|
140 | \brief The result of the latest evaluaion function call is |
---|
141 | stored and can be retrieved with this function. |
---|
142 | |
---|
143 | \return The latest evaluated value. |
---|
144 | */ |
---|
145 | double evaluation(void) const; |
---|
146 | |
---|
147 | /** |
---|
148 | \brief This function returns the minimum number of points |
---|
149 | required by the interpolation type. |
---|
150 | |
---|
151 | For example, Akima spline interpolation requires a minimum of 5 |
---|
152 | points. |
---|
153 | |
---|
154 | \return The minimum number of points required. |
---|
155 | */ |
---|
156 | unsigned int min_size(void) const; |
---|
157 | |
---|
158 | protected: |
---|
159 | /** |
---|
160 | \brief The default constructor |
---|
161 | |
---|
162 | Initialization of the interpolation object for the data \f$ (x, |
---|
163 | y) \f$ where \a x and \a y are vector like objects of the same |
---|
164 | size. The content of \a x and \a y are copied for internal |
---|
165 | storage. \a x is always assumed to be strictly ordered, with |
---|
166 | increasing \a x values; the behavior for other arrangements is |
---|
167 | not defined. |
---|
168 | |
---|
169 | Some underlying GSL functions return GSL error codes. The error |
---|
170 | code is stored internally and should be checked for by the |
---|
171 | caller. Refer to the gsl_errno.h for the error code listing. |
---|
172 | |
---|
173 | \see error_status(void) |
---|
174 | */ |
---|
175 | GSLInterpolation(const gsl_interp_type*, const utility::VectorBase& x, |
---|
176 | const utility::VectorBase& y); |
---|
177 | |
---|
178 | /** |
---|
179 | \brief The destructor |
---|
180 | */ |
---|
181 | virtual ~GSLInterpolation(void); |
---|
182 | |
---|
183 | private: |
---|
184 | /** |
---|
185 | \brief Copy Constructor. (not implemented) |
---|
186 | */ |
---|
187 | GSLInterpolation(const GSLInterpolation&); |
---|
188 | |
---|
189 | gsl_interp_accel* accelerator_; |
---|
190 | double evaluation_; |
---|
191 | gsl_interp* interpolator_; |
---|
192 | int gsl_status_; |
---|
193 | double* x_; |
---|
194 | double* y_; |
---|
195 | }; |
---|
196 | |
---|
197 | }}} // of namespaces regression, yat, and theplu |
---|
198 | |
---|
199 | #endif |
---|