Simple Matrix template class in C++ with a few general utilities and mathematical operations. The objective of this class is to have a structured matrix data type that can be added to any project, allowing easy data storing, carrying and accessing in matrix form, as well as performing algebraic calculations on such data.
The Matrix Class .h and .cpp templates are in include/. The folder src/ contains source to test programs. The output of this tests can be seen in the .txt files in genfiles/ and used for checking that the class compiled fine.
# Clone repo
$ git clone <url> Matrix
# Create building directories and compile with CMake
$ mkdir build bin lib
$ cd build/
$ cmake ../
$ make
$ cd ../bin
$ ./test_matrix #For running the basic initialization test
In the project's root folder create a new directory called include. Then, Copy the files Matrix.h and Matrix.cpp to it. Create an empty directory called lib in the root folder.
In the CMake file of the project add the following lines:
# At the beginning, after setting the CMakeLists version and project name
INCLUDE_DIRECTORIES(include)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# Build Shared Libraries
add_library(Matrix SHARED include/Matrix.cpp)
# Add the library to your executables:
add_executable(exec_name src/source_code.cpp)
target_link_libraries(exec_name Matrix)
A. Constructor
Matrix(int rows, int columns, const T& init);
Requires the number of rows and columns and an intial value to fill the matrix up. As it is a template, requires specifiying the data type inside the matrix:
// Initialization
Matrix<int> M(2, 3, 0);
B. Copy constructor
Matrix(const Matrix<T>& A);
Requires another matrix as argument.
A. Matrix size.
Get the number of rows and columns of the matrix:
int getRows() const;
int getColumns() const;
Member functions that do not modify the class instance should be declared as const. Functions that are useful to resize new matrices and vectors resulting from mathematical operations.
B. Accessing individual elements.
T& getElement(int row, int col);
Returns the element at selected row and column.
C. Updating individual elements.
void updateElement(int row, int col, T value);
Updates the value of the element at the selected row and column.
A. Printing the matrix in the console.
printMatrix();
Just outputs it as:
Matrix[3][3]:
1,2,-1
2,4,-2
3,6,-3
A. Using ( ) to acces matrix element.
T& operator()(const int& row, const int& col);
Returns the element M(row, col).
const T& operator()(const unsigned& row, const unsigned& col) const
Returns const element at the selected row and column.
A. Assignment operator.
Matrix<T>& operator=(const Matrix<T>& A);
This operation is needed to produce new matrices resulting from the below operations (like C = A + B).
B. Sum operator.
Matrix<T> operator+(const Matrix<T>& A);
Sum of two matrices.
C. Substraction operator.
-Matrix<T> operator-(const Matrix<T>& A);
Substraction of two matrices.
D. Multiplication operator.
-Matrix<T> operator*(const Matrix<T>& A);
Multiplication of two matrices.
A. Product of a matrix times a column vector.
vector<T> matrixByColumnVector(vector<T> vec);
Returns the vector z resulting from the operation z [m x 1] = A [m x n] * v [n x 1]. If the number of columns in A and the size of v do not match, returns a vector of zeros and an error message.
B. Product of a column vector times a row vector.
Matrix<T> columnVectorByRowVector(vector<T> a, vector<T> b);
Returns the matrix R resulting from the operation R [m x m] = a [m x 1] * b [1 x m]. If the size of a and b do not match, returns a matrix of zeros and an error message.
C. Product of a row vector times a matrix.
Matrix<T> columnVectorByRowVector(vector<T> a, vector<T> b);
Returns the vector y resulting from the operation y (1 x n) = x (1 x m) * H (m x n). If the size of x does not match with the number of rows in H, returns a vector of zeros and an error message.
A. Determinant of the matrix.
void getCoFactors(vector<vector<T> > array, vector<vector<T> >& temp, int size, int r, int c);
Calculates co-factors at a given matrix position and stores them in a vector.
T iterateDet(vector<vector<T> > array, int size);
Performs the iteration over the several co-factors needed to get the determinant of the matrix.
T getDeterminant(int size);
Calls both of the previous functions and returns the final value of the determinant. Returns error and an empty matrix if it is not square. See this video for more details on how to calculate the determinant of a large matrix.
B. Multiplication of a matrix by a scalar.
Matrix<T> scalarTimesMatrix(T scalar);
Returns a matrix resulting for multiplying a single scalar times itself. Use it to perform scalar division as Matrix * 1/scalar.
B. Sum of a matrix by a scalar.
Matrix<T> scalarPlusMatrix(T scalar);
Returns a matrix resulting for adding a single scalar to it.
D. Substraction of a matrix by a scalar.
Matrix<T> scalarMinusMatrix(T scalar);
Returns a matrix resulting for substracting a single scalar to it.
E. Diagonal Vector.
vector<T> diagonalVector();
Returns a vector with the diagonal components of the square matrix.
A. Identity Matrix.
Matrix<T> identity();
Returns the identity matrix of the size used to construct the matrix.
B. Transpose Matrix.
Matrix<T> transpose();
Returns the transpose of the original matrix.
C. Adjoint Matrix.
Matrix<T> adjoint();
Returns the adjoint matrix of the input one. Returns error and an empty matrix if it is not square. See more details about the adjoint matrix here.
D. Inverse of the Matrix.
Matrix<T> inverse();
Returns the inverse of the given matrix A: inv(A) = adj(A) * 1/det(A) . Returns error and an empty matrix if it is not square.
New methods:
- Computation of rotation matrices from RPY angles.
- Computation of RPY vectors from rotation matrices.
- Computation of Homogeneous Transform matrices from translation + RPY vectors.
- Computation of Homogeneous Transfrom matrices from translation vector + rotation matrix.
- Hadamard product between 2 matrices.
- Hadamard product between 2 vectors.
New tools:
- Command line tool for operations with RPY transform matrices.
- Carlos Viescas.
- gitcvh95@gmail.com
- MSc Robot Systems, University of Southern Denmark.
- Math Insight.
- QuantStart
- Determinant of large matrices.