mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
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#ifndef mx_math_logInterpolator_hpp
30#define mx_math_logInterpolator_hpp
31
32#include <cmath>
33
34#include "../mxlib.hpp"
35#include "gslInterpolator.hpp"
36
37namespace mx
38{
39namespace math
40{
41
42/// Interpolate a function in log space
43/** Given a discrete function, conduct linear interpolation in log space.
44 * The input vectors are converted to their log10 values. Linear interpolation
45 * using gslInterpolator is conducted on the log10 of the input value. The output
46 * is converted back. So the result is
47 * \code
48 * y = pow(10, interp(log10(x))).
49 * \endcode
50 *
51 * \ingroup interpolation
52 */
53template <typename interpT>
55{
56
57 public:
58 typedef typename interpT::realT realT;
59
60 protected:
61 gslInterpolator<interpT> m_interp; ///< The interpolator
62
63 std::vector<realT> m_logx; ///< Internal storage of the log10 values of the x values
64 std::vector<realT> m_logy; ///< Internal storage of the lgo10 values of the y values
65
66 public:
67 /// Default constructor
69 {
70 }
71
72 /// Convert the inputs to their log10 values, and construct the interpolator.
73 /**
74 * \throws mxException if vectors are not the same size.
75 */
76 logInterpolator( const std::vector<realT> &x, /// [in] the input x-axis
77 const std::vector<realT> &y /// [in] the input y-axis
78 )
79 {
80 setup( x, y );
81 }
82
83 /// Convert the inputs to their log10 values, and construct the interpolator.
84 /**
85 * \throws mx::err::sizeerr if vectors are not the same size.
86 * \throws mx::err::invalidarg if any of the values are <= 0
87 */
88 void setup( const std::vector<realT> &x, /// [in] the input x-axis
89 const std::vector<realT> &y /// [in] the input y-axis
90 )
91 {
92 if( x.size() != y.size() )
93 {
94 throw(mx::exception(error_t::sizeerr, "vectors must have same size" ));
95 }
96
97 m_logx.resize( x.size() );
98 m_logy.resize( y.size() );
99
100 for( size_t n = 0; n < x.size(); ++n )
101 {
102 if( x[n] <= 0 )
103 {
104 throw(mx::exception(error_t::invalidarg, "x values must > 0" ));
105 }
106
107 if( y[n] <= 0 )
108 {
109 throw(mx::exception(error_t::invalidarg, "y values must > 0" ));
110 }
111
112 m_logx[n] = std::log10( x[n] );
113 m_logy[n] = std::log10( y[n] );
114 }
115
116 m_interp.setup( m_logx, m_logy );
117 }
118
119 /// Calculate the interpolated value at the input \par x.
120 /**
121 * \returns the interpolated value
122 */
123 realT operator()( const realT &x )
124 {
125 return std::pow( static_cast<realT>( 10 ), m_interp( std::log10( x ) ) );
126 }
127};
128
129} // namespace math
130} // namespace mx
131
132#endif // mx_math_logInterpolator_hpp
Augments an exception with the source file and line.
Definition exception.hpp:42
Class to manage interpolation using the GSL interpolation library.
logInterpolator()
Default constructor.
std::vector< realT > m_logx
Internal storage of the log10 values of the x values.
gslInterpolator< interpT > m_interp
The interpolator.
logInterpolator(const std::vector< realT > &x, const std::vector< realT > &y)
Convert the inputs to their log10 values, and construct the interpolator.
realT operator()(const realT &x)
Calculate the interpolated value at the input.
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.
@ sizeerr
A size was invalid or calculated incorrectly.
Definition error_t.hpp:35
@ invalidarg
An argument was invalid.
Definition error_t.hpp:29
Class for managing 1-D interpolation using the GNU Scientific Library.
Declarations of some libarary wide utilities.
The mxlib c++ namespace.
Definition mxlib.hpp:37