Matrix arithmeticsΒΆ
The following description of matrix arithmetics applies to all storage classes. The following matrix operators have been implemented:
C++
1GMatrix A(10,10); // A 10 x 10 matrix
2GMatrix B(10,10); // Another 10 x 10 matrix
3GMatrix C; // Result matrix
4GVector v(10); // Vector with 10 elements
5double s = 2.0; // Floating point value
6C = A + B; // Matrix addition
7C = A - B; // Matrix subtraction
8C = A * B; // Matrix multiplication
9C = A * v; // Vector multiplication
10C = A * s; // Right-handed scalar multiplication
11C = s * A; // Left-handed scalar Matrix multiplication (only C++)
12C = A / s; // Scalar division
13C = -A; // Negation
14A += B; // Unary matrix addition
15A -= B; // Unary matrix subtraction
16A *= B; // Unary matrix multiplications
17A *= s; // Unary matrix scalar multiplication
18A /= s; // Unary matrix scalar division
Python
1A = gammalib.GMatrix(10,10) # A 10 x 10 matrix
2B = gammalib.GMatrix(10,10) # Another 10 x 10 matrix
3v = gammalib.GVector(10) # Vector with 10 elements
4s = 2.0 # Floating point value
5C = A + B # Matrix addition
6C = A - B # Matrix subtraction
7C = A * B # Matrix multiplication
8C = A * v # Vector multiplication
9C = A * s # Scalar multiplication
10C = A / s # Scalar division
11C = -A # Negation
12A += B # Unary matrix addition
13A -= B # Unary matrix subtraction
14A *= B # Unary matrix multiplications
15A *= s # Unary matrix scalar multiplication
16A /= s # Unary matrix scalar division
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 a matrix:
C++
1C = A.abs(); // Matrix with absolute values of all matrix elements
2C = A.transpose(); // Transpose matrix
3C = A.invert(); // Invert matrix
4v = A.solve(v); // Solve matrix equation x = M x v
5s = A.min(); // Minimum element of matrix
6s = A.max(); // Maximum element of matrix
7s = A.sum(); // Sum of matrix elements
Python
1C = A.abs() # Matrix with absolute values of all matrix elements
2C = A.transpose() # Transpose matrix
3C = A.invert() # Invert matrix
4v = A.solve(v) # Solve matrix equation x = M x v
5s = A.min() # Minimum element of matrix
6s = A.max() # Maximum element of matrix
7s = A.sum() # Sum of matrix elements
Warning
The invert()
and solve()
methods are so far only implemented for
sparse matrices.