Matrix Library. v1.6.4

Dependents:   Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more

Files at this revision

API Documentation at this revision

Comitter:
Yo_Robot
Date:
Fri Oct 21 03:33:53 2011 +0000
Parent:
2:493402568a5e
Child:
4:c0c8f3edd60e
Commit message:
version 1.6.0.1 just fixed some typos in comments

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
Operators.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Matrix.cpp	Thu Oct 20 23:42:13 2011 +0000
+++ b/Matrix.cpp	Fri Oct 21 03:33:53 2011 +0000
@@ -1,7 +1,7 @@
 /**
- * @file: Matrix.cpp
- * @author: Ernesto Palacios
- * @brief: Source Code for the Matrix Class.
+ * @file Matrix.cpp
+ * @author Ernesto Palacios
+ * @brief Source Code for the Matrix Class.
  *
  * Created on September 2011.
  *
@@ -102,29 +102,6 @@
 
 
 
-/// Test elment-by-element. Prints detailed error.
-bool Matrix::Equals( const Matrix& mat1, const Matrix& mat2 )
-{
-    if( mat1._nCols == mat2._nCols  &&  mat1._nRows == mat2._nRows )
-    {
-        bool equal = false;
-
-        for( int i = 0; i < mat1._nRows; i++ )
-            for( int j = 0; j < mat1._nCols; j++ )
-                if( mat1._matrix[i][j] != mat2._matrix[i][j] )
-                    equal = equal || true;
-
-        return !equal;
-
-    }else{
-        printf( "\n\nERROR:\nDiferent Size Matrices!\n" );
-        printf( "mat1._nRows = %u\nmat1._nCols = %u\n",mat1._nRows, mat1._nCols );
-        printf( "mat2._nRows = %u\nmat2._nCols = %u\n",mat2._nRows, mat2._nCols );
-        return false;
-    }
-}
-
-
 /// To add (Insert) a Single Row to a Matrix.
 void Matrix::AddRow(Matrix& Mat, int Row)
 {
--- a/Matrix.h	Thu Oct 20 23:42:13 2011 +0000
+++ b/Matrix.h	Fri Oct 21 03:33:53 2011 +0000
@@ -1,7 +1,7 @@
 
 /** 
- * @file:   Matrix.h
- * @author: Ernesto Palacios
+ * @file   Matrix.h
+ * @author Ernesto Palacios
  * Created on 13 de septiembre de 2011, 03:49 PM
  * 
  * Develop Under  GPL v3.0 License
@@ -23,7 +23,7 @@
  * 
  * Intended to be ported to mbed.org 
  * 
- * v1.01
+ * v1.6
  * 
  */
 class Matrix{
@@ -37,20 +37,13 @@
     Matrix( const Matrix& base );
 
 
-    /// Reserves Memory for a New Matrix
+    /// Default Constructor
     Matrix();
 
 
 /******************************************************************************/
 
-    /** @brief
-     * This is the '=' Overloaded operator. It RESIZES assigned the matrix.
-     * Overwrites all data. To be used Carefully!
-     */
-    Matrix& operator = ( const Matrix& rightM );
 
-    
-    
     /**@brief This includes the Class to handle Matrix Operations.
      */
     friend class MatrixMath;
@@ -58,6 +51,13 @@
 
 
     /** @brief
+     * Overwrites all data. To be used Carefully!
+     */
+    Matrix& operator = ( const Matrix& rightM );
+    
+    
+
+    /** @brief
      * Overload opeartor for the compare Matrices
      *
      * @param rightM
@@ -114,8 +114,7 @@
 
 
     /**@brief
-     * Makes all elements in matrix negative.
-     * Unary operator
+     * All elements in matrix are multiplied by (-1).
      * @return A new Matrix object with inverted values.
      */
     const Matrix operator -();
@@ -251,15 +250,6 @@
 
 
     /** @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
      * 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
--- 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;
+    }
+}
+