Skip to content

Working with matrices

Martijn Koopman edited this page Mar 9, 2019 · 10 revisions

Include required for matrices:

#include <spatium/Matrix.h>

Construct a matrix

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});

Method 3: Implicitly via initalizer list and assignment

spatium::Matrix m = {{1, 2}, {3, 4}};

Implicit conversion

spatium::Matrix matrix(4, 1);
matrix(0,0) = 1;
matrix(1,0) = 2;
matrix(2,0) = 3;

spatium::Vector3 vector = matrix;

Exception handling

A std::out_of_bounds exception can be thrown in many situations.

Exception: accessing index out of bounds

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)
}

Exception: Matrix has no inverse

try {
  Matrix inv = matrix.inverse();
}
catch(std::out_of_range e)
{
  // Matrix has no inverse
}

Clone this wiki locally