VectorsΒΆ
The GVector class implements a vector as a one-dimensional array of successive double precision floating point values. You may allocate an vectors as follows
C++
1GVector vector1; // Allocates empty vector
2GVector vector2(10); // Allocates vector with 10 elements
3GVector vector3 = vector1; // Allocates vector by copying
Python
1vector1 = gammalib.GVector() # Allocates empty vector
2vector2 = gammalib.GVector(10) # Allocates vector with 10 elements
3vector3 = vector1.copy() # Allocates vector by copying
On allocation, all elements of a vector are set to 0.
There are three special constructors which you can use to allocate vectors with 1, 2 or 3 elements:
C++
1GVector vector1(1.0); // Allocates vector [1.0]
2GVector vector2(1.0, 2.0); // Allocates vector [1.0, 2.0]
3GVector vector3(1.0, 2.0, 3.0); // Allocates vector [1.0, 2.0, 3.0]
Python
1vector1 = gammalib.GVector(1.0) # Allocates vector [1.0]
2vector2 = gammalib.GVector(1.0, 2.0) # Allocates vector [1.0, 2.0]
3vector3 = gammalib.GVector(1.0, 2.0, 3.0) # Allocates vector [1.0, 2.0, 3.0]
You can access vector elements using the GVector::operator[]() operator. As usual in C++ and Python, indices start with 0:
C++
1std::cout << vector2[1] << std::endl; // Print 2nd element
22.0 // Result
Python
1print(vector2[1]) # Print 2nd element
22.0 # Result
Note that in C++ the GVector::operator[]() operator does not check the validity of the element index. If index checking is needed in C++, use the GVector::at() method:
C++
1std::cout << vector2.at(1) << std::endl; // Print 2nd element with index checking
22.0 // Result
In Python, index checking is always performed.
All GammaLib objects are printable, and vectors can be printed using
C++
1std::cout << vector3 << std::endl; // Print vector
2(1, 2, 3) // Result
Python
1print(vector3) # Print result
2(1, 2, 3) # Result
You can handle vectors pretty much the same way you handle floating point variables. GVector supports various arithmetic operations:
C++
1GVector a(1.0, 2.0, 3.0); // A vector
2GVector b(2.0, 4.0, 6.0); // Another vector
3GVector c; // Result vector
4double s = 2.0; // A floating point value
5c = a + b; // Vector + Vector addition
6c += a; // Unary vector addition
7c = a + s; // Vector + Scalar addition
8c += s; // Unary scalar addition
9c = s + b; // Scalar + Vector addition (only C++)
10c = a - b; // Vector - Vector subtraction
11c -= a; // Unary vector subtraction
12c = a - s; // Vector - Scalar subtraction
13c -= s; // Unary scalar subtraction
14c = s - b; // Scalar - Vector subtraction (only C++)
15s = a * b; // Vector * Vector multiplication (dot product)
16c = a * s; // Vector * Scalar multiplication
17c *= s; // Unary scalar multiplication
18c = s * b; // Scalar * Vector multiplication (only C++)
19c = a / s; // Vector * Scalar division
20c /= s; // Unary scalar division
21c = s; // Assigns scalar to all vector elements
22c = -a; // Vector negation
Python
1a = gammalib.GVector(1.0, 2.0, 3.0) # A vector
2b = gammalib.GVector(2.0, 4.0, 6.0) # Another vector
3c = gammalib.GVector() # Result vector
4s = 2.0 # A floating point value
5c = a + b # Vector + Vector addition
6c += a # Unary vector addition
7c = a + s # Vector + Scalar addition
8c += s # Unary scalar addition
9c = a - b # Vector - Vector subtraction
10c -= a # Unary vector subtraction
11c = a - s # Vector - Scalar subtraction
12c -= s # Unary scalar subtraction
13s = a * b # Vector * Vector multiplication (dot product)
14c = a * s # Vector * Scalar multiplication
15c *= s # Unary scalar multiplication
16c = a / s # Vector * Scalar division
17c /= s # Unary scalar division
18c = -a # Vector negation
Most of these operations operate element-wise. For example, scalar additions or subtractions add or subtract a given scalar value from every vector element. And scalar multiplications and divisions multiply or divide every vector element by a given value. The dot product implements the usual formula
(where \(N\) is the number of vector elements). It is obvious that the dot product, as well as vector addition and subtraction, require vectors of identical dimensions. If vectors are not identical, an GException::vector_mismatch exception will be thrown.
You can compare vectors using
Finally, you can use the comparison operators
C++
1int equal = (a == b); // True if all elements equal
2int unequal = (a != b); // True if at least one elements unequal
Python
1equal = (a == b) # True if all elements equal
2unequal = (a != b) # True if at least one elements unequal
In addition to the operators, you can apply the following mathematical functions to vectors
C++
1c = acos(a); // Vector with acos of all vector elements
2c = acosh(a); // Vector with acosh of all vector elements
3c = asin(a); // Vector with asin of all vector elements
4c = asinh(a); // Vector with asinh of all vector elements
5c = atan(a); // Vector with atan of all vector elements
6c = atanh(a); // Vector with atanh of all vector elements
7c = cos(a); // Vector with cos of all vector elements
8c = cosh(a); // Vector with cosh of all vector elements
9c = exp(a); // Vector with exp of all vector elements
10c = abs(a); // Vector with absolute values of all vector elements
11c = log(a); // Vector with natural logarithm of all vector elements
12c = log10(a); // Vector with base 10 logarithm of all vector elements
13c = sin(a); // Vector with sin of all vector elements
14c = sinh(a); // Vector with sinh of all vector elements
15c = tan(a); // Vector with tan of all vector elements
16c = tanh(a); // Vector with tanh of all vector elements
17c = pow(a, 2.7); // Vector with power of 2.7 of all vector elements (only C++)
18c = cross(a, b); // Vector with cross product of 3 element vectors
19s = norm(a); // Vector norm |a|
20s = min(a); // Minimum element of vector
21s = max(a); // Maximum element of vector
22s = sum(a); // Sum of vector elements
Python
1c = a.acos() # Vector with acos of all vector elements
2c = a.acosh() # Vector with acosh of all vector elements
3c = a.asin() # Vector with asin of all vector elements
4c = a.asinh() # Vector with asinh of all vector elements
5c = a.atan() # Vector with atan of all vector elements
6c = a.atanh() # Vector with atanh of all vector elements
7c = a.cos() # Vector with cos of all vector elements
8c = a.cosh() # Vector with cosh of all vector elements
9c = a.exp() # Vector with exp of all vector elements
10c = a.abs() # Vector with absolute values of all vector elements
11c = a.log() # Vector with natural logarithm of all vector elements
12c = a.log10() # Vector with base 10 logarithm of all vector elements
13c = a.sin() # Vector with sin of all vector elements
14c = a.sinh() # Vector with sinh of all vector elements
15c = a.tan() # Vector with tan of all vector elements
16c = a.tanh() # Vector with tanh of all vector elements
17c = a.cross(b) # Vector with cross product of 3 element vectors
18s = a.norm() # Vector norm |a|
19s = a.min() # Minimum element of vector
20s = a.max() # Maximum element of vector
21s = a.sum() # Sum of vector elements
Finally, the following methods exist:
C++
1int size = a.size(); // Returns number of vector elements
2int nonzeros = a.non_zeros(); // Returns number of non-zero vector elements
Python
1size = a.size(a) # Returns number of vector elements
2nonzeros = a.non_zeros() # Returns number of non-zero vector elements