GammaLib  1.7.0.dev
 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-2016 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  void load(const GFilename& filename);
101  void save(const GFilename& filename,
102  const bool& clobber = false) const;
103  void read(const GFitsTable& table);
104  void write(GFits& fits,
105  const std::string& extname = "NODES") const;
106  std::string print(const GChatter& chatter = NORMAL) const;
107 
108 private:
109  // Methods
110  void init_members(void);
111  void copy_members(const GNodeArray& array);
112  void free_members(void);
113  void setup(void) const;
114 
115  // Node values
116  std::vector<double> m_node; //!< Array of nodes
117 
118  // Evaluation cache
119  mutable bool m_need_setup; //!< Call of setup is required
120  mutable bool m_is_linear; //!< Nodes form a linear array
121  mutable bool m_has_last_value; //!< Last value is valid
122  mutable std::vector<double> m_step; //!< Distance to next node
123  mutable double m_last_value; //!< Last requested value
124  mutable double m_linear_slope; //!< Slope for linear array
125  mutable double m_linear_offset; //!< Offset for linear array
126  mutable int m_inx_left; //!< Index of left node for linear interpolation
127  mutable int m_inx_right; //!< Index of right node for linear interpolation
128  mutable double m_wgt_left; //!< Weight for left node for linear interpolation
129  mutable double m_wgt_right; //!< Weight for right node for linear interpolation
130 };
131 
132 
133 /***********************************************************************//**
134  * @brief Return class name
135  *
136  * @return String containing the class name ("GNodeArray").
137  ***************************************************************************/
138 inline
139 std::string GNodeArray::classname(void) const
140 {
141  return ("GNodeArray");
142 }
143 
144 
145 /***********************************************************************//**
146  * @brief Node access operator
147  *
148  * @param[in] index Node index [0,...,size()-1].
149  * @return Node value.
150  *
151  * Returns a reference to the node with the specified @p index. No range
152  * checking is performed on @p index. As this operator may change the
153  * values of the node array, the setup method needs to be called before
154  * doing the interpolation.
155  ***************************************************************************/
156 inline
157 double& GNodeArray::operator[](const int& index)
158 {
159  m_need_setup = true;
160  return (m_node[index]);
161 }
162 
163 
164 /***********************************************************************//**
165  * @brief Node access operator (const version)
166  *
167  * @param[in] index Node index [0,...,size()-1].
168  * @return Node value.
169  *
170  * Returns a reference to the node with the specified @p index. No range
171  * checking is performed on @p index.
172  ***************************************************************************/
173 inline
174 const double& GNodeArray::operator[](const int& index) const
175 {
176  return (m_node[index]);
177 }
178 
179 
180 /***********************************************************************//**
181  * @brief Return number of nodes in node array
182  *
183  * @return Number of nodes in node array.
184  *
185  * Returns the number of nodes in the node array.
186  ***************************************************************************/
187 inline
188 int GNodeArray::size(void) const
189 {
190  return (int)m_node.size();
191 }
192 
193 
194 /***********************************************************************//**
195  * @brief Signals if there are no nodes in node array
196  *
197  * @return True if node array is empty, false otherwise.
198  *
199  * Signals if the node array does not contain any node.
200  ***************************************************************************/
201 inline
202 bool GNodeArray::is_empty(void) const
203 {
204  return (m_node.empty());
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Reserves space for nodes in node array
210  *
211  * @param[in] num Number of nodes.
212  *
213  * Reserves space for @p num nodes in the node array.
214  ***************************************************************************/
215 inline
216 void GNodeArray::reserve(const int& num)
217 {
218  m_node.reserve(num);
219  return;
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Returns left node index
225  *
226  * @return Left node index.
227  *
228  * Returns the left node index to be used for interpolation.
229  ***************************************************************************/
230 inline
231 const int& GNodeArray::inx_left(void) const
232 {
233  return m_inx_left;
234 }
235 
236 
237 /***********************************************************************//**
238  * @brief Returns right node index
239  *
240  * @return Right node index.
241  *
242  * Returns the right node index to be used for interpolation.
243  ***************************************************************************/
244 inline
245 const int& GNodeArray::inx_right(void) const
246 {
247  return m_inx_right;
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief Returns left node weight
253  *
254  * @return Left node weight.
255  *
256  * Returns the weighting factor for the left node to be used for
257  * interpolation.
258  ***************************************************************************/
259 inline
260 const double& GNodeArray::wgt_left(void) const
261 {
262  return m_wgt_left;
263 }
264 
265 
266 /***********************************************************************//**
267  * @brief Returns right node weight
268  *
269  * @return Right node weight.
270  *
271  * Returns the weighting factor for the right node to be used for
272  * interpolation.
273  ***************************************************************************/
274 inline
275 const double& GNodeArray::wgt_right(void) const
276 {
277  return m_wgt_right;
278 }
279 
280 #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:188
virtual ~GNodeArray(void)
Destructor.
Definition: GNodeArray.cpp:183
std::string classname(void) const
Return class name.
Definition: GNodeArray.hpp:139
Node array class.
Definition: GNodeArray.hpp:60
void free_members(void)
Delete class members.
Definition: GNodeArray.cpp:956
void load(const GFilename &filename)
Load nodes from FITS file.
Definition: GNodeArray.cpp:704
bool m_has_last_value
Last value is valid.
Definition: GNodeArray.hpp:121
double m_linear_offset
Offset for linear array.
Definition: GNodeArray.hpp:125
const double & wgt_left(void) const
Returns left node weight.
Definition: GNodeArray.hpp:260
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:216
FITS file class.
Definition: GFits.hpp:63
void set_value(const double &value) const
Set indices and weighting factors for interpolation.
Definition: GNodeArray.cpp:580
double m_linear_slope
Slope for linear array.
Definition: GNodeArray.hpp:124
std::vector< double > m_node
Array of nodes.
Definition: GNodeArray.hpp:116
bool is_empty(void) const
Signals if there are no nodes in node array.
Definition: GNodeArray.hpp:202
void save(const GFilename &filename, const bool &clobber=false) const
Save node array into FITS file.
Definition: GNodeArray.cpp:746
void write(GFits &fits, const std::string &extname="NODES") const
Write nodes into FITS object.
Definition: GNodeArray.cpp:807
void init_members(void)
Initialise class members.
Definition: GNodeArray.cpp:906
double m_wgt_right
Weight for right node for linear interpolation.
Definition: GNodeArray.hpp:129
double m_last_value
Last requested value.
Definition: GNodeArray.hpp:123
double m_wgt_left
Weight for left node for linear interpolation.
Definition: GNodeArray.hpp:128
void setup(void) const
Compute distance array and linear slope/offset.
Definition: GNodeArray.cpp:971
const double & wgt_right(void) const
Returns right node weight.
Definition: GNodeArray.hpp:275
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:122
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:231
bool m_need_setup
Call of setup is required.
Definition: GNodeArray.hpp:119
GNodeArray(void)
Void constructor.
Definition: GNodeArray.cpp:69
int m_inx_left
Index of left node for linear interpolation.
Definition: GNodeArray.hpp:126
int m_inx_right
Index of right node for linear interpolation.
Definition: GNodeArray.hpp:127
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:245
std::string print(const GChatter &chatter=NORMAL) const
Print nodes.
Definition: GNodeArray.cpp:845
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:770
Definition of interface for container classes.
Vector class.
Definition: GVector.hpp:46
bool m_is_linear
Nodes form a linear array.
Definition: GNodeArray.hpp:120
void append(const double &node)
Append one node to array.
Definition: GNodeArray.cpp:351
Interface class for container classes.
Definition: GContainer.hpp:52
double & operator[](const int &index)
Node access operator.
Definition: GNodeArray.hpp:157
void copy_members(const GNodeArray &array)
Copy class members.
Definition: GNodeArray.cpp:932