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:
Sun Oct 30 16:29:23 2011 +0000
Parent:
4:c0c8f3edd60e
Commit message:
Version 1.6.4 View Log.c for changes.

Changed in this revision

Log.c Show annotated file Show diff for this revision Revisions of this file
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
diff -r c0c8f3edd60e -r a4014ab0a8cf Log.c
--- a/Log.c	Sat Oct 22 23:19:51 2011 +0000
+++ b/Log.c	Sun Oct 30 16:29:23 2011 +0000
@@ -1,4 +1,34 @@
-/** @brief Keep track of changes since version 1.6             e-mail: mecatronica.mid@gmail.com
+/**  
+ * @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
diff -r c0c8f3edd60e -r a4014ab0a8cf Matrix.cpp
--- a/Matrix.cpp	Sat Oct 22 23:19:51 2011 +0000
+++ b/Matrix.cpp	Sun Oct 30 16:29:23 2011 +0000
@@ -1,10 +1,13 @@
 /**
- * @file Matrix.cpp
+ * @brief  Source Code for the Matrix Class.
+ * @file   Matrix.cpp
  * @author Ernesto Palacios
- * @brief Source Code for the Matrix Class.
  *
  * Created on September 2011.
  *
+ * Develop Under  GPL v3.0 License
+ * http://www.gnu.org/licenses/gpl-3.0.html
+ *
  */
 
 #include "mbed.h"
@@ -57,7 +60,7 @@
 /***********************************************************************/
 
 /// Returns true if matrix is full of zeros
-bool Matrix::isZero()
+bool Matrix::isZero() const
 {
     bool zero = false;
     for( int i = 0; i < this->_nRows; i++ )
@@ -69,7 +72,7 @@
 
 
 /// Returns true if Matrix is Single Row ot Single Column.
-bool Matrix::isVector()
+bool Matrix::isVector() const
 {
     if( _nRows == 1 || _nCols == 1 )
         return true;
@@ -133,7 +136,7 @@
     Matrix::AddRow( Receip, index );  //Make Room
 
     --index;
-    for( int i; i < Receip._nCols; i++ )
+    for( int i = 0; i < Receip._nCols; i++ )
         Receip._matrix[index][i] = Row._matrix[0][i];   //Copy Data.
 
 }
@@ -245,7 +248,7 @@
         SingleRow.Clear();
 
         for( int j = 0; j < Mat._nCols; j++ )
-	    SingleRow._matrix[0][j] = Mat._matrix[row][j];
+        SingleRow._matrix[0][j] = Mat._matrix[row][j];
 
         SingleRow._pCol = SingleRow._nCols;
         SingleRow._pRow = 0;
@@ -317,7 +320,7 @@
 
 
 /// Prints out Matrix.
-void Matrix::print()
+void Matrix::print() const
 {
     for( int i = 0; i < _nRows; i++ )
     {
@@ -361,24 +364,24 @@
 
 
 /// Adds all elements in matrix and returns the answer.
-float Matrix::sum()
+float Matrix::sum() const
 {
-    float total;
+    float total = 0;
 
-    for( int i = 0; i < this->_nRows; i++ )
-        for( int j = 0; j < this->_nCols; j++ )
-            total += this->_matrix[i][j];
+    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 )
+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(){ return this->_nRows; }
+int Matrix::getRows() const{ return this->_nRows; }
 
 
 /// Returns the number of Columns in Matrix.
-int Matrix::getCols(){ return this->_nCols; }
\ No newline at end of file
+int Matrix::getCols() const{ return this->_nCols; }
\ No newline at end of file
diff -r c0c8f3edd60e -r a4014ab0a8cf Matrix.h
--- a/Matrix.h	Sat Oct 22 23:19:51 2011 +0000
+++ b/Matrix.h	Sun Oct 30 16:29:23 2011 +0000
@@ -1,6 +1,8 @@
 /**
+ * @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
@@ -9,17 +11,16 @@
  */
 
 #ifndef MATRIX_H
-#define	MATRIX_H
+#define MATRIX_H
 
 
 #include <vector>
 
 class MatrixMath;
 
-using namespace std;
 /**
  * @brief This class provide basic manipulation for 2D matrices see Log.c for more info
- * version 1.6.2.
+ * version 1.6.4.
  *
  */
 class Matrix{
@@ -45,6 +46,25 @@
     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!
      */
@@ -57,7 +77,7 @@
      * @param rightM
      * @return Boolean 'false' if different.
      */
-    bool operator == ( const Matrix& rightM );
+    friend bool operator == ( const Matrix& leftM, const Matrix& rightM );
 
 
     /** @brief
@@ -66,7 +86,7 @@
      * @param rightM
      * @return Boolean 'true' if different
      */
-    bool operator != ( const Matrix& rightM );
+    friend bool operator != ( const Matrix& leftM, const Matrix& rightM );
 
 
     /** @brief
@@ -74,7 +94,7 @@
      * @param rightM
      * @return A new Matrix to be assigned to itself.
      */
-    Matrix& operator += ( const Matrix& rightM );
+    friend Matrix& operator += ( Matrix& leftM, const Matrix& rightM );
 
 
     /** @brief
@@ -82,7 +102,7 @@
      * @param rightM Right hand matrix
      * @return A new Matrix to be assigned to itself
      */
-    Matrix& operator -= ( const Matrix& rightM );
+    friend Matrix& operator -= ( Matrix& leftM, const Matrix& rightM );
 
 
     /** @brief
@@ -90,7 +110,7 @@
      * @param rightM
      * @return
      */
-    Matrix& operator *=( const Matrix& rightM );
+    friend Matrix& operator *=( Matrix& leftM, const Matrix& rightM );
 
 
     /** @brief
@@ -98,7 +118,7 @@
      * @param number
      * @return
      */
-    Matrix& operator *=( float number );
+    friend Matrix& operator *=( Matrix& leftM, float number );
 
 
 
@@ -208,13 +228,13 @@
      * Returns TRUE if the matrix is zero, FALSE otherwhise
      * @param mat: Matrix to be tested
      */
-    bool isZero();
+    bool isZero() const;
 
 
     /** @brief
      * Determines weather a Matrix is a Single Column or Row.
      */
-    bool isVector();
+    bool isVector() const;
 
 
     /** @brief
@@ -327,7 +347,7 @@
     /** @brief
      * Prints the entire Matrix using standard PRINTF
      */
-    virtual void print();
+    virtual void print() const;
 
 
     /** @brief
@@ -352,7 +372,7 @@
     /** @brief
      * Returns the sum of every cell in the Matrix.
      */
-    float sum();
+    float sum() const;
 
 
     /** @brief
@@ -361,19 +381,19 @@
      * @param Col = number of Col in matrix
      * @return Num = float number in matrix
      */
-    float getNumber( int Row, int Col );
+    float getNumber( int Row, int Col ) const;
 
 
     /**@brief
      * Retuns the number of Columns in Matrix, index starts at 1.
      */
-    int  getCols();
+    int  getCols() const;
 
 
     /**@brief
      *Retruns the number of Rows in Matrix, index starts at 1.
      */
-    int  getRows();
+    int  getRows() const;
 
 
 private:
@@ -399,12 +419,5 @@
 
 };
 
-#endif	/* MATRIX_H */
+#endif    /* MATRIX_H */
 
-/*
- * Things To Change -> Enhace -> Modify
- *
- * Submatrix
-  // .submat(first_row, first_column, last_row, last_column)
- *
- */
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;
     }
 }
-