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