mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
gslInterpolation.hpp
Go to the documentation of this file.
1 /** \file gslInterpolation.hpp
2  * \brief Wrappers for using the GNU Scientific Library 1-D interpolation functions
3  *
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  * \ingroup gen_math_files
7  *
8  */
9 
10 //***********************************************************************//
11 // Copyright 2015-2022 Jared R. Males (jaredmales@gmail.com)
12 //
13 // This file is part of mxlib.
14 //
15 // mxlib is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 // mxlib is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
27 //***********************************************************************//
28 
29 #ifndef gslInterpolation_hpp
30 #define gslInterpolation_hpp
31 
32 #include <vector>
33 #include <gsl/gsl_interp.h>
34 #include <gsl/gsl_errno.h>
35 
36 #include "../mxException.hpp"
37 
38 namespace mx
39 {
40 namespace math
41 {
42 
43 ///Interpolate a 1-D data X vs Y discrete function onto a new X axis
44 /**
45  * \param interpT one of the <a href="https://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html#Interpolation-Types">gsl interpolation types</a>.
46  * \param [in] xin the input x-axis
47  * \param [in] yin the input y-values
48  * \param [in] Nin the size of the input x-y data series
49  * \param [in] xout the desired x-axis
50  * \param [out] yout the output interpolated y-values, pre-allocated
51  * \param [in] Nout the size of the output x-y axis
52  *
53  * \returns 0 on success
54  * \returns -1 on a gsl error.
55  *
56  * \todo report errors iaw mxlib standard in gsl_interpolate
57  *
58  * \ingroup interpolation
59  */
60 template<typename realT>
61 int gsl_interpolate( const gsl_interp_type * interpT,
62  realT *xin,
63  realT *yin,
64  size_t Nin,
65  realT *xout,
66  realT *yout,
67  size_t Nout )
68 {
69  static_assert( std::is_same<double, typename std::remove_cv<realT>::type>::value, "GSL Interpolation only works with double");
70 
71  gsl_set_error_handler_off();
72 
73  gsl_interp * interp = gsl_interp_alloc(interpT, Nin);
74  if(interp == nullptr)
75  {
76  std::cerr << "gsl_interpolate: gsl_interp_alloc failed\n";
77  return -1;
78  }
79 
80  gsl_interp_accel * acc = gsl_interp_accel_alloc ();
81  if(acc == nullptr)
82  {
83  std::cerr << "gsl_interpolate: gsl_interp_accel_alloc failed\n";
84  return -1;
85 
86  }
87  int gsl_errno = gsl_interp_init(interp, xin, yin, Nin);
88 
89  if(gsl_errno != 0)
90  {
91  std::cerr << "gsl_interpolate: error from gsl_interp_init [" << gsl_strerror(gsl_errno) << "]\n";
92  gsl_interp_free(interp);
93  gsl_interp_accel_free (acc);
94  return -1;
95  }
96 
97  gsl_errno = 0;
98  for(size_t i=0;i<Nout; ++i)
99  {
100  //Don't error check, let it set NAN
101  gsl_errno += gsl_interp_eval_e (interp, xin, yin, xout[i], acc, &yout[i]);
102  }
103 
104  gsl_interp_free(interp);
105  gsl_interp_accel_free (acc);
106 
107  if(gsl_errno)
108  {
109  std::cerr << "gsl_interpolate: error(s) reported by gsl_interp_eval_e\n";
110  return -1;
111  }
112 
113  return 0;
114 }
115 
116 ///Interpolate a 1-D data X vs Y discrete function onto a new X axis (vector version)
117 /**
118  * \param interpT of the <a href="https://www.gnu.org/software/gsl/doc/html/interp.html#d-interpolation-types">gsl interpolation types</a>.
119  * \param [in] xin the input x-axis
120  * \param [in] yin the input y-values
121  * \param [in] xout the desired x-axis
122  * \param [out] yout the output interpolated y-values, does not need to be allocated
123  *
124  * \retval
125  *
126  * \ingroup interpolation
127  */
128 template<typename realT>
129 int gsl_interpolate( const gsl_interp_type * interpT,
130  std::vector<realT> & xin,
131  std::vector<realT> & yin,
132  std::vector<realT> & xout,
133  std::vector<realT> & yout )
134 {
135  yout.resize(xout.size());
136  return gsl_interpolate(interpT, xin.data(), yin.data(), xin.size(), xout.data(), yout.data(), xout.size());
137 }
138 
139 } //namespace math
140 } //namespace mx
141 
142 #endif //gslInterpolation_hpp
int gsl_interpolate(const gsl_interp_type *interpT, std::vector< realT > &xin, std::vector< realT > &yin, std::vector< realT > &xout, std::vector< realT > &yout)
Interpolate a 1-D data X vs Y discrete function onto a new X axis (vector version)
The mxlib c++ namespace.
Definition: mxError.hpp:107