GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GVector.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GVector.hpp - Vector class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2021 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 GVector.hpp
23  * @brief Vector class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GVECTOR_HPP
28 #define GVECTOR_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <cmath>
32 #include <string>
33 #include "GBase.hpp"
34 #include "GException.hpp"
35 
36 
37 /***********************************************************************//**
38  * @class GVector
39  *
40  * @brief Vector class
41  *
42  * This class implement a double precision floating point vector class that
43  * is intended to be used for numerical computation (it is not meant to
44  * replace the std::vector template class).
45  ***************************************************************************/
46 class GVector : public GBase {
47 
48  // Friend functions
49  friend GVector cross(const GVector& a, const GVector& b);
50  friend double operator*(const GVector& a, const GVector& b);
51  friend double norm(const GVector& vector);
52  friend double min(const GVector& vector);
53  friend double max(const GVector& vector);
54  friend double sum(const GVector& vector);
55  friend double angle(const GVector& a, const GVector& b);
56  friend GVector perm(const GVector& vector, const int *p);
57  friend GVector iperm(const GVector& vector, const int *p);
58  friend GVector acos(const GVector& vector);
59  friend GVector acosh(const GVector& vector);
60  friend GVector asin(const GVector& vector);
61  friend GVector asinh(const GVector& vector);
62  friend GVector atan(const GVector& vector);
63  friend GVector atanh(const GVector& vector);
64  friend GVector cos(const GVector& vector);
65  friend GVector cosh(const GVector& vector);
66  friend GVector exp(const GVector& vector);
67  friend GVector abs(const GVector& vector);
68  friend GVector log(const GVector& vector);
69  friend GVector log10(const GVector& vector);
70  friend GVector sin(const GVector& vector);
71  friend GVector sinh(const GVector& vector);
72  friend GVector sqrt(const GVector& vector);
73  friend GVector tan(const GVector& vector);
74  friend GVector tanh(const GVector& vector);
75  friend GVector pow(const GVector& vector, const double& power);
76 
77 public:
78  // Constructors and destructors
79  GVector(void);
80  explicit GVector(const int& num);
81  explicit GVector(const double& a);
82  explicit GVector(const double& a, const double& b);
83  explicit GVector(const double& a, const double& b, const double& c);
84  GVector(const GVector& vector);
85  virtual ~GVector(void);
86 
87  // Vector element access operators
88  double& operator[](const int& index);
89  const double& operator[](const int& index) const;
90 
91  // Vector operators
92  bool operator==(const GVector& vector) const;
93  bool operator!=(const GVector& vector) const;
94  GVector& operator=(const GVector& vector);
95  GVector& operator+=(const GVector& vector);
96  GVector& operator-=(const GVector& vector);
97  GVector& operator=(const double& scalar);
98  GVector& operator+=(const double& scalar);
99  GVector& operator-=(const double& scalar);
100  GVector& operator*=(const double& scalar);
101  GVector& operator/=(const double& scalar);
102  GVector operator-(void) const;
103 
104  // Vector methods
105  void clear(void);
106  GVector* clone(void) const;
107  std::string classname(void) const;
108  const int& size(void) const;
109  double& at(const int& index);
110  const double& at(const int& index) const;
111  int non_zeros(void) const;
112  int first_nonzero(void) const;
113  int last_nonzero(void) const;
114  GVector slice(const int& start, const int& stop) const;
115  std::string print(const GChatter& chatter = NORMAL) const;
116 
117 private:
118  // Private methods
119  void init_members(void);
120  void alloc_members(void);
121  void copy_members(const GVector& vector);
122  void free_members(void);
123 
124  // Private data area
125  int m_num; //!< Number of elements in vector
126  double* m_data; //!< Vector array
127 };
128 
129 
130 /***********************************************************************//**
131  * @brief Return class name
132  *
133  * @return String containing the class name ("GVector").
134  ***************************************************************************/
135 inline
136 std::string GVector::classname(void) const
137 {
138  return ("GVector");
139 }
140 
141 
142 /***********************************************************************//**
143  * @brief Vector element access operator
144  *
145  * @param[in] index Element index [0,...,size()-1]
146  * @return Reference to vector element.
147  ***************************************************************************/
148 inline
149 double& GVector::operator[](const int& index)
150 {
151  // Return vector element
152  return m_data[index];
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Vector element access operator (const variant)
158  *
159  * @param[in] index Element index [0,...,size()-1]
160  * @return Reference to vector element.
161  ***************************************************************************/
162 inline
163 const double& GVector::operator[](const int& index) const
164 {
165  // Return vector element
166  return m_data[index];
167 }
168 
169 
170 /***********************************************************************//**
171  * @brief Return size of vector
172  *
173  * @return Size of vector
174  *
175  * Returns the number of elements in the vector.
176  ***************************************************************************/
177 inline
178 const int& GVector::size() const
179 {
180  return m_num;
181 }
182 
183 
184 /***********************************************************************//**
185  * @brief Add two vectors
186  *
187  * @param[in] a Vector.
188  * @param[in] b Vector.
189  * @return Sum of vectors @p a and @p b.
190  *
191  * Returns the sum of vectors @p a and @p b.
192  ***************************************************************************/
193 inline
194 GVector operator+(const GVector& a, const GVector& b)
195 {
196  GVector result = a;
197  result += b;
198  return result;
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Add scalar to vector
204  *
205  * @param[in] vector Vector.
206  * @param[in] scalar Scalar.
207  * @return Vector with @p scalar added to all elements.
208  *
209  * Returns a vector for which the @p scalar has been added to all elements.
210  ***************************************************************************/
211 inline
212 GVector operator+(const GVector& vector, const double& scalar)
213 {
214  GVector result = vector;
215  result += scalar;
216  return result;
217 }
218 
219 
220 /***********************************************************************//**
221  * @brief Add scalar to vector
222  *
223  * @param[in] scalar Scalar.
224  * @param[in] vector Vector.
225  * @return Vector with @p scalar added to all elements.
226  *
227  * Returns a vector for which the @p scalar has been added to all elements.
228  ***************************************************************************/
229 inline
230 GVector operator+(const double& scalar, const GVector& vector)
231 {
232  GVector result = vector;
233  result += scalar;
234  return result;
235 }
236 
237 
238 /***********************************************************************//**
239  * @brief Subtract vector from vector
240  *
241  * @param[in] a Vector.
242  * @param[in] b Vector.
243  * @return Difference between vector @p a and @p b.
244  *
245  * Returns the difference between vector @p a and @p b.
246  ***************************************************************************/
247 inline
248 GVector operator-(const GVector& a, const GVector& b)
249 {
250  GVector result = a;
251  result -= b;
252  return result;
253 }
254 
255 
256 /***********************************************************************//**
257  * @brief Subtract scalar from vector
258  *
259  * @param[in] vector Vector.
260  * @param[in] scalar Scalar.
261  * @return Vector with @p scalar subtracted from all elements.
262  *
263  * Returns a vector for which the @p scalar has been subtracted from all
264  * elements.
265  ***************************************************************************/
266 inline
267 GVector operator-(const GVector& vector, const double& scalar)
268 {
269  GVector result = vector;
270  result -= scalar;
271  return result;
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Subtract vector from scalar
277  *
278  * @param[in] scalar Scalar.
279  * @param[in] vector Vector.
280  * @return Vector with all elements subtracted from @p scalar.
281  *
282  * Returns a vector for which all elements have been subtracted from the
283  * @p scalar.
284  ***************************************************************************/
285 inline
286 GVector operator-(const double& scalar, const GVector& vector)
287 {
288  GVector result = -vector;
289  result += scalar;
290  return result;
291 }
292 
293 
294 /***********************************************************************//**
295  * @brief Multiply vector by scalar
296  *
297  * @param[in] vector Vector.
298  * @param[in] scalar Scalar.
299  * @return Vector for which all elements have be multiplied by @p scalar.
300  *
301  * Returns a vector for which all elements have be multiplied by @p scalar.
302  ***************************************************************************/
303 inline
304 GVector operator*(const GVector& vector, const double& scalar)
305 {
306  GVector result = vector;
307  result *= scalar;
308  return result;
309 }
310 
311 
312 /***********************************************************************//**
313  * @brief Multiply vector by scalar
314  *
315  * @param[in] scalar Scalar.
316  * @param[in] vector Vector.
317  * @return Vector for which all elements have be multiplied by @p scalar.
318  *
319  * Returns a vector for which all elements have be multiplied by @p scalar.
320  ***************************************************************************/
321 inline
322 GVector operator*(const double& scalar, const GVector& vector)
323 {
324  GVector result = vector;
325  result *= scalar;
326  return result;
327 }
328 
329 
330 /***********************************************************************//**
331  * @brief Divide vector by scalar
332  *
333  * @param[in] vector Vector.
334  * @param[in] scalar Scalar.
335  * @return Vector for which all elements have be divided by @p scalar.
336  *
337  * Returns a vector for which all elements have be divided by @p scalar.
338  ***************************************************************************/
339 inline
340 GVector operator/(const GVector& vector, const double& scalar)
341 {
342  GVector result = vector;
343  result /= scalar;
344  return result;
345 }
346 
347 #endif /* GVECTOR_HPP */
GVector slice(const int &start, const int &stop) const
Vector slice operator.
Definition: GVector.cpp:634
friend GVector abs(const GVector &vector)
Computes absolute of vector elements.
Definition: GVector.cpp:1253
void alloc_members(void)
Allocate vector.
Definition: GVector.cpp:710
GArf operator/(const GArf &arf, const double &scale)
Auxiliary Response File vision operator friend.
Definition: GArf.hpp:357
friend GVector cosh(const GVector &vector)
Computes cosh of vector elements.
Definition: GVector.cpp:1211
friend double max(const GVector &vector)
Computes maximum vector element.
Definition: GVector.cpp:915
double & operator[](const int &index)
Vector element access operator.
Definition: GVector.hpp:149
GVector & operator*=(const double &scalar)
Scalar unary multiplication operator.
Definition: GVector.cpp:426
friend GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition: GVector.cpp:1316
friend GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Definition: GVector.cpp:1295
friend GVector acos(const GVector &vector)
Computes arccos of vector elements.
Definition: GVector.cpp:1064
int last_nonzero(void) const
Return the index of the last non-zero element in a vector.
Definition: GVector.cpp:609
Definition of interface for all GammaLib classes.
friend double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:864
int first_nonzero(void) const
Return the index of the first non-zero element in a vector.
Definition: GVector.cpp:583
GVector & operator=(const GVector &vector)
Assignment operator.
Definition: GVector.cpp:228
friend double sum(const GVector &vector)
Computes vector sum.
Definition: GVector.cpp:944
GArf operator+(const GArf &a, const GArf &b)
Auxiliary Response File addition operator friend.
Definition: GArf.hpp:293
std::string classname(void) const
Return class name.
Definition: GVector.hpp:136
friend GVector tan(const GVector &vector)
Computes tangens of vector elements.
Definition: GVector.cpp:1379
friend double angle(const GVector &a, const GVector &b)
Computes angle between vectors.
Definition: GVector.cpp:974
double & at(const int &index)
Vector element access with range checking.
Definition: GVector.cpp:522
friend GVector atanh(const GVector &vector)
Computes atanh of vector elements.
Definition: GVector.cpp:1169
void clear(void)
Clear vector.
Definition: GVector.cpp:489
virtual ~GVector(void)
Destructor.
Definition: GVector.cpp:206
friend GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition: GVector.cpp:1106
friend GVector atan(const GVector &vector)
Computes arctan of vector elements.
Definition: GVector.cpp:1148
double * m_data
Vector array.
Definition: GVector.hpp:126
friend GVector asinh(const GVector &vector)
Computes asinh of vector elements.
Definition: GVector.cpp:1127
friend GVector tanh(const GVector &vector)
Computes tanh of vector elements.
Definition: GVector.cpp:1400
int non_zeros(void) const
Returns number of non-zero elements in vector.
Definition: GVector.cpp:559
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
friend GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
Definition: GVector.cpp:1011
bool operator==(const GVector &vector) const
Equality operator.
Definition: GVector.cpp:258
int m_num
Number of elements in vector.
Definition: GVector.hpp:125
GChatter
Definition: GTypemaps.hpp:33
GVector & operator+=(const GVector &vector)
Unary addition operator.
Definition: GVector.cpp:305
GArf operator*(const GArf &arf, const double &scale)
Auxiliary Response File scaling operator friend.
Definition: GArf.hpp:325
friend GVector cos(const GVector &vector)
Computes cosine of vector elements.
Definition: GVector.cpp:1190
friend GVector cross(const GVector &a, const GVector &b)
Vector cross product.
Definition: GVector.cpp:793
GVector * clone(void) const
Clone vector.
Definition: GVector.cpp:507
void init_members(void)
Initialise class members.
Definition: GVector.cpp:696
friend double min(const GVector &vector)
Computes minimum vector element.
Definition: GVector.cpp:886
GVector operator-(void) const
Unary minus operator.
Definition: GVector.cpp:465
friend GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1358
void copy_members(const GVector &vector)
Copy class members.
Definition: GVector.cpp:735
friend double operator*(const GVector &a, const GVector &b)
Vector scalar product.
Definition: GVector.cpp:836
friend GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
Definition: GVector.cpp:1038
friend GVector pow(const GVector &vector, const double &power)
Computes tanh of vector elements.
Definition: GVector.cpp:1422
GVector & operator/=(const double &scalar)
Scalar unary division operator.
Definition: GVector.cpp:446
GVector(void)
Void vector constructor.
Definition: GVector.cpp:54
Exception handler interface definition.
friend GVector exp(const GVector &vector)
Computes exponential of vector elements.
Definition: GVector.cpp:1232
friend GVector acosh(const GVector &vector)
Computes acosh of vector elements.
Definition: GVector.cpp:1085
friend GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1274
bool operator!=(const GVector &vector) const
Non-equality operator.
Definition: GVector.cpp:284
const int & size(void) const
Return size of vector.
Definition: GVector.hpp:178
Vector class.
Definition: GVector.hpp:46
friend GVector sinh(const GVector &vector)
Computes sinh of vector elements.
Definition: GVector.cpp:1337
std::string print(const GChatter &chatter=NORMAL) const
Print vector information.
Definition: GVector.cpp:661
void free_members(void)
Delete class members.
Definition: GVector.cpp:761
GArf operator-(const GArf &a, const GArf &b)
Auxiliary Response File subtraction operator friend.
Definition: GArf.hpp:309
GVector & operator-=(const GVector &vector)
Unary subtraction operator.
Definition: GVector.cpp:337