Matrix - library for Arduino matrix calculation!

  Matrix.h - Library for matrix calculation. Invalid dimension for calculation
  will lead to empty matrix, therefore, one should check whether the matrix is
  empty after calculation.

  This library is easy to use, I have overloaded operators, one can use +,*,/,-,
  =,==,!= as they want, but there is no definition for matrix division (like A/B).
  One should use the inverse function inv() instead.

  To get a better performance, it's recommended to use /=, *=, -= and += if you
  have A = A*B, A = A+B or A = A/b (b is a scalar). If you have A = B*C,
  the A.times(const Matrix & B,const Matrix & C) function gives faster performance.

  However, using this library to do matrix calculation is not very
  efficient, if you have a time-critical sketch, it would be better to write your
  own code which is optimised for your project.

  The transpose() and inv() functions are static functions, which should use
  Matrix<Type>::transpose() and Matrix<Type>::inv() format to call, Type is the
  type you used to create the matrix.

  Created by Yudi Ren, Jan 05, 2018.

  Feedback is welcome!

  renyudicn@outlook.com
  Version 0.2

FEATURES

This library supports c++ template, which means you could use any type you want. Here we use word "Any" to represent the type. At first, you have to create the instance of the matrix as shown below:

Matrix<Any> A();  //create an empty matrix
Matrix<Any> B(2,2);  //create a 2 by 2 matrix with all elements are zero
Matrix<Any> C(5,2,5);   //create a 5 by 2 matrix with all elements equal 5
Matrix<Any> D(4,4,'I');   //create a 4 by 4 identity matrix, it has to be a square matrix, 
                       //otherwise it creates an empty matrix
Any array[3][4] = {{1,2,3,10},{4,5,6,11},{7,8,9,12}};
Matrix<Any> E(3,4,(Any*)array); //create a 3 by 4 matrix and initialise it by the array
Matrix<Any> D(E);  //copy the matrix E

Note you HAVE to cast the array into (Any*) type if you want to use an array to initialise it.

This library is easy to use, I have overloaded operators, one can use +,*,/,-, =,==,!= as they want, but there is no definition for matrix division (like A/B). One should use the inverse function inv() instead.

To get a better performance, it's recommended to use /=, *=, -= and += if you have A = A*B, A = A+B or A = A/b (b is a scalar).

This library also provides functions for transpose, inverse, swap row, display and to check whether the matrix is empty or not:

static Matrix inv(const Matrix & A); //The inverse function uses the Gauss-Jordan 
                                     //Elimination,it won't modify the original matrix and it returns the inverse of  matrix A
static Matrix transpose(const Matrix & A);  //The transpose function won't modify the original matrix and it returns the transpose of matrix A
void swapRow(int i, int j);  //i: the row needs to be swaped, j: the position the row i goes to, this function modifies the original matrix
void show(int decimal = 0);  //decimal: the numbers of decimal place
bool notEmpty();  //check if it's an empty matrix.

The transpose() and inv() functions are static functions, which should use Matrix<Any>::transpose() and Matrix<Any>::inv() format to call. See the example for more information.

However, using this library to do matrix calculation is not very efficient, if you have a time-critical sketch, it would be better to write your own code which is optimised for your project.

How to install

The source is on GitHub, put all the files and folders in one folder called "Matrix" and then place this new folder in the library folder of Arduino. Refer to https://forum.arduino.cc/index.php?topic=88380.0 for more information.

After this step, you could open a new Arduino IDE window, from the menu bar "File->Examples->Matrix->matrix_example" to use the example.

Share