mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
logInterpolator.hpp
Go to the documentation of this file.
1 /** \file logInterpolator.hpp
2  * \brief Interpolation in log space.
3  *
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  * \ingroup gen_math_files
7  *
8  */
9 
10 //***********************************************************************//
11 // Copyright 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 #include "../mxException.hpp"
30 #include "gslInterpolator.hpp"
31 
32 namespace mx
33 {
34 namespace math
35 {
36 
37 /// Interpolate a function in log space
38 /** Given a discrete function, conduct linear interpolation in log space.
39  * The input vectors are converted to their log10 values. Linear interpolation
40  * using gslInterpolator is conducted on the log10 of the input value. The output
41  * is converted back. So the result is
42  * \code
43  * y = pow(10, interp(log10(x))).
44  * \endcode
45  *
46  * \ingroup interpolation
47  */
48 template<typename realT>
50 {
51 
52 protected:
54 
55  std::vector<realT> m_logx; ///< Internal storage of the log10 values of the x values
56  std::vector<realT> m_logy; ///< Internal storage of the lgo10 values of the y values
57 
58 public:
59  /// Default constructor
61 
62  /// Convert the inputs to their log10 values, and construct the interpolator.
63  /**
64  * \throws mxException if vectors are not the same size.
65  */
66  logInterpolator( const std::vector<realT> & x, /// [in] the input x-axis
67  const std::vector<realT> & y /// [in] the input y-axis
68  )
69  {
70  setup(x,y);
71  }
72 
73  /// Convert the inputs to their log10 values, and construct the interpolator.
74  /**
75  * \throws mx::err::sizeerr if vectors are not the same size.
76  * \throws mx::err::invalidarg if any of the values are <= 0
77  */
78  void setup( const std::vector<realT> & x, /// [in] the input x-axis
79  const std::vector<realT> & y /// [in] the input y-axis
80  )
81  {
82  if(x.size() != y.size())
83  {
84  mxThrowException(err::sizeerr, "radprofIntegral", "vectors must have same size");
85  }
86 
87  m_logx.resize(x.size());
88  m_logy.resize(y.size());
89 
90  for(size_t n = 0; n < x.size(); ++n)
91  {
92  if(x[n] <= 0)
93  {
94  mxThrowException(err::invalidarg, "radprofIntegral", "x values must > 0");
95  }
96 
97  if(y[n] <= 0)
98  {
99  mxThrowException(err::invalidarg, "radprofIntegral", "y values must > 0");
100  }
101 
102 
103  m_logx[n] = log10(x[n]);
104  m_logy[n] = log10(y[n]);
105 
106  }
107 
108  m_interp.setup(m_logx, m_logy);
109  }
110 
111  /// Calculate the interpolated value at the input \par x.
112  /**
113  * \returns the interpolated value
114  */
115  realT operator()(const realT & x)
116  {
117  return pow(static_cast<realT>(10), m_interp(log10(x)));
118  }
119 
120 };
121 
122 } //namespace math
123 } //namespace mx
mxException for invalid arguments
mxException for a size error
Class to manage interpolation using the GSL interpolation library.
Interpolate a function in log space.
logInterpolator()
Default constructor.
logInterpolator(const std::vector< realT > &x, const std::vector< realT > &y)
Convert the inputs to their log10 values, and construct the interpolator.
gslInterpolator< gsl_interp_linear< realT > > m_interp
The interpolator.
void setup(const std::vector< realT > &x, const std::vector< realT > &y)
Convert the inputs to their log10 values, and construct the interpolator.
std::vector< realT > m_logy
Internal storage of the lgo10 values of the y values.
std::vector< realT > m_logx
Internal storage of the log10 values of the x values.
realT operator()(const realT &x)
Calculate the interpolated value at the input.
Class for managing 1-D interpolation using the GNU Scientific Library.
The mxlib c++ namespace.
Definition: mxError.hpp:107