GammaLib 2.0.0
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-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 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 ***************************************************************************/
44class 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
70public:
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 double& at(const int& ix);
115 double& at(const int& ix, const int& iy);
116 double& at(const int& ix, const int& iy, const int& iz);
117 double& at(const std::vector<int>& i);
118 const double& at(const int& ix) const;
119 const double& at(const int& ix, const int& iy) const;
120 const double& at(const int& ix, const int& iy, const int& iz) const;
121 const double& at(const std::vector<int>& i) const;
122 const double* data(void) const;
123 double* data(void);
124 std::string print(const GChatter& chatter = NORMAL) const;
125
126protected:
127 // Protected methods
128 void init_members(void);
129 void copy_members(const GNdarray& array);
130 void free_members(void);
131 bool has_same_shape(const GNdarray& array) const;
132 void require_same_shape(const std::string& method, const GNdarray& array) const;
133
134 // Protected members
135 std::vector<int> m_shape; //!< Array dimensions
136 std::vector<int> m_strides; //!< Steps in each dimension when traversing array
137 std::vector<double> m_data; //!< Array data
138};
139
140
141/***********************************************************************//**
142 * @brief Return class name
143 *
144 * @return String containing the class name ("GNdarray").
145 ***************************************************************************/
146inline
147std::string GNdarray::classname(void) const
148{
149 return ("GNdarray");
150}
151
152
153/***********************************************************************//**
154 * @brief 1-dimensional array element access operator
155 *
156 * @param[in] ix Element index [0,...,shape(0)-1].
157 * @return Reference to array element.
158 ***************************************************************************/
159inline
160double& GNdarray::operator()(const int& ix)
161{
162 // Return array element
163 return m_data[ix];
164}
165
166
167/***********************************************************************//**
168 * @brief 2-dimensional array element access operator
169 *
170 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
171 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
172 * @return Reference to array element.
173 ***************************************************************************/
174inline
175double& GNdarray::operator()(const int& ix, const int& iy)
176{
177 // Return array element
178 return m_data[ix+m_strides[1]*iy];
179}
180
181
182/***********************************************************************//**
183 * @brief 3-dimensional array element access operator
184 *
185 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
186 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
187 * @param[in] iz Index in third dimension [0,...,shape(2)-1].
188 * @return Reference to array element.
189 ***************************************************************************/
190inline
191double& GNdarray::operator()(const int& ix, const int& iy, const int& iz)
192{
193 // Return array element
194 return m_data[ix+m_strides[1]*iy+m_strides[2]*iz];
195}
196
197
198/***********************************************************************//**
199 * @brief n-dimensional array element access operator
200 *
201 * @param[in] i Index vector.
202 * @return Reference to array element.
203 ***************************************************************************/
204inline
205double& GNdarray::operator()(const std::vector<int>& i)
206{
207 // Return array element
208 return m_data[index(i)];
209}
210
211
212/***********************************************************************//**
213 * @brief 1-dimensional array element access operator (const variant)
214 *
215 * @param[in] ix Element index [0,...,shape(0)-1].
216 * @return Const reference to array element.
217 ***************************************************************************/
218inline
219const double& GNdarray::operator()(const int& ix) const
220{
221 // Return array element
222 return m_data[ix];
223}
224
225
226/***********************************************************************//**
227 * @brief 2-dimensional array element access operator (const variant)
228 *
229 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
230 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
231 * @return Const reference to array element.
232 ***************************************************************************/
233inline
234const double& GNdarray::operator()(const int& ix, const int& iy) const
235{
236 // Return array element
237 return m_data[ix+m_strides[1]*iy];
238}
239
240
241/***********************************************************************//**
242 * @brief 3-dimensional array element access operator (const variant)
243 *
244 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
245 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
246 * @param[in] iz Index in third dimension [0,...,shape(2)-1].
247 * @return Const reference to array element.
248 ***************************************************************************/
249inline
250const double& GNdarray::operator()(const int& ix, const int& iy, const int& iz) const
251{
252 // Return array element
253 return m_data[ix+m_strides[1]*iy+m_strides[2]*iz];
254}
255
256
257/***********************************************************************//**
258 * @brief n-dimensional array element access operator (const variant)
259 *
260 * @param[in] i Index vector.
261 * @return Const reference to array element.
262 ***************************************************************************/
263inline
264const double& GNdarray::operator()(const std::vector<int>& i) const
265{
266 // Return array element
267 return m_data[index(i)];
268}
269
270
271/***********************************************************************//**
272 * @brief Return dimension of array
273 *
274 * @return Dimension of array
275 *
276 * Returns the dimension of the array.
277 ***************************************************************************/
278inline
279int GNdarray::dim(void) const
280{
281 return int(m_shape.size());
282}
283
284
285/***********************************************************************//**
286 * @brief Return number of elements in array
287 *
288 * @return Number of elements in array
289 *
290 * Returns the number of elements in the array.
291 ***************************************************************************/
292inline
293int GNdarray::size(void) const
294{
295 return int(m_data.size());
296}
297
298
299/***********************************************************************//**
300 * @brief Return shape of array
301 *
302 * @return Shape of array
303 *
304 * Returns the shape of the array.
305 ***************************************************************************/
306inline
307const std::vector<int>& GNdarray::shape(void) const
308{
309 return m_shape;
310}
311
312
313/***********************************************************************//**
314 * @brief Return strides of array
315 *
316 * @return Strides of array
317 *
318 * Returns the strides of the array.
319 ***************************************************************************/
320inline
321const std::vector<int>& GNdarray::strides(void) const
322{
323 return m_strides;
324}
325
326
327/***********************************************************************//**
328 * @brief 1-dimensional array element access with range checking
329 *
330 * @param[in] ix Element index [0,...,shape(0)-1].
331 * @return Reference to array element.
332 ***************************************************************************/
333inline
334double& GNdarray::at(const int& ix)
335{
336 return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix));
337}
338
339
340/***********************************************************************//**
341 * @brief 2-dimensional array element access with range checking
342 *
343 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
344 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
345 * @return Reference to array element.
346 ***************************************************************************/
347inline
348double& GNdarray::at(const int& ix, const int& iy)
349{
350 return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix,iy));
351}
352
353
354/***********************************************************************//**
355 * @brief 3-dimensional array element access with range checking
356 *
357 * @param[in] ix Index in first dimension [0,...,shape(0)-1].
358 * @param[in] iy Index in second dimension [0,...,shape(1)-1].
359 * @param[in] iz Index in third dimension [0,...,shape(2)-1].
360 * @return Reference to array element.
361 ***************************************************************************/
362inline
363double& GNdarray::at(const int& ix, const int& iy, const int& iz)
364{
365 return const_cast<double &>(static_cast<const GNdarray &>(*this).at(ix,iy,iz));
366}
367
368
369/***********************************************************************//**
370 * @brief n-dimensional array element access with range checking
371 *
372 * @param[in] i Index vector.
373 * @return Reference to array element.
374 ***************************************************************************/
375inline
376double& GNdarray::at(const std::vector<int>& i)
377{
378 return const_cast<double &>(static_cast<const GNdarray &>(*this).at(i));
379}
380
381
382/***********************************************************************//**
383 * @brief Data access method (const version)
384 *
385 * @return Const reference to array data.
386 ***************************************************************************/
387inline
388const double* GNdarray::data(void) const
389{
390 return (&(m_data[0]));
391}
392
393
394/***********************************************************************//**
395 * @brief Data access method
396 *
397 * @return Reference to array data.
398 ***************************************************************************/
399inline
400double* GNdarray::data(void)
401{
402 return (&(m_data[0]));
403}
404
405
406/***********************************************************************//**
407 * @brief Return sum of two arrays
408 *
409 * @param[in] a First array.
410 * @param[in] b Second array.
411 * @return Sum of arrays @p a and @p b.
412 *
413 * Returns the sum of arrays @p a and @p b.
414 ***************************************************************************/
415inline
417{
418 GNdarray result = a;
419 result += b;
420 return result;
421}
422
423
424/***********************************************************************//**
425 * @brief Add value to array (right addition)
426 *
427 * @param[in] array Array.
428 * @param[in] value Value.
429 * @return Array with @p value added to all elements.
430 *
431 * Returns an array for which the @p value has been added to all elements.
432 ***************************************************************************/
433inline
434GNdarray operator+(const GNdarray& array, const double& value)
435{
436 GNdarray result = array;
437 result += value;
438 return result;
439}
440
441
442/***********************************************************************//**
443 * @brief Add value to array (left addition)
444 *
445 * @param[in] value Value.
446 * @param[in] array Array.
447 * @return Array with @p value added to all elements.
448 *
449 * Returns an array for which the @p value has been added to all elements.
450 ***************************************************************************/
451inline
452GNdarray operator+(const double& value, const GNdarray& array)
453{
454 GNdarray result = array;
455 result += value;
456 return result;
457}
458
459
460/***********************************************************************//**
461 * @brief Return difference of arrays
462 *
463 * @param[in] a First array.
464 * @param[in] b Second array.
465 * @return Difference between array @p a and @p b.
466 *
467 * Returns the difference between array @p a and @p b.
468 ***************************************************************************/
469inline
471{
472 GNdarray result = a;
473 result -= b;
474 return result;
475}
476
477
478/***********************************************************************//**
479 * @brief Subtract value from array
480 *
481 * @param[in] array Array.
482 * @param[in] value Value.
483 * @return Array with @p value subtracted from all elements.
484 *
485 * Returns an array for which the @p value has been subtracted from all
486 * elements. For example
487 *
488 * double value = 5.0;
489 * GNdarray result = array - value;
490 ***************************************************************************/
491inline
492GNdarray operator-(const GNdarray& array, const double& value)
493{
494 GNdarray result = array;
495 result -= value;
496 return result;
497}
498
499
500/***********************************************************************//**
501 * @brief Subtract array from value
502 *
503 * @param[in] value Value.
504 * @param[in] array Array.
505 * @return Array with @p value subtracted from all elements.
506 *
507 * Returns an array for which all elements have been subtracted from
508 * @p value. For example
509 *
510 * double value = 5.0;
511 * GNdarray result = value - array;
512 ***************************************************************************/
513inline
514GNdarray operator-(const double& value, const GNdarray& array)
515{
516 GNdarray result = -array;
517 result += value;
518 return result;
519}
520
521
522/***********************************************************************//**
523 * @brief Multiply array by value (right multiplication)
524 *
525 * @param[in] array Array.
526 * @param[in] value Value.
527 * @return Array for which all elements have be multiplied by @p value.
528 *
529 * Returns an array for which all elements have be multiplied by @p value.
530 ***************************************************************************/
531inline
532GNdarray operator*(const GNdarray& array, const double& value)
533{
534 GNdarray result = array;
535 result *= value;
536 return result;
537}
538
539
540/***********************************************************************//**
541 * @brief Multiply array by value (left multiplication)
542 *
543 * @param[in] value Value.
544 * @param[in] array Array.
545 * @return Array for which all elements have be multiplied by @p value.
546 *
547 * Returns an array for which all elements have be multiplied by @p value.
548 ***************************************************************************/
549inline
550GNdarray operator*(const double& value, const GNdarray& array)
551{
552 GNdarray result = array;
553 result *= value;
554 return result;
555}
556
557
558/***********************************************************************//**
559 * @brief Divide array by value
560 *
561 * @param[in] array Array.
562 * @param[in] value Value.
563 * @return Array for which all elements have be divided by @p value.
564 *
565 * Returns an array for which all elements have be divided by @p value.
566 ***************************************************************************/
567inline
568GNdarray operator/(const GNdarray& array, const double& value)
569{
570 GNdarray result = array;
571 result /= value;
572 return result;
573}
574
575#endif /* GNDARRAY_HPP */
Definition of interface for all GammaLib classes.
GNdarray operator*(const GNdarray &array, const double &value)
Multiply array by value (right multiplication)
Definition GNdarray.hpp:532
GNdarray operator/(const GNdarray &array, const double &value)
Divide array by value.
Definition GNdarray.hpp:568
GNdarray operator-(const GNdarray &a, const GNdarray &b)
Return difference of arrays.
Definition GNdarray.hpp:470
GNdarray operator+(const GNdarray &a, const GNdarray &b)
Return sum of two arrays.
Definition GNdarray.hpp:416
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
N-dimensional array class.
Definition GNdarray.hpp:44
friend GNdarray cosh(const GNdarray &array)
Computes cosh of array elements.
GNdarray & operator*=(const GNdarray &array)
Unary multiplication operator.
Definition GNdarray.cpp:345
friend double max(const GNdarray &array)
Computes maximum array element.
Definition GNdarray.cpp:959
const double * data(void) const
Data access method (const version)
Definition GNdarray.hpp:388
void copy_members(const GNdarray &array)
Copy class members.
Definition GNdarray.cpp:830
friend GNdarray asin(const GNdarray &array)
Computes arcsin of array elements.
GNdarray operator-(void) const
Unary minus operator.
Definition GNdarray.cpp:503
friend GNdarray tanh(const GNdarray &array)
Computes tanh of array elements.
void require_same_shape(const std::string &method, const GNdarray &array) const
Throw exception if array shapes differ.
Definition GNdarray.cpp:885
int size(void) const
Return number of elements in array.
Definition GNdarray.hpp:293
bool operator!=(const GNdarray &array) const
Non-equality operator.
Definition GNdarray.cpp:304
friend GNdarray sqrt(const GNdarray &array)
Computes square root of array elements.
friend GNdarray atan(const GNdarray &array)
Computes arctan of array elements.
std::string classname(void) const
Return class name.
Definition GNdarray.hpp:147
std::vector< int > m_shape
Array dimensions.
Definition GNdarray.hpp:135
friend GNdarray asinh(const GNdarray &array)
Computes asinh of array elements.
friend GNdarray sinh(const GNdarray &array)
Computes sinh of array elements.
bool operator==(const GNdarray &array) const
Equality operator.
Definition GNdarray.cpp:275
void clear(void)
Clear array.
Definition GNdarray.cpp:527
std::string print(const GChatter &chatter=NORMAL) const
Print array information.
Definition GNdarray.cpp:775
GNdarray & operator/=(const GNdarray &array)
Unary division operator.
Definition GNdarray.cpp:368
int dim(void) const
Return dimension of array.
Definition GNdarray.hpp:279
friend GNdarray acosh(const GNdarray &array)
Computes acosh of array elements.
GNdarray & operator-=(const GNdarray &array)
Unary subtraction operator.
Definition GNdarray.cpp:401
void init_members(void)
Initialise class members.
Definition GNdarray.cpp:810
void free_members(void)
Delete class members.
Definition GNdarray.cpp:845
friend GNdarray atanh(const GNdarray &array)
Computes atanh of array elements.
virtual ~GNdarray(void)
Destructor.
Definition GNdarray.cpp:224
friend double min(const GNdarray &array)
Computes minimum array element.
Definition GNdarray.cpp:930
GNdarray(void)
Void constructor.
Definition GNdarray.cpp:57
GNdarray & operator+=(const GNdarray &array)
Unary addition operator.
Definition GNdarray.cpp:322
std::vector< int > m_strides
Steps in each dimension when traversing array.
Definition GNdarray.hpp:136
GNdarray & operator=(const GNdarray &array)
Assignment operator.
Definition GNdarray.cpp:246
double & operator()(const int &ix)
1-dimensional array element access operator
Definition GNdarray.hpp:160
friend GNdarray pow(const GNdarray &array, const double &power)
Computes tanh of array elements.
friend double sum(const GNdarray &array)
Computes array sum.
Definition GNdarray.cpp:988
const std::vector< int > & shape(void) const
Return shape of array.
Definition GNdarray.hpp:307
friend GNdarray acos(const GNdarray &array)
Computes arccos of array elements.
int index(const std::vector< int > &i) const
Compute array element index.
Definition GNdarray.cpp:754
friend GNdarray log10(const GNdarray &array)
Computes base10 logarithm of array elements.
const std::vector< int > & strides(void) const
Return strides of array.
Definition GNdarray.hpp:321
bool has_same_shape(const GNdarray &array) const
Check if array has the same shape.
Definition GNdarray.cpp:858
friend GNdarray log(const GNdarray &array)
Computes natural logarithm of array elements.
friend GNdarray sin(const GNdarray &array)
Computes sine of array elements.
double & at(const int &ix)
1-dimensional array element access with range checking
Definition GNdarray.hpp:334
friend GNdarray abs(const GNdarray &array)
Computes absolute of array elements.
friend GNdarray tan(const GNdarray &array)
Computes tangens of array elements.
friend GNdarray cos(const GNdarray &array)
Computes cosine of array elements.
std::vector< double > m_data
Array data.
Definition GNdarray.hpp:137
GNdarray * clone(void) const
Clone array.
Definition GNdarray.cpp:545
friend GNdarray sign(const GNdarray &array)
Computes sign of array elements.
friend GNdarray exp(const GNdarray &array)
Computes exponential of array elements.