GammaLib 2.0.0
Loading...
Searching...
No Matches
GNodeArray.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GNodeArray.hpp - Array of nodes class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2008-2020 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GNodeArray.hpp
23 * @brief Node array class interface definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GNODEARRAY_HPP
28#define GNODEARRAY_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <vector>
32#include <string>
33#include "GContainer.hpp"
34
35/* __ Forward declarations _______________________________________________ */
36class GFilename;
37class GVector;
38class GFits;
39class GFitsTable;
40
41
42/***********************************************************************//**
43 * @class GNodeArray
44 *
45 * @brief Node array class
46 *
47 * The node array class collects nodes \f$x_i\f$ that may be used to describe
48 * a functional relation \f$y_i=f(x_i)\f$. This class may be used to perform
49 * a linear interpolation between the nodes to determine any value of
50 * \f$y=f(x)\f$.
51 * Nodes are allocated either from a double precision array, a GVector object
52 * or a std::vector using the nodes() method. Alternatively, the node array
53 * may be built on the fly using the append() method.
54 * Interpolation can be either performed using the interpolate() method
55 * or using the set_value(). In the latter case, the node indices and
56 * weighting factors can be recovered using inx_left(), inx_right(),
57 * wgt_left() and wgt_right().
58 * If the nodes are equally spaced, interpolation is more rapid.
59 ***************************************************************************/
60class GNodeArray : public GContainer {
61
62public:
63 // Constructors and destructors
64 GNodeArray(void);
65 explicit GNodeArray(const GFilename& filename);
66 explicit GNodeArray(const GVector& vector);
67 explicit GNodeArray(const std::vector<double>& vector);
68 GNodeArray(const int& num, const double* array);
69 GNodeArray(const GNodeArray& array);
70 virtual ~GNodeArray(void);
71
72 // Operators
73 GNodeArray& operator= (const GNodeArray & array);
74 double& operator[](const int& index);
75 const double& operator[](const int& index) const;
76
77 // Methods
78 void clear(void);
79 GNodeArray* clone(void) const;
80 std::string classname(void) const;
81 double& at(const int& index);
82 const double& at(const int& index) const;
83 int size(void) const;
84 bool is_empty(void) const;
85 void append(const double& node);
86 void insert(const int& index, const double& node);
87 void remove(const int& index);
88 void reserve(const int& num);
89 void extend(const GNodeArray& nodes);
90 void nodes(const int& num, const double* array);
91 void nodes(const GVector& vector);
92 void nodes(const std::vector<double>& vector);
93 double interpolate(const double& value,
94 const std::vector<double>& vector) const;
95 void set_value(const double& value) const;
96 const int& inx_left(void) const;
97 const int& inx_right(void) const;
98 const double& wgt_left(void) const;
99 const double& wgt_right(void) const;
100 const double& wgt_grad_left(void) const;
101 const double& wgt_grad_right(void) const;
102 void load(const GFilename& filename);
103 void save(const GFilename& filename,
104 const bool& clobber = false) const;
105 void read(const GFitsTable& table);
106 void write(GFits& fits,
107 const std::string& extname = "NODES") const;
108 std::string print(const GChatter& chatter = NORMAL) const;
109
110private:
111 // Methods
112 void init_members(void);
113 void copy_members(const GNodeArray& array);
114 void free_members(void);
115 void setup(void) const;
116
117 // Node values
118 std::vector<double> m_node; //!< Array of nodes
119
120 // Evaluation cache
121 mutable bool m_need_setup; //!< Call of setup is required
122 mutable bool m_is_linear; //!< Nodes form a linear array
123 mutable bool m_has_last_value; //!< Last value is valid
124 mutable std::vector<double> m_step; //!< Distance to next node
125 mutable double m_last_value; //!< Last requested value
126 mutable double m_linear_slope; //!< Slope for linear array
127 mutable double m_linear_offset; //!< Offset for linear array
128 mutable int m_inx_left; //!< Index of left node for linear interpolation
129 mutable int m_inx_right; //!< Index of right node for linear interpolation
130 mutable double m_wgt_left; //!< Weight for left node for linear interpolation
131 mutable double m_wgt_right; //!< Weight for right node for linear interpolation
132 mutable double m_wgt_grad_left; //!< Weight gradient for left node
133 mutable double m_wgt_grad_right; //!< Weight gradient for right node
134};
135
136
137/***********************************************************************//**
138 * @brief Return class name
139 *
140 * @return String containing the class name ("GNodeArray").
141 ***************************************************************************/
142inline
143std::string GNodeArray::classname(void) const
144{
145 return ("GNodeArray");
146}
147
148
149/***********************************************************************//**
150 * @brief Node access operator
151 *
152 * @param[in] index Node index [0,...,size()-1].
153 * @return Node value.
154 *
155 * Returns a reference to the node with the specified @p index. No range
156 * checking is performed on @p index. As this operator may change the
157 * values of the node array, the setup method needs to be called before
158 * doing the interpolation.
159 ***************************************************************************/
160inline
161double& GNodeArray::operator[](const int& index)
162{
163 m_need_setup = true;
164 return (m_node[index]);
165}
166
167
168/***********************************************************************//**
169 * @brief Node access operator (const version)
170 *
171 * @param[in] index Node index [0,...,size()-1].
172 * @return Node value.
173 *
174 * Returns a reference to the node with the specified @p index. No range
175 * checking is performed on @p index.
176 ***************************************************************************/
177inline
178const double& GNodeArray::operator[](const int& index) const
179{
180 return (m_node[index]);
181}
182
183
184/***********************************************************************//**
185 * @brief Return number of nodes in node array
186 *
187 * @return Number of nodes in node array.
188 *
189 * Returns the number of nodes in the node array.
190 ***************************************************************************/
191inline
192int GNodeArray::size(void) const
193{
194 return (int)m_node.size();
195}
196
197
198/***********************************************************************//**
199 * @brief Signals if there are no nodes in node array
200 *
201 * @return True if node array is empty, false otherwise.
202 *
203 * Signals if the node array does not contain any node.
204 ***************************************************************************/
205inline
206bool GNodeArray::is_empty(void) const
207{
208 return (m_node.empty());
209}
210
211
212/***********************************************************************//**
213 * @brief Reserves space for nodes in node array
214 *
215 * @param[in] num Number of nodes.
216 *
217 * Reserves space for @p num nodes in the node array.
218 ***************************************************************************/
219inline
220void GNodeArray::reserve(const int& num)
221{
222 m_node.reserve(num);
223 return;
224}
225
226
227/***********************************************************************//**
228 * @brief Returns left node index
229 *
230 * @return Left node index.
231 *
232 * Returns the left node index to be used for interpolation.
233 ***************************************************************************/
234inline
235const int& GNodeArray::inx_left(void) const
236{
237 return m_inx_left;
238}
239
240
241/***********************************************************************//**
242 * @brief Returns right node index
243 *
244 * @return Right node index.
245 *
246 * Returns the right node index to be used for interpolation.
247 ***************************************************************************/
248inline
249const int& GNodeArray::inx_right(void) const
250{
251 return m_inx_right;
252}
253
254
255/***********************************************************************//**
256 * @brief Returns left node weight
257 *
258 * @return Left node weight.
259 *
260 * Returns the weighting factor for the left node to be used for
261 * interpolation.
262 ***************************************************************************/
263inline
264const double& GNodeArray::wgt_left(void) const
265{
266 return m_wgt_left;
267}
268
269
270/***********************************************************************//**
271 * @brief Returns right node weight
272 *
273 * @return Right node weight.
274 *
275 * Returns the weighting factor for the right node to be used for
276 * interpolation.
277 ***************************************************************************/
278inline
279const double& GNodeArray::wgt_right(void) const
280{
281 return m_wgt_right;
282}
283
284
285/***********************************************************************//**
286 * @brief Returns left node weight gradient
287 *
288 * @return Left node weight gradient.
289 *
290 * Returns the weighting factor gradient with respect to the value for the
291 * left node to be used for interpolation.
292 ***************************************************************************/
293inline
294const double& GNodeArray::wgt_grad_left(void) const
295{
296 return m_wgt_grad_left;
297}
298
299
300/***********************************************************************//**
301 * @brief Returns right node weight gradient
302 *
303 * @return Right node weight gradient.
304 *
305 * Returns the weighting factor gradient with respect to the value for the
306 * right node to be used for interpolation.
307 ***************************************************************************/
308inline
309const double& GNodeArray::wgt_grad_right(void) const
310{
311 return m_wgt_grad_right;
312}
313
314#endif /* GNODEARRAY_HPP */
Definition of interface for container classes.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for container classes.
Filename class.
Definition GFilename.hpp:62
Abstract interface for FITS table.
FITS file class.
Definition GFits.hpp:63
Node array class.
double m_wgt_grad_left
Weight gradient for left node.
void save(const GFilename &filename, const bool &clobber=false) const
Save node array into FITS file.
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
GNodeArray & operator=(const GNodeArray &array)
Assignment operator.
GNodeArray(void)
Void constructor.
const double & wgt_grad_left(void) const
Returns left node weight gradient.
double interpolate(const double &value, const std::vector< double > &vector) const
Interpolate value.
void copy_members(const GNodeArray &array)
Copy class members.
double & operator[](const int &index)
Node access operator.
void free_members(void)
Delete class members.
bool m_has_last_value
Last value is valid.
double m_wgt_right
Weight for right node for linear interpolation.
double m_linear_slope
Slope for linear array.
double m_wgt_grad_right
Weight gradient for right node.
virtual ~GNodeArray(void)
Destructor.
bool m_need_setup
Call of setup is required.
std::vector< double > m_node
Array of nodes.
const int & inx_right(void) const
Returns right node index.
const int & inx_left(void) const
Returns left node index.
const double & wgt_grad_right(void) const
Returns right node weight gradient.
bool is_empty(void) const
Signals if there are no nodes in node array.
double & at(const int &index)
Node access operator.
void reserve(const int &num)
Reserves space for nodes in node array.
const double & wgt_right(void) const
Returns right node weight.
bool m_is_linear
Nodes form a linear array.
const double & wgt_left(void) const
Returns left node weight.
void remove(const int &index)
Remove one node into array.
int m_inx_right
Index of right node for linear interpolation.
void load(const GFilename &filename)
Load nodes from FITS file.
void init_members(void)
Initialise class members.
void insert(const int &index, const double &node)
Insert one node into array.
void clear(void)
Clear node array.
std::string classname(void) const
Return class name.
GNodeArray * clone(void) const
Clone node array.
void extend(const GNodeArray &nodes)
Append node array.
void setup(void) const
Compute distance array and linear slope/offset.
void nodes(const int &num, const double *array)
Set node array.
double m_wgt_left
Weight for left node for linear interpolation.
std::string print(const GChatter &chatter=NORMAL) const
Print nodes.
int m_inx_left
Index of left node for linear interpolation.
int size(void) const
Return number of nodes in node array.
double m_linear_offset
Offset for linear array.
std::vector< double > m_step
Distance to next node.
void write(GFits &fits, const std::string &extname="NODES") const
Write nodes into FITS object.
double m_last_value
Last requested value.
void append(const double &node)
Append one node to array.
void read(const GFitsTable &table)
Read nodes from FITS table.
Vector class.
Definition GVector.hpp:46