GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GNdarray.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GNdarray.hpp - N-dimensional array class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2016-2022 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 GNdarray.hpp
23  * @brief N-dimensional array class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GNDARRAY_HPP
28 #define GNDARRAY_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GBase.hpp"
34 
35 
36 /***********************************************************************//**
37  * @class GNdarray
38  *
39  * @brief N-dimensional array class
40  *
41  * This class implements a n-dimensional double precision floating point
42  * array.
43  ***************************************************************************/
44 class GNdarray : public GBase {
45 
46  // Friend functions
47  friend double min(const GNdarray& array);
48  friend double max(const GNdarray& array);
49  friend double sum(const GNdarray& array);
50  friend GNdarray acos(const GNdarray& array);
51  friend GNdarray acosh(const GNdarray& array);
52  friend GNdarray asin(const GNdarray& array);
53  friend GNdarray asinh(const GNdarray& array);
54  friend GNdarray atan(const GNdarray& array);
55  friend GNdarray atanh(const GNdarray& array);
56  friend GNdarray cos(const GNdarray& array);
57  friend GNdarray cosh(const GNdarray& array);
58  friend GNdarray exp(const GNdarray& array);
59  friend GNdarray abs(const GNdarray& array);
60  friend GNdarray log(const GNdarray& array);
61  friend GNdarray log10(const GNdarray& array);
62  friend GNdarray sign(const GNdarray& array);
63  friend GNdarray sin(const GNdarray& array);
64  friend GNdarray sinh(const GNdarray& array);
65  friend GNdarray sqrt(const GNdarray& array);
66  friend GNdarray tan(const GNdarray& array);
67  friend GNdarray tanh(const GNdarray& array);
68  friend GNdarray pow(const GNdarray& array, const double& power);
69 
70 public:
71  // Constructors and destructors
72  GNdarray(void);
73  explicit GNdarray(const int& nx);
74  GNdarray(const int& nx, const int& ny);
75  GNdarray(const int& nx, const int& ny, const int& nz);
76  GNdarray(const std::vector<int>& n);
77  GNdarray(const GNdarray& array);
78  virtual ~GNdarray(void);
79 
80  // Element access operators
81  double& operator()(const int& ix);
82  double& operator()(const int& ix, const int& iy);
83  double& operator()(const int& ix, const int& iy, const int& iz);
84  double& operator()(const std::vector<int>& i);
85  const double& operator()(const int& ix) const;
86  const double& operator()(const int& ix, const int& iy) const;
87  const double& operator()(const int& ix, const int& iy, const int& iz) const;
88  const double& operator()(const std::vector<int>& i) const;
89 
90  // Operators
91  GNdarray& operator=(const GNdarray& array);
92  bool operator==(const GNdarray& array) const;
93  bool operator!=(const GNdarray& array) const;
94  GNdarray& operator+=(const GNdarray& array);
95  GNdarray& operator-=(const GNdarray& array);
96  GNdarray& operator*=(const GNdarray& array);
97  GNdarray& operator/=(const GNdarray& array);
98  GNdarray& operator+=(const double& value);
99  GNdarray& operator-=(const double& value);
100  GNdarray& operator*=(const double& value);
101  GNdarray& operator/=(const double& value);
102  GNdarray operator-(void) const;
103 
104  // Methods
105  void clear(void);
106  GNdarray* clone(void) const;
107  std::string classname(void) const;
108  int dim(void) const;
109  int size(void) const;
110  const std::vector<int>& shape(void) const;
111  const std::vector<int>& strides(void) const;
112  void shape(const std::vector<int>& shape);
113  int index(const std::vector<int>& i) const;
114  std::vector<int> index(const int& index) const;
115  double& at(const int& ix);
116  double& at(const int& ix, const int& iy);
117  double& at(const int& ix, const int& iy, const int& iz);
118  double& at(const std::vector<int>& i);
119  const double& at(const int& ix) const;
120  const double& at(const int& ix, const int& iy) const;
121  const double& at(const int& ix, const int& iy, const int& iz) const;
122  const double& at(const std::vector<int>& i) const;
123  const double* data(void) const;
124  double* data(void);
125  std::string print(const GChatter& chatter = NORMAL) const;
126 
127 protected:
128  // Protected methods
129  void init_members(void);
130  void copy_members(const GNdarray& array);
131  void free_members(void);
132  bool has_same_shape(const GNdarray& array) const;
133  void require_same_shape(const std::string& method, const GNdarray& array) const;
134 
135  // Protected members
136  std::vector<int> m_shape; //!< Array dimensions
137  std::vector<int> m_strides; //!< Steps in each dimension when traversing array
138  std::vector<double> m_data; //!< Array data
139 };
140 
141 
142 /***********************************************************************//**
143  * @brief Return class name
144  *
145  * @return String containing the class name ("GNdarray").
146  ***************************************************************************/
147 inline
148 std::string GNdarray::classname(void) const
149 {
150  return ("GNdarray");
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief 1-dimensional array element access operator
156  *
157  * @param[in] ix Element index [0,...,shape(0)-1].
158  * @return Reference to array element.
159  ***************************************************************************/
160 inline
161 double& GNdarray::operator()(const int& ix)
162 {
163  // Return array element
164  return m_data[ix];
165 }
166 
167 
168 /***********************************************************************//**
169  * @brief 2-dimensional array element access operator
170  *
171  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
172  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
173  * @return Reference to array element.
174  ***************************************************************************/
175 inline
176 double& GNdarray::operator()(const int& ix, const int& iy)
177 {
178  // Return array element
179  return m_data[ix+m_strides[1]*iy];
180 }
181 
182 
183 /***********************************************************************//**
184  * @brief 3-dimensional array element access operator
185  *
186  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
187  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
188  * @param[in] iz Index in third dimension [0,...,shape(2)-1].
189  * @return Reference to array element.
190  ***************************************************************************/
191 inline
192 double& GNdarray::operator()(const int& ix, const int& iy, const int& iz)
193 {
194  // Return array element
195  return m_data[ix+m_strides[1]*iy+m_strides[2]*iz];
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief n-dimensional array element access operator
201  *
202  * @param[in] i Index vector.
203  * @return Reference to array element.
204  ***************************************************************************/
205 inline
206 double& GNdarray::operator()(const std::vector<int>& i)
207 {
208  // Return array element
209  return m_data[index(i)];
210 }
211 
212 
213 /***********************************************************************//**
214  * @brief 1-dimensional array element access operator (const variant)
215  *
216  * @param[in] ix Element index [0,...,shape(0)-1].
217  * @return Const reference to array element.
218  ***************************************************************************/
219 inline
220 const double& GNdarray::operator()(const int& ix) const
221 {
222  // Return array element
223  return m_data[ix];
224 }
225 
226 
227 /***********************************************************************//**
228  * @brief 2-dimensional array element access operator (const variant)
229  *
230  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
231  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
232  * @return Const reference to array element.
233  ***************************************************************************/
234 inline
235 const double& GNdarray::operator()(const int& ix, const int& iy) const
236 {
237  // Return array element
238  return m_data[ix+m_strides[1]*iy];
239 }
240 
241 
242 /***********************************************************************//**
243  * @brief 3-dimensional array element access operator (const variant)
244  *
245  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
246  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
247  * @param[in] iz Index in third dimension [0,...,shape(2)-1].
248  * @return Const reference to array element.
249  ***************************************************************************/
250 inline
251 const double& GNdarray::operator()(const int& ix, const int& iy, const int& iz) const
252 {
253  // Return array element
254  return m_data[ix+m_strides[1]*iy+m_strides[2]*iz];
255 }
256 
257 
258 /***********************************************************************//**
259  * @brief n-dimensional array element access operator (const variant)
260  *
261  * @param[in] i Index vector.
262  * @return Const reference to array element.
263  ***************************************************************************/
264 inline
265 const double& GNdarray::operator()(const std::vector<int>& i) const
266 {
267  // Return array element
268  return m_data[index(i)];
269 }
270 
271 
272 /***********************************************************************//**
273  * @brief Return dimension of array
274  *
275  * @return Dimension of array
276  *
277  * Returns the dimension of the array.
278  ***************************************************************************/
279 inline
280 int GNdarray::dim(void) const
281 {
282  return int(m_shape.size());
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Return number of elements in array
288  *
289  * @return Number of elements in array
290  *
291  * Returns the number of elements in the array.
292  ***************************************************************************/
293 inline
294 int GNdarray::size(void) const
295 {
296  return int(m_data.size());
297 }
298 
299 
300 /***********************************************************************//**
301  * @brief Return shape of array
302  *
303  * @return Shape of array
304  *
305  * Returns the shape of the array.
306  ***************************************************************************/
307 inline
308 const std::vector<int>& GNdarray::shape(void) const
309 {
310  return m_shape;
311 }
312 
313 
314 /***********************************************************************//**
315  * @brief Return strides of array
316  *
317  * @return Strides of array
318  *
319  * Returns the strides of the array.
320  ***************************************************************************/
321 inline
322 const std::vector<int>& GNdarray::strides(void) const
323 {
324  return m_strides;
325 }
326 
327 
328 /***********************************************************************//**
329  * @brief 1-dimensional array element access with range checking
330  *
331  * @param[in] ix Element index [0,...,shape(0)-1].
332  * @return Reference to array element.
333  ***************************************************************************/
334 inline
335 double& GNdarray::at(const int& ix)
336 {
337  return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix));
338 }
339 
340 
341 /***********************************************************************//**
342  * @brief 2-dimensional array element access with range checking
343  *
344  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
345  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
346  * @return Reference to array element.
347  ***************************************************************************/
348 inline
349 double& GNdarray::at(const int& ix, const int& iy)
350 {
351  return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix,iy));
352 }
353 
354 
355 /***********************************************************************//**
356  * @brief 3-dimensional array element access with range checking
357  *
358  * @param[in] ix Index in first dimension [0,...,shape(0)-1].
359  * @param[in] iy Index in second dimension [0,...,shape(1)-1].
360  * @param[in] iz Index in third dimension [0,...,shape(2)-1].
361  * @return Reference to array element.
362  ***************************************************************************/
363 inline
364 double& GNdarray::at(const int& ix, const int& iy, const int& iz)
365 {
366  return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix,iy,iz));
367 }
368 
369 
370 /***********************************************************************//**
371  * @brief n-dimensional array element access with range checking
372  *
373  * @param[in] i Index vector.
374  * @return Reference to array element.
375  ***************************************************************************/
376 inline
377 double& GNdarray::at(const std::vector<int>& i)
378 {
379  return const_cast<double &>(static_cast<const GNdarray &>(*this).at(i));
380 }
381 
382 
383 /***********************************************************************//**
384  * @brief Data access method (const version)
385  *
386  * @return Const reference to array data.
387  ***************************************************************************/
388 inline
389 const double* GNdarray::data(void) const
390 {
391  return (&(m_data[0]));
392 }
393 
394 
395 /***********************************************************************//**
396  * @brief Data access method
397  *
398  * @return Reference to array data.
399  ***************************************************************************/
400 inline
401 double* GNdarray::data(void)
402 {
403  return (&(m_data[0]));
404 }
405 
406 
407 /***********************************************************************//**
408  * @brief Return sum of two arrays
409  *
410  * @param[in] a First array.
411  * @param[in] b Second array.
412  * @return Sum of arrays @p a and @p b.
413  *
414  * Returns the sum of arrays @p a and @p b.
415  ***************************************************************************/
416 inline
417 GNdarray operator+(const GNdarray& a, const GNdarray& b)
418 {
419  GNdarray result = a;
420  result += b;
421  return result;
422 }
423 
424 
425 /***********************************************************************//**
426  * @brief Add value to array (right addition)
427  *
428  * @param[in] array Array.
429  * @param[in] value Value.
430  * @return Array with @p value added to all elements.
431  *
432  * Returns an array for which the @p value has been added to all elements.
433  ***************************************************************************/
434 inline
435 GNdarray operator+(const GNdarray& array, const double& value)
436 {
437  GNdarray result = array;
438  result += value;
439  return result;
440 }
441 
442 
443 /***********************************************************************//**
444  * @brief Add value to array (left addition)
445  *
446  * @param[in] value Value.
447  * @param[in] array Array.
448  * @return Array with @p value added to all elements.
449  *
450  * Returns an array for which the @p value has been added to all elements.
451  ***************************************************************************/
452 inline
453 GNdarray operator+(const double& value, const GNdarray& array)
454 {
455  GNdarray result = array;
456  result += value;
457  return result;
458 }
459 
460 
461 /***********************************************************************//**
462  * @brief Return difference of arrays
463  *
464  * @param[in] a First array.
465  * @param[in] b Second array.
466  * @return Difference between array @p a and @p b.
467  *
468  * Returns the difference between array @p a and @p b.
469  ***************************************************************************/
470 inline
471 GNdarray operator-(const GNdarray& a, const GNdarray& b)
472 {
473  GNdarray result = a;
474  result -= b;
475  return result;
476 }
477 
478 
479 /***********************************************************************//**
480  * @brief Subtract value from array
481  *
482  * @param[in] array Array.
483  * @param[in] value Value.
484  * @return Array with @p value subtracted from all elements.
485  *
486  * Returns an array for which the @p value has been subtracted from all
487  * elements. For example
488  *
489  * double value = 5.0;
490  * GNdarray result = array - value;
491  ***************************************************************************/
492 inline
493 GNdarray operator-(const GNdarray& array, const double& value)
494 {
495  GNdarray result = array;
496  result -= value;
497  return result;
498 }
499 
500 
501 /***********************************************************************//**
502  * @brief Subtract array from value
503  *
504  * @param[in] value Value.
505  * @param[in] array Array.
506  * @return Array with @p value subtracted from all elements.
507  *
508  * Returns an array for which all elements have been subtracted from
509  * @p value. For example
510  *
511  * double value = 5.0;
512  * GNdarray result = value - array;
513  ***************************************************************************/
514 inline
515 GNdarray operator-(const double& value, const GNdarray& array)
516 {
517  GNdarray result = -array;
518  result += value;
519  return result;
520 }
521 
522 
523 /***********************************************************************//**
524  * @brief Multiply array by value (right multiplication)
525  *
526  * @param[in] array Array.
527  * @param[in] value Value.
528  * @return Array for which all elements have be multiplied by @p value.
529  *
530  * Returns an array for which all elements have be multiplied by @p value.
531  ***************************************************************************/
532 inline
533 GNdarray operator*(const GNdarray& array, const double& value)
534 {
535  GNdarray result = array;
536  result *= value;
537  return result;
538 }
539 
540 
541 /***********************************************************************//**
542  * @brief Multiply array by value (left multiplication)
543  *
544  * @param[in] value Value.
545  * @param[in] array Array.
546  * @return Array for which all elements have be multiplied by @p value.
547  *
548  * Returns an array for which all elements have be multiplied by @p value.
549  ***************************************************************************/
550 inline
551 GNdarray operator*(const double& value, const GNdarray& array)
552 {
553  GNdarray result = array;
554  result *= value;
555  return result;
556 }
557 
558 
559 /***********************************************************************//**
560  * @brief Divide array by value
561  *
562  * @param[in] array Array.
563  * @param[in] value Value.
564  * @return Array for which all elements have be divided by @p value.
565  *
566  * Returns an array for which all elements have be divided by @p value.
567  ***************************************************************************/
568 inline
569 GNdarray operator/(const GNdarray& array, const double& value)
570 {
571  GNdarray result = array;
572  result /= value;
573  return result;
574 }
575 
576 #endif /* GNDARRAY_HPP */
GArf operator/(const GArf &arf, const double &scale)
Auxiliary Response File vision operator friend.
Definition: GArf.hpp:357
friend GNdarray sqrt(const GNdarray &array)
Computes square root of array elements.
Definition: GNdarray.cpp:1373
const std::vector< int > & strides(void) const
Return strides of array.
Definition: GNdarray.hpp:322
bool operator!=(const GNdarray &array) const
Non-equality operator.
Definition: GNdarray.cpp:304
void clear(void)
Clear array.
Definition: GNdarray.cpp:527
friend GNdarray sign(const GNdarray &array)
Computes sign of array elements.
Definition: GNdarray.cpp:1269
void free_members(void)
Delete class members.
Definition: GNdarray.cpp:876
friend double sum(const GNdarray &array)
Computes array sum.
Definition: GNdarray.cpp:1019
GNdarray * clone(void) const
Clone array.
Definition: GNdarray.cpp:545
friend GNdarray asinh(const GNdarray &array)
Computes asinh of array elements.
Definition: GNdarray.cpp:1101
Definition of interface for all GammaLib classes.
friend GNdarray sinh(const GNdarray &array)
Computes sinh of array elements.
Definition: GNdarray.cpp:1352
const std::vector< int > & shape(void) const
Return shape of array.
Definition: GNdarray.hpp:308
friend GNdarray acos(const GNdarray &array)
Computes arccos of array elements.
Definition: GNdarray.cpp:1038
friend GNdarray exp(const GNdarray &array)
Computes exponential of array elements.
Definition: GNdarray.cpp:1206
std::vector< int > m_shape
Array dimensions.
Definition: GNdarray.hpp:136
friend GNdarray log(const GNdarray &array)
Computes natural logarithm of array elements.
Definition: GNdarray.cpp:1248
GArf operator+(const GArf &a, const GArf &b)
Auxiliary Response File addition operator friend.
Definition: GArf.hpp:293
void copy_members(const GNdarray &array)
Copy class members.
Definition: GNdarray.cpp:861
friend GNdarray asin(const GNdarray &array)
Computes arcsin of array elements.
Definition: GNdarray.cpp:1080
double & at(const int &ix)
1-dimensional array element access with range checking
Definition: GNdarray.hpp:335
GNdarray & operator-=(const GNdarray &array)
Unary subtraction operator.
Definition: GNdarray.cpp:401
std::vector< double > m_data
Array data.
Definition: GNdarray.hpp:138
GNdarray & operator/=(const GNdarray &array)
Unary division operator.
Definition: GNdarray.cpp:368
GNdarray & operator=(const GNdarray &array)
Assignment operator.
Definition: GNdarray.cpp:246
void require_same_shape(const std::string &method, const GNdarray &array) const
Throw exception if array shapes differ.
Definition: GNdarray.cpp:916
friend GNdarray atanh(const GNdarray &array)
Computes atanh of array elements.
Definition: GNdarray.cpp:1143
friend GNdarray cos(const GNdarray &array)
Computes cosine of array elements.
Definition: GNdarray.cpp:1164
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
friend GNdarray abs(const GNdarray &array)
Computes absolute of array elements.
Definition: GNdarray.cpp:1227
const double * data(void) const
Data access method (const version)
Definition: GNdarray.hpp:389
std::string print(const GChatter &chatter=NORMAL) const
Print array information.
Definition: GNdarray.cpp:806
bool operator==(const GNdarray &array) const
Equality operator.
Definition: GNdarray.cpp:275
int size(void) const
Return number of elements in array.
Definition: GNdarray.hpp:294
GChatter
Definition: GTypemaps.hpp:33
GArf operator*(const GArf &arf, const double &scale)
Auxiliary Response File scaling operator friend.
Definition: GArf.hpp:325
GNdarray(void)
Void constructor.
Definition: GNdarray.cpp:57
friend GNdarray log10(const GNdarray &array)
Computes base10 logarithm of array elements.
Definition: GNdarray.cpp:1310
friend GNdarray tanh(const GNdarray &array)
Computes tanh of array elements.
Definition: GNdarray.cpp:1415
N-dimensional array class.
Definition: GNdarray.hpp:44
std::string classname(void) const
Return class name.
Definition: GNdarray.hpp:148
friend GNdarray tan(const GNdarray &array)
Computes tangens of array elements.
Definition: GNdarray.cpp:1394
GNdarray & operator*=(const GNdarray &array)
Unary multiplication operator.
Definition: GNdarray.cpp:345
friend GNdarray atan(const GNdarray &array)
Computes arctan of array elements.
Definition: GNdarray.cpp:1122
friend double max(const GNdarray &array)
Computes maximum array element.
Definition: GNdarray.cpp:990
friend double min(const GNdarray &array)
Computes minimum array element.
Definition: GNdarray.cpp:961
int index(const std::vector< int > &i) const
Compute array element index.
Definition: GNdarray.cpp:754
friend GNdarray sin(const GNdarray &array)
Computes sine of array elements.
Definition: GNdarray.cpp:1331
bool has_same_shape(const GNdarray &array) const
Check if array has the same shape.
Definition: GNdarray.cpp:889
friend GNdarray acosh(const GNdarray &array)
Computes acosh of array elements.
Definition: GNdarray.cpp:1059
void init_members(void)
Initialise class members.
Definition: GNdarray.cpp:841
friend GNdarray pow(const GNdarray &array, const double &power)
Computes tanh of array elements.
Definition: GNdarray.cpp:1437
GNdarray & operator+=(const GNdarray &array)
Unary addition operator.
Definition: GNdarray.cpp:322
GArf operator-(const GArf &a, const GArf &b)
Auxiliary Response File subtraction operator friend.
Definition: GArf.hpp:309
virtual ~GNdarray(void)
Destructor.
Definition: GNdarray.cpp:224
int dim(void) const
Return dimension of array.
Definition: GNdarray.hpp:280
double & operator()(const int &ix)
1-dimensional array element access operator
Definition: GNdarray.hpp:161
GNdarray operator-(void) const
Unary minus operator.
Definition: GNdarray.cpp:503
std::vector< int > m_strides
Steps in each dimension when traversing array.
Definition: GNdarray.hpp:137
friend GNdarray cosh(const GNdarray &array)
Computes cosh of array elements.
Definition: GNdarray.cpp:1185