1 | #ifndef _theplu_yat_regression_gslinterpolation_ |
---|
2 | #define _theplu_yat_regression_gslinterpolation_ |
---|
3 | |
---|
4 | // $Id: GSLInterpolation.h 1654 2008-12-16 16:29:35Z peter $ |
---|
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 the |
---|
38 | <A HREF= |
---|
39 | "http://www.gnu.org/software/gsl/manual/html_node/Interpolation.html"> |
---|
40 | GSL Manual.</A> |
---|
41 | The GSL library provides a variety of interpolation methods, |
---|
42 | including Cubic splines and Akima splines. Interpolations can be |
---|
43 | defined for both normal and periodic boundary |
---|
44 | conditions. Additional functions are available for computing |
---|
45 | derivatives and integrals of interpolating functions. |
---|
46 | |
---|
47 | Given a set of data points \f$ (x_1, y_1) \dots (x_n, y_n) \f$ |
---|
48 | the sub classes compute a continuous interpolating function \f$ |
---|
49 | y(x) \f$ such that \f$ y(x_i) = y_i \f$. The interpolation is |
---|
50 | piecewise smooth, and its behavior at the end-points is |
---|
51 | determined by the type of interpolation used. |
---|
52 | |
---|
53 | Some underlying GSL functions return GSL error codes. The error |
---|
54 | code is stored internally and should be checked for by the caller |
---|
55 | using GSLInterpolation::error_status(void). Refer to the |
---|
56 | gsl_errno.h for the error code listing. |
---|
57 | |
---|
58 | \since New in yat 0.5 |
---|
59 | */ |
---|
60 | class GSLInterpolation |
---|
61 | { |
---|
62 | |
---|
63 | public: |
---|
64 | /** |
---|
65 | \brief Search index. |
---|
66 | |
---|
67 | This function returns the index \f$ i \f$ of the array \a |
---|
68 | x_array such that \f$ x_array[i] <= x < x_array[i+1] \f$. The |
---|
69 | index is searched for in the range |
---|
70 | \f$ [index_lo, index_hi] \f$. |
---|
71 | */ |
---|
72 | size_t bsearch(const double x_array[], double x, size_t index_lo, |
---|
73 | size_t index_hi) const; |
---|
74 | |
---|
75 | /** |
---|
76 | \brief Check the error status |
---|
77 | |
---|
78 | Some underlying GSL functions return GSL error codes. The error |
---|
79 | code is stored internally and should be checked for by the |
---|
80 | caller. Refer to the gsl_errno.h for the error code listing. |
---|
81 | |
---|
82 | \return The current error status. A zero return values |
---|
83 | indicates no errors where encountered. |
---|
84 | */ |
---|
85 | int error_status(void) const; |
---|
86 | |
---|
87 | /** |
---|
88 | \brief Calculate the interpolated value for \a x. |
---|
89 | |
---|
90 | GSL error status is set if evaluation is requested outside the |
---|
91 | range defined by the interpolation algorithm. Use function |
---|
92 | error_status to check for errors. |
---|
93 | |
---|
94 | \return The interpolated value of \f$ y \f$ for a given point |
---|
95 | \a x. |
---|
96 | |
---|
97 | \see error_status(void) |
---|
98 | */ |
---|
99 | double evaluate(double x); |
---|
100 | |
---|
101 | /** |
---|
102 | \brief Calculate the derivative of the interpolated function at |
---|
103 | \a x. |
---|
104 | |
---|
105 | GSL error status is set if evaluation is requested outside the |
---|
106 | range defined by the interpolation algorithm. Use function |
---|
107 | error_status to check for errors. |
---|
108 | |
---|
109 | \return The derivative. |
---|
110 | |
---|
111 | \see error_status(void) |
---|
112 | */ |
---|
113 | double evaluate_derivative(double x); |
---|
114 | |
---|
115 | /** |
---|
116 | \brief Calculate the 2nd derivative of the interpolated |
---|
117 | function at \a x. |
---|
118 | |
---|
119 | GSL error status is set if evaluation is requested outside the |
---|
120 | range defined by the interpolation algorithm. Use function |
---|
121 | error_status to check for errors. |
---|
122 | |
---|
123 | \return The 2nd derivative. |
---|
124 | |
---|
125 | \see error_status(void) |
---|
126 | */ |
---|
127 | double evaluate_derivative2(double x); |
---|
128 | |
---|
129 | /** |
---|
130 | \brief Calculate the numerical integral of the interpolated |
---|
131 | function over the range \f$ [a,b] \f$. |
---|
132 | |
---|
133 | GSL error status is set if evaluation is requested outside the |
---|
134 | range defined by the interpolation algorithm. Use function |
---|
135 | error_status to check for errors. |
---|
136 | |
---|
137 | \return The integral. |
---|
138 | |
---|
139 | \see error_status(void) |
---|
140 | */ |
---|
141 | double evaluate_integral(double a, double b); |
---|
142 | |
---|
143 | /** |
---|
144 | \brief The result of the latest evaluaion function call is |
---|
145 | stored and can be retrieved with this function. |
---|
146 | |
---|
147 | \return The latest evaluated value. |
---|
148 | */ |
---|
149 | double evaluation(void) const; |
---|
150 | |
---|
151 | /** |
---|
152 | \brief This function returns the minimum number of points |
---|
153 | required by the interpolation type. |
---|
154 | |
---|
155 | For example, Akima spline interpolation requires a minimum of 5 |
---|
156 | points. |
---|
157 | |
---|
158 | \return The minimum number of points required. |
---|
159 | */ |
---|
160 | unsigned int min_size(void) const; |
---|
161 | |
---|
162 | protected: |
---|
163 | /** |
---|
164 | \brief The default constructor |
---|
165 | |
---|
166 | Initialization of the interpolation object for the data \f$ (x, |
---|
167 | y) \f$ where \a x and \a y are vector like objects of the same |
---|
168 | size. The content of \a x and \a y are copied for internal |
---|
169 | storage. \a x is always assumed to be strictly ordered, with |
---|
170 | increasing \a x values; the behavior for other arrangements is |
---|
171 | not defined. |
---|
172 | |
---|
173 | Some underlying GSL functions return GSL error codes. The error |
---|
174 | code is stored internally and should be checked for by the |
---|
175 | caller. Refer to the gsl_errno.h for the error code listing. |
---|
176 | |
---|
177 | \see error_status(void) |
---|
178 | */ |
---|
179 | GSLInterpolation(const gsl_interp_type*, const utility::VectorBase& x, |
---|
180 | const utility::VectorBase& y); |
---|
181 | |
---|
182 | /** |
---|
183 | \brief The destructor |
---|
184 | */ |
---|
185 | virtual ~GSLInterpolation(void); |
---|
186 | |
---|
187 | private: |
---|
188 | /** |
---|
189 | \brief Copy Constructor. (not implemented) |
---|
190 | */ |
---|
191 | GSLInterpolation(const GSLInterpolation&); |
---|
192 | |
---|
193 | gsl_interp_accel* accelerator_; |
---|
194 | double evaluation_; |
---|
195 | gsl_interp* interpolator_; |
---|
196 | int gsl_status_; |
---|
197 | double* x_; |
---|
198 | double* y_; |
---|
199 | }; |
---|
200 | |
---|
201 | }}} // of namespaces regression, yat, and theplu |
---|
202 | |
---|
203 | #endif |
---|