GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 _______________________________________________ */
36 class GFilename;
37 class GVector;
38 class GFits;
39 class 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  ***************************************************************************/
60 class GNodeArray : public GContainer {
61 
62 public:
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 
110 private:
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  ***************************************************************************/
142 inline
143 std::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  ***************************************************************************/
160 inline
161 double& 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  ***************************************************************************/
177 inline
178 const 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  ***************************************************************************/
191 inline
192 int 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  ***************************************************************************/
205 inline
206 bool 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  ***************************************************************************/
219 inline
220 void 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  ***************************************************************************/
234 inline
235 const 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  ***************************************************************************/
248 inline
249 const 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  ***************************************************************************/
263 inline
264 const 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  ***************************************************************************/
278 inline
279 const 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  ***************************************************************************/
293 inline
294 const 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  ***************************************************************************/
308 inline
309 const double& GNodeArray::wgt_grad_right(void) const
310 {
311  return m_wgt_grad_right;
312 }
313 
314 #endif /* GNODEARRAY_HPP */
GNodeArray & operator=(const GNodeArray &array)
Assignment operator.
Definition: GNodeArray.cpp:205
int size(void) const
Return number of nodes in node array.
Definition: GNodeArray.hpp:192
virtual ~GNodeArray(void)
Destructor.
Definition: GNodeArray.cpp:183
std::string classname(void) const
Return class name.
Definition: GNodeArray.hpp:143
Node array class.
Definition: GNodeArray.hpp:60
const double & wgt_grad_left(void) const
Returns left node weight gradient.
Definition: GNodeArray.hpp:294
void free_members(void)
Delete class members.
Definition: GNodeArray.cpp:968
void load(const GFilename &filename)
Load nodes from FITS file.
Definition: GNodeArray.cpp:709
bool m_has_last_value
Last value is valid.
Definition: GNodeArray.hpp:123
double m_linear_offset
Offset for linear array.
Definition: GNodeArray.hpp:127
const double & wgt_left(void) const
Returns left node weight.
Definition: GNodeArray.hpp:264
void clear(void)
Clear node array.
Definition: GNodeArray.cpp:235
void reserve(const int &num)
Reserves space for nodes in node array.
Definition: GNodeArray.hpp:220
FITS file class.
Definition: GFits.hpp:63
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
Definition: GNodeArray.cpp:587
double m_wgt_grad_left
Weight gradient for left node.
Definition: GNodeArray.hpp:132
double m_linear_slope
Slope for linear array.
Definition: GNodeArray.hpp:126
std::vector< double > m_node
Array of nodes.
Definition: GNodeArray.hpp:118
bool is_empty(void) const
Signals if there are no nodes in node array.
Definition: GNodeArray.hpp:206
void save(const GFilename &filename, const bool &clobber=false) const
Save node array into FITS file.
Definition: GNodeArray.cpp:751
void write(GFits &fits, const std::string &extname="NODES") const
Write nodes into FITS object.
Definition: GNodeArray.cpp:812
void init_members(void)
Initialise class members.
Definition: GNodeArray.cpp:914
double m_wgt_right
Weight for right node for linear interpolation.
Definition: GNodeArray.hpp:131
double m_last_value
Last requested value.
Definition: GNodeArray.hpp:125
double m_wgt_left
Weight for left node for linear interpolation.
Definition: GNodeArray.hpp:130
void setup(void) const
Compute distance array and linear slope/offset.
Definition: GNodeArray.cpp:983
const double & wgt_right(void) const
Returns right node weight.
Definition: GNodeArray.hpp:279
Filename class.
Definition: GFilename.hpp:62
double & at(const int &index)
Node access operator.
Definition: GNodeArray.cpp:271
std::vector< double > m_step
Distance to next node.
Definition: GNodeArray.hpp:124
double interpolate(const double &value, const std::vector< double > &vector) const
Interpolate value.
Definition: GNodeArray.cpp:536
void extend(const GNodeArray &nodes)
Append node array.
Definition: GNodeArray.cpp:440
Abstract interface for FITS table.
Definition: GFitsTable.hpp:44
GChatter
Definition: GTypemaps.hpp:33
const int & inx_left(void) const
Returns left node index.
Definition: GNodeArray.hpp:235
bool m_need_setup
Call of setup is required.
Definition: GNodeArray.hpp:121
GNodeArray(void)
Void constructor.
Definition: GNodeArray.cpp:69
int m_inx_left
Index of left node for linear interpolation.
Definition: GNodeArray.hpp:128
int m_inx_right
Index of right node for linear interpolation.
Definition: GNodeArray.hpp:129
void nodes(const int &num, const double *array)
Set node array.
Definition: GNodeArray.cpp:325
const int & inx_right(void) const
Returns right node index.
Definition: GNodeArray.hpp:249
double m_wgt_grad_right
Weight gradient for right node.
Definition: GNodeArray.hpp:133
std::string print(const GChatter &chatter=NORMAL) const
Print nodes.
Definition: GNodeArray.cpp:850
GNodeArray * clone(void) const
Clone node array.
Definition: GNodeArray.cpp:253
void insert(const int &index, const double &node)
Insert one node into array.
Definition: GNodeArray.cpp:376
void read(const GFitsTable &table)
Read nodes from FITS table.
Definition: GNodeArray.cpp:775
Definition of interface for container classes.
Vector class.
Definition: GVector.hpp:46
bool m_is_linear
Nodes form a linear array.
Definition: GNodeArray.hpp:122
void append(const double &node)
Append one node to array.
Definition: GNodeArray.cpp:351
const double & wgt_grad_right(void) const
Returns right node weight gradient.
Definition: GNodeArray.hpp:309
Interface class for container classes.
Definition: GContainer.hpp:52
double & operator[](const int &index)
Node access operator.
Definition: GNodeArray.hpp:161
void copy_members(const GNodeArray &array)
Copy class members.
Definition: GNodeArray.cpp:942