Mathlib
1.0.0.0
A hobbyist C++ library for numerical algorithms
|
MathLib is a simple C++ library for numerical solutions of problems. This is a very short guide on how to get started with MathLib.
In order to use MathLib, you just need to download MathLib's source code. I have coded MathLib as a header-only library, so the header files in /src subdirectory are the only files required to compile programs with MathLib.
When you run the program, it produces the following output :
This page aims to provide an overview and some details on how to perform arithmetic between matrices, vectors and scalars with MathLib.
MathLib offers matrix/vector arithmetic operations through overloads of common C++ operators such as +, -, *, or through dot(), cross(), transpose(), row(), col() etc. For the Matrix class operators are only overloaded to support basic linear algebra operations.
All matrices and vectors are objects of the Matrix template class. Vectors are just a special case of matrices, with 1 row or 1 column.
The Matrix class takes three mandatory template parameters.
Matrix<typename scalarType, int rowsAtCompileTime, int colsAtCompileTime>
Some convenience typedefs are defined to cover the usual cases. For example, Matrix4f
is a 4x4 matrix of floats. Here is how it is defined in MathLib:
typedef Matrix<float, 4, 4> Matrix4f;
MathLib is not limited to matrices whose dimensions are known at compile time. If the dimensions of the matrix are unknown at compile-time, the MatrixX template class can be used. In MathLib, I refer to such a size as dynamic size, while a size that is known at compile-time is called a fixed-size matrix.
A default constructor is always available, never performs any dynamic memory allocation. You can do:
Here,
Constructors for a dynamic matrix taking a user-supplied size is also available.
Matrices and vectors can be initialized from lists of coefficients. Coefficients have to be grouped by rows and passed as an initializer list of initializer lists.
The primary coefficient accessor and mutator in MathLib is the overloaded parentheses operator. The numbering starts at zero.
Matrix and vector coefficients can be conveniently set using the comma-initializer syntax.
Assignment is the action of copying one matrix into the other, using the operator =
.
The left and right hand side matrices must of course be of the same size. They must also have the same scalarType
as MathLib does not support automatic type promotion for now. The operators here are:
Multiplication and division by scalars is very simple too. The operators here are:
k*A
.A*=k
.I have implemented row()
, col()
and block()
operations to support manipulation of rows, columns and sub-matrices of a matrix. For example,
This assigns the row vector \((1,2,3)\) to the row index 1 (second row) of the matrix.