1. sboz
  2. LinAlg

Wiki

Clone wiki

LinAlg / Home

Overview

LinAlg is a C++ library for basic matrix and vector operations like dot product, cross product, matrix multiplication for small dimensions. The data of vectors and matrices are stored on stack, which can be useful for calculations with temporary variables. The main focus is on simplicity. The matrices and vectors are of double type.

Examples

The base class of the library is an M x N matrix. Number of rows and columns are template parameters.

#include "LinAlg/Matrix.h"
using namespace linalg;

Matrix<4,5> matrix;

The base matrix class provides a convenient element access:

matrix.getValue(0,0) = 0;
matrix(0, 1) = -1;
matrix[2] = 2;
matrix.getValue(3) = 4;
double x = matrix.getValue(2, 1);

The difference between the getValue and operator access is that the getValue functions check for bounds and throw an exception, whereas operator() and operator[] do not do this check. It is also possible to access the whole column or row at once:

Matrix<4, 1> column = matrix.getColumn(1);
column[2] = 10;
matrix.setColumn(3, column);
Matrix<5, 1> row = matrix.getRow(1);

For square matrices the major diagonal can be accessed in the same way:

Matrix<4,4> matrix;
Matrix<4, 1> diag = matrix.getDiagonal();
diag[2] = 10;
matrix.setDiagonal(diag);

There are methods to form widely used matrices: all zero matrix, unit matrix, scaling matrix (diagonal matrix).

matrix.makeZero() //all zero
matrix.makeScaling(2.0)

The base matrix class also provides expected arithmetic operations like matrix sum, scaling matrix with a number, transpose and matrix multiplication. In addition for square matrices raise to power is implemented.

Matrix<4,4> matrix1, matrix2;
matrix1 *= 2.0*matrix2;
matrix1 /= 3.0;
matrix2 = matrix.transpose();

For printing matrices use stream operations:

std::cout<<matrix;

Matrix2D and Matrix3D are derived matrices of size 2 x 2 and 3 x 3 correspondingly. In addition to standard operations they provide finding determinant, inverse matrix and producing matrices for rotation and dot/cross product.

#include "LinAlg/Matrix2D.h"
#include "LinAlg/Matrix3D.h"

using namespace linalg;

Matrix<2,1> col1, col2;
Matrix2D mat2(col1, col2);
mat2.getDeterminant();
mat2.invert();
Matrix3D mat3;
Matrix<3, 1> axis;
axis[2] = 1.0;
mat3.makeRotation(axis, M_PI/4.); //45 degree rotation around z-axis

Vector class is a specialization of the Matrix of dimensions N x 1. Vector class provides convenient constructors, assignment operators, and getters to interoperate with plain arrays, STL vector and vallaray.

#include "LinAlg/Vector.h"

using namespace linalg;

std::vector<double> stl;
stl.push_back(1);
stl.push_back(2);
Vector<3> vec(stl);
std::vallaray val;
vec.getValue(val);

In addition Vector class provides dot product, cross product, magnitude finding and norming.

Vector<3> vec1, vec2, res;
double x = vec1.dotProduct(vec2);
vec1.crossProduct(vec2, res);
res = vec1.crossProduct(vec2);
x = vec1.getMagnitude();
vec2.normalize();

Since Vector is a derived Matrix class, it is possible to perform matrix and vector multiplications. Vector2D and Vector3D are vector specializations for 2d and 3d space.

Build

Installation is standard for cmake projects.

>>> mkdir build && cd build
>>> cmake ..
>>> make
>>> make install

To build tests Boost.Tests is required.

>>> make tests
>>> tests/tests --log_level=test_suite

API

Doxygen generated

Updated