mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
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
36namespace mx
37{
38namespace math
39{
40
41/// Interpolate a 1-D data X vs Y discrete function onto a new X axis
42/**
43 * \param interpT one of the <a
44 * href="https://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html#Interpolation-Types">gsl
45 * interpolation types</a>. \param [in] xin the input x-axis \param [in] yin the input y-values \param [in] Nin the
46 * size of the input x-y data series \param [in] xout the desired x-axis \param [out] yout the output interpolated
47 * y-values, pre-allocated \param [in] Nout the size of the output x-y axis
48 *
49 * \returns 0 on success
50 * \returns -1 on a gsl error.
51 *
52 * \todo report errors iaw mxlib standard in gsl_interpolate
53 *
54 * \ingroup interpolation
55 */
56template <typename realT>
58 const gsl_interp_type *interpT, realT *xin, realT *yin, size_t Nin, realT *xout, realT *yout, size_t Nout )
59{
60 static_assert( std::is_same<double, typename std::remove_cv<realT>::type>::value,
61 "GSL Interpolation only works with double" );
62
64
65 gsl_interp *interp = gsl_interp_alloc( interpT, Nin );
66 if( interp == nullptr )
67 {
68 std::cerr << "gsl_interpolate: gsl_interp_alloc failed\n";
69 return -1;
70 }
71
73 if( acc == nullptr )
74 {
75 std::cerr << "gsl_interpolate: gsl_interp_accel_alloc failed\n";
76 return -1;
77 }
78 int gsl_errno = gsl_interp_init( interp, xin, yin, Nin );
79
80 if( gsl_errno != 0 )
81 {
82 std::cerr << "gsl_interpolate: error from gsl_interp_init [" << gsl_strerror( gsl_errno ) << "]\n";
83 gsl_interp_free( interp );
85 return -1;
86 }
87
88 gsl_errno = 0;
89 for( size_t i = 0; i < Nout; ++i )
90 {
91 // Don't error check, let it set NAN
92 gsl_errno += gsl_interp_eval_e( interp, xin, yin, xout[i], acc, &yout[i] );
93 }
94
95 gsl_interp_free( interp );
97
98 if( gsl_errno )
99 {
100 std::cerr << "gsl_interpolate: error(s) reported by gsl_interp_eval_e\n";
101 return -1;
102 }
103
104 return 0;
105}
106
107/// Interpolate a 1-D data X vs Y discrete function onto a new X axis (vector version)
108/**
109 * \param interpT of the <a href="https://www.gnu.org/software/gsl/doc/html/interp.html#d-interpolation-types">gsl
110 * interpolation types</a>. \param [in] xin the input x-axis \param [in] yin the input y-values \param [in] xout the
111 * desired x-axis \param [out] yout the output interpolated y-values, does not need to be allocated
112 *
113 * \retval
114 *
115 * \ingroup interpolation
116 */
117template <typename realT>
119 std::vector<realT> &xin,
120 std::vector<realT> &yin,
121 std::vector<realT> &xout,
122 std::vector<realT> &yout )
123{
124 yout.resize( xout.size() );
125 return gsl_interpolate( interpT, xin.data(), yin.data(), xin.size(), xout.data(), yout.data(), xout.size() );
126}
127
128} // namespace math
129} // namespace mx
130
131#endif // gslInterpolation_hpp
constexpr floatT six_fifths()
Return 6/5 in the specified precision.
int gsl_interpolate(const gsl_interp_type *interpT, realT *xin, realT *yin, size_t Nin, realT *xout, realT *yout, size_t Nout)
Interpolate a 1-D data X vs Y discrete function onto a new X axis.
The mxlib c++ namespace.
Definition mxlib.hpp:37