GammaLib  1.7.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-2014 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 GVector perm(const GVector& vector, const int *p);
56  friend GVector iperm(const GVector& vector, const int *p);
57  friend GVector acos(const GVector& vector);
58  friend GVector acosh(const GVector& vector);
59  friend GVector asin(const GVector& vector);
60  friend GVector asinh(const GVector& vector);
61  friend GVector atan(const GVector& vector);
62  friend GVector atanh(const GVector& vector);
63  friend GVector cos(const GVector& vector);
64  friend GVector cosh(const GVector& vector);
65  friend GVector exp(const GVector& vector);
66  friend GVector abs(const GVector& vector);
67  friend GVector log(const GVector& vector);
68  friend GVector log10(const GVector& vector);
69  friend GVector sin(const GVector& vector);
70  friend GVector sinh(const GVector& vector);
71  friend GVector sqrt(const GVector& vector);
72  friend GVector tan(const GVector& vector);
73  friend GVector tanh(const GVector& vector);
74  friend GVector pow(const GVector& vector, const double& power);
75 
76 public:
77  // Constructors and destructors
78  GVector(void);
79  explicit GVector(const int& num);
80  explicit GVector(const double& a);
81  explicit GVector(const double& a, const double& b);
82  explicit GVector(const double& a, const double& b, const double& c);
83  GVector(const GVector& vector);
84  virtual ~GVector(void);
85 
86  // Vector element access operators
87  double& operator[](const int& index);
88  const double& operator[](const int& index) const;
89 
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 
118 private:
119  // Private methods
120  void init_members(void);
121  void alloc_members(void);
122  void copy_members(const GVector& vector);
123  void free_members(void);
124 
125 
126  // Private data area
127  int m_num; //!< Number of elements in vector
128  double* m_data; //!< Vector array
129 };
130 
131 
132 /***********************************************************************//**
133  * @brief Return class name
134  *
135  * @return String containing the class name ("GVector").
136  ***************************************************************************/
137 inline
138 std::string GVector::classname(void) const
139 {
140  return ("GVector");
141 }
142 
143 
144 /***********************************************************************//**
145  * @brief Vector element access operator
146  *
147  * @param[in] index Element index [0,...,size()-1]
148  * @return Reference to vector element.
149  ***************************************************************************/
150 inline
151 double& GVector::operator[](const int& index)
152 {
153  // Return vector element
154  return m_data[index];
155 }
156 
157 
158 /***********************************************************************//**
159  * @brief Vector element access operator (const variant)
160  *
161  * @param[in] index Element index [0,...,size()-1]
162  * @return Reference to vector element.
163  ***************************************************************************/
164 inline
165 const double& GVector::operator[](const int& index) const
166 {
167  // Return vector element
168  return m_data[index];
169 }
170 
171 
172 /***********************************************************************//**
173  * @brief Return size of vector
174  *
175  * @return Size of vector
176  *
177  * Returns the number of elements in the vector.
178  ***************************************************************************/
179 inline
180 const int& GVector::size() const
181 {
182  return m_num;
183 }
184 
185 
186 /***********************************************************************//**
187  * @brief Add two vectors
188  *
189  * @param[in] a Vector.
190  * @param[in] b Vector.
191  * @return Sum of vectors @p a and @p b.
192  *
193  * Returns the sum of vectors @p a and @p b.
194  ***************************************************************************/
195 inline
196 GVector operator+(const GVector& a, const GVector& b)
197 {
198  GVector result = a;
199  result += b;
200  return result;
201 }
202 
203 
204 /***********************************************************************//**
205  * @brief Add scalar to vector
206  *
207  * @param[in] vector Vector.
208  * @param[in] scalar Scalar.
209  * @return Vector with @p scalar added to all elements.
210  *
211  * Returns a vector for which the @p scalar has been added to all elements.
212  ***************************************************************************/
213 inline
214 GVector operator+(const GVector& vector, const double& scalar)
215 {
216  GVector result = vector;
217  result += scalar;
218  return result;
219 }
220 
221 
222 /***********************************************************************//**
223  * @brief Add scalar to vector
224  *
225  * @param[in] scalar Scalar.
226  * @param[in] vector Vector.
227  * @return Vector with @p scalar added to all elements.
228  *
229  * Returns a vector for which the @p scalar has been added to all elements.
230  ***************************************************************************/
231 inline
232 GVector operator+(const double& scalar, const GVector& vector)
233 {
234  GVector result = vector;
235  result += scalar;
236  return result;
237 }
238 
239 
240 /***********************************************************************//**
241  * @brief Subtract vector from vector
242  *
243  * @param[in] a Vector.
244  * @param[in] b Vector.
245  * @return Difference between vector @p a and @p b.
246  *
247  * Returns the difference between vector @p a and @p b.
248  ***************************************************************************/
249 inline
250 GVector operator- (const GVector& a, const GVector& b)
251 {
252  GVector result = a;
253  result -= b;
254  return result;
255 }
256 
257 
258 /***********************************************************************//**
259  * @brief Subtract scalar from vector
260  *
261  * @param[in] vector Vector.
262  * @param[in] scalar Scalar.
263  * @return Vector with @p scalar subtracted from all elements.
264  *
265  * Returns a vector for which the @p scalar has been subtracted from all
266  * elements.
267  ***************************************************************************/
268 inline
269 GVector operator-(const GVector& vector, const double& scalar)
270 {
271  GVector result = vector;
272  result -= scalar;
273  return result;
274 }
275 
276 
277 /***********************************************************************//**
278  * @brief Subtract vector from scalar
279  *
280  * @param[in] scalar Scalar.
281  * @param[in] vector Vector.
282  * @return Vector with all elements subtracted from @p scalar.
283  *
284  * Returns a vector for which all elements have been subtracted from the
285  * @p scalar.
286  ***************************************************************************/
287 inline
288 GVector operator-(const double& scalar, const GVector& vector)
289 {
290  GVector result = -vector;
291  result += scalar;
292  return result;
293 }
294 
295 
296 /***********************************************************************//**
297  * @brief Multiply vector by scalar
298  *
299  * @param[in] vector Vector.
300  * @param[in] scalar Scalar.
301  * @return Vector for which all elements have be multiplied by @p scalar.
302  *
303  * Returns a vector for which all elements have be multiplied by @p scalar.
304  ***************************************************************************/
305 inline
306 GVector operator*(const GVector& vector, const double& scalar)
307 {
308  GVector result = vector;
309  result *= scalar;
310  return result;
311 }
312 
313 
314 /***********************************************************************//**
315  * @brief Multiply vector by scalar
316  *
317  * @param[in] scalar Scalar.
318  * @param[in] vector Vector.
319  * @return Vector for which all elements have be multiplied by @p scalar.
320  *
321  * Returns a vector for which all elements have be multiplied by @p scalar.
322  ***************************************************************************/
323 inline
324 GVector operator*(const double& scalar, const GVector& vector)
325 {
326  GVector result = vector;
327  result *= scalar;
328  return result;
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Divide vector by scalar
334  *
335  * @param[in] vector Vector.
336  * @param[in] scalar Scalar.
337  * @return Vector for which all elements have be divided by @p scalar.
338  *
339  * Returns a vector for which all elements have be divided by @p scalar.
340  ***************************************************************************/
341 inline
342 GVector operator/(const GVector& vector, const double& scalar)
343 {
344  GVector result = vector;
345  result /= scalar;
346  return result;
347 }
348 
349 #endif /* GVECTOR_HPP */
GVector slice(const int &start, const int &stop) const
Vector slice operator.
Definition: GVector.cpp:610
friend GVector abs(const GVector &vector)
Computes absolute of vector elements.
Definition: GVector.cpp:1163
void alloc_members(void)
Allocate vector.
Definition: GVector.cpp:686
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:1121
friend double max(const GVector &vector)
Computes maximum vector element.
Definition: GVector.cpp:872
double & operator[](const int &index)
Vector element access operator.
Definition: GVector.hpp:151
GVector & operator*=(const double &scalar)
Scalar unary multiplication operator.
Definition: GVector.cpp:402
friend GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition: GVector.cpp:1226
friend GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Definition: GVector.cpp:1205
friend GVector acos(const GVector &vector)
Computes arccos of vector elements.
Definition: GVector.cpp:974
int last_nonzero(void) const
Return the index of the last non-zero element in a vector.
Definition: GVector.cpp:585
Definition of interface for all GammaLib classes.
friend double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:821
int first_nonzero(void) const
Return the index of the first non-zero element in a vector.
Definition: GVector.cpp:559
GVector & operator=(const GVector &vector)
Assignment operator.
Definition: GVector.cpp:212
friend double sum(const GVector &vector)
Computes vector sum.
Definition: GVector.cpp:901
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:138
friend GVector tan(const GVector &vector)
Computes tangens of vector elements.
Definition: GVector.cpp:1289
double & at(const int &index)
Vector element access with range checking.
Definition: GVector.cpp:498
friend GVector atanh(const GVector &vector)
Computes atanh of vector elements.
Definition: GVector.cpp:1079
void clear(void)
Clear vector.
Definition: GVector.cpp:465
virtual ~GVector(void)
Destructor.
Definition: GVector.cpp:190
friend GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition: GVector.cpp:1016
friend GVector atan(const GVector &vector)
Computes arctan of vector elements.
Definition: GVector.cpp:1058
double * m_data
Vector array.
Definition: GVector.hpp:128
friend GVector asinh(const GVector &vector)
Computes asinh of vector elements.
Definition: GVector.cpp:1037
friend GVector tanh(const GVector &vector)
Computes tanh of vector elements.
Definition: GVector.cpp:1310
int non_zeros(void) const
Returns number of non-zero elements in vector.
Definition: GVector.cpp:535
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:921
bool operator==(const GVector &vector) const
Equality operator.
Definition: GVector.cpp:242
int m_num
Number of elements in vector.
Definition: GVector.hpp:127
GChatter
Definition: GTypemaps.hpp:33
GVector & operator+=(const GVector &vector)
Unary addition operator.
Definition: GVector.cpp:289
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:1100
friend GVector cross(const GVector &a, const GVector &b)
Vector cross product.
Definition: GVector.cpp:762
GVector * clone(void) const
Clone vector.
Definition: GVector.cpp:483
void init_members(void)
Initialise class members.
Definition: GVector.cpp:672
friend double min(const GVector &vector)
Computes minimum vector element.
Definition: GVector.cpp:843
GVector operator-(void) const
Unary minus operator.
Definition: GVector.cpp:441
friend GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1268
void copy_members(const GVector &vector)
Copy class members.
Definition: GVector.cpp:709
friend double operator*(const GVector &a, const GVector &b)
Vector scalar product.
Definition: GVector.cpp:797
friend GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
Definition: GVector.cpp:948
friend GVector pow(const GVector &vector, const double &power)
Computes tanh of vector elements.
Definition: GVector.cpp:1332
GVector & operator/=(const double &scalar)
Scalar unary division operator.
Definition: GVector.cpp:422
GVector(void)
Void vector constructor.
Definition: GVector.cpp:52
Exception handler interface definition.
friend GVector exp(const GVector &vector)
Computes exponential of vector elements.
Definition: GVector.cpp:1142
friend GVector acosh(const GVector &vector)
Computes acosh of vector elements.
Definition: GVector.cpp:995
friend GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1184
bool operator!=(const GVector &vector) const
Non-equality operator.
Definition: GVector.cpp:268
const int & size(void) const
Return size of vector.
Definition: GVector.hpp:180
Vector class.
Definition: GVector.hpp:46
friend GVector sinh(const GVector &vector)
Computes sinh of vector elements.
Definition: GVector.cpp:1247
std::string print(const GChatter &chatter=NORMAL) const
Print vector information.
Definition: GVector.cpp:637
void free_members(void)
Delete class members.
Definition: GVector.cpp:730
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:317