Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more
Revision 0:3abd8c2d7c34, committed 2011-09-23
- Comitter:
- Yo_Robot
- Date:
- Fri Sep 23 21:12:17 2011 +0000
- Child:
- 1:48f417da268e
- Commit message:
- Matrix_v1.0
Changed in this revision
| Matrix.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Matrix.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Matrix.cpp Fri Sep 23 21:12:17 2011 +0000
@@ -0,0 +1,427 @@
+#include "mbed.h"
+#include "Matrix.h"
+
+// Rows by Cols Matrix Constructor
+Matrix::Matrix(int Cols, int Rows): _Cols(Cols), _Rows(Rows)
+{
+ _matrix.resize(_Rows);
+ for( int i = 0; i < _Rows; i++ )
+ _matrix[i].resize(_Cols);
+}
+
+
+
+// Defines the same parameters as 'base' Matrix
+Matrix::Matrix(const Matrix &base): _Cols(base._Cols), _Rows(base._Rows)
+{
+ _matrix.resize(_Rows);
+ for( int i = 0; i < _Rows; i++ )
+ _matrix[i].resize(_Cols);
+}
+
+
+
+// Square Matrix Constructor
+Matrix::Matrix(int square): _Cols(square), _Rows(square)
+{
+ _matrix.resize(_Rows);
+ for( int i = 0; i < _Rows; i++ )
+ _matrix[i].resize(_Cols);
+}
+
+
+
+// Default Constructor
+Matrix::Matrix(){/*Intentionally left Blank*/}
+
+
+//***** OPERATORS ****//
+
+// Overloaded Asign Operator
+Matrix& Matrix::operator = ( const Matrix &rightM )
+{
+ if( *this != rightM )
+ {
+ this->_Rows = rightM._Rows;
+ this->_Cols = rightM._Cols;
+
+ this->_matrix.resize( rightM._Rows );
+ for( int i = 0; i < rightM._Rows; i++ )
+ this->_matrix [i].resize(rightM._Cols);
+
+ for( int i = 0; i < _Rows; i++ )
+ for( int j = 0; j < _Cols; j++ )
+ this->_matrix[i][j] = rightM._matrix[i][j];
+
+ return *this;
+ }
+ else
+ {
+ Matrix ret (*this);
+ return ret;
+ }
+}
+
+
+bool Matrix::operator == ( const Matrix& rightM )
+{
+ if( _Rows == rightM._Rows && _Cols == rightM._Cols )
+ {
+ bool equal = false;
+
+ for( int i = 0; i < _Rows; i++ )
+ for( int j = 0; j < _Cols; j++ )
+ if( _matrix[i][j] != rightM._matrix[i][j] )
+ equal = equal || true;
+
+ return !equal;
+
+ }else{ return false; }
+}
+
+
+bool Matrix::operator != ( const Matrix &rightM )
+{
+ return !( *this == rightM );
+}
+
+
+Matrix& Matrix::operator +=(const Matrix& rightM)
+{
+ if( _Rows == rightM._Rows && _Cols == rightM._Cols )
+ {
+ for( int i = 0; i < _Rows; i++ )
+ for( int j = 0; j < _Cols; j++ )
+ _matrix[i][j] += rightM._matrix[i][j];
+
+ return *this;
+
+ }else{
+
+ printf( "\nERROR\nDiferent Dimensions @ += Assignment\n" );
+
+ // Code below doesn't work! I dunno why. But anyway at this point
+ // the program should break anyway.
+ //
+ //Matrix::Matrix error( 4 );
+ //error.Clear();
+ //return error;
+
+ }
+}
+
+
+Matrix& Matrix::operator -=(const Matrix& rightM)
+{
+ if( _Rows == rightM._Rows && _Cols == rightM._Cols )
+ {
+ for( int i = 0; i < _Rows; i++ )
+ for( int j = 0; j < _Cols; j++ )
+ _matrix[i][j] -= rightM._matrix[i][j];
+
+ return *this;
+
+ }else{
+ printf( "\nERROR\nDiferent Dimensions @ -= Assignment\n" );
+
+ // return rightM;
+ }
+}
+
+const Matrix Matrix::operator +(const Matrix& rightM)
+{
+ Matrix result = *this;
+ result += rightM;
+ return result;
+}
+
+const Matrix Matrix::operator -(const Matrix& rightM)
+{
+ Matrix result = *this;
+ result -= rightM;
+ return result;
+}
+
+//------------------------------------------
+
+//*** Method that return MATRIX objects ***//
+
+
+
+Matrix Matrix::GetRow(Matrix mat, int Row)
+{
+ if( Row > mat._Rows )
+ {
+ printf( "WARNING: Row out of dimmensions @ GetRow().\nEmpty Matrix Returned\n" );
+ Matrix err( 1 , mat._Cols );
+ err.Clear();
+ return err;
+ }else{
+
+ Matrix SingleRow( 1 , mat._Cols );
+
+ for( int i = 0; i < mat._Cols; i++ )
+ SingleRow._matrix[0][i] = mat._matrix[Row][i];
+
+ return SingleRow;
+ }
+}
+
+
+
+Matrix Matrix::GetCol(Matrix mat, int Col)
+{
+ if( Col > mat._Cols )
+ {
+ printf( "WARNING: Column out of dimmensions @ GetCol.\nEmpty matrix returned\n" );
+ Matrix err( mat._Rows , 1 );
+ err.Clear();
+ return err;
+ }else{
+
+ Matrix SingleCol( mat._Rows, 1 );
+ for(int i = 0; i < mat._Rows; i++ )
+ SingleCol._matrix[i][0] = mat._matrix[i][Col];
+ return SingleCol;
+ }
+}
+
+
+
+Matrix Matrix::ToPackedVector()
+{
+ Matrix Shattered( ( this->_Rows * this->_Cols ) , 1 );
+
+ int cont = 0;
+ for( int i = 0; i < this->_Rows; i++ )
+ {
+ for( int j = 0; j < this->_Cols; j++ )
+ {
+ Shattered._matrix[0][cont] = this->_matrix[i][j];
+ cont++;
+ }
+ }
+
+ return Shattered;
+}
+//----------------------------------------------------
+
+//*** Method returning BOOLEAN ***//
+
+
+
+
+bool Matrix::isZero()
+{
+ bool zero = false;
+ for( int i = 0; i < this->_Rows; i++ )
+ for( int j = 0; j < this->_Cols; j++ )
+ if( this->_matrix[i][j] != 0 )
+ zero = zero || true;
+ return !zero;
+}
+
+
+
+bool Matrix::isVector()
+{
+ if( this->_Rows == 1 || this->_Cols == 1 )
+ return true;
+ else
+ return false;
+}
+
+//-------------------------------------
+
+//*** Static Methods ***//
+
+const bool Matrix::Equals( Matrix mat1, Matrix mat2 )
+{
+ if( mat1._Cols == mat2._Cols && mat1._Rows == mat2._Rows )
+ {
+ bool equal = false;
+
+ for( int i = 0; i < mat1._Rows; i++ )
+ for( int j = 0; j < mat1._Cols; j++ )
+ if( mat1._matrix[i][j] != mat2._matrix[i][j] )
+ equal = equal || true;
+
+ return !equal;
+
+ }else{
+ printf( "ERROR: Diferent Size Matrices!\n" );
+ printf( "mat1._Rows = %u\nmat1._Cols = %u\n",mat1._Rows, mat1._Cols );
+ printf( "mat2._Rows = %u\nmat2._Cols = %u\n",mat2._Rows, mat2._Cols );;
+ return false;
+ }
+}
+
+
+
+Matrix Matrix::CreateColumnMatrix(int Cols)
+{
+ Matrix ColMatrix(1,Cols);
+ return ColMatrix;
+}
+
+
+
+Matrix Matrix::CreateRowMatrix(int Rows)
+{
+ Matrix RowMatrix( Rows, 1);
+ return RowMatrix;
+}
+
+
+
+void Matrix::Clone( const Matrix &Source, Matrix Receip )
+{
+ Receip._Cols = Source._Cols;
+ Receip._Rows = Source._Rows;
+
+ Receip._matrix.resize(Receip._Rows);
+ for( int i = 0; i < Receip._Rows; i++ )
+ Receip._matrix[i].resize(Receip._Cols);
+
+ for( int i = 0; i < Receip._Rows; i++ )
+ for( int j = 0; j < Receip._Cols; j++ )
+ Receip._matrix[i][j] = Source._matrix[i][j];
+}
+
+
+void Matrix::DeleteRow(Matrix& Mat, int Row)
+{
+ --Row;
+
+ if( Row > Mat._Rows )
+ {
+ printf("\nERROR:\nColumn out of Limits on DeleteCol()\n");
+
+ }else{
+
+ for( int i = Row; i < Mat._Rows - 1; i++ )
+
+ for( int j = 0; j < Mat._Cols; j++ )
+ Mat._matrix[i][j] = Mat._matrix[i+1][j];
+ Mat._Rows--;
+ Mat._matrix.resize(Mat._Rows);
+ }
+}
+
+
+
+void Matrix::DeleteCol( Matrix &Mat, int Col)
+{
+ --Col; // Because of Column zero.
+
+ if( Col > Mat._Cols )
+ {
+ printf("\nERROR:\nColumn out of Limits on DeleteCol()\n");
+
+ }else{
+
+ for( int i = 0; i < Mat._Rows; i++ )
+ for( int j = Col; j < Mat._Cols; j++ )
+ Mat._matrix[i][j] = Mat._matrix[i][j+1];
+
+ // Decrease one column
+ Mat._Cols--;
+
+ //Erase last Column
+ for( int i = 0; i < Mat._Rows; i++ )
+ Mat._matrix[i].reserve(Mat._Cols);
+
+ }
+}
+
+//---------------------------------------------------------------
+
+//*** Methods returning nothing VOID ***//
+
+void Matrix::Add(int Row, int Col, float number)
+{
+ if( Row > _Rows || Col > _Cols )
+ {
+ printf("\nERROR:\n@ Matrix::Add, Out of limits of Matrix\n");
+
+ }else{
+ _matrix[Row][Col] = number;
+ }
+
+
+}
+
+void Matrix::Resize( int Rows, int Cols )
+{
+ this->_Rows = Rows;
+ this->_Cols = Cols;
+ this->_matrix.resize( _Rows );
+
+ for( int i = 0; i< _Rows ; i++ )
+ _matrix[i].resize(_Cols);
+}
+
+
+void Matrix::FillMatrix()
+{
+ for(int i = 0; i < _Rows; i++)
+ {
+ for(int j = 0; j < _Cols; j++)
+ {
+ printf( "Position [%u][%u]: ", i, j );
+ float numero;
+ scanf( "%f", &numero );
+ printf("%.3f ", numero);
+ this->Add( i, j, numero);
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+
+void Matrix::Clear()
+{
+ for( int i = 0; i < _Rows; i++ )
+ for( int j = 0; j < _Cols; j++ )
+ _matrix[i][j] = 0;
+ }
+
+void Matrix::print()
+{
+ for( int i = 0; i < _Rows; i++ )
+ {
+ for( int j = 0; j < _Cols; j++ )
+ {
+ printf( "%.3f, ",_matrix[i][j] );
+ }
+ printf( "\n" );
+ }
+}
+
+
+
+float Matrix::Sum()
+{
+ float total;
+
+ for( int i = 0; i < this->_Rows; i++ )
+ for( int j = 0; j < this->_Cols; j++ )
+ total += this->_matrix[i][j];
+ return total;
+}
+
+
+
+float Matrix::GetNumber( int Row, int Col )
+{
+ return this->_matrix[Row][Col];
+}
+
+
+int Matrix::getRows(){ return this->_Rows; }
+
+int Matrix::getCols(){ return this->_Cols; }
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Matrix.h Fri Sep 23 21:12:17 2011 +0000
@@ -0,0 +1,323 @@
+
+/**
+ * @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
+ *
+ */
+
+#include <vector>
+
+#ifndef MATRIX_H
+#define MATRIX_H
+
+/**
+ * @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.
+ *
+ * myMatrix.Fill(); // Fill the matrix using printf() and scanf().
+ * myMatrix.print(); // Prints the value of the Matrix. printf()
+ *
+ * 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
+ * anotherMatrix += myMatrix; // Adds one-by-one elements and saves to anotherMatrix
+ * anotherMatrix.print();
+ * @endcode
+ *
+ */
+class Matrix{
+public:
+
+ /// Creates a nex Matrix of Size [ Row x Cols ]
+ Matrix( int Rows, int Cols );
+
+ /// Creats a Square Matrix o Size [ Roxs x Rows ]
+ Matrix( int Rows );
+
+
+ /// Creates a new Matrix identical in size to an input Matrix
+ Matrix( const Matrix &base );
+
+
+ /// Reserves Memory for a New Matrix
+ Matrix();
+
+
+
+ /** @brief Description:
+ * This is the '=' Overloaded operator. It RESIZES assigned the matrix.
+ * Overwrites all data. To be used Carefully!
+ */
+ Matrix& operator = ( const Matrix &rightM );
+
+
+ /**@brief Description:
+ * Overload opeartor for the compare Matrices
+ *
+ * @param rightM
+ * @return Boolean 'false' if different.
+ */
+ bool operator == ( const Matrix &rightM );
+
+
+
+ /**@brief Description:
+ * Overload opeartor for the compare Matrices
+ *
+ * @param rightM
+ * @return Boolean 'true' if different
+ */
+ bool operator != ( const Matrix &rightM );
+
+ /**@brief
+ * Description:
+ * Overload Copmpound assignment.
+ * @param rightM
+ * @return A new Matrix to be assigned to itself.
+ */
+ Matrix& operator += ( const Matrix &rightM );
+
+
+
+ /**@brief
+ * Description:
+ * Overload Compund decrease.
+ * @param rightM Right hand matrix
+ * @return A new Matrix to be assigned to itself
+ */
+ Matrix& operator -= ( const Matrix &rightM );
+
+
+
+ /**@brief
+ * Description:
+ * Overloads the Plus, returns a new object.
+ * @param rightM right side Matrix
+ * @return Retuns an instance of the answer.
+ */
+ const Matrix operator +( const Matrix &rightM );
+
+
+ /**@brief
+ * Description:
+ * Overloads the Minus, returns a new object
+ * @param rightM
+ * @return Returns an instance of the asnwer
+ */
+ const Matrix operator -( const Matrix &rightM );
+
+
+
+ /** @brief
+ * Description:
+ * This method extracts a Row from a Matrix.
+ * If Row is out of the parameters, it returns
+ * an empty Row and prints a warning
+ * @param Matrix: Matrix to extract from
+ * @param Row: row number of the matrix
+ */
+ Matrix GetRow(Matrix mat, int Row);
+
+
+
+ /** @brief
+ * Description:
+ * This method extracts a Column from a Matrix.
+ * If Row is out of the parameters, it returns
+ * an empty Column and prints a warning
+ */
+ Matrix GetCol(Matrix mat, int Col);
+
+
+
+ /** @brief
+ * Description:
+ * Shatters the matrix into a single Row Vector.
+ * Important: Returns NEW matrix, does no modify existing one.
+ */
+ Matrix ToPackedVector();
+
+
+
+ /** @brief
+ * Description:
+ * Returns TRUE if the matrix is zero, FALSE otherwhise
+ * @param mat: Matrix to be tested
+ */
+ bool isZero();
+
+
+
+ /** @brief
+ * Description:
+ * Determines weather a Matrix is a Single Column or Row.
+ */
+ bool isVector();
+
+
+
+ /**@brief
+ * Description:
+ * Determines if two matrices are equal.
+ * @param mat1: First Matrix
+ * @param mat2: Sencond Matrix
+ */
+ static const bool Equals( Matrix mat1, Matrix mat2 );
+
+
+
+ /**@brief
+ * Description:
+ * 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
+ * Description:
+ * 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);
+
+
+
+ /**@brief
+ * Description:
+ * 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
+ * Description:
+ * 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
+ * Description:
+ * Static Function Deletes Column from Matrix, it's Static to prevent
+ * missuse.
+ * Print error and does nothing if out of limits.
+ * @Mat: Matrix to delete column from
+ * @param Col: Number of Col to delete (first Col = 1)
+ */
+ static void DeleteCol( Matrix &Mat, int Col );
+
+
+
+ /**@brief
+ * Description:
+ * Assigns a float number to the matrix in position
+ *
+ * @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
+ * Description:
+ * This function resizes the Matrix to fit new data.
+ * @param Rows: New Number of Rows
+ * @param Cols: New numbler of columns
+ */
+ void Resize( int Rows, int Cols );
+
+
+
+ /** @brief
+ * Description:
+ * Asks user for numbers to fill the Matrix elements. One by one
+ */
+ void FillMatrix();
+
+
+
+ /** @brief
+ * Description:
+ * Makes all values on Matrix object zero.
+ */
+ void Clear();
+
+
+
+ /** @brief
+ * Description:
+ * Prints the entire Matrix
+ */
+ void print();
+
+
+
+ /** \brief
+ * Description:
+ * Returns the sum of every cell in the Matrix.
+ */
+ float Sum();
+
+
+ /** @brief
+ * Description:
+ * 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 );
+
+
+
+ /**Retuns the number of Columns in Matrix*/
+ int getCols();
+
+
+
+ /**Retruns the number of Rows in Matrix */
+ int getRows();
+
+
+private:
+
+ /** 2-D Vector Array*/
+ vector < vector<float> > _matrix;
+
+ /** Number of Rows in Matrix*/
+ int _Rows;
+
+ /**Number of Columns in Matrix*/
+ int _Cols;
+
+};
+
+#endif /* MATRIX_H */