Matrix Library. v1.6.4
Dependents: Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more
Diff: Operators.cpp
- Revision:
- 3:589fb80932b5
- Parent:
- 2:493402568a5e
- Child:
- 5:a4014ab0a8cf
diff -r 493402568a5e -r 589fb80932b5 Operators.cpp --- a/Operators.cpp Thu Oct 20 23:42:13 2011 +0000 +++ b/Operators.cpp Fri Oct 21 03:33:53 2011 +0000 @@ -1,293 +1,294 @@ -/** - * - */ - -#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; - } -} - +/** + * @file Matrix.cpp + * @author Ernesto Palacios + * @brief Source Code for the Operator of Matrix Class. + * + * Created on September 2011. + * + */ +#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& 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; + } +} +