nanimo kaete nai
Revision 0:4ff90c5befd9, committed 2022-06-14
- Comitter:
- tyuito
- Date:
- Tue Jun 14 07:37:36 2022 +0000
- Commit message:
- nanimo kaete nai to omou
Changed in this revision
diff -r 000000000000 -r 4ff90c5befd9 Log.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Log.c Tue Jun 14 07:37:36 2022 +0000 @@ -0,0 +1,61 @@ +/** + * @brief Keep track of changes since version 1.6 e-mail: mecatronica.mid@gmail.com + * @file Log.c + * @author Ernesto Palacios + */ + +/* +1.6.4.0 30/10/2011 + -->> MATRIX_H + * Eliminated namespace std; already included in mbed.h + * Operator Overloaded (). For assignment and getValue. + * Almost all operators declared as friend functions. + + -->> MATRIXMATH_H + * Added Function Eye(). Creates an identity Matrix of specified dimensions. + * Added Function dotProduct(). to find the dot product of two vectors. + -->> You need to pass two Vector Matrices of any dimmensions. + They could be in the form: A( 1,n ) B( 1,n ) + A( n,1 ) B( 1,n ) + A( n,1 ) B( n,1 ) + A( 1,n ) B( n,1 ) + As long as have same 'depth' and are vectors, the dot product. + will be returned. + + -->> MATRIXMATH_Kinematics.cpp + * Created file MatrixMath_Kinematics.cpp To Hold the definitions of + kinematic operations. + + * Define Functions RotX, RotY, RotZ, Transl. for Matrix Transformation + operations. + + +1.6.2.0 22/10/2011 + -->> MATRIX_H + * Changed static member Matrix::AddColumn( ... ) -> Matrix::AddCol( ... ) + * Overload AddCol/AddRow, it now can accept SingleCol/ SingleRow as arguments. + Still works the same for inserting new Col/Row. Usage: + + Matrix::AddRow( myMatrix, 3 ); // Inserts an empty col at index 3 of myMatrix + + Matrix::AddCol( myMatrix, SingleCol, 3 ); // Inserts a SingleCol Matrix into index 3 of myMarix + + -->> MATRIXMATH_H + * float det = MatrixMath::det( myMatrix ); + Returns the determinant of any nxn Matrix. + + * Matrix Inv = MatrixMath::Inv( myMatrix ) + Returns the determinant of any nxn Matrix, if it's not a Singular matrix + + + WARNING: If it is a Singular Matrix it will return the same Matrix. + A singular Matrix is one whose inverse does not exists. + +1.6.0.1 21/10/2011 + First class ready to work. but still some rough edges to polish. Better use 1.6.2 + +1.0 15/09/2011 + + First Version.- Buggy and no longer supported. + +*/
diff -r 000000000000 -r 4ff90c5befd9 Matrix.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Matrix.cpp Tue Jun 14 07:37:36 2022 +0000 @@ -0,0 +1,387 @@ +/** + * @brief Source Code for the Matrix Class. + * @file Matrix.cpp + * @author Ernesto Palacios + * + * Created on September 2011. + * + * Develop Under GPL v3.0 License + * http://www.gnu.org/licenses/gpl-3.0.html + * + */ + +#include "mbed.h" +#include "Matrix.h" + +/// Rows by Cols Matrix Constructor +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. +} + + +/// 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); + + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + _matrix[i][j] = base._matrix[i][j]; +} + + +/// Default Constructor +Matrix::Matrix() +{ + _nCols = 0; + _nRows = 0; + + _pRow = 0; + _pCol = 0; + +} + +/***********************************************************************/ + +/// Returns true if matrix is full of zeros +bool Matrix::isZero() const +{ + bool zero = false; + for( int i = 0; i < this->_nRows; i++ ) + for( int j = 0; j < this->_nCols; j++ ) + if( _matrix[i][j] != 0 ) + zero = zero || true; + return !zero; +} + + +/// Returns true if Matrix is Single Row ot Single Column. +bool Matrix::isVector() const +{ + if( _nRows == 1 || _nCols == 1 ) + return true; + else + return false; +} + +/*************************************************************************/ + +/// Returns all elements in Matrix as a single Row vector. +const Matrix Matrix::ToPackedVector( const Matrix& Mat ) +{ + + Matrix Crushed( 1, Mat._nRows * Mat._nCols ); + + int cont = 0; + + for( int i = 0; i < Mat._nRows; i++ ) + for( int j = 0; j < Mat._nCols; j++ ) + { + Crushed._matrix[0][cont] = Mat._matrix[i][j]; + cont++; + } + + Crushed._pRow = Crushed._nRows; + Crushed._pCol = Crushed._nCols; + + return Crushed; +} + + + +/// To add (Insert) a Single Row to a Matrix. +void Matrix::AddRow(Matrix& Mat, int index) +{ + --index; + + if( index > Mat._nRows + 1) + { + printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); + + }else{ + + Mat._nRows++; + Mat._matrix.resize( Mat._nRows ); + + Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); + + for( int i = Mat._nRows - 1; i > index; i-- ) + for( int j = 0; j < Mat._nCols; j++ ) + Mat._matrix[i][j] = Mat._matrix[i - 1][j]; + + for( int j = 0; j < Mat._nCols; j++ ) + Mat._matrix[index][j] = 0.0; + } +} + + +void Matrix::AddRow(Matrix& Receip, const Matrix& Row, int index) +{ + Matrix::AddRow( Receip, index ); //Make Room + + --index; + for( int i = 0; i < Receip._nCols; i++ ) + Receip._matrix[index][i] = Row._matrix[0][i]; //Copy Data. + +} + + +/// To add (Insert) a single Column to a Matrix +void Matrix::AddCol( Matrix& Mat, int index ) +{ + --index; + + if( index > Mat._nCols + 1 ) + { + printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); + + }else{ + + + Mat._nCols++; + for( int i = 0; i < Mat._nRows; i++ ) + Mat._matrix[i].resize( Mat._nCols ); + + for( int i = 0; i < Mat._nRows; i++ ) + for( int j = Mat._nCols; j > index; j-- ) + Mat._matrix[i][j] = Mat._matrix[i][j - 1]; + + for( int i = 0; i < Mat._nRows; i++ ) + Mat._matrix[i][index] = 0.0; + + } +} + + +void Matrix::AddCol(Matrix& Receip, const Matrix& Row, int index) +{ + Matrix::AddCol( Receip, index ); // Make Rom + + --index; + for( int i = 0; i < Receip._nRows; i++ ) + Receip._matrix[i][index] = Row._matrix[i][0]; //Copy Data. +} + + +/// Delete a Single Column From Matrix. +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--; + + //Erase last Column + for( int i = 0; i < Mat._nRows; i++ ) + Mat._matrix[i].reserve(Mat._nCols); + + } +} + + +/// Delete a Single Row form Matrix +void Matrix::DeleteRow(Matrix& Mat, int Row) +{ + --Row; + + 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--; + Mat._matrix.resize(Mat._nRows); + } +} + +/*****************************************************************************************/ + +/// Extracts a single row form calling matrix and saves it to another matrix. +const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) +{ + --row; + + if( row > Mat._nRows ) + { + printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" + "Nothing Done.\n\n" ); + + }else{ + + Matrix SingleRow( 1 , Mat._nCols ); + SingleRow.Clear(); + + 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. +const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) +{ + --col; + + if( col > Mat._nCols ) + { + printf( "\n\nERROR:\nColumn out of dimmensions.\n" + "Nothing Done.\n\n" ); + }else{ + + 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; + } +} + + +/// Makes matrix Bigger! +void Matrix::Resize( int Rows, int Cols ) +{ + _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! +} + + +/// Ask user for elemnts in Matrix +void Matrix::FillMatrix() +{ + for(int i = 0; i < _nRows; i++) + { + for(int j = 0; j < _nCols; j++) + { + printf( "Position [%u][%u]: ", i, j ); + float numero; + scanf( "%f", &numero ); + printf("%.3f ", numero); + this->_matrix[i][j] = numero; + } + printf("\n"); + } + printf("\n"); + + _pRow = _nRows; + _pCol = _nCols; +} + + +/// Prints out Matrix. +void Matrix::print() const +{ + for( int i = 0; i < _nRows; i++ ) + { + for( int j = 0; j < _nCols; j++ ) + { + printf( "%.3f, ",_matrix[i][j] ); + + } + printf( "\n" ); + } +} + + +/// Fills matrix with zeros. +void Matrix::Clear() +{ + 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; + } +} + + +/// Adds all elements in matrix and returns the answer. +float Matrix::sum() const +{ + float total = 0; + + for( int i = 0; i < _nRows; i++ ) + for( int j = 0; j < _nCols; j++ ) + total += _matrix[i][j]; + return total; +} + + +/// Returns the specified element. Index Starts at [1][1]. +float Matrix::getNumber( int Row, int Col ) const +{ return this->_matrix[Row -1][Col - 1]; } + +/// Returns the number of Rows in Matrix. +int Matrix::getRows() const{ return this->_nRows; } + + +/// Returns the number of Columns in Matrix. +int Matrix::getCols() const{ return this->_nCols; } \ No newline at end of file
diff -r 000000000000 -r 4ff90c5befd9 Matrix.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Matrix.h Tue Jun 14 07:37:36 2022 +0000 @@ -0,0 +1,425 @@ +/** + * @brief API for Matrix Library + * @file Matrix.h + * @author Ernesto Palacios + * + * Created on 13 de septiembre de 2011, 03:49 PM + * + * Develop Under GPL v3.0 License + * http://www.gnu.org/licenses/gpl-3.0.html + * + */ + +#ifndef MATRIX_H +#define MATRIX_H + + +#include <vector> + +using namespace std; + +class MatrixMath; + +/** + * @brief This class provide basic manipulation for 2D matrices see Log.c for more info + * version 1.6.4. + * + */ +class Matrix{ +public: + + /// Creates a nex Matrix of Size [ Row x Cols ] + Matrix( int Rows, int Cols ); + + + /// Creates a new Matrix identical to an input Matrix + Matrix( const Matrix& base ); + + + /// Default Constructor + Matrix(); + + +/******************************************************************************/ + + + /**@brief This includes the Class to handle Matrix Operations. + */ + friend class MatrixMath; + + + /**@brief + * Subindex for Matrix elements assignation. + * @param row + * @param col + * @return pointer to the element. + */ + float& operator() ( int row, int col ); + + + /**@brief + *Subindex for Matrix element. + * @param row + * @param col + * @return the element. + */ + float operator() ( int row, int col ) const; + + + + /** @brief + * Overwrites all data. To be used Carefully! + */ + Matrix& operator = ( const Matrix& rightM ); + + + /** @brief + * Overload opeartor for the compare Matrices + * + * @param rightM + * @return Boolean 'false' if different. + */ + friend bool operator == ( const Matrix& leftM, const Matrix& rightM ); + + + /** @brief + * Overload opeartor for the compare Matrices + * + * @param rightM + * @return Boolean 'true' if different + */ + friend bool operator != ( const Matrix& leftM, const Matrix& rightM ); + + + /** @brief + * Overload Copmpound assignment. + * @param rightM + * @return A new Matrix to be assigned to itself. + */ + friend Matrix& operator += ( Matrix& leftM, const Matrix& rightM ); + + + /** @brief + * Overload Compund decrease. + * @param rightM Right hand matrix + * @return A new Matrix to be assigned to itself + */ + friend Matrix& operator -= ( Matrix& leftM, const Matrix& rightM ); + + + /** @brief + * Overload Compound CrossProduct Matrix operation. + * @param rightM + * @return + */ + friend Matrix& operator *=( Matrix& leftM, const Matrix& rightM ); + + + /** @brief + * Overload Compund Element-by-elemnt scalar multiplication. + * @param number + * @return + */ + friend Matrix& operator *=( Matrix& leftM, float number ); + + + + /**@brief + * All elements in matrix are multiplied by (-1). + * @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 + * 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. + */ + 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 + * Returns TRUE if the matrix is zero, FALSE otherwhise + * @param mat: Matrix to be tested + */ + bool isZero() const; + + + /** @brief + * Determines weather a Matrix is a Single Column or Row. + */ + bool isVector() const; + + + /** @brief + * Shatters the matrix into a single Row Vector. + * Important: Returns NEW matrix, does no modify existing one. + */ + static const Matrix ToPackedVector( const Matrix& Mat ); + + + /** @brief + * 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 Row: Number of row to insert, starts with one, not zero. + */ + static void AddRow( Matrix& Mat, int index ); + + + /**@brief + * Adds to Receip a new Row from another Matrix in desired index. + * Must be same size. + * The Row matrix must be SingleRow Matrix, you can use ExportRow + * to extract a Row from another Matrix. + * @param Receip Matrix to be Modified. + * @param Row Row to be added. + * @param index position in wich to be added, _nRow + 1 last position. + */ + static void AddRow( Matrix& Receip, const Matrix& Row, int index ); + + + /** @brief + * 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 AddCol( Matrix& Mat, int index ); + + + /**@brief + * This will copy a Column Matrix into Receip in desired Position, + * Must be same size. + * The Col Matrix must be a SingleCol Matrix, you can use ExportCol + * to extract a Column from another Matrix. + * @param Receip Matrix to be modified. + * @param Column Data to be copied. + * @param index Postion in Receip Matrix . + */ + static void AddCol( Matrix& Receip, const Matrix& Col, int index ); + + + /** @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 ); + + + /** @brief + * Static Function Deletes Column from Matrix, it's Static to prevent + * missuse. + * Print error and does nothing if out of limits. + * @param Col: Number of Col to delete (first Col = 1) + * @param Mat: Matrix to delete from. + */ + static void DeleteCol( Matrix& Mat, int Col ); + + + /** @brief + * 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 extract from. + * @return New Row Matrix. + */ + static const Matrix ExportRow( const Matrix& Mat, int row ); + + + /** @brief + * 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, + * operator << can overwrite entire Matrix. + * + * @param Rows: New Number of Rows + * @param Cols: New numbler of columns + */ + void Resize( int Rows, int Cols ); + + + /** @brief + * Asks user for numbers to fill the Matrix elements, one by one. + * It uses printf(); by default the USBTX, USBRX, 9600, 1N8. + */ + virtual void FillMatrix(); + + + /** @brief + * Prints the entire Matrix using standard PRINTF + */ + virtual void print() const; + + + /** @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]. + * + * @param number: Number to be set + * @param Row: Row of Matrix + * @param Col: Column of Matrix + */ + void add( int Row, int Col, float number ); + + + /** @brief + * Returns the sum of every cell in the Matrix. + */ + float sum() const; + + + /** @brief + * Return the number in position [Row],[Col] + * @param Row = number of row in matrix + * @param Col = number of Col in matrix + * @return Num = float number in matrix + */ + float getNumber( int Row, int Col ) const; + + + /**@brief + * Retuns the number of Columns in Matrix, index starts at 1. + */ + int getCols() const; + + + /**@brief + *Retruns the number of Rows in Matrix, index starts at 1. + */ + int getRows() const; + + +private: + + /** 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 */ +
diff -r 000000000000 -r 4ff90c5befd9 Operators.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Operators.cpp Tue Jun 14 07:37:36 2022 +0000 @@ -0,0 +1,320 @@ +/** + * @brief Source Code for the Operator of Matrix Class. + * @file Operators.cpp + * @author Ernesto Palacios + * + * Created on September 2011. + * + * Develop Under GPL v3.0 License + * http://www.gnu.org/licenses/gpl-3.0.html + * + */ +#include "mbed.h" +#include "Matrix.h" + +/// Subindex in Matrix left side +float& Matrix::operator ()(int row, int col) +{ + --row; --col; + + if( row >= _nRows || col >= _nCols) + { + printf("\n\nError:\nOut of limits @ Matrix::operator()\n"); + }else{ + return _matrix[row][col]; + } +} + +/// Subindex in Matrix right side +float Matrix::operator ()(int row, int col) const +{ + --row; --col; + + if( row >= _nRows || col >= _nCols) + { + printf("\n\nError:\nOut of limits @ Matrix::operator()\n"); + }else{ + return _matrix[row][col]; + } +} + + +/// 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; + +} + + +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; +} + + +/// Comapre element by element +bool operator == ( const Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) + { + bool equal = false; + + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + if( leftM._matrix[i][j] != rightM._matrix[i][j] ) + equal = equal || true; + + return !equal; + + }else{ return false; } +} + + +/// Calls for '==' operator +bool operator != ( const Matrix& leftM, const Matrix& rightM ) +{ + return !( leftM == rightM ); +} + + +/// Matrices must be same size. +/// Element by element adition. +Matrix& operator +=( Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) + { + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + leftM._matrix[i][j] += rightM._matrix[i][j]; + + return leftM; + + }else{ printf( "\n\nERROR:\nDiferent Dimensions @ += operator\n" ); } +} + + +/// Matrices must be same size. +/// Element by element Substraction +Matrix& operator -=( Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) + { + for( int i = 0; i < leftM._nRows; i++ ) + for( int j = 0; j < leftM._nCols; j++ ) + leftM._matrix[i][j] -= rightM._matrix[i][j]; + + return leftM; + + }else{ + printf( "\n\nERROR:\nDiferent Dimensions @ -= operator\n" ); + } +} + + +Matrix& operator *=( Matrix& leftM, const Matrix& rightM ) +{ + if( leftM._nCols == rightM._nRows ) + { + Matrix resultM ( leftM._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] += leftM._matrix[i][m] * rightM._matrix[m][j]; + + return resultM; + }else{ + printf( "\n\nERROR:\nDiferent Dimensions @ *= operator\n" ); + } +} + + +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; +} + + +/*****************************************************************************/ + +// 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; + } +}