Ernesto Palacios / Matrix

Dependents:   Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more

Files at this revision

API Documentation at this revision

Comitter:
Yo_Robot
Date:
Sat Oct 22 23:19:51 2011 +0000
Parent:
3:589fb80932b5
Child:
5:a4014ab0a8cf
Commit message:
version 1.6.2 Read Log.c for details

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Log.c	Sat Oct 22 23:19:51 2011 +0000
@@ -0,0 +1,31 @@
+/** @brief Keep track of changes since version 1.6             e-mail: mecatronica.mid@gmail.com
+
+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.
+
+*/
--- a/Matrix.cpp	Fri Oct 21 03:33:53 2011 +0000
+++ b/Matrix.cpp	Sat Oct 22 23:19:51 2011 +0000
@@ -51,7 +51,7 @@
 
     _pRow = 0;
     _pCol = 0;
-    
+
 }
 
 /***********************************************************************/
@@ -82,7 +82,7 @@
 /// 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;
@@ -103,11 +103,11 @@
 
 
 /// To add (Insert) a Single Row to a Matrix.
-void Matrix::AddRow(Matrix& Mat, int Row)
+void Matrix::AddRow(Matrix& Mat, int index)
 {
-    --Row;
+    --index;
 
-    if( Row > Mat._nRows + 1)
+    if( index > Mat._nRows + 1)
     {
         printf("\n\nERROR:\nRow out of Limits @ AddRow()\n");
 
@@ -118,22 +118,33 @@
 
        Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols );
 
-       for( int i = Mat._nRows - 1; i > Row; i-- )
+       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[Row][j] = 0.0;
+           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; 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::AddColumn( Matrix& Mat, int Col )
+void Matrix::AddCol( Matrix& Mat, int index )
 {
-    --Col;
+    --index;
 
-    if( Col > Mat._nCols + 1 )
+    if( index > Mat._nCols + 1 )
     {
         printf("\n\nERROR:\nRow out of Limits on AddCol()\n");
 
@@ -145,16 +156,26 @@
                 Mat._matrix[i].resize( Mat._nCols );
 
             for( int i = 0; i < Mat._nRows; i++ )
-                for( int j = Mat._nCols; j > Col; j-- )
+                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][Col] = 0.0;
+                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)
 {
@@ -222,7 +243,7 @@
 
         Matrix SingleRow( 1 , Mat._nCols );
         SingleRow.Clear();
-        
+
         for( int j = 0; j < Mat._nCols; j++ )
 	    SingleRow._matrix[0][j] = Mat._matrix[row][j];
 
@@ -238,7 +259,7 @@
 const Matrix Matrix::ExportCol( const Matrix& Mat, int col )
 {
     --col;
-    
+
     if( col > Mat._nCols )
     {
         printf( "\n\nERROR:\nColumn out of dimmensions.\n"
@@ -248,10 +269,10 @@
         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;
     }
 }
--- a/Matrix.h	Fri Oct 21 03:33:53 2011 +0000
+++ b/Matrix.h	Sat Oct 22 23:19:51 2011 +0000
@@ -1,9 +1,8 @@
-
-/** 
+/**
  * @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
  *
@@ -19,12 +18,9 @@
 
 using namespace std;
 /**
- * @brief This class provide basic manipulation for 2D matrices.
- * 
- * Intended to be ported to mbed.org 
- * 
- * v1.6
- * 
+ * @brief This class provide basic manipulation for 2D matrices see Log.c for more info
+ * version 1.6.2.
+ *
  */
 class Matrix{
 public:
@@ -49,13 +45,11 @@
     friend class MatrixMath;
 
 
-
     /** @brief
      * Overwrites all data. To be used Carefully!
      */
     Matrix& operator = ( const Matrix& rightM );
-    
-    
+
 
     /** @brief
      * Overload opeartor for the compare Matrices
@@ -66,7 +60,6 @@
     bool operator == ( const Matrix& rightM );
 
 
-
     /** @brief
      * Overload opeartor for the compare Matrices
      *
@@ -76,7 +69,6 @@
     bool operator != ( const Matrix& rightM );
 
 
-
     /** @brief
      * Overload Copmpound assignment.
      * @param rightM
@@ -85,7 +77,6 @@
     Matrix& operator += ( const Matrix& rightM );
 
 
-
     /** @brief
      * Overload Compund decrease.
      * @param rightM Right hand matrix
@@ -94,7 +85,6 @@
     Matrix& operator -= ( const Matrix& rightM );
 
 
-
     /** @brief
      * Overload Compound CrossProduct Matrix operation.
      * @param rightM
@@ -103,7 +93,6 @@
     Matrix& operator *=( const Matrix& rightM );
 
 
-
     /** @brief
      * Overload Compund Element-by-elemnt scalar multiplication.
      * @param number
@@ -120,7 +109,6 @@
     const Matrix operator -();
 
 
-
     /**@brief
      * Overload Compound add with scalar.
      * Because the '=' operator checks for self Assign, no extra operations
@@ -130,7 +118,6 @@
     friend const Matrix operator +=( Matrix& leftM, float number );
 
 
-
     /**@brief
      * Compound substract with scalar.
      * @return Same matrix to self Assign.
@@ -138,7 +125,6 @@
     friend const Matrix operator -=( Matrix& leftM, float number );
 
 
-
     /** @brief
      * Adds two matrices of the same dimensions, element-by-element.
      * If diferent dimensions -> ERROR.
@@ -147,7 +133,6 @@
     friend const Matrix operator +( const Matrix& leftM, const Matrix& rightM);
 
 
-
     /** @brief
      * Adds the given nomber to each element of matrix.
      * Mimic MATLAB operation.
@@ -164,7 +149,6 @@
     friend const Matrix operator +( float number, const Matrix& leftM );
 
 
-
     /**@brief
      * Substracts two matrices of the same size, element-by-element.
      * If different dimensions -> ERROR.
@@ -173,7 +157,6 @@
     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.
@@ -181,7 +164,6 @@
     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.
@@ -189,15 +171,13 @@
     friend const Matrix operator -( float number, const Matrix& leftM );
 
 
-
     /**
      * Preforms Crossproduct between two matrices.
-     * @return 
+     * @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.
@@ -205,7 +185,6 @@
     friend const Matrix operator *( const Matrix& leftM, float number );
 
 
-
     /**@brief
      * Multiplies a scalar number with each element on Matrix.
      * @return
@@ -213,7 +192,6 @@
     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 ).
@@ -233,14 +211,12 @@
     bool isZero();
 
 
-
     /** @brief
      * Determines weather a Matrix is a Single Column or Row.
      */
     bool isVector();
 
 
-
     /** @brief
      * Shatters the matrix into a single Row Vector.
      * Important: Returns NEW matrix, does no modify existing one.
@@ -248,7 +224,6 @@
     static const Matrix ToPackedVector( const Matrix& Mat );
 
 
-
     /** @brief
      * Invoking this static method will increase a Row in Mat in the desired
      * position.
@@ -257,7 +232,19 @@
      * @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 Row );
+    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
@@ -266,7 +253,19 @@
      * @param Mat: Matrix in wich to insert a Column
      * @param Col: Number of column, strats with one, not zero.
      */
-    static void AddColumn( Matrix& Mat, int Col );
+    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
@@ -287,8 +286,6 @@
     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.
@@ -299,7 +296,6 @@
      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.
@@ -311,11 +307,9 @@
      static const Matrix ExportCol( const Matrix& Mat, int col );
 
 
-
     /** @brief
      * This function resizes the Matrix to fit new data or cropped it,
-     * to use newly created memory use  .add()  not << operator.
-     * << operator overwites everithing
+     * operator << can overwrite entire Matrix.
      *
      * @param Rows: New Number of Rows
      * @param Cols: New numbler of columns
@@ -323,7 +317,6 @@
     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.
@@ -331,14 +324,12 @@
     virtual void FillMatrix();
 
 
-
     /** @brief
      * Prints the entire Matrix using standard PRINTF
      */
     virtual void print();
 
 
-
     /** @brief
      * Makes all values on Matrix object zero.
      * Also make posible use the '<<' operator to add elements and keep
@@ -358,7 +349,6 @@
     void add( int Row, int Col, float number );
 
 
-
     /** @brief
      * Returns the sum of every cell in the Matrix.
      */
@@ -374,14 +364,12 @@
     float getNumber( int Row, int Col );
 
 
-
     /**@brief
      * Retuns the number of Columns in Matrix, index starts at 1.
      */
     int  getCols();
 
 
-
     /**@brief
      *Retruns the number of Rows in Matrix, index starts at 1.
      */
@@ -394,7 +382,7 @@
     vector < vector<float> > _matrix;
 
 
-    
+
     /** Number of Rows in Matrix*/
     int _nRows;
 
@@ -413,3 +401,10 @@
 
 #endif	/* MATRIX_H */
 
+/*
+ * Things To Change -> Enhace -> Modify
+ *
+ * Submatrix
+  // .submat(first_row, first_column, last_row, last_column)
+ *
+ */