This is a test Program for the Class Matrix version 1.6.4

Dependencies:   Matrix Kinematics mbed TrackVector2D MatrixMath

Committer:
Yo_Robot
Date:
Sat Oct 22 23:20:26 2011 +0000
Revision:
1:9e4cb305fb24
Parent:
0:63af78c5943c
Child:
2:e3b963c560d8
Added Invert, Determinant.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yo_Robot 0:63af78c5943c 1 #include "mbed.h"
Yo_Robot 0:63af78c5943c 2 #include "Matrix.h"
Yo_Robot 0:63af78c5943c 3 #include "MatrixMath.h"
Yo_Robot 0:63af78c5943c 4
Yo_Robot 0:63af78c5943c 5 int main() {
Yo_Robot 0:63af78c5943c 6
Yo_Robot 0:63af78c5943c 7 DigitalOut myled(LED1);
Yo_Robot 1:9e4cb305fb24 8 Timer t;
Yo_Robot 0:63af78c5943c 9
Yo_Robot 1:9e4cb305fb24 10 t.start();
Yo_Robot 1:9e4cb305fb24 11
Yo_Robot 1:9e4cb305fb24 12 //---
Yo_Robot 0:63af78c5943c 13 Matrix myMatrix(3,3);
Yo_Robot 0:63af78c5943c 14 Matrix anotherMatrix;
Yo_Robot 0:63af78c5943c 15
Yo_Robot 0:63af78c5943c 16 // Fill Matrix with data.
Yo_Robot 0:63af78c5943c 17 myMatrix << 1 << 2 << 3
Yo_Robot 0:63af78c5943c 18 << 4 << 5 << 6
Yo_Robot 0:63af78c5943c 19 << 7 << 8 << 9;
Yo_Robot 0:63af78c5943c 20
Yo_Robot 1:9e4cb305fb24 21 printf( "\nmyMatrix \n");
Yo_Robot 0:63af78c5943c 22 myMatrix.print();
Yo_Robot 0:63af78c5943c 23 printf( "\n" );
Yo_Robot 0:63af78c5943c 24
Yo_Robot 0:63af78c5943c 25
Yo_Robot 0:63af78c5943c 26 /** Matrix operations **/
Yo_Robot 0:63af78c5943c 27
Yo_Robot 0:63af78c5943c 28 // Add 5 to negative Matrix
Yo_Robot 0:63af78c5943c 29 anotherMatrix = - myMatrix + 5;
Yo_Robot 0:63af78c5943c 30
Yo_Robot 0:63af78c5943c 31 printf( "Result Matrix: anotherMatrix = - myMatrix + 5\n" );
Yo_Robot 0:63af78c5943c 32 anotherMatrix.print();
Yo_Robot 0:63af78c5943c 33 printf( "\n" );
Yo_Robot 0:63af78c5943c 34
Yo_Robot 0:63af78c5943c 35 // Matrix Multiplication *
Yo_Robot 0:63af78c5943c 36 anotherMatrix *= myMatrix;
Yo_Robot 0:63af78c5943c 37
Yo_Robot 0:63af78c5943c 38 printf( "anotherMatrix = anotherMatrix * myMatrix\n" );
Yo_Robot 0:63af78c5943c 39 anotherMatrix.print();
Yo_Robot 0:63af78c5943c 40 printf( "\n" );
Yo_Robot 0:63af78c5943c 41
Yo_Robot 0:63af78c5943c 42 // Scalar Matrix Multiplication anotherMatrix *= 0.5
Yo_Robot 0:63af78c5943c 43 anotherMatrix *= 0.5;
Yo_Robot 0:63af78c5943c 44
Yo_Robot 0:63af78c5943c 45 printf( "Result Matrix *= 0.5:\n" );
Yo_Robot 0:63af78c5943c 46 anotherMatrix.print();
Yo_Robot 0:63af78c5943c 47 printf( " _______________________________ \n" );
Yo_Robot 0:63af78c5943c 48
Yo_Robot 0:63af78c5943c 49
Yo_Robot 0:63af78c5943c 50 printf("** MEMBER OPERATIONS ** \n\n");
Yo_Robot 0:63af78c5943c 51
Yo_Robot 0:63af78c5943c 52 //Copy myMatrix
Yo_Robot 0:63af78c5943c 53 Matrix temp( myMatrix );
Yo_Robot 0:63af78c5943c 54
Yo_Robot 0:63af78c5943c 55 // Resize Matrix
Yo_Robot 0:63af78c5943c 56 temp.Resize(4,4);
Yo_Robot 1:9e4cb305fb24 57 printf("\nAdded one Column, one Row to the limitsof myMatrix saved in temp Matrix\n");
Yo_Robot 0:63af78c5943c 58 temp.print();
Yo_Robot 0:63af78c5943c 59
Yo_Robot 0:63af78c5943c 60 //Delete those new elements, we don't need them anyway.
Yo_Robot 0:63af78c5943c 61 Matrix::DeleteRow( temp, 4 );
Yo_Robot 0:63af78c5943c 62 Matrix::DeleteCol( temp, 4 );
Yo_Robot 0:63af78c5943c 63
Yo_Robot 0:63af78c5943c 64 printf("\nBack to normal\n");
Yo_Robot 0:63af78c5943c 65 temp.print();
Yo_Robot 0:63af78c5943c 66
Yo_Robot 0:63af78c5943c 67
Yo_Robot 0:63af78c5943c 68 // Make room at the begining of Matrix
Yo_Robot 0:63af78c5943c 69 Matrix::AddRow( temp, 1 );
Yo_Robot 1:9e4cb305fb24 70 Matrix::AddCol( temp, 1 );
Yo_Robot 1:9e4cb305fb24 71
Yo_Robot 0:63af78c5943c 72 printf("\nAdded Just one Row and column to the beginning\n");
Yo_Robot 0:63af78c5943c 73 temp.print();
Yo_Robot 0:63af78c5943c 74
Yo_Robot 0:63af78c5943c 75 // Take the second Row as a New Matrix
Yo_Robot 0:63af78c5943c 76 anotherMatrix = Matrix::ExportRow( temp, 2 );
Yo_Robot 0:63af78c5943c 77 printf("\nExport Second Row \n");
Yo_Robot 0:63af78c5943c 78 anotherMatrix.print();
Yo_Robot 0:63af78c5943c 79
Yo_Robot 0:63af78c5943c 80 // The second Column as a ner Matrix, then transpose it to make it a Row
Yo_Robot 0:63af78c5943c 81 anotherMatrix = Matrix::ExportCol( temp, 2 );
Yo_Robot 0:63af78c5943c 82 anotherMatrix = MatrixMath::Transpose( anotherMatrix );
Yo_Robot 0:63af78c5943c 83 printf("\nAnd Export Second Column and Transpose it \n");
Yo_Robot 0:63af78c5943c 84 anotherMatrix.print();
Yo_Robot 0:63af78c5943c 85
Yo_Robot 0:63af78c5943c 86 // This will Check to see if your are reduce to a single Row or Column
Yo_Robot 0:63af78c5943c 87 temp = Matrix::ToPackedVector( myMatrix );
Yo_Robot 0:63af78c5943c 88 printf("\nInitial Matrix turned into a single Row\n");
Yo_Robot 0:63af78c5943c 89 temp.print();
Yo_Robot 0:63af78c5943c 90
Yo_Robot 1:9e4cb305fb24 91 /** Mtrix Math **/
Yo_Robot 1:9e4cb305fb24 92 printf("\n Matrix Inverse and Determinant\n");
Yo_Robot 1:9e4cb305fb24 93
Yo_Robot 1:9e4cb305fb24 94 Matrix BigMat( 5, 5 );
Yo_Robot 1:9e4cb305fb24 95 BigMat << 1 << 0 << 1 << 1 << 3
Yo_Robot 1:9e4cb305fb24 96 << 2 << 3 << 4 << 0 << 4
Yo_Robot 1:9e4cb305fb24 97 << 1 << 6 << 1 << 1 << 2
Yo_Robot 1:9e4cb305fb24 98 << 1 << 0 << 2 << 1 << 1
Yo_Robot 1:9e4cb305fb24 99 << 2 << 3 << 4 << 2 << 0;
Yo_Robot 1:9e4cb305fb24 100
Yo_Robot 1:9e4cb305fb24 101 printf( "\nBigMat:\n");
Yo_Robot 1:9e4cb305fb24 102 BigMat.print();
Yo_Robot 1:9e4cb305fb24 103 printf( "\n" );
Yo_Robot 1:9e4cb305fb24 104
Yo_Robot 1:9e4cb305fb24 105 float determ = MatrixMath::det( BigMat );
Yo_Robot 1:9e4cb305fb24 106
Yo_Robot 1:9e4cb305fb24 107 printf( "\nBigMat's Determinant is: %f \n", determ);
Yo_Robot 1:9e4cb305fb24 108 printf( "\n" );
Yo_Robot 1:9e4cb305fb24 109
Yo_Robot 1:9e4cb305fb24 110 Matrix myInv = MatrixMath::Inv( BigMat );
Yo_Robot 1:9e4cb305fb24 111
Yo_Robot 1:9e4cb305fb24 112 printf( "\nBigMat's Inverse is:\n");
Yo_Robot 1:9e4cb305fb24 113 myInv.print();
Yo_Robot 1:9e4cb305fb24 114 printf( "\n" );
Yo_Robot 1:9e4cb305fb24 115
Yo_Robot 1:9e4cb305fb24 116 //---
Yo_Robot 1:9e4cb305fb24 117
Yo_Robot 1:9e4cb305fb24 118
Yo_Robot 1:9e4cb305fb24 119 t.stop();
Yo_Robot 1:9e4cb305fb24 120
Yo_Robot 1:9e4cb305fb24 121 printf( "\nThe time for all those operations in mbed was : %f seconds\n", t.read() );
Yo_Robot 1:9e4cb305fb24 122 printf( "\nMost of it is due to printf though in my core i5 takes 100ms with printf() \n" );
Yo_Robot 1:9e4cb305fb24 123 printf( "\nOnly operations witout any print takes: 0.025 seconds :) \n" );
Yo_Robot 0:63af78c5943c 124
Yo_Robot 0:63af78c5943c 125 while(1) {
Yo_Robot 0:63af78c5943c 126 myled = 1;
Yo_Robot 0:63af78c5943c 127 wait(0.2);
Yo_Robot 0:63af78c5943c 128 myled = 0;
Yo_Robot 0:63af78c5943c 129 wait(0.2);
Yo_Robot 0:63af78c5943c 130 }
Yo_Robot 0:63af78c5943c 131 }