Matrix Library. v1.6.4
Dependents: Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more
Revision 2:493402568a5e, committed 2011-10-20
- Comitter:
- Yo_Robot
- Date:
- Thu Oct 20 23:42:13 2011 +0000
- Parent:
- 1:48f417da268e
- Child:
- 3:589fb80932b5
- Commit message:
- Everything working fine, dropped some useless methods, Matrix Class is Complete, maybe I\ll keep adding stuff but basic usage is covered in my opinion.
Changed in this revision
--- a/Matrix.cpp Mon Sep 26 03:23:35 2011 +0000 +++ b/Matrix.cpp Thu Oct 20 23:42:13 2011 +0000 @@ -1,152 +1,60 @@ +/** + * @file: Matrix.cpp + * @author: Ernesto Palacios + * @brief: Source Code for the Matrix Class. + * + * Created on September 2011. + * + */ + #include "mbed.h" #include "Matrix.h" - /// Rows by Cols Matrix Constructor -Matrix::Matrix(int Rows, int Cols): _nCols(Cols), _nRows(Rows) +Matrix::Matrix(int Rows, int Cols): _nRows(Rows), _nCols(Cols) { _matrix.resize(_nRows); for( int i = 0; i < _nRows; i++ ) _matrix[i].resize(_nCols); + + _pRow = 0; + _pCol = 0; + + this->Clear(); //Make all elements zero by default. } -/// Defines the same parameters as 'base' Matrix -Matrix::Matrix(const Matrix &base): _nCols(base._nCols), _nRows(base._nRows) +/// Copies one matrix into a new one +Matrix::Matrix(const Matrix& base) { + _nCols = base._nCols; + _nRows = base._nRows; + + _pRow = base._pRow; + _pCol = base._pCol; + _matrix.resize(_nRows); for( int i = 0; i < _nRows; i++ ) _matrix[i].resize(_nCols); -} - -/// Square Matrix Constructor -Matrix::Matrix(int square): _nCols(square), _nRows(square) -{ - _matrix.resize(_nRows); for( int i = 0; i < _nRows; i++ ) - _matrix[i].resize(_nCols); + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] = base._matrix[i][j]; } /// Default Constructor -Matrix::Matrix(){/*Intentionally left Blank*/} - - -/************************************************************************/ - - -/// Overloaded Asign Operator. Resizes Matrix -Matrix& Matrix::operator = ( const Matrix &rightM ) +Matrix::Matrix() { - if( *this != rightM ) - { - this->_nRows = rightM._nRows; - this->_nCols = rightM._nCols; - - this->_matrix.resize( rightM._nRows ); - for( int i = 0; i < rightM._nRows; i++ ) - this->_matrix [i].resize(rightM._nCols); - - for( int i = 0; i < _nRows; i++ ) - for( int j = 0; j < _nCols; j++ ) - this->_matrix[i][j] = rightM._matrix[i][j]; - - return *this; - - }else{ + _nCols = 0; + _nRows = 0; - Matrix ret (*this); - return ret; - } -} - -/// Comapre element by element -bool Matrix::operator == ( const Matrix& rightM ) -{ - if( _nRows == rightM._nRows && _nCols == rightM._nCols ) - { - bool equal = false; - - for( int i = 0; i < _nRows; i++ ) - for( int j = 0; j < _nCols; j++ ) - if( _matrix[i][j] != rightM._matrix[i][j] ) - equal = equal || true; - - return !equal; - - }else{ return false; } -} - - -/// Calls for '==' operator -bool Matrix::operator != ( const Matrix &rightM ) -{ - return !( *this == rightM ); + _pRow = 0; + _pCol = 0; + } - -/// Matrices must be same size. -/// Element by element adition. -Matrix& Matrix::operator +=(const Matrix& rightM) -{ - if( _nRows == rightM._nRows && _nCols == rightM._nCols ) - { - for( int i = 0; i < _nRows; i++ ) - for( int j = 0; j < _nCols; j++ ) - _matrix[i][j] += rightM._matrix[i][j]; - - return *this; - - }else{ - printf( "\n\nERROR\nDiferent Dimensions @ += \n" ); - // Matrix error(4); - // error.Clear(); - // return error; - } -} - - -/// Matrices must be same size. -/// Element by element Substraction -Matrix& Matrix::operator -=(const Matrix& rightM) -{ - if( _nRows == rightM._nRows && _nCols == rightM._nCols ) - { - for( int i = 0; i < _nRows; i++ ) - for( int j = 0; j < _nCols; j++ ) - _matrix[i][j] -= rightM._matrix[i][j]; - - return *this; - - }else{ - printf( "\n\nERROR\nDiferent Dimensions @ -= \n" ); - //Matrix error(4); - //error.Clear(); - //return error; - } -} - - -/// Element by element Adition. Must be same sizes -const Matrix Matrix::operator +(const Matrix& rightM) -{ - Matrix result = *this; - result += rightM; - return result; -} - - -/// Element by element Substraction. Must be same sizes -const Matrix Matrix::operator -(const Matrix& rightM) -{ - Matrix result = *this; - result -= rightM; - return result; -} - -/************************************************************************/ - +/***********************************************************************/ /// Returns true if matrix is full of zeros bool Matrix::isZero() @@ -154,7 +62,7 @@ bool zero = false; for( int i = 0; i < this->_nRows; i++ ) for( int j = 0; j < this->_nCols; j++ ) - if( this->_matrix[i][j] != 0 ) + if( _matrix[i][j] != 0 ) zero = zero || true; return !zero; } @@ -163,7 +71,7 @@ /// Returns true if Matrix is Single Row ot Single Column. bool Matrix::isVector() { - if( this->_nRows == 1 || this->_nCols == 1 ) + if( _nRows == 1 || _nCols == 1 ) return true; else return false; @@ -172,27 +80,30 @@ /*************************************************************************/ /// Returns all elements in Matrix as a single Row vector. -Matrix Matrix::ToPackedVector() +const Matrix Matrix::ToPackedVector( const Matrix& Mat ) { - Matrix Shattered( ( this->_nRows * this->_nCols ) , 1 ); + + Matrix Crushed( 1, Mat._nRows * Mat._nCols ); int cont = 0; - for( int i = 0; i < this->_nRows; i++ ) - { - for( int j = 0; j < this->_nCols; j++ ) + + for( int i = 0; i < Mat._nRows; i++ ) + for( int j = 0; j < Mat._nCols; j++ ) { - Shattered._matrix[0][cont] = this->_matrix[i][j]; + Crushed._matrix[0][cont] = Mat._matrix[i][j]; cont++; } - } - return Shattered; + Crushed._pRow = Crushed._nRows; + Crushed._pCol = Crushed._nCols; + + return Crushed; } -/***************************************************************************/ + /// Test elment-by-element. Prints detailed error. -bool Matrix::Equals( const Matrix mat1, const Matrix mat2 ) +bool Matrix::Equals( const Matrix& mat1, const Matrix& mat2 ) { if( mat1._nCols == mat2._nCols && mat1._nRows == mat2._nRows ) { @@ -214,39 +125,7 @@ } -/// To Implicitly create a Single Column Matrix. -Matrix Matrix::CreateColumnMatrix(int Cols) -{ - Matrix ColMatrix(1,Cols); - return ColMatrix; -} - - -/// To Implicitly create a Single Row Matrix. -Matrix Matrix::CreateRowMatrix(int Rows) -{ - Matrix RowMatrix( Rows, 1); - return RowMatrix; -} - - -/// To implicitly Clone a Matrix. Although '=' works the same. -void Matrix::Clone( const Matrix &Source, Matrix &Receip ) -{ - Receip._nCols = Source._nCols; - Receip._nRows = Source._nRows; - - Receip._matrix.resize(Receip._nRows); - for( int i = 0; i < Receip._nRows; i++ ) - Receip._matrix[i].resize(Receip._nCols); - - for( int i = 0; i < Receip._nRows; i++ ) - for( int j = 0; j < Receip._nCols; j++ ) - Receip._matrix[i][j] = Source._matrix[i][j]; -} - - -/// To add (Insert) a Single Row to a Matrix. +/// To add (Insert) a Single Row to a Matrix. void Matrix::AddRow(Matrix& Mat, int Row) { --Row; @@ -254,7 +133,7 @@ if( Row > Mat._nRows + 1) { printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); - + }else{ Mat._nRows++; @@ -273,14 +152,14 @@ /// To add (Insert) a single Column to a Matrix -void Matrix::AddColumn( Matrix &Mat, int Col ) +void Matrix::AddColumn( Matrix& Mat, int Col ) { --Col; if( Col > Mat._nCols + 1 ) { printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); - + }else{ @@ -300,20 +179,25 @@ /// Delete a Single Column From Matrix. -void Matrix::DeleteCol( Matrix &Mat, int Col) +void Matrix::DeleteCol( Matrix& Mat, int Col) { --Col; // Because of Column zero. if( Col > Mat._nCols ) { printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); - + }else{ for( int i = 0; i < Mat._nRows; i++ ) for( int j = Col; j < Mat._nCols; j++ ) Mat._matrix[i][j] = Mat._matrix[i][j+1]; + // If adressing last element of Column, + // wich no longer exists + if( Mat._pCol == Mat._nCols ) + Mat._pCol--; + // Decrease one column Mat._nCols--; @@ -333,11 +217,11 @@ if( Row > Mat._nRows ) { printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); - + }else{ for( int i = Row; i < Mat._nRows - 1; i++ ) - + for( int j = 0; j < Mat._nCols; j++ ) Mat._matrix[i][j] = Mat._matrix[i+1][j]; Mat._nRows--; @@ -348,41 +232,50 @@ /*****************************************************************************************/ /// Extracts a single row form calling matrix and saves it to another matrix. -void Matrix::ExportRow( int Row, Matrix &Mat ) +const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) { - --Row; + --row; - if( Row > this->_nRows ) + if( row > Mat._nRows ) { - printf( "\n\nWARNING: Row out of dimmensions @ GetRow\n" + printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" "Nothing Done.\n\n" ); }else{ - Matrix SingleRow( 1 , this->_nCols ); + Matrix SingleRow( 1 , Mat._nCols ); SingleRow.Clear(); - for( int j = 0; j < this->_nCols; j++ ) - SingleRow._matrix[0][j] = this->_matrix[Row][j]; - - Mat = SingleRow; + for( int j = 0; j < Mat._nCols; j++ ) + SingleRow._matrix[0][j] = Mat._matrix[row][j]; + + SingleRow._pCol = SingleRow._nCols; + SingleRow._pRow = 0; + + return SingleRow; } } /// Extracts a single column form calling matrix and saves it to another matrix. -void Matrix::ExportCol( int Col, Matrix &Mat ) +const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) { - if( Col > this->_nCols ) + --col; + + if( col > Mat._nCols ) { - printf( "\n\nWARNING:\nCol out of dimmensions.\n" + printf( "\n\nERROR:\nColumn out of dimmensions.\n" "Nothing Done.\n\n" ); }else{ - Matrix SingleCol( this->_nRows, 1 ); - for(int i = 0; i < this->_nRows; i++ ) - SingleCol._matrix[i][0] = this->_matrix[i][Col]; - Mat = SingleCol; + Matrix SingleCol( Mat._nRows, 1 ); + for(int i = 0; i < Mat._nRows; i++ ) + SingleCol._matrix[i][0] = Mat._matrix[i][col]; + + SingleCol._pCol = 0; + SingleCol._pRow = SingleCol._nRows; + + return SingleCol; } } @@ -390,12 +283,16 @@ /// Makes matrix Bigger! void Matrix::Resize( int Rows, int Cols ) { - this->_nRows = Rows; - this->_nCols = Cols; - this->_matrix.resize( _nRows ); - + _nRows = Rows; //Decreases one because internally + _nCols = Cols; // Index starts at zero. + + _matrix.resize( _nRows ); + for( int i = 0; i< _nRows ; i++ ) _matrix[i].resize(_nCols); + + _pRow = 0; // If matrix is resized the << + _pCol = 0; // operator overwrites everything! } @@ -415,6 +312,9 @@ printf("\n"); } printf("\n"); + + _pRow = _nRows; + _pCol = _nCols; } @@ -439,19 +339,23 @@ for( int i = 0; i < _nRows; i++ ) for( int j = 0; j < _nCols; j++ ) _matrix[i][j] = 0; + + _pCol = 0; // New data can be added + _pRow = 0; } /********************************************************************************/ + /// Inserts a Single element in a desired Position( Index starts at [1][1] ); void Matrix::add(int Row, int Col, float number) { --Col; --Row; - + if( Row > _nRows || Col > _nCols ) { printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); - + }else{ _matrix[Row][Col] = number; } @@ -479,4 +383,4 @@ /// Returns the number of Columns in Matrix. -int Matrix::getCols(){ return this->_nCols; } +int Matrix::getCols(){ return this->_nCols; } \ No newline at end of file
--- a/Matrix.h Mon Sep 26 03:23:35 2011 +0000 +++ b/Matrix.h Thu Oct 20 23:42:13 2011 +0000 @@ -10,35 +10,21 @@ */ #ifndef MATRIX_H -#define MATRIX_H +#define MATRIX_H + #include <vector> +class MatrixMath; + using namespace std; - /** - * @brief This class is intended to provide basic functionality to a Matrix like vector - * it's meant for float elements. - * - * IMPORTAT: Asignment "=" operator is overloaded so that it resizes the 'lefthand' Matrix - * to acomodate the Matrix it's been passed. - * - * An example of how it works: - * @code - * Matrix myMatrix( 3, 4 ); //Declare a 3 Rows by 4 Columns Matrix. + * @brief This class provide basic manipulation for 2D matrices. * - * myMatrix.Fill(); // Fill the matrix using printf() and scanf(). - * myMatrix.print(); // Prints the value of the Matrix. printf() + * Intended to be ported to mbed.org * - * Matrix::DeleteCol( myMatrix, 2 ); //Deletes Second Column. (startin' with 1 not 0) - * myMatrix.print(); // Re-prints Matrix - * - * Matrix anotherMatrix (3); // Squared 3x3 Matrix - * anotherMatrix = myMatrix; // Re-Sizes 'anotherMatrix' to fit myMatrix and copies data. - * anotherMatrix += myMatrix; // Adds one-by-one elements and saves to anotherMatrix - * anotherMatrix.print(); - * @endcode - * + * v1.01 + * */ class Matrix{ public: @@ -46,34 +32,38 @@ /// Creates a nex Matrix of Size [ Row x Cols ] Matrix( int Rows, int Cols ); - /// Creats a Square Matrix o Size [ Rows x Rows ] - Matrix( int Rows ); - - /// Creates a new Matrix identical in size to an input Matrix - Matrix( const Matrix &base ); + /// Creates a new Matrix identical to an input Matrix + Matrix( const Matrix& base ); /// Reserves Memory for a New Matrix Matrix(); +/******************************************************************************/ /** @brief * This is the '=' Overloaded operator. It RESIZES assigned the matrix. * Overwrites all data. To be used Carefully! */ - Matrix& operator = ( const Matrix &rightM ); + Matrix& operator = ( const Matrix& rightM ); + + + + /**@brief This includes the Class to handle Matrix Operations. + */ + friend class MatrixMath; - + /** @brief * Overload opeartor for the compare Matrices * * @param rightM * @return Boolean 'false' if different. */ - bool operator == ( const Matrix &rightM ); + bool operator == ( const Matrix& rightM ); @@ -83,16 +73,16 @@ * @param rightM * @return Boolean 'true' if different */ - bool operator != ( const Matrix &rightM ); + bool operator != ( const Matrix& rightM ); - - + + /** @brief * Overload Copmpound assignment. * @param rightM * @return A new Matrix to be assigned to itself. */ - Matrix& operator += ( const Matrix &rightM ); + Matrix& operator += ( const Matrix& rightM ); @@ -101,28 +91,142 @@ * @param rightM Right hand matrix * @return A new Matrix to be assigned to itself */ - Matrix& operator -= ( const Matrix &rightM ); + Matrix& operator -= ( const Matrix& rightM ); + + + + /** @brief + * Overload Compound CrossProduct Matrix operation. + * @param rightM + * @return + */ + Matrix& operator *=( const Matrix& rightM ); + + + + /** @brief + * Overload Compund Element-by-elemnt scalar multiplication. + * @param number + * @return + */ + Matrix& operator *=( float number ); + + + + /**@brief + * Makes all elements in matrix negative. + * Unary operator + * @return A new Matrix object with inverted values. + */ + const Matrix operator -(); + + + + /**@brief + * Overload Compound add with scalar. + * Because the '=' operator checks for self Assign, no extra operations + * are needed. + * @return Same Matrix to self Assign. + */ + friend const Matrix operator +=( Matrix& leftM, float number ); + + + + /**@brief + * Compound substract with scalar. + * @return Same matrix to self Assign. + */ + friend const Matrix operator -=( Matrix& leftM, float number ); + + + + /** @brief + * Adds two matrices of the same dimensions, element-by-element. + * If diferent dimensions -> ERROR. + * @return A new object Matrix with the result. + */ + friend const Matrix operator +( const Matrix& leftM, const Matrix& rightM); /** @brief - * Overloads the Plus, returns a new object. - * @param rightM right side Matrix - * @return Retuns an instance of the answer. + * Adds the given nomber to each element of matrix. + * Mimic MATLAB operation. + * @return A new matrix object with the result. + */ + friend const Matrix operator +( const Matrix& leftM, float number ); + + + + /**@brief + * Adds the given number to each element in Matrix. + * @return A new Matrix object with the result. + */ + friend const Matrix operator +( float number, const Matrix& leftM ); + + + + /**@brief + * Substracts two matrices of the same size, element-by-element. + * If different dimensions -> ERROR. + * @return A new object Matrix with the result. + */ + friend const Matrix operator -( const Matrix& leftM, const Matrix& rightM ); + + + + /**@brief + * Substracts each element in Matrix by number. + * @return A new matrix object with the result. */ - const Matrix operator +( const Matrix &rightM ); + friend const Matrix operator -( const Matrix& leftM, float number ); + + + + /**@brief + * Substracts each element in Matrix by number + * @return A new matrix object with the result. + */ + friend const Matrix operator -( float number, const Matrix& leftM ); + + + + /** + * Preforms Crossproduct between two matrices. + * @return + */ + friend const Matrix operator *( const Matrix& leftM, const Matrix& rightM ); + + + + /**@brief + * Multiplies a scalar number with each element on Matrix. + * @return A new object with the result. + */ + friend const Matrix operator *( const Matrix& leftM, float number ); + + + + /**@brief + * Multiplies a scalar number with each element on Matrix. + * @return + */ + friend const Matrix operator *( float number, const Matrix& leftM ); + + /**@brief + * Inputs numbres into a Matrix, the matrix needs to be costructed as + * Matrix( _nRows, _nCols ). + * This does NOT work on an only declared Matrix such as: + * Matrix obj; + * obj << 5; //Error + * @return + */ + friend Matrix& operator <<( Matrix& leftM, float number ); - /** @brief - * Overloads the Minus, returns a new object - * @param rightM - * @return Returns an instance of the asnwer - */ - const Matrix operator -( const Matrix &rightM ); +/***********************************************************************/ - - /** @brief * Returns TRUE if the matrix is zero, FALSE otherwhise * @param mat: Matrix to be tested @@ -137,79 +241,50 @@ bool isVector(); - + /** @brief * Shatters the matrix into a single Row Vector. * Important: Returns NEW matrix, does no modify existing one. */ - Matrix ToPackedVector(); + static const Matrix ToPackedVector( const Matrix& Mat ); - + /** @brief * Determines if two matrices are equal. * @param mat1: First Matrix * @param mat2: Sencond Matrix */ - static bool Equals( const Matrix mat1, const Matrix mat2 ); - - - - /**@brief - * This Funcrions instanciates a new Row Matrix. Since this is a - * static function it needs to be assigned to an Empy Matrix Object - * @param Rows: Number of Rows - */ - static Matrix CreateRowMatrix(int Rows); - - - - /** @brief - * This function instanciates a new Column Matrix. Since this is a - * static function it needs to be assigned to an Empty Matrix object - * @param Cols: Number of Columns in Matrix - */ - static Matrix CreateColumnMatrix(int Cols); + static bool Equals( const Matrix& mat1, const Matrix& mat2 ); /** @brief - * This STATIC function Clones two Matrices. - * The Source Matrix can be any Size. - * The Receip Matrix MUST be just initialized - * as in: Matrix NewMatrix(); - * @param Source: Base Matrix to Clone. - * @param Receip: Destination shell matrix - */ - static void Clone( const Matrix &Source, Matrix &Receip ); - - - /** @brief - * Invoking this static method will increase a Row in Mat in the desired + * Invoking this static method will increase a Row in Mat in the desired * position. * The current Row will be moved down to allocate space, and all elements will * be initialized to zero in the new row. - * @param Mat: Matrix in wich to insert a Row + * @param Mat: Matrix in wich to insert a Row * @param Row: Number of row to insert, starts with one, not zero. - */ - static void AddRow( Matrix &Mat, int Row ); + */ + static void AddRow( Matrix& Mat, int Row ); - + /** @brief - * Invoking this static method will increase a Column in Matrix in the + * Invoking this static method will increase a Column in Matrix in the * desired Position. * @param Mat: Matrix in wich to insert a Column * @param Col: Number of column, strats with one, not zero. */ - static void AddColumn( Matrix &Mat, int Col ); - + static void AddColumn( Matrix& Mat, int Col ); + /** @brief * Static Function Deletes Row from Matrix, Static to prevent missuse * @param Mat: Matrix to delete Row from * @param Row: Number of Row (first Row = 1) */ - static void DeleteRow( Matrix &Mat, int Row ); + static void DeleteRow( Matrix& Mat, int Row ); /** @brief @@ -219,7 +294,7 @@ * @param Col: Number of Col to delete (first Col = 1) * @param Mat: Matrix to delete from. */ - static void DeleteCol( Matrix &Mat, int Col ); + static void DeleteCol( Matrix& Mat, int Col ); @@ -228,28 +303,30 @@ * This method extracts a Row from a Matrix and Saves it in Mat. * If Row is out of the parameters it does nothing, but prints a warning. * @param Row: number of row to extract elements. this->_nRows. - * @param Mat: Matrix to resize if neccesaty and save data. + * @param Mat: Matrix to extract from. + * @return New Row Matrix. */ - void ExportRow( int Row, Matrix &Mat); - - - - - - - - /** @brief - * This method extracts a Column from a Matrix and saves it to Mat. - * If Row is out of the parameters, it does nothing and prints a warning. - * @param Col: number of Column to extract elements. this->_nCols. - * @param Mat: Matrix to save data to. - */ - void ExportCol( int Col, Matrix &Mat ); + static const Matrix ExportRow( const Matrix& Mat, int row ); /** @brief - * This function resizes the Matrix to fit new data. + * This method extracts a Column from a Matrix and returns the Column + * as a new Matrix. + * If Row is out of the parameters, it does nothing and prints a warning. + * @param Col: number of Column to extract elements. this->_nCols. + * @param Mat: Matrix to extract from. + * @return New Row Matrix. + */ + static const Matrix ExportCol( const Matrix& Mat, int col ); + + + + /** @brief + * This function resizes the Matrix to fit new data or cropped it, + * to use newly created memory use .add() not << operator. + * << operator overwites everithing + * * @param Rows: New Number of Rows * @param Cols: New numbler of columns */ @@ -261,23 +338,25 @@ * Asks user for numbers to fill the Matrix elements, one by one. * It uses printf(); by default the USBTX, USBRX, 9600, 1N8. */ - void FillMatrix(); + virtual void FillMatrix(); /** @brief - * Prints the entire Matrix + * Prints the entire Matrix using standard PRINTF */ - void print(); + virtual void print(); - + /** @brief * Makes all values on Matrix object zero. + * Also make posible use the '<<' operator to add elements and keep + * track of last element added. */ void Clear(); - + /** @brief * Assigns a float number to the matrix in a specified position * Index starts at [1][1]. @@ -305,16 +384,16 @@ float getNumber( int Row, int Col ); - + /**@brief - * Retuns the number of Columns in Matrix + * Retuns the number of Columns in Matrix, index starts at 1. */ int getCols(); /**@brief - *Retruns the number of Rows in Matrix + *Retruns the number of Rows in Matrix, index starts at 1. */ int getRows(); @@ -324,12 +403,23 @@ /** 2-D Vector Array*/ vector < vector<float> > _matrix; + + /** Number of Rows in Matrix*/ int _nRows; /**Number of Columns in Matrix*/ int _nCols; + + + /**Last Element Row position in Matrix*/ + int _pRow; + + /**Last Element Col position in Matrix*/ + int _pCol; + }; -#endif /* MATRIX_H */ +#endif /* MATRIX_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Operators.cpp Thu Oct 20 23:42:13 2011 +0000 @@ -0,0 +1,293 @@ +/** + * + */ + +#include "mbed.h" +#include "Matrix.h" + +/// Overloaded Asign Operator. Resizes Matrix +Matrix& Matrix::operator = ( const Matrix& rightM ) +{ + if (this != &rightM ) + { + + _nRows = rightM._nRows; + _nCols = rightM._nCols; + + _matrix.resize( rightM._nRows ); + for( int i = 0; i < rightM._nRows; i++ ) + _matrix [i].resize(rightM._nCols); + + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] = rightM._matrix[i][j]; + } + return *this; + +} + +/// Comapre element by element +bool Matrix::operator == ( const Matrix& rightM ) +{ + if( _nRows == rightM._nRows && _nCols == rightM._nCols ) + { + bool equal = false; + + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + if( _matrix[i][j] != rightM._matrix[i][j] ) + equal = equal || true; + + return !equal; + + }else{ return false; } +} + + +/// Calls for '==' operator +bool Matrix::operator != ( const Matrix& rightM ) +{ + return !( *this == rightM ); +} + + +/// Matrices must be same size. +/// Element by element adition. +Matrix& Matrix::operator +=(const Matrix& rightM) +{ + if( _nRows == rightM._nRows && _nCols == rightM._nCols ) + { + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] += rightM._matrix[i][j]; + + return *this; + + }else{ + printf( "\n\nERROR\nDiferent Dimensions @ += operator\n" ); + //Matrix error(4); + //error.Clear(); + //return error; + } +} + + +/// Matrices must be same size. +/// Element by element Substraction +Matrix& Matrix::operator -=(const Matrix& rightM) +{ + if( _nRows == rightM._nRows && _nCols == rightM._nCols ) + { + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] -= rightM._matrix[i][j]; + + return *this; + + }else{ + printf( "\n\nERROR\nDiferent Dimensions @ -= operator \n" ); + //Matrix error(4); + //error.Clear(); + //return error; + } +} + + +Matrix& Matrix::operator *=( const Matrix& rightM ) +{ + if( this->_nCols == rightM._nRows ) + { + Matrix resultM ( this->_nRows, rightM._nCols ); + + for( int i = 0; i < resultM._nRows; i++ ) + for( int j = 0; j < resultM._nCols; j++ ) + for( int m = 0; m < rightM._nRows; m++ ) + resultM._matrix[i][j] += this->_matrix[i][m] * rightM._matrix[m][j]; + + *this = resultM; + } + + return *this; +} + + +Matrix& Matrix::operator *=(float number) +{ + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] *= number; + + return *this; +} + + +const Matrix Matrix::operator -() +{ + Matrix result( _nRows, _nCols ); + + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + result._matrix[i][j] = _matrix[i][j] * -1; + + return result; +} + +/*****************************************************************************/ + +// Overload operators + + +const Matrix operator +=( Matrix& leftM, float number ) +{ + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + leftM._matrix[i][j] += number; + return leftM; +} + + +const Matrix operator -=( Matrix& leftM, float number ) +{ + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + leftM._matrix[i][j] -= number; + return leftM; +} + + +const Matrix operator +( const Matrix& leftM, const Matrix& rightM) +{ + if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) + { + Matrix result( leftM._nRows, leftM._nCols ); + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + result._matrix[i][j] = leftM._matrix[i][j] + rightM._matrix[i][j]; + + return result; + + }else{ + printf( "\n\nERROR\nDiferent Dimensions @ + operator \n" ); + //Matrix error(4); + //error.Clear(); + //return error; + } +} + + +const Matrix operator +( const Matrix& leftM, float number ) +{ + Matrix result( leftM._nRows, leftM._nCols ); + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + result._matrix[i][j] = leftM._matrix[i][j] + number; + + return result; +} + + +const Matrix operator +( float number, const Matrix& leftM ) +{ + return ( leftM + number ); +} + + +const Matrix operator -( const Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) + { + Matrix result( leftM._nRows, leftM._nCols ); + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + result._matrix[i][j] = leftM._matrix[i][j] - rightM._matrix[i][j]; + + return result; + + }else{ + printf( "\n\nERROR:\nDiferent Dimensions @ + operator \n" ); + + } +} + + +const Matrix operator -( const Matrix& leftM, float number ) +{ + Matrix result( leftM._nRows, leftM._nCols ); + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + result._matrix[i][j] = leftM._matrix[i][j] - number; + + return result; +} + + +const Matrix operator -( float number, const Matrix& leftM ) +{ + return ( leftM - number ); +} + + +const Matrix operator *( const Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nCols == rightM._nRows ) + { + Matrix resultM ( leftM._nRows, rightM._nCols ); + resultM.Clear(); + + for( int i = 0; i < resultM._nRows; i++ ) + for( int j = 0; j < resultM._nCols; j++ ) + for( int m = 0; m < rightM._nRows; m++ ) + resultM._matrix[i][j] += leftM._matrix[i][m] * rightM._matrix[m][j]; + + return resultM; + + } else { + + printf("\n\nERROR:\nDiferent Dimension matrices @ * operator"); + } + +} + + +const Matrix operator *( const Matrix& leftM, float number ) +{ + Matrix result( leftM._nRows, leftM._nCols ); + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + result._matrix[i][j] = leftM._matrix[i][j] * number; + + return result; +} + +const Matrix operator *( float number, const Matrix& leftM ) +{ + return ( leftM * number ); +} + + +Matrix& operator <<( Matrix& leftM, float number ) +{ + if( leftM._pCol == leftM._nCols ) //end of Row + { + leftM._pCol = 0; + leftM._pRow++; + } + if( leftM._pRow > leftM._nRows ) + { + printf( "\n\nERROR:\nAssignment out of limits @ << operator" ); + return leftM; + + }else{ + + leftM._matrix[ leftM._pRow ][ leftM._pCol ] = number; + leftM._pCol++; + + return leftM; + } +} +