use from Ernesto Palacios

Dependents:   RoboticArm DXL_SDK_Porting_Test

Fork of Matrix by Ernesto Palacios

Revision:
5:a4014ab0a8cf
Parent:
3:589fb80932b5
Child:
7:7a564cf28ab6
diff -r c0c8f3edd60e -r a4014ab0a8cf Operators.cpp
--- a/Operators.cpp	Sat Oct 22 23:19:51 2011 +0000
+++ b/Operators.cpp	Sun Oct 30 16:29:23 2011 +0000
@@ -1,14 +1,44 @@
 /**
- * @file Matrix.cpp
+ * @brief  Source Code for the Operator of Matrix Class.
+ * @file   Operators.cpp
  * @author Ernesto Palacios
- * @brief Source Code for the Operator of Matrix Class.
- *
+ * 
  * 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 )
 {
@@ -30,97 +60,6 @@
 
 }
 
-/// 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 -()
 {
@@ -133,6 +72,94 @@
     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
@@ -291,4 +318,3 @@
         return leftM;
     }
 }
-