You are viewing an older revision! See the latest version

MatrixClass

A class to handle Matrices

This class is intended for handling float matrices, to re-arrange and perform operations on them, similar to a MATLAB enviroment. I haven't seen something like this for mbed, so I decided I'll make one.

I created this class using <vector>'s. I write it and debugged it in Netbeans then port it to mbed. The idea is to be able to work with Neural Networks and Kinematics eventually.

I'm also working on a MatrixMath class, to give basic matrix operations to Matrix objects. So far this class only provide TRANSPOSE operation, I'll write functions to find the DETERMINANT and INVERSE of a Matrix, and in a near future (hopefully) add more and more operations.

This is my biggest and best C++ program. I'm still learning so any advice or improvement is well recieved.

Ok so the Class I'm talking about is this:

Import libraryMatrix

Matrix Library. v1.6.4

Creating a Matrix object

To create a Matrix you need to define the number of Rows and Columns. Or use another Matrix to create an exact copy.

 //This will create a 3 by 3 matrix 
//all elements initialized to zero.

Matrix myMatrix(3,3);

 //This will call the Copy constructor
Matrix anotherMatrix( myMatrix );

 //Or just initialize a matrix to later determine its dimmensions
Matrix temp;

Change the dimmensions of a Matrix

Say you want to resize a matrix, add more space or reduce it. In that case you use the function Resize.

But if you want to increment a new Row to the middle of the matrix, or a new column? Then you use the static function AddRow/AddCol and to delete one the functions are DeleteRow/DeleteCol.

myMatrix.Resize( 4,4 )  //Expanded Matrix dimensions

Matrix::AddRow( anotherMatrix, 2 );        // Moves Rows 2,3,...  down and creates a new Row where 2 used to be.
Matrix::DeleteCol( anotherMatrix, 3 );    //  Deletes Column 3.

Filling a Matrix

Now that you have a Matrix with dimensions, you can input data to it using:

  //The fastest way if you know your data is
myMatrix << 1 << 2 <<3
         << 4 << 5 <<6
         << 7 << 8 <<9;

 // Or ask the user to fill it.
myMatrix.FillMatrix();

 // Or change a specific element
myMatrix.add( 2, 2, 0 );  // Re-writes element at position Row[2]Col[2] to a vlaue of 0
                         // In this case changes 5 to 0

Important

Matrix indexing starts with one, not zero, so to say the first element of Matrix is in position Row[1]Col[1].
Also, FillMatrix() is a virtual function to use with whatever Comunication protocol you want, but take a look at my implementation first for details.
You may have noticed that the INPUT (<<) operator fills the Matrix as if it were an array, this is because the Matrix dimensions (Row, Col) have already been defined, this operand will NOT work in declared only matrices( Matrix obj; ).

Matrix Operations

Your matrices now have data, let's see what operations you can perform on them.

temp = -myMatrix + 5;                  //Multiplies each element in Matrix by (-1) then adds
                                      // (5) to each one.

anotherMatrix *= myMatrix;           // Performs Cross Product multiplication between itself and myMatrix.

anotherMatrix -= myMatrix;          // Decrements element-by-element. Both matrices must be same dimensions.

if( Matrix::Equals( anotherMatrix, myMatrix ) )         // Checks element-by-element, returns true if
    printf( "These two matrices are equal!\n" );       //  matrices are identical.


temp.Clear();                                        // Makes all elements in matrix zero.

if( temp.isZero() )                                 // Will be true 'cause all elements are zero.
    printf( "This matrix is empty!\n" );

myMatrix.print();                                 // This will Oputput the matrix to the default USB RxTx port
                                                 //  serial 9600 8,n,1

Information

Operators also support
obj1 = obj2 * obj3;
obj1 = obj2 * 0.5;
obj2 = obj1 - 5;

There are more SATATIC functions to provide more flexibility with the matrix.

WARNING!

If you haven't noticed yet the Assignment operator overwrites a Matrix, that is to say destroys whatever data the old matrix contained and puts new data into it.

So don't rely that because two Matrices are different dimensions the assignment will not occur, it will.

Your code <here>

This covers the basic of matrix handling and operations, but it's time to use all this and create your own programs, and these functions will help you do that.

int lengthX, lenghtY;

lengthX = myMatrix.getCols();   //returns the number of columns.
lengthY = myMatrix.getRows();   //returns the number of rows.

int firstNumber;

firstNumber = myMatrix.getNumber( 1, 1 );  //First element of Matrix.

Matrix vectorM = Matrix::ToPackedArray( myMatrix );  // Flaterns Matrix.

vectorM = Matrix::ExtractRow( myMatrix, 1 );         //Extracts first Row. Similar Function for Column

if( vectorM.isVector() )
    printf( "This is either a SingleRow or SingleColumn Matrix");

I hope the documentation is easy to understand, there are a few other functions I didn't cover here, and an example program here to better ilustrate the class.

I'm looking forward to make a classTemplate with similar characteristics but for all sort of data types. I'll write another class on top of these for Kinematics, very simple at first coding a Jacobian function will be quite a challenge.

API Versions

If you make use of this class let me know, so I can maintain the API and don break your code with an update. But as version 1.6 of this class I will be maintaining the functionality.


All wikipages