LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiGenericMatrix.h

00001 /*
00002  * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiGenericMatrix.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 09.04.99
00030  * revisions ..: $Id: ltiGenericMatrix.h,v 1.14 2006/02/08 12:24:46 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_GENERIC_MATRIX_H_
00034 #define _LTI_GENERIC_MATRIX_H_
00035 
00036 #include "ltiMathObject.h"
00037 #include "ltiGenericVector.h"
00038 #include "ltiPoint.h"
00039 #include "ltiRectangle.h"
00040 
00041 namespace lti {
00042   /**
00043    * GenericMatrix container class.
00044    *
00045    * The lti::genericMatrix class allows the representation of \e n x
00046    * \e m matrices of any type showing .  The rows will be indexed
00047    * between 0 and n-1, and the columns between 0 and m-1.
00048    *
00049    * All types defined in ltiTypes.h use static members and can be contained
00050    * by the lti::genericVector and lti::genericMatrix classes.
00051    *
00052    * The genericMatrix class is a container class implemented as template.
00053    *
00054    * If you need to create a genericMatrix of floats with 20 rows and
00055    * 15 columns, all elements initialized with an initial value of
00056    * 4.27 just create it:
00057    *
00058    * \code
00059    * lti::genericMatrix<float> myMat(20,15,4.27f) // creates genericMatrix 
00060    *                                              // with 300 elements
00061    *                                              // all initialized with 4.27f
00062    * \endcode
00063    *
00064    * To access the genericMatrix elements use the access operators.
00065    * There are many possibilities.  With at(const int row, const int
00066    * col) is possible to access an element directly.  With at(const
00067    * int row) you can get the row vector.  You cannot for instance
00068    * resize nor change the memory referenced in this vector (see
00069    * lti::genericVector::resize()).  For example:
00070    *
00071    * \code
00072    * float accu = 0; // initialize accumulator
00073    * lti::genericMatrix<float> myMat(20,15,4.27f)
00074    * lti::vector<float> myVct;
00075    *
00076    * for (int j = 0; j < myMat.rows(); j++) {
00077    *   for (int i = 0; i < myMat.columns(); i++) {
00078    *   tmp += myMat.at(j,i); // access each element of the genericMatrix:
00079    *                         // j is the row and i the column
00080    *   }
00081    * }
00082    *
00083    * myMat.getRowCopy(5,myVct); // copy the sixth row in myVct!
00084    * myVct.resize(6);           // Valid, the vector has its own memory!
00085    * myMat.at(5).resize(6)      // ERROR!! the vector is not resizable!
00086    *
00087    * \endcode
00088    *
00089    *
00090    * The genericMatrix has following methods:
00091    * - Constructors Constructors
00092    *   -  You can construct an empty genericMatrix with the default constructor
00093    *      (genericMatrix()).
00094    *   - If you know the number of rows and columns use
00095    *     genericMatrix(const int rows,const int columns,const T&
00096    *     initialValue)
00097    *   - You can create a copy of another genericMatrix or just copy a
00098    *     subgenericMatrix with the copy constructor (genericMatrix(const
00099    *     genericMatrix<T>& otherGenericMatrix))
00100    * - Access members
00101    *   - at(), operator[]()
00102    *   - The rows() member returns the number of rows of the genericMatrix.
00103    *   - The columns() member returns the number of columns of the
00104          genericMatrix.
00105    * - Fill and Copy members
00106    *   - With the fill() members you can fill the genericMatrix with a given
00107    *     constant value or with values taken from other matrices.
00108    *   - With the copy() member you can copy another genericMatrix.
00109    *   - You can specify, that the genericMatrix should be used just as a
00110    *     wrapper-object to access external memory regions: useExternData().
00111    *     To check if a genericMatrix is a wrapper-object you can use 
00112    *     ownsData().
00113    * - Iterators
00114    *   - It is possible to iterate within the genericMatrix by making use of
00115    *     the genericMatrix iterators.  (see begin() for more information)
00116    *   - Instead of reverse_iterators as in the STL we use iterators
00117    *     going backwards, due to faster execution times (see
00118    *     inverseBegin() for more information)
00119    *
00120    * @ingroup gAggregate
00121    */
00122   template<class T>
00123   class genericMatrix : public mathObject {
00124     public:
00125     /**
00126      * type of the contained data
00127      */
00128     typedef T value_type;
00129 
00130     /**
00131      * The data can be stored sequentially in the memory or in lines.
00132      * The "Line" modus will be used when a matrix is created as a
00133      * submatrix of another "Connected" matrix.
00134      */
00135     typedef enum {
00136       Connected, /**< the data is stored as a single memory block */
00137       Line       /**< each line has its own memory block.  The iterators
00138                       do not work on this mode, but you can iterate on each
00139                       row */
00140     } eStoreMode;
00141 
00142 #   ifdef NDEBUG
00143     /**
00144      * Iterator type (allows read and write operations)
00145      *
00146      * The use of the iterator classes is similar to the iterators of
00147      * the STL (Standard Template Library). See lti::genericMatrix::begin()
00148      * for an example .
00149      *
00150      * For the debugging version of the iterators, boundary check will be
00151      * done!  This explains the low speed of the iterators of the debug
00152      * version.  In the release version, no boundary check will be done,
00153      * and the iterators are sometimes a factor 10 faster than the
00154      * debug iterators.
00155      *
00156      * The use of the access operator at(.) is faster than the iterators
00157      * in the debug version only.  If you need to iterate on a genericMatrix use
00158      * iterators instead (in the release version iterators are approx. a
00159      * factor 3 faster than "at(.)").
00160      *
00161      * Iterators do not work on lined matrices.  This means, if you have
00162      * a submatrix reference of another one (getMode() == Lined), the
00163      * iterators won't follow the submatrix but the original data.  This will
00164      * not change since it would imply reducing the performance for connected
00165      * matrices, for which the iterators were designed on the first place!
00166      *
00167      * CAUTION: Try to use the prefix incremental operator
00168      * (i.e. ++it) instead of the postfix operator (i.e. it++) to
00169      * allow efficient code also in debug-modus!
00170      */
00171     typedef T* iterator;
00172 
00173     /**
00174      * const iterator type (allows read-only operations)
00175      * The use of the iterator classes is similar to the iterators of
00176      * the STL (Standard Template Library). See lti::genericMatrix::begin()
00177      * for an example.
00178      *
00179      * For the debugging version of the iterators, boundary check will be
00180      * done!  This explains the low speed of the iterators of the debug
00181      * version.  In the release version, no boundary check will be done,
00182      * and the iterators are sometimes a factor 10 faster than the
00183      * debug iterators.
00184      *
00185      * The use of the access operator at(.) is faster than the iterators in
00186      * the debug version only.  If you need to iterate on a genericMatrix use
00187      * iterators instead (in the release version iterators are approx. a
00188      * factor 3 faster than "at(.)").
00189      *
00190      * Iterators do not work on lined matrices.  This means, if you have
00191      * a submatrix reference of another one (getMode() == Lined), the
00192      * iterators won't follow the submatrix but the original data.  This will
00193      * not change since it would imply reducing the performance for connected
00194      * matrices, for which the iterators were designed on the first place!
00195      *
00196      * CAUTION: Try to use the prefix incremental operator
00197      * (i.e. ++it) instead of the postfix operator (i.e. it++) to
00198      * allow efficient code also in debug-modus!
00199      */
00200     typedef const T* const_iterator;
00201 
00202 #   else
00203 
00204     class const_iterator;
00205 
00206     /**
00207      * iterator type (allows read and write operations).
00208      *
00209      * The use of the iterator classes is similar to the iterators of
00210      * the STL (Standard Template Library). See lti::genericMatrix::begin()
00211      * for an example
00212      *
00213      * For the debugging version of the iterators, boundary check will be
00214      * done!  This explains the low speed of the iterators of the debug
00215      * version.  In the release version, no boundary check will be done,
00216      * and the iterators are sometimes a factor 10 faster than the
00217      * debug iterators.
00218      *
00219      * The use of the access operator at(.) is faster than the iterators
00220      * in the debug version only.  If you need to iterate on a genericMatrix use
00221      * iterators instead (in the release version iterators are approx. a
00222      * factor 3 faster than "at(.)").
00223      *
00224      * Iterators don't work on lined matrices.
00225      *
00226      * CAUTION: Try to use the prefix incremental operator
00227      * (i.e. ++it) instead of the postfix operator (i.e. it++) to
00228      * allow efficient code also in debug-modus!
00229      */
00230     class iterator {
00231       friend class genericMatrix<T>;
00232 
00233 #     ifdef _LTI_MSC_6
00234       friend class const_iterator;
00235 #     else
00236       friend class genericMatrix<T>::const_iterator;
00237 #     endif
00238 
00239     public:
00240       /**
00241        * default constructor
00242        */
00243       iterator() : pos(0), theGenericMatrix(0) {};
00244 
00245       /**
00246        * copy constructor
00247        */
00248       iterator(const iterator& other)
00249         : pos(other.pos),theGenericMatrix(other.theGenericMatrix) {};
00250 
00251       /**
00252        * advance to next item
00253        */
00254       inline iterator& operator++() {++pos; return *this;};   // prefix
00255 
00256       /**
00257        * advance to next item
00258        */
00259       inline iterator operator++(int) {
00260         iterator tmp(*this); pos++; return tmp;
00261       }; // postfix
00262 
00263       /**
00264        * recede to previos item
00265        */
00266       inline iterator& operator--() {--pos; return *this;};   // prefix
00267 
00268       /**
00269        * recede to previos item
00270        */
00271       inline iterator operator--(int) {
00272         iterator tmp(*this); pos--; return tmp;
00273       }; // postfix
00274 
00275       /**
00276        * advance (skip) some elements.
00277        * Use this operator with care! Note that you can skip the end of
00278        * the genericMatrix, and read (or even worse: write!) out of bounds!
00279        */
00280       inline iterator& operator+=(const int n) {pos+=n; return *this;};
00281 
00282       /**
00283        * recede (skip) some elements.
00284        * Use this operator with care! Note that you can skip the end of
00285        * the genericMatrix, and read (or even worse: write!) out of bounds!
00286        */
00287       inline iterator& operator-=(const int n) {pos-=n; return *this;};
00288 
00289       /**
00290        * advance (skip) some elements.
00291        * Use this operator with care! Note that you can skip the end of
00292        * the vector, and read (or even worse: write!) out of bounds!
00293        */
00294       inline iterator operator+(const int n) {
00295         return iterator(pos+n,theGenericMatrix);
00296       };
00297 
00298       /**
00299        * recede (skip) some elements.
00300        * Use this operator with care! Note that you can skip the beginning of
00301        * the vector, and read (or even worse: write!) out of bounds!
00302        */
00303       inline iterator operator-(const int n) {
00304         return iterator(pos-n,theGenericMatrix);
00305       };
00306 
00307       /**
00308        * compare
00309        */
00310       inline bool operator==(const iterator& other) const {
00311         return (pos == other.pos);
00312       };
00313 
00314       /**
00315        * compare
00316        */
00317       inline bool operator!=(const iterator& other) const {
00318         return (pos != other.pos);
00319       };
00320 
00321       /**
00322        * compare if the position of the first iterator is smaller than
00323        * the position of the second iterator
00324        */
00325       inline bool operator<(const iterator& other) const {
00326         return (pos < other.pos);
00327       };
00328 
00329       /**
00330        * compare if the position of the first iterator is greater than
00331        * the position of the second iterator
00332        */
00333       inline bool operator>(const iterator& other) const {
00334         return (pos > other.pos);
00335       };
00336 
00337       /**
00338        * compare if the position of the first iterator is smaller or equal than
00339        * the position of the second iterator
00340        */
00341       inline bool operator<=(const iterator& other) const {
00342         return (pos <= other.pos);
00343       };
00344 
00345       /**
00346        * compare if the position of the first iterator is greater or equal
00347        * than the position of the second iterator
00348        */
00349       inline bool operator>=(const iterator& other) const {
00350         return (pos >= other.pos);
00351       };
00352 
00353       /**
00354        * get pointed data
00355        */
00356       inline T& operator*() {
00357         return theGenericMatrix->at(pos);
00358       };
00359 
00360       /**
00361        * copy member
00362        */
00363       inline iterator& operator=(const iterator& other) {
00364         pos = other.pos;
00365         theGenericMatrix = other.theGenericMatrix;
00366         return *this;
00367       };
00368 
00369     protected:
00370       /**
00371        * for internal use only!!!
00372        * This method does not exist in the release version!
00373        */
00374       inline int getPos() const {return pos;}
00375 
00376       /**
00377        * for internal use only!!!
00378        * This method does not exist in the release version!
00379        */
00380       const genericMatrix<T>* getGenericMatrix() const {
00381         return theGenericMatrix;
00382       }
00383 
00384       /**
00385        * default constructor (for internal use only)
00386        * NEVER USE THIS CONSTRUCTOR, OR YOUR CODE WILL NOT
00387        * COMPILE IN THE RELEASE VERSION!
00388        */
00389       explicit iterator(const int startPos,genericMatrix<T>* vct)
00390         : pos(startPos), theGenericMatrix(vct) {};
00391 
00392     private:
00393       /**
00394        * actual genericMatrix index
00395        */
00396       int pos;
00397 
00398       /**
00399        * pointer to the actual genericMatrix
00400        */
00401       genericMatrix<T>* theGenericMatrix;
00402     };
00403 
00404     /**
00405      * const iterator type (allows read-only operations).
00406      *
00407      * The use of the iterator classes is similar to the iterators of
00408      * the STL (Standard Template Library). See lti::genericMatrix::begin()
00409      * for an example.
00410      *
00411      * For the debugging version of the iterators, boundary check will be
00412      * done!  This explains the low speed of the iterators of the debug
00413      * version.  In the release version, no boundary check will be done,
00414      * and the iterators are sometimes a factor 10 faster than the
00415      * debug iterators.
00416      *
00417      * The use of the access operator at(.) is faster than the iterators
00418      * in the debug version only.  If you need to iterate on a genericMatrix use
00419      * iterators instead (in the release version iterators are approx. a
00420      * factor 3 faster than "at(.)").
00421      *
00422      * Iterators don't work on lined matrices.
00423      *
00424      * CAUTION: Try to use the prefix incremental operator
00425      * (i.e. ++it) instead of the postfix operator (i.e. it++) to
00426      * allow efficient code also in debug-modus!
00427      */
00428     class const_iterator {
00429       friend class genericMatrix<T>;
00430     public:
00431       /**
00432        * default constructor
00433        */
00434       const_iterator() : pos(0), theGenericMatrix(0) {};
00435 
00436       /**
00437        * copy constructor
00438        */
00439       const_iterator(const const_iterator& other) {(*this)=other;};
00440 
00441       /**
00442        * copy constructor
00443        */
00444       const_iterator(const iterator& other) {(*this)=other;};
00445 
00446       /**
00447        * advance to next item  -- prefix
00448        */
00449       inline const_iterator& operator++() {++pos; return *this;};
00450 
00451       /**
00452        * recede to previous item  -- prefix
00453        */
00454       inline const_iterator& operator--() {--pos; return *this;};
00455 
00456       /**
00457        * advance to next item  -- postfix
00458        */
00459       inline const_iterator operator++(int) {
00460         const_iterator tmp(*this); pos++; return tmp;
00461       };
00462 
00463       /**
00464        * recede to previous item  -- postfix
00465        */
00466       inline const_iterator operator--(int) {
00467         const_iterator tmp(*this); pos--; return tmp;
00468       };
00469 
00470       /**
00471        * advance (skip) some elements.
00472        * Use this operator with care! Note that you can skip the end of
00473        * the genericMatrix, and read (or even worse: write!) out of bounds!
00474        */
00475       inline const_iterator& operator+=(const int n) {pos+=n; return *this;};
00476 
00477       /**
00478        * recede (skip) some elements.
00479        * Use this operator with care! Note that you can skip the beginning of
00480        * the genericMatrix, and read (or even worse: write!) out of bounds!
00481        */
00482       inline const_iterator& operator-=(const int n) {pos-=n; return *this;};
00483 
00484       /**
00485        * advance (skip) some elements.
00486        * Use this operator with care! Note that you can skip the end of
00487        * the vector, and read (or even worse: write!) out of bounds!
00488        */
00489       inline const_iterator operator+(const int n) {
00490         return const_iterator(pos+n,theGenericMatrix);
00491       };
00492 
00493       /**
00494        * recede (skip) some elements.
00495        * Use this operator with care! Note that you can skip the beginning of
00496        * the vector, and read (or even worse: write!) out of bounds!
00497        */
00498       inline const_iterator operator-(const int n) {
00499         return const_iterator(pos-n,theGenericMatrix);
00500       };
00501 
00502       /**
00503        * compare
00504        */
00505       inline bool operator==(const const_iterator& other) const {
00506         return (pos == other.pos);
00507       };
00508 
00509       /**
00510        * compare
00511        */
00512       inline bool operator!=(const const_iterator& other) const {
00513         return (pos != other.pos);
00514       };
00515 
00516       /**
00517        * compare
00518        */
00519       inline bool operator==(const iterator& other) const {
00520         return (pos == other.getPos());
00521       };
00522 
00523       /**
00524        * compare
00525        */
00526       inline bool operator!=(const iterator& other) const {
00527         return (pos != other.getPos());
00528       };
00529 
00530       /**
00531        * compare if the position of the first iterator is smaller than
00532        * the position of the second iterator
00533        */
00534       inline bool operator<(const iterator& other) const {
00535         return (pos < other.pos);
00536       };
00537 
00538       /**
00539        * compare if the position of the first iterator is greater than
00540        * the position of the second iterator
00541        */
00542       inline bool operator>(const iterator& other) const {
00543         return (pos > other.pos);
00544       };
00545 
00546       /**
00547        * compare if the position of the first iterator is smaller or equal than
00548        * the position of the second iterator
00549        */
00550       inline bool operator<=(const iterator& other) const {
00551         return (pos <= other.pos);
00552       };
00553 
00554       /**
00555        * compare if the position of the first iterator is greater or equal
00556        * than the position of the second iterator
00557        */
00558       inline bool operator>=(const iterator& other) const {
00559         return (pos >= other.pos);
00560       };
00561 
00562       /**
00563        * compare if the position of the first iterator is smaller than
00564        * the position of the second iterator
00565        */
00566       inline bool operator<(const const_iterator& other) const {
00567         return (pos < other.pos);
00568       };
00569 
00570       /**
00571        * compare if the position of the first iterator is greater than
00572        * the position of the second iterator
00573        */
00574       inline bool operator>(const const_iterator& other) const {
00575         return (pos > other.pos);
00576       };
00577 
00578       /**
00579        * compare if the position of the first iterator is smaller or equal than
00580        * the position of the second iterator
00581        */
00582       inline bool operator<=(const const_iterator& other) const {
00583         return (pos <= other.pos);
00584       };
00585 
00586       /**
00587        * compare if the position of the first iterator is greater or equal
00588        * than the position of the second iterator
00589        */
00590       inline bool operator>=(const const_iterator& other) const {
00591         return (pos >= other.pos);
00592       };
00593 
00594       /**
00595        * get pointed data
00596        */
00597       inline const T& operator*() {return theGenericMatrix->at(pos);};
00598 
00599       /**
00600        * copy member
00601        */
00602       inline const_iterator& operator=(const const_iterator& other) {
00603         pos = other.pos;
00604         theGenericMatrix = other.theGenericMatrix;
00605         return *this;
00606       };
00607 
00608       /**
00609        * copy member
00610        */
00611       inline const_iterator& operator=(const iterator& other) {
00612         pos = other.getPos();
00613         theGenericMatrix = other.getGenericMatrix();
00614         return *this;
00615       };
00616 
00617     protected:
00618       /**
00619        * protected constructor
00620        * DO NOT EXPLICITLY USE THIS CONSTRUCTOR. OTHERWISE YOUR CODE WILL NOT
00621        * COMPILE IN THE RELEASE VERSION!
00622        */
00623       explicit const_iterator(const int startPos,const genericMatrix<T>* vct)
00624         : pos(startPos), theGenericMatrix(vct) {};
00625 
00626     private:
00627       /**
00628        * actual genericMatrix index
00629        */
00630       int pos;
00631 
00632       /**
00633        * pointer to the actual genericMatrix
00634        */
00635       const genericMatrix<T>* theGenericMatrix;
00636     };
00637 
00638 #   endif
00639 
00640     /**
00641      * Default constructor creates an empty genericMatrix
00642      */
00643     genericMatrix();
00644 
00645     /**
00646      * Create a <code>rows x cols</code> genericMatrix and
00647      * initializes all elements with \c iniValue.
00648      *
00649      * @param rows number of rows of the genericMatrix
00650      * @param cols number of columns of the genericMatrix
00651      * @param iniValue all elements will be initialized with this value
00652      */
00653     genericMatrix(const int rows,const int cols,const T& iniValue = T());
00654 
00655     /**
00656      * Create a <code>rows x cols</code> genericMatrix and initialize all 
00657      * elements with the data pointed by <code>data</code>.
00658      *
00659      * The first <code>cols</code>-elements of the data will be copied on the
00660      * first row, the next ones on the second row and so on.
00661      *
00662      * @param rows number of rows of the genericMatrix
00663      * @param cols number of columns of the genericMatrix
00664      * @param data pointer to the memory block with the data to be initialized
00665      *            with.
00666      */
00667     genericMatrix(const int rows,const int cols,const T data[]);
00668 
00669     /**
00670      * This constructor creates a connected <code>size.y x size.x</code>
00671      * GenericMatrix and initializes all elements with <code>iniValue</code>
00672      *
00673      * @param size lti::point with the size of the genericMatrix
00674      *             (size.x is the number of columns and
00675      *              size.y the number of rows)
00676      * @param iniValue all elements will be initialized with this value
00677      */
00678     genericMatrix(const ipoint& size,const T& iniValue = T());
00679 
00680     /**
00681      * Copy constructor.
00682      *
00683      * create this genericMatrix as a connected copy of another
00684      * genericMatrix for this const version, the data will be always
00685      * copied!  It is also possible to create a copy of a
00686      * subgenericMatrix of another genericMatrix.
00687      *
00688      * @param other the genericMatrix to be copied.
00689      * @param fromRow initial row of the other genericMatrix to be copied
00690      * @param toRow last row to be copied of the other genericMatrix
00691      * @param fromCol initial column of the other genericMatrix to be copied
00692      * @param toCol last column to be copied of the other genericMatrix
00693      *
00694      * Example:
00695      * \code
00696      * lti::genericMatrix<int> m(4,6,0); // integer genericMatrix with 25 
00697      *                                   // elements
00698      * // ...
00699      * // initialize GenericMatrix with:
00700      * //        0  1  2  3  4  5
00701      * //        2  1  5  4  0  3
00702      * //        1  2  1  2  3  2
00703      * //        3  3  2  1  2  3
00704      *
00705      * lti::genericMatrix<int> sm(m,0,2,1,3)  // last line will lead to
00706      * //                                 following contents in sm:
00707      * //        1  2  3
00708      * //        1  5  4
00709      * //        2  1  2
00710      * \endcode
00711      */
00712     genericMatrix(const genericMatrix<T>& other,
00713                   const int fromRow=0,const int toRow=MaxInt32,
00714                   const int fromCol=0,const int toCol=MaxInt32);
00715 
00716     /**
00717      * If \a init is true this constructor is equivalent to calling
00718      * genericMatrix(const int rows, const int cols), and thus initializing
00719      * all elements with T(). However, in some cases the elements need
00720      * not be initialized during construction, since complex
00721      * initializion is required. Especially for large matrices, the
00722      * unnecessary constructor initialization is very time consuming.
00723      *
00724      * If \a init is false, memory is allocated but no initialization
00725      * takes place. Thus the following is equivalent:
00726      * \code
00727      * genericMatrix<int> a(false,100,100);
00728      *
00729      * genericMatrix<int> a;
00730      * a.resize(100,100,0,false,false);
00731      * \endcode
00732      *
00733      * @param init initialize genericMatrix or not
00734      * @param rows number of rows of the genericMatrix
00735      * @param cols number of columns of the genericMatrix
00736      */
00737     genericMatrix(const bool init, const int rows, const int cols);
00738 
00739     /**
00740      * If \a init is true this constructor is equivalent to calling
00741      * genericMatrix(const int rows, const int cols), and thus initializing
00742      * all elements with T(). However, in some cases the elements need
00743      * not be initialized during construction, since complex
00744      * initializion is required. Especially for large matrices, the
00745      * unnecessary constructor initialization is very time consuming.
00746      *
00747      * If \a init is false, memory is allocated but no initialization
00748      * takes place. Thus the following is equivalent:
00749      * \code
00750      * genericMatrix<int> a(false,100,100);
00751      *
00752      * genericMatrix<int> a;
00753      * a.resize(100,100,0,false,false);
00754      * \endcode
00755      *
00756      * @param init initialize genericMatrix or not
00757      * @param size desired size for the genericMatrix
00758      */
00759     genericMatrix(const bool init, const ipoint& size);
00760 
00761     /**
00762      * copy constructor.
00763      *
00764      * create this genericMatrix as a connected copy of another genericMatrix
00765      * taking only the rows indicated by the vector.
00766      * for this const version, the data will be always copied!
00767      * Multiple occurence of one row index in <code>rows</code> is allowed.
00768      *
00769      * @param other the genericMatrix to be copied.
00770      * @param rows inidices of the rows to be copied
00771      *
00772      * Example:
00773      * \code
00774      * lti::vector<int> rows(2);
00775      * // initialize with
00776      * // 1 3
00777      * lti::genericMatrix<int> m(4,6,0); // integer genericMatrix with 25
00778      *                                   // elements
00779      * // ...
00780      * // initialize GenericMatrix with:
00781      * //        0  1  2  3  4  5
00782      * //        2  1  5  4  0  3
00783      * //        1  2  1  2  3  2
00784      * //        3  3  2  1  2  3
00785      *
00786      * lti::genericMatrix<int> sm(m,rows)     // last line will lead to
00787      * //                                 following contents in sm:
00788      * //        2  1  5  4  0  3
00789      * //        3  3  2  1  2  3
00790      * \endcode
00791      */
00792     genericMatrix(const genericMatrix<T>& other,
00793                   const genericVector<int>& rows);
00794 
00795     /**
00796      * copy constructor (reference to a submatrix).
00797      * creates submatrix of another matrix.
00798      *
00799      * if <code>copyData == true</code>, the new object has its own data
00800      * (equivalent to previous copy constructor).
00801      *
00802      * if <code>copyData == false</code>, the new object has references to
00803      * the other matrix, which means that the data is not necessarily
00804      * consecutive.  (This will not be a connected but a lined matrix)
00805      *
00806      * Those algorithms which use direct access to the matrix memory block
00807      * should check first if the memory lies in a consecutive block!
00808      * (see getMode())
00809      *
00810      * @param copyData should the data of the other matrix be copied or not
00811      * @param other the matrix with the data to be copied or to be shared
00812      * @param fromRow initial row of the other matrix to be copied
00813      * @param toRow last row to be copied of the other matrix
00814      * @param fromCol initial column of the other matrix to be copied
00815      * @param toCol last column to be copied of the other matrix
00816      */
00817     genericMatrix(const bool copyData, genericMatrix<T>& other,
00818                   const int fromRow=0,const int toRow=MaxInt32,
00819                   const int fromCol=0,const int toCol=MaxInt32);
00820 
00821     /**
00822      * destructor
00823      */
00824     virtual ~genericMatrix();
00825 
00826     /**
00827      * returns the name of this class: "genericMatrix"
00828      */
00829     virtual const char* getTypeName() const;
00830 
00831     /**
00832      * owns this %object the data?
00833      * returns <em>false</em> if this genericMatrix contains a reference to
00834      *         extern data.
00835      */
00836     inline bool ownsData() const;
00837 
00838     /**
00839      * If this object does not own its data, this member will create a
00840      * new memory buffer with the same data and will make this
00841      * genericMatrix as its owner.  You can also be sure, that the new
00842      * memory block will be connected (see also getMode() ).  If this
00843      * genericMatrix already owns its data nothing happens.
00844      */
00845     void restoreOwnership();
00846 
00847     /**
00848      * Reference to extern data.
00849      *
00850      * This member allows the use of this %object as an access-functor for
00851      * the 'data'. An access to the element at (r,c) is equivalent to
00852      * data[r*columns() + c].
00853      * The user must take care for memory allocation and deallocation:
00854      * this object will never delete the data!.
00855      * If <em>rows</em> and <em>cols</em> are invalid dimensions, the
00856      * behaviour will be unpredictible.
00857      * @param rows number of rows
00858      * @param cols number of columns
00859      * @param data a pointer to the memory block to be used
00860      *
00861      * For an example see attach()
00862      */
00863     void useExternData(const int rows,const int cols,T* data);
00864 
00865     /**
00866      * Attach extern data to the genericMatrix.
00867      *
00868      * This member allows the use of this %object as an access-functor for
00869      * the 'data'. An access to the element at (r,c) is equivalent to
00870      * data[r*columns() + c].
00871      * If <em>rows</em> and <em>cols</em> are invalid dimensions, the
00872      * behaviour will be unpredictible.
00873      *
00874      * The memory will be administrated by this genericMatrix object,
00875      * and may be deleted if required (genericMatrix deleted or
00876      * resized!).  The user should not try to manipulate the memory
00877      * allocation of the data after the attachment!  See also
00878      * useExternData().
00879      *
00880      * @param rows number of rows
00881      * @param cols number of columns
00882      * @param data a pointer to the memory block to be used
00883      *
00884      * Example:
00885      * \code
00886      * lti::genericMatrix<int> myMat;
00887      * int block1[25];
00888      * int* block2;
00889      * block2 = new int[25];
00890      *
00891      * myMat.useExternData(5,5,block1); // ok
00892      * myMat.attach(5,5,block1); // wrong!!! genericMatrix will try 
00893      *                           // to manipulate stack memory: 
00894      *                           // DO NOT DO THIS!!!!!
00895      * myMat.attach(5,5,block2); // ok!  but do not try to delete the memory
00896      *                           //      block2!!
00897      * \endcode
00898      */
00899     void attach(const int rows,const int cols,T* data);
00900 
00901     /**
00902      * Free the data of this object and hand it over to the
00903      * "receiver". The value of ownsData is also transfered to the
00904      * receiver. (see Note).
00905      *
00906      * This function makes a "memory block transfusion" to another
00907      * genericMatrix.  It is a very efficient way to make a copy of
00908      * this genericMatrix, if you don't need the source data anymore!
00909      *
00910      * \b Note: This method will fail if this genericMatrix is not
00911      * connected. Also take care that if the attach() or
00912      * useExternData() methods of this genericMatrix have been called before
00913      * detachment, the same rules for memory management apply now for
00914      * the receiver.
00915      *
00916      * At the end of the detachment, this genericMatrix will be empty.
00917      *
00918      * @param receiver the genericMatrix which will receive the memory
00919      *        block.  All data of that genericMatrix will be first deleted!
00920      */
00921     void detach(genericMatrix<T>& receiver);
00922 
00923     /**
00924      * Free the data of this object and hand it over to the
00925      * "receiver". The value of ownsData is also transfered to the
00926      * receiver. (see Note).
00927      *
00928      * This function makes a "memory block transfusion" to a vector by
00929      * concatenating the rows of the genericMatrix.  It is a very efficient
00930      * way to move the data of this genericMatrix into a vector, if you don't
00931      * need the source data anymore!
00932      *
00933      * \b Note: This method will fail if this genericMatrix is not
00934      * connected. Also take care that if the attach() or
00935      * useExternData() methods of this genericMatrix have been called before
00936      * detachment, the same rules for memory management apply now for
00937      * the receiver.
00938      *
00939      * At the end of the detachment, this genericMatrix will be empty.
00940      * @param receiver the genericMatrix which will receive the memory block.
00941      *                 All data of that genericMatrix will be first deleted!
00942      */
00943     void detach(genericVector<T>& receiver);
00944 
00945     /**
00946      * \deprecated Please use swap instead
00947      */
00948     void exchange(genericMatrix<T>& other);
00949 
00950     /**
00951      * Exchange (in a fast way) the data between this and the other 
00952      * genericMatrix.
00953      *
00954      * Similar to detach(), this method will exchange the complete memory
00955      * blocks, avoiding an element-wise copy.
00956      * @param other the genericMatrix with which the data will be exchanged.
00957      */
00958     void swap(genericMatrix<T>& other);
00959 
00960     /**
00961      * Data storage mode.
00962      *
00963      * Returns the data storage mode, which can be:
00964      * - <code>Connected</code> if the memory is a single block, or
00965      * - <code>Line</code>      if the memory of each line is allocated
00966      *                          in different places.
00967      *
00968      * For the lined-matrices the interators will not work.  You can however
00969      * iterate on each individual row, which are always connected.
00970      *
00971      * The only possible way to get a lined-matrix is as a submatrix of
00972      * another one, using the appropriate constructor:
00973      * genericMatrix(const bool, genericMatrix<T>&,const int,const int,
00974      * const int,const int)
00975      */
00976     inline eStoreMode getMode() const;
00977 
00978     /**
00979      * number of rows of the genericMatrix
00980      */
00981     inline int rows() const;
00982 
00983     /**
00984      * number of columns of the genericMatrix
00985      */
00986     inline int columns() const;
00987 
00988     /**
00989      * index of the last row (rows()-1)
00990      */
00991     inline int lastRow() const;
00992 
00993     /**
00994      * Index of the last columns (columns()-1)
00995      */
00996     inline int lastColumn() const;
00997 
00998     /**
00999      * Number of "physical" rows of the matrix.
01000      *
01001      * @return If this is a <code>Connected</code> Matrix, (see getMode()),
01002      * this member returns the same value as rows().
01003      *
01004      * If this is a <code>Line</code> Matrix, this value is bigger or equal
01005      * than rows().  If this was created with the copy constructor for a
01006      * submatrix with "no copy data", this value will return the size of the
01007      * original matrix.
01008      */
01009     inline int metaRows() const;
01010 
01011     /**
01012      * Number of "physical" columns of the matrix.
01013      *
01014      * @return If this is a <code>Connected</code> Matrix, (see getMode()),
01015      * this member returns the same value as columns().
01016      *
01017      * If this is a <code>Line</code> Matrix, this value is bigger or equal
01018      * than columns().  If this was created with the copy constructor for a
01019      * submatrix with "no copy data", this value will return the number of
01020      * columns of the original matrix.
01021      */
01022     inline int metaColumns() const;
01023 
01024     /**
01025      * return type of the size() member
01026      */
01027     typedef ipoint size_type;
01028 
01029     /**
01030      * returns the size of the %genericMatrix in a lti::point structure.
01031      * @return lti::point with the number of columns in its
01032      *         <code>x</code> coordinate and the number of rows in its
01033      *         <code>y</code> coordinate.
01034      */
01035     inline const size_type& size() const;
01036 
01037     /**
01038      * returns iterator to the begin of the genericMatrix
01039      * The use of the interators is similar to the iterators of the
01040      * Standard Template Library (STL).
01041      * If you need to iterate on all elements of the genericMatrix, you can
01042      * use following code:
01043      * \code
01044      * int tmp,accu;                  // a temporal variable
01045      * lti::genericMatrix<int> myMat(10,8,1); // a vector with 10 elements
01046      * lti::genericMatrix<int>::iterator it;  // an iterator
01047      *
01048      * for (it=myMat.begin();it!=myMat.end();it++) {
01049      *   tmp = *it;                   // tmp has value of element pointed
01050      *                                // by the iterator.
01051      *   accu += tmp;
01052      *   (*it) = accu;                // change the value in the genericMatrix.
01053      * }
01054      * \endcode
01055      * Please note that if you define <code>it</code> as a const_iterator,
01056      * you can not make something like <code>*it=accu</code>.
01057      */
01058     inline iterator begin();
01059 
01060     /**
01061      * returns first element of the genericMatrix as a const_iterator.
01062      * Note that you can not change the values of the genericMatrix
01063      * elements when you use a const_iterator. See also begin() for an example
01064      */
01065     inline const_iterator begin() const;
01066 
01067     /**
01068      * returns iterator to the end of the genericMatrix
01069      */
01070     inline iterator end();
01071 
01072     /**
01073      * returns iterator to the end of the genericMatrix
01074      */
01075     inline const_iterator end() const;
01076 
01077 
01078     /**
01079      * This method returns an iterator that points to the \b last
01080      * valid element of the genericMatrix. It is used for inverse order
01081      * iteration through the genericMatrix using normal iterators (as opposed
01082      * to reverse iterators). This has the advantage that iterators
01083      * going from front to end and in the inverse direction are the
01084      * same and can thus be compared, copied etc. Further the
01085      * implementation of reverse_iterators is not as fast as that of
01086      * iterators and thus not desired in the LTI-Lib.
01087      *
01088      * See lti::genericVector<T>::inverseBegin() for an example.
01089      */
01090     inline iterator inverseBegin();
01091 
01092     /**
01093      * This method returns an iterator that points to the \b last
01094      * valid element of the genericMatrix. See inverseBegin() for more details.
01095      */
01096     inline const_iterator inverseBegin() const;
01097 
01098     /**
01099      * This method returns an iterator that points to the element \b
01100      * before the \b first valid element of the genericMatrix. It is used to
01101      * mark the end for inverse order iteration through the genericMatrix
01102      * using normal iterators (as opposed to reverse iterators). This
01103      * has the advantage that iterators going from front to end and in
01104      * the inverse direction are the same and can thus be compared,
01105      * copied etc.
01106      */
01107     inline iterator inverseEnd();
01108 
01109     /**
01110      * This method returns an iterator that points to the element \b
01111      * before the \b first valid element of the genericMatrix.
01112      */
01113     inline const_iterator inverseEnd() const;
01114 
01115     /**
01116      * change the dimensions of the genericMatrix.
01117      * @param newRows new number of rows
01118      * @param newCols new number of columns
01119      * @param iniValue the initialization value.
01120      * @param copyData if this parameter is true, the old data of the
01121      *                 genericMatrix will be copied.  If it is false, the old 
01122      *                 data will be lost.
01123      * @param initNew  if this parameter is true, then all new elements (if
01124      *                 they exist) will be initialized with
01125      *                 <code>iniValue</code>.
01126      *                 if <code>initNew</code> is false, then the new
01127      *                 elements are left uninitialized.
01128      *
01129      * For example:
01130      * \code
01131      *   lti::genericMatrix<int> myMat;  // creates empty genericMatrix
01132      *   myMat.resize(5,5,0);     // genericMatrix with 5x5 elements 
01133      *                            // initialized with 0
01134      *   myMat.resize(10,7,2);    // genericMatrix has now 10x7 elements: the
01135      *                            // subgenericMatrix 5x5 at (0,0) has still 0s
01136      *                            // and the rest have a 2
01137      *   myMat.resize(20,10,0,false,false); // now the genericMatrix has 20
01138      *                                      // elements but their values
01139      *                                      // are unknown.
01140      *   myMat.resize(5,5,1,false,true); // the genericMatrix has now 5x5
01141      *                                   // elements all initialized with 1
01142      *
01143      *   // note that the last line could also be written:
01144      *
01145      *   myMat.resize(5,5,1,false);
01146      *
01147      * \endcode
01148      *
01149      * If the new size is not equal to the old size, the genericMatrix
01150      * always owns the data afterwards (i.e. new memory is allocated)
01151      * even if it didn't own the data before. Otherwise the ownership
01152      * remains unchanged. You can use restoreOwnership() if you just
01153      * want to own the data.
01154      */
01155     void resize(const int newRows,const int newCols,
01156                 const T& iniValue = T(),
01157                 const bool copyData=true,
01158                 const bool initNew=true);
01159 
01160     /**
01161      * clears the genericMatrix (at the end this will be an empty
01162      * genericMatrix)
01163      */
01164     void clear();
01165 
01166     /**
01167      * returns true if the genericMatrix is empty
01168      */
01169     bool empty() const;
01170 
01171     /**
01172      * change the dimensions of the genericMatrix.
01173      * @param newDim   new dimensions of the genericMatrix
01174      * @param iniValue the initialization value.
01175      * @param copyData if this parameter is true, the old data of the
01176      *                 genericMatrix will be copied.  If it is false, the old
01177      *                  data will be lost.
01178      * @param initNew  if this parameter is true, then all new elements (if
01179      *                 they exist) will be initialized with
01180      *                 <code>iniValue</code>.
01181      *                 if <code>initNew</code> is false, then the new
01182      *                 elements are left uninitialized.
01183      *
01184      * If the resize is possible (see useExternData()), this %object
01185      * will always owns the data afterwards!
01186      *
01187      * This is equivalent to call
01188      *   resize(newDim.y,newDim.x,iniValue,copyData,initNew)
01189      */
01190     inline void resize(const ipoint& newDim,
01191                        const T& iniValue = T(),
01192                        const bool copyData=true,
01193                        const bool initNew=true);
01194 
01195     /**
01196      * fills genericMatrix elements with <code>iniValue</code>.
01197      * The fill "area" is limited by <code>fromCol</code>,<code>toCol</code>,
01198      * <code>fromRow</code> and <code>toRow</code>.
01199      * If these values are out of bounds, they will be (internally)
01200      * adjusted to correct values.
01201      *
01202      * WARNING: Syntax changed for the parameters:  the old version was
01203      *          fromRow,toRow, fromCol,toCol.
01204      *          Now the order is:
01205      *          fromRow,FROMCOL,TOROW,toCol.  This allow to give the
01206      *          first coordinates only!
01207      *
01208      * @param iniValue the elements will be initialized with this value
01209      * @param fromRow  first row of the subgenericMatrix to be filled
01210      * @param fromCol  first column of the subgenericMatrix to be filled
01211      * @param toRow    last row of the subgenericMatrix to be filled
01212      * @param toCol    last column of the subgenericMatrix to be filled
01213     */
01214     void fill(const T& iniValue,
01215               const int fromRow=0,
01216               const int fromCol=0,
01217               const int toRow=MaxInt32,
01218               const int toCol=MaxInt32);
01219 
01220     /**
01221      * fills genericMatrix elements with <code>iniValue</code>.
01222      * The fill "area" is limited by <code>from</code> and <code>to</code>
01223      * points
01224      * If these values are out of bounds, they will be (internally)
01225      * adjusted to correct values.
01226      *
01227      * @param iniValue the elements will be initialized with this value
01228      * @param from first position of the subgenericMatrix to be filled
01229      * @param to   last row of the subgenericMatrix to be filled
01230     */
01231     inline void fill(const T& iniValue,
01232                      const ipoint& from,
01233                      const ipoint& to=point(MaxInt32,MaxInt32));
01234 
01235     /**
01236      * fills genericMatrix elements with <code>iniValue</code>.
01237      * The fill "area" is limited by <code>window</code>.
01238      * If these values are out of bounds, they will be (internally)
01239      * adjusted to correct values.
01240      * @param iniValue the elements will be initialized with this value
01241      * @param window   the window to be filled
01242      */
01243     inline void fill(const T& iniValue,const irectangle& window);
01244 
01245     /**
01246      * fills genericMatrix elements with the data pointed by <code>data</code>.
01247      * The fill "area" is limited by <code>fromCol</code>,<code>toCol</code>,
01248      * <code>fromRow</code> and <code>toRow</code>.
01249      * If these values are out of bounds, they will be (internally)
01250      * adjusted to correct values.
01251      *
01252      * WARNING: Syntax changed for the parameters:  the old version was
01253      *          fromRow,toRow, fromCol,toCol.
01254      *          Now the order is:
01255      *          fromRow,FROMCOL,TOROW,toCol.  This allow to give the
01256      *          first coordinates only!
01257      *
01258      * @param data     pointer to the data to be copied.
01259      * @param fromRow  first row of the subgenericMatrix to be filled
01260      * @param fromCol  first column of the subgenericMatrix to be filled
01261      * @param toRow    last row of the subgenericMatrix to be filled
01262      * @param toCol    last column of the subgenericMatrix to be filled
01263      */
01264     void fill(const T data[],
01265               const int fromRow=0,
01266               const int fromCol=0,
01267               const int toRow=MaxInt32,
01268               const int toCol=MaxInt32);
01269 
01270     /**
01271      * fills genericMatrix elements with the data pointed by <code>data</code>.
01272      * The fill "area" is limited by <code>fromCol</code>,<code>toCol</code>,
01273      * <code>fromRow</code> and <code>toRow</code>.
01274      * If these values are out of bounds, they will be (internally)
01275      * adjusted to correct values.
01276      *
01277      * @param data  pointer to the data to be copied.
01278      * @param from  first position of the subgenericMatrix to be filled
01279      * @param to    last position of the subgenericMatrix to be filled
01280      */
01281     inline void fill(const T data[],
01282                      const ipoint& from,
01283                      const ipoint& to=point(MaxInt32,MaxInt32));
01284 
01285     /**
01286      * fills genericMatrix elements with <code>iniValue</code>.
01287      * The fill "area" is limited by <code>window</code>.
01288      * If these values are out of bounds, they will be (internally)
01289      * adjusted to correct values.
01290      * @param data   pointer to the data to be copied
01291      * @param window the window to be filled
01292      */
01293     inline void fill(const T data[],const irectangle& window);
01294 
01295     /**
01296      * fills this genericMatrix between the "from's" and "to's" with
01297      * the contents of the genericMatrix <code>mat</code> starting at
01298      * <code>startAtRow</code> and <code>startAtCol</code>
01299      *
01300      * WARNING: Syntax changed for the parameters:  the old version was
01301      *          fromRow,toRow, fromCol,toCol.
01302      *          Now the order is:
01303      *          fromRow,FROMCOL,TOROW,toCol.  This allow to give the
01304      *          from coordinates only!
01305      *
01306      * @param mat      the genericMatrix with the data to be copied
01307      * @param fromRow  first row of the subgenericMatrix to be filled
01308      * @param fromCol  first column of the subgenericMatrix to be filled
01309      * @param toRow    last row of the subgenericMatrix to be filled
01310      * @param toCol    last column of the subgenericMatrix to be filled
01311      * @param startAtRow starting row of <code>mat</code> where the data is
01312      *                   located.
01313      * @param startAtCol starting column of <code>mat</code> where the data
01314      *                   is located.
01315      */
01316     void fill(const genericMatrix<T>& mat,
01317               const int fromRow=0,
01318               const int fromCol=0,
01319               const int toRow=MaxInt32,
01320               const int toCol=MaxInt32,
01321               const int startAtRow=0,
01322               const int startAtCol=0);
01323 
01324     /**
01325      * fills this genericMatrix between the "from's" and "to's" with
01326      * the contents of the genericMatrix <code>mat</code> starting at
01327      * <code>startAtRow</code> and <code>startAtCol</code>
01328      *
01329      * @param mat     the genericMatrix with the data to be copied
01330      * @param from    first position of the subgenericMatrix to be filled
01331      * @param to      last position of the subgenericMatrix to be filled
01332      * @param startAt starting position of <code>mat</code> where the data is
01333      *                located.
01334      */
01335     inline void fill(const genericMatrix<T>& mat,
01336                      const ipoint& from,
01337                      const ipoint& to=point(MaxInt32,MaxInt32),
01338                      const ipoint& startAt=point(0,0));
01339 
01340     /**
01341      * fills the region of this genericMatrix specified by
01342      * <code>window</code> with the contents of the genericMatrix
01343      * <code>mat</code> starting at <code>start</code>.  If these
01344      * values are out of bounds, they will be (internally) adjusted to
01345      * correct values.
01346      *
01347      * @param mat    pointer to the data to be copied
01348      * @param window the window to be filled
01349      * @param start  the start position of the region to be copied of the
01350      *               genericMatrix <code>mat</code>
01351      */
01352     inline void fill(const genericMatrix<T>& mat,
01353                      const irectangle& window,
01354                      const ipoint& start=point(0,0));
01355 
01356     /**
01357      * access element at the given row and column
01358      * @param row the row of the element
01359      * @param col the column of the element
01360      * @return a reference to the genericMatrix element
01361      */
01362     inline T& at(const int row, const int col);
01363 
01364     /**
01365      * read-only access at the given row and column
01366      * @param row the row of the element
01367      * @param col the column of the element
01368      * @return a reference to the genericMatrix element
01369      */
01370     inline const T& at(const int row, const int col) const;
01371 
01372     /**
01373      * access element at the given position.
01374      * (can be used only in connected matrices.  See constructors for
01375      *  more information on this.)
01376      *
01377      * With this operator the genericMatrix can be accessed as a
01378      * vector, where the rows of the genericMatrix are concatenated.
01379      * The access to the genericMatrix with at(row,col) is equivalent
01380      * to at(row*columns()+col).
01381      *
01382      * @param pos the index of the element of the genericMatrix
01383      * @return a reference to the genericMatrix element
01384      */
01385     inline T& at(const int pos);
01386 
01387     /**
01388      * access element at the given position.
01389      * (can be used only in connected matrices.  See constructors for
01390      *  more information on this.)
01391      *
01392      * With this operator the genericMatrix can be accessed as a
01393      * vector, where the rows of the genericMatrix are concatenated.
01394      * The access to the genericMatrix with at(row,col) is equivalent
01395      * to at(row*columns()+col).
01396      *
01397      * @param pos the index of the element of the genericMatrix
01398      * @return a reference to the genericMatrix element
01399      */
01400     inline const T& at(const int pos) const;
01401 
01402     /**
01403      * access operator of genericMatrix element as a point in a 2D-Map
01404      *
01405      * @param p position of the element (this is equivalent to at(p.y,p.x))
01406      * @return a reference to the genericMatrix element
01407      */
01408     inline T& at(const ipoint& p);
01409 
01410     /**
01411      * const access operator of genericMatrix element as a point in a 2D-Map
01412      *
01413      * @param p position of the element (this is equivalent to at(p.y,p.x))
01414      * @return a const reference to the vector element
01415      */
01416     inline const T& at(const ipoint& p) const;
01417 
01418     /**
01419      * return genericMatrix-row as a vector.
01420      *
01421      * This method works fast, since it returns a reference to the row vector.
01422      * The data will NOT be copied.
01423      *
01424      * @param row the row to be accessed
01425      * @return a reference to the vector row
01426      */
01427     inline genericVector<T>& getRow(const int row);
01428 
01429     /**
01430      * return genericMatrix-row as a const vector.
01431      * This method works fast, since it returns a reference to the row vector.
01432      * The data will NOT be copied.
01433      *
01434      * @param row the row to be accessed
01435      * @return a const reference to the vector row
01436      */
01437     inline const genericVector<T>& getRow(const int row) const;
01438 
01439     /**
01440      * alias for getRow()
01441      */
01442     inline genericVector<T>& operator[](const int row);
01443 
01444     /**
01445      * alias for getRow()
01446      */
01447     inline const genericVector<T>& operator[](const int row) const;
01448 
01449     /**
01450      * Copy a row vector in the given parameter.
01451      *
01452      * This method copies the data of a given row of the genericMatrix
01453      * in the given vector.
01454      *
01455      * @param row the number of the row to be copied
01456      * @param theRow the vector, where the data will be copied
01457      * @see getRow()
01458      */
01459     inline void getRowCopy(const int row,genericVector<T>& theRow) const;
01460 
01461     /**
01462      * return genericMatrix-row as a vector.
01463      * This method copies the data of the genericMatrix, therefore is not as
01464      * fast as getRow()
01465      * @param row the number of tthe row to be copied
01466      * @return a vector with the contents of the row of the genericMatrix
01467      */
01468     inline genericVector<T> getRowCopy(const int row) const;
01469 
01470     /**
01471      * return genericMatrix-column as a vector.
01472      * This method copies the data of the genericMatrix, therefore is not as
01473      * fast as getRow()
01474      * @param col the number of the column to be copied
01475      * @param theCol a vector, where the column vector of the genericMatrix
01476      *               should be copied.
01477      */
01478     void getColumnCopy(const int col,genericVector<T>& theCol) const;
01479 
01480     /**
01481      * return genericMatrix-column as a vector.
01482      * This method copies the data of the genericMatrix, therefore is not as
01483      * fast as getRow()
01484      * @param col the number of the column to be copied
01485      * @return a vector with the contents of the column of the genericMatrix
01486      */
01487     inline genericVector<T> getColumnCopy(const int col) const;
01488 
01489     /**
01490      * Return the diagonal elements of the genericMatrix as vector.
01491      * This method copies the diagonal elements of the genericMatrix into
01492      * the vector. If the genericMatrix is non-symmetrical, the vector will
01493      * be of dimension min(rows(),columns()).
01494      * @param diag a vector, where the diagonal of the genericMatrix
01495      *             should be copied.
01496      */
01497     void getDiagonal(genericVector<T>& diag) const;
01498 
01499     /**
01500      * Return the diagonal elements of the genericMatrix as vector.
01501      * This method copies the diagonal elements of the genericMatrix into
01502      * the vector. If the genericMatrix is non-symmetrical, the vector will
01503      * be of dimension min(rows(),columns()).
01504      * @return a vector with the diagonal elements of the genericMatrix.
01505      */
01506     inline genericVector<T> getDiagonal() const;
01507 
01508     /**
01509      * Sets the diagonal of the genericMatrix to the values given in
01510      * the genericVector \a diag. Let \c r be the number of rows and
01511      * \c c be the number of columns of the matrix. Then \c minRC is
01512      * \c min(r,c). Also let \c d be the size of \a diag. Only \c
01513      * min(minRC,d) values are copied from \a diag. If \c d is smaller
01514      * than \c minRC the remaining values on the diagonal are left
01515      * untouched. The copying always starts at (0,0) of the matrix.
01516      *
01517      * @param diag values to be copied into the diagonal of the matrix
01518      */
01519     void setDiagonal(const genericVector<T>& diag);
01520 
01521     /**
01522      * copy the data of a vector in a given row
01523      * @param row the row that receives the data.
01524      * @param theRow the vector with the data to be copied
01525      */
01526     inline void setRow(const int row,const genericVector<T>& theRow);
01527 
01528     /**
01529      * copy the data of a vector in a given column
01530      * @param col the column that receives the data.
01531      * @param theCol the vector with the data to be copied
01532      */
01533     void setColumn(const int col,const genericVector<T>& theCol);
01534 
01535     /**
01536      * copy the contents of <code>other</code> in this object.
01537      *
01538      * The result of the copy is always a connected genericMatrix.
01539      * I.e. you cannot copy the sub-genericMatrix property of another
01540      * genericMatrix.
01541      *
01542      * @param other the other genericMatrix to be copied
01543      */
01544     genericMatrix<T>& copy(const genericMatrix<T>& other);
01545 
01546     /**
01547      * copy the contents of the rectangle described by the indices of
01548      * <code>other</code> into this object.
01549      *
01550      * The result of the copy is always a connected genericMatrix.
01551      * I.e. you cannot copy the sub-genericMatrix property of another
01552      * genericMatrix. 
01553      *
01554      * @param other the other genericMatrix to be copied
01555      * @param fromRow initial row of the other genericMatrix to be copied
01556      * @param toRow last row to be copied of the other genericMatrix
01557      * @param fromCol initial column of the other genericMatrix to be copied
01558      * @param toCol last column to be copied of the other genericMatrix
01559      */
01560     genericMatrix<T>& copy(const genericMatrix<T>& other,
01561                            const int fromRow,
01562                            const int toRow=MaxInt32,
01563                            const int fromCol=0,
01564                            const int toCol=MaxInt32);
01565 
01566     /**
01567      * copy the contents in the rectangle \c windows of <code>other</code>
01568      * in this object.
01569      *
01570      * The result of the copy is always a connected genericMatrix.
01571      * I.e. you cannot copy the sub-genericMatrix property of another
01572      * genericMatrix.
01573      *
01574      * @param other the other genericMatrix to be copied
01575      * @param window rectangle define the copy area
01576      */
01577     inline genericMatrix<T>& copy(const genericMatrix<T>& other,
01578                                   const irectangle& window);
01579 
01580     /**
01581      * copy the contents of the specified rows/columns of
01582      * <code>other</code> into this object. Multiple occurence of one
01583      * row/column index in <code>idx</code> is allowed. If the
01584      * argument \b rows is true, \b idx specifies rows, if false \b
01585      * idx specifies columns.
01586      *
01587      * The result of the copy is always a connected genericMatrix.
01588      * I.e. you cannot copy the sub-genericMatrix property of another
01589      * genericMatrix.
01590      *
01591      * @param other the other genericMatrix to be copied
01592      * @param idx indices of the rows to be copied.
01593      * @param rows if true works on rows, else on columns
01594      */
01595     genericMatrix<T>& copy(const genericMatrix<T>& other,
01596                            const genericVector<int>& idx,
01597                            bool rows=true);
01598 
01599     /**
01600      * copy the <code>other</code> genericMatrix by casting each of
01601      * its elements
01602      *
01603      * @param other The genericMatrix to be casted
01604      *
01605      * Example:
01606      * \code
01607      *   lti::genericMatrix<int> matA(10,10,1);// a genericMatrix of integers
01608      *   lti::genericMatrix<double> matB;      // a genericMatrix of doubles
01609      *
01610      *   matB.castFrom(matA);          // this will copy matA in matB!!
01611      * \endcode
01612      */
01613     template<class U>
01614     genericMatrix<T>& castFrom(const genericMatrix<U>& other) {
01615       // only resize if necessary. castFrom doesn't guarantee ownership.
01616       if (other.size() != size()) {
01617         resize(other.rows(),other.columns(),T(),false,false);
01618       }
01619       for (int y=0;y<rows();y++) {
01620         getRow(y).castFrom(other.getRow(y));
01621       }
01622 
01623       return (*this);
01624     };
01625 
01626     /**
01627      * create a clone of this genericMatrix
01628      * @return a pointer to a copy of this genericMatrix
01629      */
01630     virtual mathObject* clone() const;
01631 
01632     /**
01633      * Compare this genericMatrix with other.
01634      *
01635      * @param other the other genericMatrix to be compared with
01636      * @return true if both matrices have the same elements and same size
01637      */
01638     bool equals(const genericMatrix<T>& other) const;
01639 
01640     /**
01641      * Compare the contents of each element of this genericMatrix with the
01642      * other one.  It assumes the type T can be compared using the
01643      * operator==.
01644      *
01645      * @param other the other genericMatrix to be compared with
01646      * @return true if both matrices have the same elements and same size
01647      */
01648     inline bool operator==(const genericMatrix<T>& other) const;
01649 
01650     /**
01651      * Assigment operator (alias for copy(other)).
01652      *
01653      * @param other the genericMatrix to be copied
01654      * @return a reference to the actual genericMatrix
01655      */
01656     inline genericMatrix<T>& operator=(const genericMatrix<T>& other);
01657 
01658     /**
01659      * @name Apply Methods
01660      * Following methods are used to apply simple functions to each element
01661      * of the vector.
01662      */
01663     //@{
01664 
01665     /**
01666      * applies a C-function to each element of the genericMatrix.
01667      * @param function a pointer to a C-function
01668      * @return a reference to the actual genericMatrix
01669      */
01670     genericMatrix<T>& apply(T (*function)(T));
01671 
01672     /**
01673      * applies a C-function to each element of the other genericMatrix
01674      * @param other the genericMatrix which elements will go through the given
01675      *              function.
01676      * @param function a pointer to a C-function
01677      * @return a reference to the actual genericMatrix
01678      */
01679     genericMatrix<T>& apply(const genericMatrix<T>& other,T (*function)(T));
01680 
01681     /**
01682      * applies a C-function to each element of the genericMatrix.
01683      * @param function a pointer to a C-function
01684      * @return a reference to the actual genericMatrix
01685      */
01686     genericMatrix<T>& apply(T (*function)(const T&));
01687 
01688     /**
01689      * applies a C-function to each element of the other genericMatrix
01690      * @param other the genericMatrix which elements will go through the given
01691      *              function.
01692      * @param function a pointer to a C-function
01693      * @return a reference to the actual genericMatrix
01694      */
01695     genericMatrix<T>& apply(const genericMatrix<T>& other,
01696                             T (*function)(const T&));
01697 
01698     /**
01699      * a two-parameter C-function receives the i-th elements of this
01700      * and the given genericMatrix and the result will be left in this
01701      * genericMatrix.  Note that both matrices must have the same size!
01702      * @param other the second genericMatrix to be considered (the first
01703      *              genericMatrix will be this object!)
01704      * @param function a pointer to C-function with two parameters
01705      * @return a reference to the actual genericMatrix
01706      */
01707     genericMatrix<T>& apply(const genericMatrix<T>& other,
01708                             T (*function)(const T&,const T&));
01709 
01710     /**
01711      * a two-parameter C-function receives the i-th elements of this
01712      * and the given genericMatrix and the result will be left in this
01713      * genericMatrix.  Note that both matrices must have the same size!
01714      * @param other the second genericMatrix to be considered (the first
01715      *              genericMatrix will be this object!)
01716      * @param function a pointer to C-function with two parameters
01717      * @return a reference to the actual genericMatrix
01718      */
01719     genericMatrix<T>& apply(const genericMatrix<T>& other,
01720                             T (*function)(T,T));
01721 
01722     /**
01723      * a two-parameter C-function receives the i-th elements of both given
01724      * matrices and leaves the result here.
01725      * Note that both matrices must have the same size!
01726      * @param a the first genericMatrix
01727      * @param b the second genericMatrix
01728      * @param function a pointer to C-function with two parameters
01729      * @return a reference to the actual genericMatrix
01730      */
01731     genericMatrix<T>& apply(const genericMatrix<T>& a,
01732                      const genericMatrix<T>& b,
01733                      T (*function)(const T&,const T&));
01734 
01735     /**
01736      * a two-parameter C-function receives the i-th elements of both given
01737      * matrices and leaves the result here.
01738      * Note that both matrices must have the same size!
01739      * @param a the first genericMatrix
01740      * @param b the second genericMatrix
01741      * @param function a pointer to C-function with two parameters
01742      * @return a reference to the actual genericMatrix
01743      */
01744     genericMatrix<T>& apply(const genericMatrix<T>& a,
01745                      const genericMatrix<T>& b,
01746                      T (*function)(T,T));
01747 
01748     //@}
01749 
01750     /**
01751      * @name Input and Output
01752      */
01753     //@{
01754 
01755     /**
01756      * write the object in the given ioHandler
01757      */
01758     virtual bool write(ioHandler& handler,const bool complete = true) const;
01759 
01760     /**
01761      * read the object from the given ioHandler
01762      */
01763     virtual bool read(ioHandler& handler,const bool complete = true);
01764     //@}
01765 
01766   protected:
01767 
01768     /**
01769      * number of rows of the genericMatrix
01770      */
01771     int& numRows;
01772 
01773     /**
01774      * number of columns of the genericMatrix
01775      */
01776     int& numColumns;
01777 
01778     /**
01779      * theSize.x is equal numColumns and theSize.y is equal numRows
01780      */
01781     ipoint theSize;
01782 
01783     /**
01784      * index of the last row
01785      */
01786     int lastRowIdx;
01787 
01788     /**
01789      * index of the last column
01790      */
01791     int lastColIdx;
01792 
01793     /**
01794      * number of rows the "physical" matrix
01795      * (always bigger than the dimensions of the "active" matrix).
01796      */
01797     int metaNumRows;
01798 
01799     /**
01800      * number of columns of the "physical" matrix
01801      * (always bigger than the dimensions of the "active" matrix).
01802      */
01803     int metaNumColumns;
01804 
01805     /**
01806      * size of theElements
01807      */
01808     int totalSize;
01809 
01810     /**
01811      * indicates if <code>theElements</code> points to own data or to
01812      * external data.
01813      */
01814     bool ownData;
01815 
01816     /**
01817      * indicate if <code>theElements</code> points to consecutive memory
01818      * or to "sparse" memory
01819      */
01820     eStoreMode mode;
01821 
01822     /**
01823      * pointer to the elements of the genericMatrix
01824      */
01825     T* theElements;
01826 
01827     /**
01828      * table of pointers to the rows
01829      */
01830     genericVector<T>* rowAddressTable;
01831 
01832     /**
01833      * Allocate \a n number of rows or the appropriate type.
01834      */
01835     inline virtual genericVector<T>* allocRows(const int n);
01836 
01837   };
01838 }
01839 
01840 /**
01841  * outputs the elements of the vector on a stream
01842  */
01843 namespace std {
01844   template <class T>
01845   ostream& operator<<(ostream& s,const lti::genericMatrix<T>& amat);
01846 }
01847 
01848 #include "ltiGenericMatrix_inline.h"
01849 #include "ltiGenericMatrix_template.h"
01850 
01851 #else
01852 #include "ltiGenericMatrix_template.h"
01853 #endif

Generated on Sat Apr 10 15:25:34 2010 for LTI-Lib by Doxygen 1.6.1