GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GVector.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GVector.hpp - Vector class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2006-2025 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 ***************************************************************************/
46class 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
77public:
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 GVector& vector);
98 GVector& operator/=(const GVector& vector);
99 GVector& operator=(const double& scalar);
100 GVector& operator+=(const double& scalar);
101 GVector& operator-=(const double& scalar);
102 GVector& operator*=(const double& scalar);
103 GVector& operator/=(const double& scalar);
104 GVector operator-(void) const;
105
106 // Vector methods
107 void clear(void);
108 GVector* clone(void) const;
109 std::string classname(void) const;
110 const int& size(void) const;
111 double& at(const int& index);
112 const double& at(const int& index) const;
113 int non_zeros(void) const;
114 int first_nonzero(void) const;
115 int last_nonzero(void) const;
116 GVector slice(const int& start, const int& stop) const;
117 std::string print(const GChatter& chatter = NORMAL) const;
118
119private:
120 // Private methods
121 void init_members(void);
122 void alloc_members(void);
123 void copy_members(const GVector& vector);
124 void free_members(void);
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 ***************************************************************************/
137inline
138std::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 ***************************************************************************/
150inline
151double& 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 ***************************************************************************/
164inline
165const 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 ***************************************************************************/
179inline
180const 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 ***************************************************************************/
195inline
196GVector 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 ***************************************************************************/
213inline
214GVector 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 ***************************************************************************/
231inline
232GVector 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 ***************************************************************************/
249inline
250GVector 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 ***************************************************************************/
268inline
269GVector 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 ***************************************************************************/
287inline
288GVector 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 ***************************************************************************/
305inline
306GVector 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 ***************************************************************************/
323inline
324GVector 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 ***************************************************************************/
341inline
342GVector operator/(const GVector& vector, const double& scalar)
343{
344 GVector result = vector;
345 result /= scalar;
346 return result;
347}
348
349#endif /* GVECTOR_HPP */
Definition of interface for all GammaLib classes.
Exception handler interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
GVector operator/(const GVector &vector, const double &scalar)
Divide vector by scalar.
Definition GVector.hpp:342
GVector operator*(const GVector &vector, const double &scalar)
Multiply vector by scalar.
Definition GVector.hpp:306
GVector operator+(const GVector &a, const GVector &b)
Add two vectors.
Definition GVector.hpp:196
GVector operator-(const GVector &a, const GVector &b)
Subtract vector from vector.
Definition GVector.hpp:250
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Vector class.
Definition GVector.hpp:46
friend GVector cos(const GVector &vector)
Computes cosine of vector elements.
Definition GVector.cpp:1258
friend GVector cosh(const GVector &vector)
Computes cosh of vector elements.
Definition GVector.cpp:1279
friend double norm(const GVector &vector)
Computes vector norm.
Definition GVector.cpp:932
bool operator==(const GVector &vector) const
Equality operator.
Definition GVector.cpp:260
friend GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition GVector.cpp:1342
friend GVector abs(const GVector &vector)
Computes absolute of vector elements.
Definition GVector.cpp:1321
friend double angle(const GVector &a, const GVector &b)
Computes angle between vectors.
Definition GVector.cpp:1042
friend double min(const GVector &vector)
Computes minimum vector element.
Definition GVector.cpp:954
void alloc_members(void)
Allocate vector.
Definition GVector.cpp:778
friend GVector pow(const GVector &vector, const double &power)
Computes tanh of vector elements.
Definition GVector.cpp:1490
std::string print(const GChatter &chatter=NORMAL) const
Print vector information.
Definition GVector.cpp:729
friend double operator*(const GVector &a, const GVector &b)
Vector scalar product.
Definition GVector.cpp:904
GVector & operator+=(const GVector &vector)
Unary addition operator.
Definition GVector.cpp:307
friend GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
Definition GVector.cpp:1106
void copy_members(const GVector &vector)
Copy class members.
Definition GVector.cpp:803
GVector slice(const int &start, const int &stop) const
Vector slice operator.
Definition GVector.cpp:702
int last_nonzero(void) const
Return the index of the last non-zero element in a vector.
Definition GVector.cpp:677
GVector & operator/=(const GVector &vector)
Unary division operator.
Definition GVector.cpp:405
friend GVector tan(const GVector &vector)
Computes tangens of vector elements.
Definition GVector.cpp:1447
friend GVector asinh(const GVector &vector)
Computes asinh of vector elements.
Definition GVector.cpp:1195
GVector & operator-=(const GVector &vector)
Unary subtraction operator.
Definition GVector.cpp:339
friend GVector acos(const GVector &vector)
Computes arccos of vector elements.
Definition GVector.cpp:1132
void clear(void)
Clear vector.
Definition GVector.cpp:557
void init_members(void)
Initialise class members.
Definition GVector.cpp:764
const int & size(void) const
Return size of vector.
Definition GVector.hpp:180
GVector * clone(void) const
Clone vector.
Definition GVector.cpp:575
GVector operator-(void) const
Unary minus operator.
Definition GVector.cpp:533
double * m_data
Vector array.
Definition GVector.hpp:128
friend GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
Definition GVector.cpp:1079
int m_num
Number of elements in vector.
Definition GVector.hpp:127
friend GVector exp(const GVector &vector)
Computes exponential of vector elements.
Definition GVector.cpp:1300
friend GVector atanh(const GVector &vector)
Computes atanh of vector elements.
Definition GVector.cpp:1237
bool operator!=(const GVector &vector) const
Non-equality operator.
Definition GVector.cpp:286
int first_nonzero(void) const
Return the index of the first non-zero element in a vector.
Definition GVector.cpp:651
GVector & operator=(const GVector &vector)
Assignment operator.
Definition GVector.cpp:230
virtual ~GVector(void)
Destructor.
Definition GVector.cpp:208
int non_zeros(void) const
Returns number of non-zero elements in vector.
Definition GVector.cpp:627
friend GVector tanh(const GVector &vector)
Computes tanh of vector elements.
Definition GVector.cpp:1468
double & operator[](const int &index)
Vector element access operator.
Definition GVector.hpp:151
friend GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition GVector.cpp:1384
GVector & operator*=(const GVector &vector)
Unary multiplication operator.
Definition GVector.cpp:371
void free_members(void)
Delete class members.
Definition GVector.cpp:829
friend GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Definition GVector.cpp:1363
friend double sum(const GVector &vector)
Computes vector sum.
Definition GVector.cpp:1012
std::string classname(void) const
Return class name.
Definition GVector.hpp:138
friend GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition GVector.cpp:1426
friend GVector atan(const GVector &vector)
Computes arctan of vector elements.
Definition GVector.cpp:1216
friend double max(const GVector &vector)
Computes maximum vector element.
Definition GVector.cpp:983
friend GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition GVector.cpp:1174
friend GVector sinh(const GVector &vector)
Computes sinh of vector elements.
Definition GVector.cpp:1405
friend GVector acosh(const GVector &vector)
Computes acosh of vector elements.
Definition GVector.cpp:1153
friend GVector cross(const GVector &a, const GVector &b)
Vector cross product.
Definition GVector.cpp:861
double & at(const int &index)
Vector element access with range checking.
Definition GVector.cpp:590
GVector(void)
Void vector constructor.
Definition GVector.cpp:56