-
Notifications
You must be signed in to change notification settings - Fork 6
Working with matrices
Martijn Koopman edited this page Mar 9, 2019
·
10 revisions
Include required for matrices:
#include <spatium/Matrix.h>
The examples below construct the following 2-by-3 matrix:
1 2 3
4 5 6
Method 1: Explicitly by constructor
spatium::Matrix m(2, 3);
m(0,0) = 1; m(0,1) = 2; m(0,2) = 3;
m(1,0) = 4; m(1,1) = 5; m(1,2) = 6;
Method2: Constructor with initalizer list
spatium::Matrix m({{1, 2, 3}, {4, 5, 6}});
Method 3: Implicitly via initalizer list and assignment
spatium::Matrix m = {{1, 2, 3}, {4, 5, 6}};
spatium::Matrix m(4, 1);
m(0,0) = 1;
m(1,0) = 2;
m(2,0) = 3;
spatium::Vector3 v = m;
Several functions of class Matrix can throw std::out_of_bounds exception. These are:
-
determinant()- If matrix is not square -
inverse()- If matrix is not square or is singular -
operator(row, col)()- Access element by index - Index out of bounds -
operator+()- Add matrices - Dimensions mismatch -
operator+()- Subtract matrices - Dimensions mismatch -
operator*()- Multiply matrices - Column count mismatch with row count of other matrix
Exception: Accessing element by index
spatium::Matrix matrix = {
{3 , 3.2, 3},
{3.5, 3.6, 3}
};
try {
double val = matrix(1, 3);
}
catch(std::out_of_range e)
{
// Column range is 0 - 2 (3 is out of bounds)
}