matrix.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           matrix.h  -  description
00003                              -------------------
00004     email                : georg.martius@web.de
00005 ***************************************************************************/
00006 // provides Matrix class with convinient operators
00007 //  and fast inversion for nonzero square matrixes
00008 //
00009 // $Log: matrix.h,v $
00010 // Revision 1.35  2010/02/22 17:52:41  martius
00011 // added square norm function and default value for plusLambdaI
00012 //
00013 // Revision 1.34  2010/01/26 09:45:55  martius
00014 // added map2P functions with Double parameter
00015 //
00016 // Revision 1.33  2009/10/27 16:52:21  martius
00017 // documentation
00018 //
00019 // Revision 1.32  2009/10/27 12:50:27  martius
00020 // remove & in comment to make doxygen (latex) work
00021 //
00022 // Revision 1.31  2009/08/05 18:28:10  martius
00023 // added isVector
00024 // mapP and toMapP support double parameters
00025 //
00026 // Revision 1.30  2009/03/27 06:16:57  guettler
00027 // support for gcc 4.3 compatibility (has to be checked), StatisticTools moves from utils to statistictools
00028 //
00029 // Revision 1.29  2009/02/02 15:21:28  martius
00030 // added pseudoinverse
00031 //
00032 // Revision 1.28  2009/01/05 08:45:00  martius
00033 // exp again as function
00034 //
00035 // Revision 1.27  2008/12/22 14:40:47  martius
00036 // added & operator for multrowwise
00037 //
00038 // Revision 1.26  2008/12/22 14:37:42  martius
00039 // comments
00040 //
00041 // Revision 1.25  2008/11/14 09:15:29  martius
00042 // tried some autovectorization but without success
00043 // moved some function to CPP file
00044 //
00045 // Revision 1.24  2008/06/18 13:46:20  martius
00046 // beside and toBeside added
00047 // addColumns and addRows is now based on toAbove and toBeside
00048 // bug fix in removeColumns
00049 //
00050 // Revision 1.23  2008/05/30 11:58:08  martius
00051 // equals method and cmath
00052 //
00053 // Revision 1.22  2008/05/07 16:45:52  martius
00054 // code cosmetics and documentation
00055 //
00056 // Revision 1.21  2008/05/02 17:20:04  martius
00057 // *** empty log message ***
00058 //
00059 // Revision 1.20  2008/04/30 14:54:14  guettler
00060 // -indextype of matrices is now unsigned int (bigger matrices than
00061 //  256x256 can be used now, if not AVR defined)
00062 // -code cleaned up
00063 //
00064 // Revision 1.19  2008/04/29 11:04:44  guettler
00065 // forgot to deactivate += operator
00066 //
00067 // Revision 1.18  2008/04/29 11:01:32  guettler
00068 // -added toMap2 and toMap2P
00069 // -removed operator + for scalar (use add or toSum instead)
00070 //
00071 // Revision 1.17  2008/04/28 15:24:14  guettler
00072 // -added deleteRows and deleteColumns
00073 // -fixed memory leak in addColumns
00074 //
00075 // Revision 1.16  2008/04/28 10:33:05  guettler
00076 // -added operator + and += for adding a scalar to each element of the matrix
00077 // -added add function and toSum for adding a scalar to each element
00078 // -added addRows and addColumns for adding new rows or colums to the matrix
00079 //
00080 // Revision 1.15  2008/03/01 01:33:30  martius
00081 // size added
00082 //
00083 // Revision 1.14  2007/08/22 08:28:07  martius
00084 // contrains for reshape relaxed
00085 //
00086 // Revision 1.13  2007/06/21 16:29:18  martius
00087 // added map2P
00088 // map2 into cpp
00089 //
00090 // Revision 1.12  2007/06/08 15:50:29  martius
00091 // added unsafeGetData
00092 //
00093 // Revision 1.11  2007/05/22 13:52:36  martius
00094 // inplace operators return *this which makes them more useable for temporary matrices
00095 //
00096 // Revision 1.10  2007/04/03 09:58:20  martius
00097 // memory management done with free and malloc
00098 //
00099 // Revision 1.9  2007/04/03 07:13:32  der
00100 // plus lambdaI
00101 //
00102 // Revision 1.8  2007/02/05 12:31:21  martius
00103 // reshape
00104 //
00105 // Revision 1.7  2006/11/29 16:22:43  martius
00106 // name is a variable of configurable and is used as such
00107 //
00108 // Revision 1.6  2006/08/04 15:16:13  martius
00109 // documentation
00110 //
00111 // Revision 1.5  2006/07/20 17:14:35  martius
00112 // removed std namespace from matrix.h
00113 // storable interface
00114 // abstract model and invertablemodel as superclasses for networks
00115 //
00116 // Revision 1.4  2006/07/19 09:26:37  martius
00117 // namespace std removed from header
00118 // store and restore
00119 // read and write
00120 // columns accessor
00121 //
00122 // Revision 1.3  2006/07/18 14:13:09  martius
00123 // crucial bug fixing in *= operator!
00124 //
00125 // Revision 1.2  2006/07/14 12:24:01  martius
00126 // selforg becomes HEAD
00127 //
00128 // Revision 1.1.2.2  2006/07/14 08:56:53  der
00129 // New function NullTimesNull
00130 //
00131 // Revision 1.1.2.1  2006/07/10 12:01:02  martius
00132 // Matrixlib moved to selforg
00133 //
00134 // Revision 1.20.6.4  2006/03/30 23:08:53  martius
00135 // comments
00136 //
00137 // Revision 1.20.6.3  2006/03/30 12:34:45  martius
00138 // documentation updated
00139 //
00140 // Revision 1.20.6.2  2006/03/29 15:12:46  martius
00141 // column accessor function added
00142 //
00143 // Revision 1.20.6.1  2005/12/06 17:38:10  martius
00144 // *** empty log message ***
00145 //
00146 // Revision 1.20  2005/10/21 11:58:25  martius
00147 // map2 (similar to map but for 2 matrices)
00148 // changed naming of functions to be more consistent.
00149 //  Functions with "to" in front indicate the change of this. (Still not consistent with add, mult ...)
00150 //
00151 // Revision 1.19  2005/10/06 17:10:06  martius
00152 // convertToList
00153 // above and toAbove
00154 //
00155 // Revision 1.18  2005/09/21 08:43:01  martius
00156 // convertToBuffer is const
00157 //
00158 // Revision 1.17  2005/08/06 20:47:36  martius
00159 // Commented
00160 //
00161 // Revision 1.16  2005/07/21 15:13:36  martius
00162 // mapP addid (mapping with additional parameter)
00163 //
00164 // Revision 1.15  2005/06/21 15:35:21  martius
00165 // hide invert3x3 and invert_nonzero for AVR to minimize binary size
00166 //
00167 // Revision 1.14  2005/06/17 15:19:03  martius
00168 // version workes with avr
00169 //
00170 // Revision 1.13  2005/06/10 08:23:15  martius
00171 // mult???wise are copy operations now!
00172 // toMult???wise are the inplace variant.
00173 //
00174 // Revision 1.11  2005/06/09 11:52:03  martius
00175 // multMT (M * M^T) and multTM (M^T * M)
00176 //
00177 // Revision 1.10  2005/06/02 22:48:54  martius
00178 // copy is inline and works correct now
00179 //
00180 // Revision 1.9  2005/06/02 08:48:31  martius
00181 // mult_row/column_wise
00182 // convertToBuffer
00183 //
00184 // Revision 1.8  2005/05/30 22:40:56  martius
00185 // map becomes toMap and the new map returns a new matrix
00186 // exp becomes toExp
00187 //
00188 // Revision 1.7  2005/05/30 21:46:54  martius
00189 // *** empty log message ***
00190 //
00191 // Revision 1.6  2005/05/30 17:21:26  martius
00192 // added zero
00193 // set() and constructor(m,n,0) initialise with zero
00194 // id returns void (more consistent)
00195 //
00196 // Revision 1.5  2005/05/30 16:42:56  martius
00197 // map function included (component-wise function application)
00198 //
00199 // Revision 1.4  2005/05/30 11:28:09  martius
00200 // marked "row" as untested
00201 //
00202 // Revision 1.3  2005/05/30 10:14:54  martius
00203 // 3x3 explicit matrix inversion
00204 //
00205 // Revision 1.2  2005/05/30 09:45:46  martius
00206 // Working. Interface not tested in pratice
00207 //
00208 // Revision 1.1  2005/05/29 22:43:08  martius
00209 // proper Makefile with dependencies and tag generator
00210 //
00211 /***************************************************************************/
00212 
00213 #ifndef MATRIX_H
00214 #define MATRIX_H
00215 
00216 #include <string.h>
00217 #include <assert.h>
00218 #include <list>
00219 #include <cstdlib>
00220 
00221 #ifndef AVR
00222 #include <iostream>
00223 #endif
00224 
00225 #include "storeable.h"
00226 
00227 // TODO: add doxygen section
00228 
00229 /**
00230  * namespace for the matrix library
00231  *@author Georg Martius
00232  */
00233 namespace matrix{
00234 
00235   /// integer constant for use with exp function and (^) operator to transpose the matrix
00236   extern const int T;
00237 
00238 #ifndef AVR
00239 /// type for matrix indices
00240   typedef unsigned int I;
00241   /// type for matrix elements
00242   typedef double D;
00243 #else
00244 /// type for matrix indices, if AVR
00245 /// NOTE: You cannot use matrices bigger than m*n>255 if AVR!
00246   typedef unsigned short I;
00247   /// type for matrix elements
00248   typedef float D;
00249 #endif
00250 
00251 
00252 #define D_Zero 0
00253 #define D_One 1
00254   /** Matrix type. Type D is datatype of matrix elements,
00255    * which is fixed to double.
00256    * Type I is the indextype of matrix elements,
00257    * which is fixed to unsigned int, if AVR is not defined.
00258    * There are basicly two different types of operation:
00259    * Inplace operations and copy operations.
00260    * Please use the latter ones unless you know what you are doing.
00261    * Just in case of critical performance optimisation use the inplace
00262    * operations.
00263    * The most convinient way is to use the overloaded operators
00264    * (like + * ...).
00265    * All constructed matrices are initialised with zero elements
00266    * (unless data is given).
00267    * All functions perform range checks if in debug mode
00268    * (NDEBUG is not defined).
00269    * Please use debug the version (default) for testing
00270    * @see examples/matrix/matrixexample.cpp
00271    *
00272    * @author Georg Martius
00273    */
00274   class Matrix : public Storeable {
00275   public:
00276     /// default constructor: zero matrix (0x0)
00277     Matrix()
00278       : m(0), n(0), buffersize(0), data(0) {};
00279     /** constucts a matrix with the given size.
00280         If _data is null then the matrix is filled with zeros.
00281         otherwise matrix will be filled with _data in a row-wise manner.
00282         In this case _data must be at least _m*_n elements long
00283     */
00284     Matrix(I _m, I _n, const D* _data=0);
00285     /** constucts a matrix with the given size and fills it with the default value
00286     */
00287     Matrix(I _m, I _n, D def);
00288     /// constucts a instance on the base of a deep copy of the given matrix
00289     Matrix (const Matrix& c);
00290     ~Matrix() { if(data) free(data); };
00291 
00292   public:
00293     //  /////////////////// Accessors ///////////////////////////////
00294     /** @return number of rows */
00295     I getM() const { return m; };
00296     /** @return number of columns */
00297     I getN() const { return n; };
00298     /// @return number number elements in the matrix (getM()*getN())
00299     I size() const { return n*m; };
00300 
00301 
00302     /** @return element at position i,j (row, column index) */
00303     inline D val(I i, I j) const {
00304       assert( i<m && j<n);
00305       return data[i*n+j];
00306     };
00307     /** @return reference to element at position i,j
00308         (can be used as left side value) */
00309     inline D& val(I i, I j) {
00310       assert( i<m && j<n);
00311       return data[i*n+j];
00312     };
00313 
00314     /** @return element at position i,j (row, column index) and 0 if out of bounds */
00315     inline D valDef0(I i, I j) const {
00316       if(0<=i && i<m && 0<=j && j<n)
00317         return data[i*n+j];
00318       else return 0;
00319     };
00320 
00321     /** sets the size of the matrix and maybe the data if given (row-wise).
00322         If data=null then the matrix is set to zero
00323         @see toZero()
00324         @see constructor Matrix(m,n,data)
00325     */
00326     void set(I _m, I _n, const D* _data=0);
00327     /** sets the data (row-wise).
00328         @param _data if null then matrix elements are set to zero
00329         otherwise the field MUST have the length should be getM()*getN()*/
00330     void set(const D* _data);
00331     /** @return row-vector(as 1xN matrix) containing the index'th row */
00332     Matrix row(I index) const;
00333     /** @returns submatrix (as KxN matrix)
00334         containing row from startindex to endindex inclusively (K=stopindex-endindex)
00335         indices can be out of bounds, they are clipped in any case
00336     */
00337     Matrix rows(I startindex, I endindex) const;
00338     /** @returns column-vector(as Mx1 matrix) containing the index'th column */
00339     Matrix column(I index) const;
00340     /** @returns submatrix (as MxK matrix) 
00341         containing column from startindex to endindex inclusively (K=endindex-startindex) 
00342         indices can be out of bounds, they are clipped in any case
00343     */
00344     Matrix columns(I startindex, I endindex) const;
00345 
00346 
00347     /** stores the content of the matrix (row-wise) in the given buffer
00348         @param buffer Buffer for storing the elements (should have the length given by len)
00349         @param len Length of the provided buffer.
00350                In any case only min(len, getM()*getN()) elements are copied.
00351         @return number of actually written elements
00352      */
00353     int convertToBuffer(D* buffer, I len) const;
00354 
00355     /** @return a list of the content of the matrix (row-wise)
00356      */
00357     std::list<D> convertToList() const;
00358 
00359     /// returns a pointer to the data. UNSAFE!!!
00360     const double* unsafeGetData() const{return data;}
00361 
00362     /*       STOREABLE       */
00363     /** stores the Matrix into the given file stream (binary)
00364      */
00365     bool store(FILE* f) const;
00366 
00367     /** reads a Matrix from the given file stream (binary)
00368      */
00369     bool restore(FILE* f);
00370 
00371     /** writes the Matrix into the given file stream (ascii)
00372      */
00373     bool write(FILE* f) const;
00374 
00375     /** reads a Matrix from the given file stream (ascii)
00376      */
00377     bool read(FILE* f);
00378 
00379 
00380   public:
00381     // ////////////////////////////////////////////////////////////////////
00382     // /////////////  operations  /////////////////////////////
00383     //  (the result of the operation is not stored in one of the operands)
00384     void add(const Matrix& a, const Matrix& b); ///< addition:    this = a + b
00385     void add(const Matrix& a, const D& summand); /// add scalar to each element
00386     void sub(const Matrix& a, const Matrix& b); ///< subtraction: this = a - b
00387     void mult(const Matrix& a, const Matrix& b);///< multiplication: this = a * b
00388     void mult(const Matrix& a, const D& fac);///< scaling: this = a * fac
00389 
00390     void exp(const Matrix& a, int exponent);///< exponent, this = a^i, @see toExp    
00391 
00392     /// returns true if matrix is a 0x0 matrix
00393     bool isNulltimesNull() const;
00394 
00395     /// returns true if matrix is a vector
00396     bool isVector() const;
00397 
00398     /** bytewise comparison (compares data buffer bytewise, which implies that
00399         n1*m1 == n2*m2 but not necessarily n1==n2) */
00400     bool equals(const Matrix& a) const;
00401 
00402     /** calculates the pseudoinverse, depending on the shape of the matrix
00403         the left or right pseudoinverse is used.
00404         If the matrix has more columns than rows then we use 
00405         \f[A^{+} = (A^T A + \lambda \mathbb I)^{-1}A^T\f]
00406         otherwise 
00407         \f[A^{+} = A^T(A A^T + \lambda \mathbb I)^{-1}\f]
00408      */
00409     Matrix pseudoInverse(const D& lambda = 1e-8) const ;
00410 
00411     /**  maps the matrix to a new matrix
00412          with all elements mapped with the given function
00413     */
00414     Matrix map(D (*fun)(D)) const;
00415     /**  like map but with additional double parameter for the mapping function 
00416          (first argument of fun is parameter, the second is the value)*/
00417     Matrix mapP(D param, D (*fun)(D, D)) const;
00418     /**  like map but with additional arbitrary parameter for the mapping function */
00419     Matrix mapP(void* param, D (*fun)(void*, D)) const;
00420 
00421     // Exotic operations ///////////
00422     /** binary map operator for matrices.
00423        The resulting matrix consists of the function values applied to the elements of a and b.
00424        In haskell this would something like: map (uncurry . fun) $ zip a b
00425     */
00426     static Matrix map2( D (*fun)(D,D), const Matrix& a, const Matrix& b);
00427 
00428     /** like map2 but with additional parameter.
00429         The first argument of fun is the parameter and the second and third
00430         comes from the matrix elements.
00431         In haskell this would something like: map (uncurry . (fun p)) $ zip a b
00432      */
00433     static Matrix map2P( D param, D (*fun)(D, D,D), const Matrix& a, const Matrix& b);
00434     /** like map2P but with arbitrary paramters (void*) instead of double
00435      */
00436     static Matrix map2P( void* param, D (*fun)(void*, D,D), const Matrix& a, const Matrix& b);
00437 
00438 
00439 
00440     /** row-wise multiplication
00441         @param factors column vector (Mx1) of factors, one for each row
00442     */
00443     Matrix multrowwise(const Matrix& factors) const;
00444     /** column-wise multiplication
00445         @param factors column vector (Mx1) of factors, one for each column
00446     */
00447     Matrix multcolwise(const Matrix& factors) const;
00448 
00449     /// optimised multiplication of Matrix with its transposed: M * M^T
00450     Matrix multMT() const;
00451     /// optimised multiplication of transpsoed of Matrix with itself: M^T * M
00452     Matrix multTM() const;
00453 
00454     /// returns the product of all elements (\f$ \Pi_{ij} m_{ij} \f$)
00455     D elementProduct() const;
00456     /// returns the sum of all elements (\f$ \sum_{ij} m_{ij} \f$)
00457     D elementSum() const;
00458 
00459     /** returns the sum of all squares of all elements (\f$ \sum_{ij} m_{ij}^2 \f$)
00460         this is also known as the square of the Frobenius norm.
00461      */
00462     D norm_sqr() const;
00463 
00464     /// returns a matrix that consists of this matrix above A (number of rows is getM + a.getM())
00465     Matrix above(const Matrix& a) const ;
00466     /// returns a matrix that consists of this left beside A  (number of columns is getN + a.getN())
00467     Matrix beside(const Matrix& a) const ;
00468 
00469   public:   // normal binary Operators
00470     /// deep copy
00471     Matrix& operator = (const Matrix& c) { copy(c); return *this; }
00472     /// sum of two matrices
00473     Matrix operator +  (const Matrix& sum) const;
00474     //    Matrix operator +  (const D& sum) const; /// new operator (guettler)
00475     /// difference of two matrices
00476     Matrix operator -  (const Matrix& sum) const;
00477     /** matrix product*/
00478     Matrix operator *  (const Matrix& fac) const;
00479     /** product with scalar (D) (only right side) */
00480     Matrix operator *  (const D& fac) const;
00481     /** special matrix potence:
00482         @param exponent -1 -> inverse;
00483                        0 -> Identity Matrix;
00484                     1 -> itself;
00485                     T -> Transpose
00486     */
00487     Matrix operator ^ (int exponent) const;
00488     /// row-wise multiplication
00489     Matrix operator & (const Matrix& b) const;
00490     /// combined assigment operator (higher performance)
00491     Matrix& operator += (const Matrix& c) {toSum(c);   return *this; }
00492     /// combined assigment operator (higher performance)
00493     Matrix& operator -= (const Matrix& c) {toDiff(c);  return *this; }
00494     /// combined assigment operator (higher performance)
00495     Matrix& operator *= (const Matrix& c) {
00496       Matrix result;
00497       result.mult(*this, c);
00498       this->copy(result);
00499       return *this;
00500     }
00501     /// combined assigment operator (higher performance)
00502     Matrix& operator *= (const D& fac) {toMult(fac); return *this; }
00503     /// combined assigment operator (higher performance)
00504     Matrix& operator &= (const Matrix& c) {toMultrowwise(c); return *this; }
00505 
00506 #ifndef AVR
00507     /// comparison operator (compares elements with tolerance distance of COMPARE_EPS)
00508     bool operator == (const Matrix& c) const;
00509     /** printing operator:
00510         output format: mxn (\n row0\n..rown \n) where rowX is tab seperated list of values
00511     */
00512     friend std::ostream& operator<<(std::ostream& , const Matrix&);
00513 #endif
00514 
00515   public:
00516     // /////////////////// inplace Operators ///////////////////////////////
00517     /** performs a deep copy of the given matrix */
00518     void copy(const Matrix& c){ // Deep copy
00519       m=c.m; n=c.n;
00520       allocate();
00521       memcpy(data,c.data,m*n*sizeof(D));
00522     }
00523 
00524     Matrix& toTranspose();  ///< inplace transpose
00525     Matrix& toZero();       ///< inplace converts matrix to zero matrix
00526     Matrix& toId();         ///< inplace converts matrix to identity (use ^0 to get a copy version of it)
00527     /// inplace addition: this = this + a
00528     Matrix& toSum(const Matrix& a);
00529     /// inplace addition: this = this + a, where a is a scalar
00530     Matrix& toSum(const D& sum);
00531 
00532     /// inplace subtraction: this = this - a
00533     Matrix& toDiff(const Matrix& a);
00534 
00535     /// inplace multiplication: this = this * a
00536     Matrix& toMult(const Matrix& a);
00537     /// inplace multiplication with scalar: this = this*fac
00538     Matrix& toMult(const D& fac);
00539 
00540     /** special inplace matrix power:
00541         @param exponent -1 -> inverse; (matrix MUST be SQUARE and NONZERO)
00542                     0 -> Identity Matrix;
00543                     1 -> itself;
00544                     T -> Transpose
00545     */
00546     Matrix& toExp(int exponent);
00547     /**  inplace mapping of matrix elements (element-wise application) */
00548     Matrix& toMap(D (*fun)(D));
00549     /**  like toMap, but with an extra double parameter for the mapping function. */
00550     Matrix& toMapP(D param, D (*fun)(D, D));
00551     /**  like toMap, but with an extra arbitrary parameter for the mapping function. */
00552     Matrix& toMapP(void* param, D (*fun)(void*, D));
00553 
00554     /**  like toMap, but with using 2 matrices */
00555     Matrix& toMap2(D (*fun)(D,D), const Matrix& b);
00556 
00557     /**  like toMap2, but with additional parameter */
00558     Matrix& toMap2P( D param, D (*fun)(D, D,D), const Matrix& b);
00559     /**  like toMap2P, but with arbitrary parameter */
00560     Matrix& toMap2P( void* param, D (*fun)(void*, D,D), const Matrix& b);
00561 
00562     // Exotic operations
00563     /** Inplace row-wise multiplication
00564         @param factors column vector of factors, one for each row
00565     */
00566     Matrix& toMultrowwise(const Matrix& factors);
00567     /** Inplace column-wise multiplication
00568         @param factors column vector of factors, one for each column
00569     */
00570     Matrix& toMultcolwise(const Matrix& factors);
00571 
00572     /// sets the matrix a below this matrix
00573     Matrix& toAbove(const Matrix& a);
00574     /// sets the matrix a right beside this matrix
00575     Matrix& toBeside(const Matrix& a);
00576 
00577     /// sorts the matrix (rowwise)
00578     Matrix& toSort();
00579 
00580     /** reshapes the matrix without destroying the data. 
00581         Remember: The data is stored rowwise.
00582 
00583         Only shrinking is allowed i.e. m*n must be lower or equal to getM()*getN()
00584     */
00585     Matrix& reshape(I m, I n);
00586 
00587     /// adds the given value to the diagonal
00588     Matrix& pluslambdaI(double lambda = 1e-8);
00589 
00590     /** adds one or more rows to the existing matrix and fills it with the given data
00591      * 
00592      * same as toAbove(Matrix(numberRows,getN(),data))
00593      * @param numberRows number of rows to add (this extends m)
00594      * @param _data data to add
00595      * @return the address of the matrix itself
00596      */
00597     Matrix& addRows(I numberRows, const D* _data=0);
00598 
00599     /**
00600      * same as toAbove(dataMatrix)
00601      * @param numberRows number of rows to add (unused)
00602      * @param dataMatrix matrix which contains the data of the new rows
00603      * @return the address of the matrix itself
00604      */
00605     Matrix& addRows(I numberRows, const Matrix& dataMatrix);
00606 
00607     /** adds one or more columns to the existing matrix
00608      * same as toBeside(Matrix(getM, numberColumns, _data))
00609      * @see toBeside()
00610      * @param numberColumns number of columns to add (this extends n)
00611      * @param _data data to add
00612      * @return the address of the matrix itself
00613      */
00614      Matrix& addColumns(I numberColumns, const D* _data=0);
00615 
00616     /**
00617      * same as toBeside(dataMatrix)
00618      * @see toBeside()
00619      * @param numberColumns number of columns to add (unused)
00620      * @param dataMatrix matrix which contains the data of the new rows
00621      * @return the address of the matrix itself
00622      */
00623     Matrix& addColumns(I numberColumns, const Matrix& dataMatrix);
00624 
00625     
00626     /** removes one or more rows of the existing matrix, 
00627         same as reshape(getM()-numberRows, getN());
00628      * @param numberRows number of rows to remove (this reduces m)
00629      * @return the address of the matrix itself
00630      */
00631     Matrix& removeRows(I numberRows);
00632 
00633     /** removes one or more columns of the existing matrix
00634      * resets the size of the matrix and deletes the appropiate data.
00635      * @param numberColumns number of columns to remove (this reduces n)
00636      * @return the address of the matrix itself
00637      */
00638     Matrix& removeColumns(I numberColumns);
00639 
00640   private:
00641     // NOTE: buffersize determines available memory storage.
00642     // m and n define the actual size
00643     I m, n;
00644     I buffersize;  // max number if elements
00645     D* data;      // where the data contents of the matrix are stored
00646 
00647 
00648   private: // internals
00649     void allocate();  //internal allocation
00650     /*inplace matrix invertation:
00651         Matrix must be SQARE, in addition, all DIAGONAL ELEMENTS MUST BE NONZERO
00652         (positive definite)
00653     */
00654 
00655 #ifndef AVR
00656     void invertnonzero();
00657     void invert3x3();
00658 #endif
00659     void invert2x2();
00660   };
00661 
00662 } // namespace matrix
00663 #endif
Generated on Fri Nov 4 10:59:39 2011 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3