CVR-Lib last update 20 Sep 2009

cvrMatrix.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2004
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  * Copyright (C) 2007-2008
00005  * Pablo Alvarado
00006  *
00007  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00008  *
00009  * The CVR-Lib is free software; you can redistribute it and/or
00010  * modify it under the terms of the BSD License.
00011  *
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright notice,
00018  *    this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright notice,
00021  *    this list of conditions and the following disclaimer in the documentation
00022  *    and/or other materials provided with the distribution.
00023  *
00024  * 3. Neither the name of the authors nor the names of its contributors may be
00025  *    used to endorse or promote products derived from this software without
00026  *    specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  * POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 /**
00042  * \file   cvrMatrix.h
00043  *         Declares the mathematical version of genericMatrix, which also
00044  *         provides some arithmetical operations and extrems search methods.
00045  * \author Pablo Alvarado
00046  * \date   09.04.1999
00047  *
00048  * $Id: cvrMatrix.h,v 1.13 2007/09/14 17:17:43 alvarado Exp $
00049  */
00050 
00051 #ifndef _CVR_MATRIX_H_
00052 #define _CVR_MATRIX_H_
00053 
00054 #include "cvrTypes.h"
00055 #include "cvrVector.h"
00056 
00057 #define _CVR_GENERIC_MATRIX_DONT_INSTANTIATE_REQUEST 1
00058 #include "cvrGenericMatrix.h"
00059 #undef _CVR_GENERIC_MATRIX_DONT_INSTANTIATE_REQUEST
00060 
00061 namespace cvr {
00062   /**
00063    * Mathematical matrix container class.
00064    *
00065    * The cvr::matrix class allows the representation of \e n x \e m matrices.
00066    * The rows will be indexed between 0 and n-1, and the columns between 0
00067    * and m-1.
00068    *
00069    * All types defined in cvrTypes.h use static members and can be contained
00070    * by the cvr::vector and cvr::matrix classes.
00071    *
00072    * The matrix class is a container class implemented as template.
00073    *
00074    * If you need to create a matrix of floats with 20 rows and 15 columns,
00075    * all elements initialized with an initial value of 4.27 just create it:
00076    *
00077    * \code
00078    * cvr::matrix<float> myMat(20,15,4.27f) // creates matrix with 300 elements
00079    *                                       // all initialized with 4.27f
00080    * \endcode
00081    *
00082    * To access the matrix elements use the access operators.  There are many
00083    * possibilities.  With at(const int row, const int col) is possible to
00084    * access an element directly.  With getRow(const int row) you can get the
00085    * row vector.  You cannot for instance resize nor change the memory 
00086    * referenced in this vector (see cvr::vector::resize).  For example:
00087    *
00088    * \code
00089    * float accu = 0; // initialize accumulator
00090    * cvr::matrix<float> myMat(20,15,4.27f)
00091    * cvr::vector<float> myVct;
00092    *
00093    * for (int j = 0; j < myMat.rows(); j++) {
00094    *   for (int i = 0; i < myMat.columns(); i++) {
00095    *   tmp += myMat.at(j,i); // access each element of the matrix:
00096    *                         // j is the row and i the column
00097    *   }
00098    * }
00099    *
00100    * myMat.getRowCopy(5,myVct); // copy the sixth row in myVct!
00101    * myVct.resize(6);           // Valid, the vector has its own memory!
00102    * myMat.at(5).resize(6)      // ERROR!! the vector is not resizable!
00103    *
00104    * \endcode
00105    *
00106    * The image representation in the CVR-Lib is based on the cvr::matrix
00107    * class.  It is quite confusing to use first the y-coordinate and then
00108    * the x-coordinate to access the image elements.  To avoid confusion use
00109    * the cvr::point class to access the elements of the matrix:
00110    *
00111    * \code
00112    *
00113    * cvr::channel8 aChannel(20,15); // creates an 8-bit image
00114    * cvr::channel8::value_type tmp; // tmp is of the element type of the
00115    *                                // channel8!
00116    * cvr::ipoint p;
00117    *
00118    *
00119    * for (p.y = 0; p.y < aChannel.rows(); p.y++) {
00120    *   for (p.x = 0; p.x < aChannel.columns(); p.x++) {
00121    *   tmp += aChannel.at(p); // access each element of the matrix:
00122    *                          // equivalent to: tmp += aChannel.at(p.y,p.x)!
00123    *   }
00124    * }
00125    *
00126    * \endcode
00127    *
00128    * @see cvr::image, cvr::channel, cvr::channel8,
00129    *      cvr::dmatrix, cvr::fmatrix, cvr::imatrix
00130    *
00131    * The parent class is cvr::genericMatrix and provides many memory
00132    * management options.
00133    *
00134    * The matrix has following methods:
00135    * - Constructors Constructors
00136    *   -  You can construct an empty matrix with the default constructor
00137    *      (matrix()).
00138    *   -  If you know the number of rows and columns use
00139    *      matrix(const int rows,const int columns,const T& initialValue)
00140    *   -  You can create a copy of another matrix or just copy a submatrix
00141    *      with the copy constructor (matrix(const matrix<T>& otherMatrix))
00142    * - Access members
00143    *   - at(), operator[]()
00144    *   - The rows() member returns the number of rows of the matrix.
00145    *   - The columns() member returns the number of columns of the matrix.
00146    * - Fill and Copy members
00147    *   -  With the fill() members you can fill the matrix with a given
00148    *      constant value or with values taken from other matrices.
00149    *   -  With the copy() member you can copy another matrix.
00150    *   -  You can specify, that the matrix should be used just as a
00151    *      wrapper-object to access external memory regions: useExternData().
00152    *      To check if a matrix is a wrapper-object you can use ownsData().
00153    * - Mathematical operations
00154    *   - Matrix multiplication: multiply()
00155    *   - Elementwise multiplication: emultiply()
00156    *   - Add another matrix to the actual matrix: add()
00157    *   - Add another <b>scaled</b> matrix to the actual matrix: addScaled()
00158    *   - Add a constant value to all elements of the matrix: add(const T& cst)
00159    *   - Subtract another vector from the actual vector: subtract()
00160    *   - Multiply the vector with a constant value: multiply()
00161    *   - Transpose the matrix: transpose()
00162    * - Iterators
00163    *   - It is possible to iterate within the matrix by making use of
00164    *     the matrix iterators.  (see begin() for more information)
00165    *   - Instead of reverse_iterators as in the STL we use iterators
00166    *     going backwards, due to faster execution times (see
00167    *     inverseBegin() for more information)
00168    *
00169    * @ingroup gLinearAlgebra
00170    * @ingroup gAggregate
00171    */
00172   template<typename T>
00173   class matrix : public genericMatrix<T> {
00174   public:
00175 
00176     /**
00177      * @name Internal classes and types
00178      */
00179     //@{
00180 
00181     /**
00182      * Type of the contained data
00183      */
00184     typedef typename genericMatrix<T>::value_type value_type;
00185 
00186     /**
00187      * Return type of the size() member
00188      */
00189     typedef typename genericMatrix<T>::size_type size_type;
00190 
00191     /**
00192      * The row type of a cvr::matrix is a cvr::vector
00193      */
00194     typedef vector<T> row_type;
00195 
00196     /**
00197      * Iterator
00198      */
00199     typedef typename genericMatrix<T>::iterator iterator;
00200 
00201     /**
00202      * Const iterator
00203      */
00204     typedef typename genericMatrix<T>::const_iterator const_iterator;
00205 
00206     //@}
00207 
00208     /**
00209      * Default constructor creates an empty matrix
00210      */
00211     explicit matrix();
00212 
00213     /**
00214      * Create a connected \c rows x \c cols matrix but leave all
00215      * elements uninitialized.
00216      *
00217      * @param rows number of rows of the matrix
00218      * @param cols number of columns of the matrix
00219      */
00220     explicit matrix(const int rows,const int cols);
00221 
00222     /**
00223      * Create a connected \c size.y x \c size.x matrix but leave all
00224      * elements uninitialized.
00225      *
00226      * @param size size of the matrix (size.x columns and size.y rows)
00227      */
00228     explicit matrix(const size_type& size);
00229 
00230     /**
00231      * Create a connected \c rows x \c cols matrix
00232      * and initializes all elements with \a iniValue
00233      *
00234      * @param rows number of rows of the matrix
00235      * @param cols number of columns of the matrix
00236      * @param iniValue all elements will be initialized with this value
00237      */
00238     explicit matrix(const int rows,const int cols,const T& iniValue);
00239 
00240     /**
00241      * Create a connected \c size.y x \c size.x matrix and initializes
00242      * all elements with \a iniValue
00243      *
00244      * @param size cvr::point with the size of the matrix
00245      *             (size.x is the number of columns and
00246      *              size.y the number of rows)
00247      * @param iniValue all elements will be initialized with this value
00248      */
00249     explicit matrix(const size_type& size,const T& iniValue);
00250 
00251     /**
00252      * Create a connected \c rows x \c cols Matrix and initializes all
00253      * elements with the data pointed by \a data.  The first
00254      * \a cols-elements of the data will be copied on the first row,
00255      * the next ones on the second row and so on.
00256      *
00257      * @param rows number of rows of the matrix
00258      * @param cols number of columns of the matrix
00259      * @param data pointer to the memory block with the data to be initialized
00260      *            with.
00261      */
00262     explicit matrix(const int rows,const int cols,const T data[]);
00263 
00264     /**
00265      * Create a connected \c dim.y x \c dim.x matrix and initializes all
00266      * elements with the data pointed by \a data.  The first
00267      * \a dim.x -elements of the data will be copied on the first row,
00268      * the next ones on the second row and so on.
00269      *
00270      * @param dim matrix dimensions
00271      * @param data pointer to the memory block with the data to be initialized
00272      *            with.
00273      */
00274     explicit matrix(const size_type& dim,const T data[]);
00275 
00276     /**
00277      * Create a \a rows x \a cols genericMatrix and use the given \a data as
00278      * if it was given through the useExternData().
00279      *
00280      * \note Please observe that the third argument makes a huge difference to
00281      *       the constructors that do not have it.  Here the data is \b not
00282      *       copied, just a reference is kept.  Without the third argument, the
00283      *       data is always copied.
00284      *
00285      * @see useExternData()
00286      *
00287      * The first \a cols-elements of the data will be copied on the
00288      * first row, the next ones on the second row and so on.
00289      *
00290      * @param rows number of rows of the genericMatrix
00291      * @param cols number of columns of the genericMatrix
00292      * @param data pointer to the memory block with the data to be initialized
00293      *            with.
00294      * @param constRef indicate if the memory block for the matrix can or
00295      *                 cannot be modified
00296      */
00297     explicit matrix(const int rows,const int cols,
00298                     T data[],
00299                     const eConstantReference constRef);
00300 
00301 
00302     /**
00303      * Create a \a rows x \a cols genericMatrix and use the given \a data as
00304      * if it was given through the useExternData().
00305      *
00306      * @see useExternData()
00307      *
00308      * The first \a cols-elements of the data will be copied on the
00309      * first row, the next ones on the second row and so on.
00310      *
00311      * \note Please observe that the third argument makes a huge difference to
00312      *       the constructors that do not have it.  Here the data is \b not
00313      *       copied, just a reference is kept.  Without the third argument, the
00314      *       data is always copied.
00315      *
00316      * @param theSize number of columns and rows of the genericMatrix
00317      * @param data pointer to the memory block with the data to be initialized
00318      *            with.
00319      * @param constRef indicate if the memory block for the matrix can or
00320      *                 cannot be modified
00321      */
00322     explicit matrix(const size_type& theSize,
00323                     T data[],
00324                     const eConstantReference constRef);
00325 
00326 
00327     /**
00328      * Copy constructor.
00329      *
00330      * Create this matrix as a connected copy of another matrix.
00331      */
00332     explicit matrix(const genericMatrix<T>& other);
00333 
00334     /**
00335      * Copy constructor.
00336      *
00337      * Create this matrix as a connected copy of another matrix
00338      * for this const version, the data will be always copied!
00339      * It is also possible to create a copy of a submatrix of another matrix.
00340      *
00341      * @param other the matrix to be copied.
00342      * @param fromRow initial row of the other matrix to be copied
00343      * @param fromCol initial column of the other matrix to be copied
00344      * @param toRow last row to be copied of the other matrix
00345      * @param toCol last column to be copied of the other matrix
00346      *
00347      * Example:
00348      * \code
00349      * cvr::matrix<int> m(4,6,0); // integer matrix with 25 elements
00350      * // ...
00351      * // initialize Matrix with:
00352      * //        0  1  2  3  4  5
00353      * //        2  1  5  4  0  3
00354      * //        1  2  1  2  3  2
00355      * //        3  3  2  1  2  3
00356      *
00357      * cvr::matrix<int> sm(m,0,2,1,3)  // last line will lead to
00358      * //                                 following contents in sm:
00359      * //        1  2  3
00360      * //        1  5  4
00361      * //        2  1  2
00362      * \endcode
00363      */
00364     explicit matrix(const genericMatrix<T>& other,
00365                     const int fromRow,
00366                     const int fromCol,
00367                     const int toRow=container::MaxIndex,
00368                     const int toCol=container::MaxIndex);
00369 
00370     /**
00371      * Copy constructor.
00372      *
00373      * Copy a submatrix of another matrix.
00374      */
00375     explicit matrix(const genericMatrix<T>& other,
00376                     const size_type& from,
00377                     const size_type& to);
00378 
00379     /**
00380      * Copy constructor.
00381      *
00382      * Create this matrix as a connected copy of another matrix
00383      * taking only the rows indicated by the vector.
00384      * for this const version, the data will be always copied!
00385      * Multiple occurence of one row index in \a rows is allowed.
00386      *
00387      * @param other the matrix to be copied.
00388      * @param rows inidices of the rows to be copied
00389      *
00390      * Example:
00391      * \code
00392      * cvr::vector<int> rows(2);
00393      * // initialize with
00394      * // 1 3
00395      * cvr::matrix<int> m(4,6,0); // integer matrix with 25 elements
00396      * // ...
00397      * // initialize Matrix with:
00398      * //        0  1  2  3  4  5
00399      * //        2  1  5  4  0  3
00400      * //        1  2  1  2  3  2
00401      * //        3  3  2  1  2  3
00402      *
00403      * cvr::matrix<int> sm(m,rows)     // last line will lead to
00404      * //                                 following contents in sm:
00405      * //        2  1  5  4  0  3
00406      * //        3  3  2  1  2  3
00407      * \endcode
00408      */
00409     explicit matrix(const genericMatrix<T>& other,
00410                     const genericVector<int>& rows);
00411 
00412     /**
00413      * Destructor
00414      */
00415     virtual ~matrix();
00416 
00417     /**
00418      * Return matrix-row as a vector.
00419      *
00420      * This method works fast, since it returns a reference to the row vector.
00421      * The data will NOT be copied.
00422      * @param row the row to be accessed
00423      * @return a reference to the vector row
00424      */
00425     inline row_type& getRow(const int row);
00426 
00427     /**
00428      * Return matrix-row as a const vector.
00429      *
00430      * This method works fast, since it returns a reference to the row vector.
00431      * The data will NOT be copied.
00432      * @param row the row to be accessed
00433      * @return a const reference to the vector row
00434      */
00435     inline const row_type& getRow(const int row) const;
00436 
00437     /**
00438      * Alias for getRow()
00439      */
00440     inline row_type& operator[](const int row);
00441 
00442     /**
00443      * Alias for getRow()
00444      */
00445     inline const row_type& operator[](const int row) const;
00446 
00447     /**
00448      * Return matrix-row as a vector.
00449      * This method copies the data of the matrix, therefore is not as
00450      * fast as getRow()
00451      * @param row the number of tthe row to be copied
00452      * @return a vector with the contents of the row of the matrix
00453      */
00454     inline row_type getRowCopy(const int row) const;
00455 
00456     /**
00457      * Copy a row vector in the given parameter.
00458      *
00459      * This method copies the data of a given row of the genericMatrix
00460      * in the given vector.
00461      *
00462      * @param row the number of the row to be copied
00463      * @param theRow the vector, where the data will be copied
00464      * @see getRow()
00465      */
00466     inline void getRowCopy(const int row,row_type& theRow) const;
00467 
00468     /**
00469      * Return matrix-column as a vector.
00470      * This method copies the data of the matrix, therefore is not as
00471      * fast as getRow()
00472      * @param col the number of the column to be copied
00473      * @return a vector with the contents of the column of the matrix
00474      */
00475     inline row_type getColumnCopy(const int col) const;
00476 
00477     /**
00478      * Return genericMatrix-column as a vector.
00479      * This method copies the data of the genericMatrix, therefore is not as
00480      * fast as getRow()
00481      * @param col the number of the column to be copied
00482      * @param theCol a vector, where the column vector of the genericMatrix
00483      *               should be copied.
00484      */
00485     inline void getColumnCopy(const int col,row_type& theCol) const;
00486 
00487     /**
00488      * Return the diagonal elements of the matrix as vector.
00489      * This method copies the diagonal elements of the matrix into
00490      * the vector. If the matrix is non-symmetrical, the vector will
00491      * be of dimension min(rows(),columns()).
00492      * @return a vector with the diagonal elements of the matrix.
00493      */
00494     inline row_type getDiagonal() const;
00495 
00496     /**
00497      * Return the diagonal elements of the genericMatrix as vector.
00498      * This method copies the diagonal elements of the genericMatrix into
00499      * the vector. If the genericMatrix is non-symmetrical, the vector will
00500      * be of dimension min(rows(),columns()).
00501      * @param diag a vector, where the diagonal of the genericMatrix
00502      *             should be copied.
00503      */
00504     inline void getDiagonal(row_type& diag) const;
00505 
00506     /**
00507      * Set the diagonal of this matrix to scale (default: 1), and all other
00508      * elements to 0. If the matrix is square (which it need not be), this
00509      * results in a scaled identity matrix.
00510      */
00511     void setIdentity(const T scale = T(1));
00512 
00513     /**
00514      * Assigment operator.
00515      *
00516      * Copy the contents of \a other in this object.
00517      *
00518      * The result of the copy is always a connected matrix.  I.e. you cannot
00519      * copy the sub-matrix property of another matrix.
00520      *
00521      * @param other the other matrix to be copied
00522      */
00523     inline matrix<T>& copy(const genericMatrix<T>& other);
00524 
00525     /**
00526      * Assigment operator.
00527      *
00528      * copy the contents of \a other in this object.
00529      *
00530      * The result of the copy is always a connected matrix.  I.e. you cannot
00531      * copy the sub-matrix property of another matrix.
00532      *
00533      * @param other the other matrix to be copied
00534      * @param fromRow initial row of the other matrix to be copied
00535      * @param fromCol initial column of the other matrix to be copied
00536      * @param toRow last row to be copied of the other matrix
00537      * @param toCol last column to be copied of the other matrix
00538      */
00539     inline matrix<T>& copy(const genericMatrix<T>& other,
00540                            const int fromRow,
00541                            const int fromCol,
00542                            const int toRow=container::MaxIndex,
00543                            const int toCol=container::MaxIndex);
00544 
00545     /**
00546      * Assigment operator.
00547      *
00548      * copy the contents of \a other in this object.
00549      *
00550      * The result of the copy is always a connected matrix.  I.e. you cannot
00551      * copy the sub-matrix property of another matrix.
00552      *
00553      * @param other the other matrix to be copied
00554      * @param from initial indices of submatrix to be copied
00555      * @param to   final indices of submatrix to be copied (included)
00556      */
00557     inline matrix<T>& copy(const genericMatrix<T>& other,
00558                            const size_type& from,
00559                            const size_type& to);
00560 
00561 
00562     /**
00563      * Assigment operator.
00564      *
00565      * Copy the contents of \a other in this object.
00566      *
00567      * The result of the copy is always a connected matrix.  I.e. you cannot
00568      * copy the sub-matrix property of another matrix.
00569      *
00570      * @param other the other matrix to be copied
00571      * @param window rectangle define the copy area
00572      */
00573     inline matrix<T>& copy(const genericMatrix<T>& other,
00574                            const irectangle& window);
00575 
00576     /**
00577      * Assigment operator.
00578      *
00579      * Copy the contents of the specified rows of
00580      * \a other into this object. Multiple occurence of one
00581      * row index in \a idx is allowed.
00582      *
00583      * @param other the other matrix to be copied
00584      * @param idx indices of the rows to be copied.
00585      */
00586     inline matrix<T>& copyRows(const genericMatrix<T>& other,
00587                                const genericVector<int>& idx);
00588 
00589     /**
00590      * Assigment operator.
00591      *
00592      * Copy the contents of the specified columns of
00593      * \a other into this object. Multiple occurence of one
00594      * column index in \a idx is allowed.
00595      *
00596      * @param other the other matrix to be copied
00597      * @param idx indices of the rows to be copied.
00598      */
00599     inline matrix<T>& copyColumns(const genericMatrix<T>& other,
00600                                   const genericVector<int>& idx);
00601 
00602 
00603     /**
00604      * Copy the \a other matrix by casting each of its elements
00605      * Example:
00606      * \code
00607      *   cvr::matrix<int> matA(10,10,1);// a matrix of integers
00608      *   cvr::matrix<double> matB;      // a matrix of doubles
00609      *
00610      *   matB.castFrom(matA);          // this will copy matA in matB!!
00611      * \endcode
00612      *
00613      * @param other The matrix to be casted
00614      * @returns reference to this matrix
00615      */
00616     template<typename U>
00617     inline matrix<T>& castFrom(const genericMatrix<U>& other);
00618 
00619     /**
00620      * Copy the \a other matrix by casting each of its elements. This
00621      * 'specialization' ensures that the copy() method is used when
00622      * U==T.
00623      *
00624      * @param other The matrix to be casted (copied)
00625      */
00626     inline matrix<T>& castFrom(const genericMatrix<T>& other);
00627 
00628     /**
00629      * Copy a submatrix of the \a other genericMatrix by casting each of
00630      * its elements.
00631      *
00632      * @param other The genericMatrix to be casted
00633      * @param fromRow initial row of the other genericMatrix to be copied
00634      * @param fromCol initial column of the other genericMatrix to be copied
00635      * @param toRow last row to be copied of the other genericMatrix
00636      * @param toCol last column to be copied of the other genericMatrix
00637      *
00638      * @return a reference to the current casted matrix.
00639      */
00640     template<typename U>
00641     inline matrix<T>& castFrom(const genericMatrix<U>& other,
00642                                const int fromRow,
00643                                const int fromCol,
00644                                const int toRow=container::MaxIndex,
00645                                const int toCol=container::MaxIndex);
00646 
00647     /**
00648      * This is just an alias for copy(const genericMatrix<T>&, const int
00649      * fromRow, const int fromCol, const int toRow, const int toCol) to
00650      * facilitate generic programming.
00651      *
00652      * @param other The genericMatrix to be copied.
00653      * @param fromRow initial row of the other genericMatrix to be copied
00654      * @param fromCol initial column of the other genericMatrix to be copied
00655      * @param toRow last row to be copied of the other genericMatrix
00656      * @param toCol last column to be copied of the other genericMatrix
00657      *
00658      * @return a reference to the current casted matrix.
00659      */
00660     matrix<T>& castFrom(const genericMatrix<T>& other,
00661                         const int fromRow,
00662                         const int fromCol,
00663                         const int toRow=container::MaxIndex,
00664                         const int toCol=container::MaxIndex);
00665 
00666     /**
00667      * Returns the name of this class.
00668      */
00669     const std::string& name() const;
00670 
00671     /**
00672      * Create a clone of this matrix
00673      * @return a pointer to a copy of this matrix
00674      */
00675     virtual matrix<T>* clone() const;
00676 
00677     /**
00678      * Create a new empty matrix
00679      * @return a pointer to a copy of this matrix
00680      */
00681     virtual matrix<T>* newInstance() const;
00682 
00683     /**
00684      * Compare this matrix with other, and use the given tolerance to
00685      * determine if the value of each element of the other matrix
00686      * approximately equals the values of the actual matrix elements.
00687      *
00688      * An element <i>x</i> is approximately equal to another element <i>y</i>
00689      * with a tolerance <i>t</i>, if following equation holds:
00690      * <i>x</i>-t < <i>y</i> < <i>x</i>+t
00691      *
00692      * This method is mainly used for matrices containing floating point
00693      * values.
00694      *
00695      * @param other the other matrix to be compared with
00696      * @param tolerance the tolerance to be used
00697      *
00698      * @return true if both matrices are approximatly equal
00699      */
00700     bool prettyCloseTo(const genericMatrix<T>& other,const T& tolerance) const;
00701 
00702     /**
00703      * Assigment operator (alias for copy(other)).
00704      * @param other the matrix to be copied
00705      * @return a reference to the actual matrix
00706      */
00707     inline matrix<T>& operator=(const genericMatrix<T>& other);
00708 
00709     /**
00710      * @name Apply Methods.
00711      *
00712      * Following methods are used to apply simple functions to each element
00713      * of the vector.
00714      */
00715     //@{
00716 
00717     /**
00718      * Applies a C-function to each element of the matrix.
00719      * @param function a pointer to a C-function
00720      * @return a reference to the actual matrix
00721      */
00722     inline matrix<T>& apply(T (*function)(T));
00723 
00724     /**
00725      * Applies a C-function to each element of the other matrix
00726      * @param other the matrix which elements will go through the given
00727      *              function.
00728      * @param function a pointer to a C-function
00729      * @return a reference to the actual matrix
00730      */
00731     inline matrix<T>& apply(const genericMatrix<T>& other,T (*function)(T));
00732 
00733     /**
00734      * Applies a C-function to each element of the matrix.
00735      * @param function a pointer to a C-function
00736      * @return a reference to the actual matrix
00737      */
00738     inline matrix<T>& apply(T (*function)(const T&));
00739 
00740     /**
00741      * Applies a C-function to each element of the other matrix
00742      * @param other the matrix which elements will go through the given
00743      *              function.
00744      * @param function a pointer to a C-function
00745      * @return a reference to the actual matrix
00746      */
00747     inline matrix<T>& apply(const genericMatrix<T>& other,
00748                             T (*function)(const T&));
00749 
00750     /**
00751      * A two-parameter C-function receives the i-th elements of this
00752      * and the given matrix and the result will be left in this
00753      * matrix.  Note that both matrices must have the same size!
00754      * @param other the second matrix to be considered (the first
00755      *              matrix will be this object!)
00756      * @param function a pointer to C-function with two parameters
00757      * @return a reference to the actual matrix
00758      */
00759     inline matrix<T>& apply(const genericMatrix<T>& other,
00760                             T (*function)(const T&,const T&));
00761 
00762     /**
00763      * A two-parameter C-function receives the i-th elements of this
00764      * and the given matrix and the result will be left in this
00765      * matrix.  Note that both matrices must have the same size!
00766      * @param other the second matrix to be considered (the first
00767      *              matrix will be this object!)
00768      * @param function a pointer to C-function with two parameters
00769      * @return a reference to the actual matrix
00770      */
00771     inline matrix<T>& apply(const genericMatrix<T>& other,T (*function)(T,T));
00772 
00773     /**
00774      * A two-parameter C-function receives the i-th elements of both given
00775      * matrices and leaves the result here.
00776      * Note that both matrices must have the same size!
00777      * @param a the first matrix
00778      * @param b the second matrix
00779      * @param function a pointer to C-function with two parameters
00780      * @return a reference to the actual matrix
00781      */
00782     inline matrix<T>& apply(const genericMatrix<T>& a,
00783                             const genericMatrix<T>& b,
00784                             T (*function)(const T&,const T&));
00785 
00786     /**
00787      * A two-parameter C-function receives the i-th elements of both given
00788      * matrices and leaves the result here.
00789      * Note that both matrices must have the same size!
00790      * @param a the first matrix
00791      * @param b the second matrix
00792      * @param function a pointer to C-function with two parameters
00793      * @return a reference to the actual matrix
00794      */
00795     inline matrix<T>& apply(const genericMatrix<T>& a,
00796                             const genericMatrix<T>& b,
00797                             T (*function)(T,T));
00798 
00799     //@}
00800 
00801     /**
00802      * @name Arithmetical Operations
00803      */
00804     //@{
00805 
00806     /**
00807      * Add \a other matrix to this matrix, and leave result here
00808      * @param other the matrix to be added with
00809      * @return a reference to the actual matrix
00810      */
00811     matrix<T>& add(const genericMatrix<T>& other);
00812 
00813     /**
00814      * Add  matrices \a a and \a b and write the result
00815      * in this matrix
00816      * @param a the first matrix
00817      * @param b the second matrix
00818      * @return a reference to the actual matrix
00819      */
00820     matrix<T>& add(const genericMatrix<T>& a, const genericMatrix<T>& b);
00821 
00822     /**
00823      * Add constant value to this matrix, and leave result here
00824      * @param value a constant value to be added to all matrix elements
00825      * @return a reference to the actual matrix
00826      */
00827     matrix<T>& add(const T value);
00828 
00829     /**
00830      * Add constant value to the other matrix and leave result here.
00831      * @param other the other matrix
00832      * @param value a constant value to be added to all matrix elements
00833      * @return a reference to the actual matrix
00834      */
00835     matrix<T>& add(const genericMatrix<T>& other,const T value);
00836 
00837     /**
00838      * Alias for add(const T value)
00839      * @param value a constant value to be added to all matrix elements
00840      * @return a reference to the actual matrix
00841      */
00842     inline matrix<T>& operator+=(const T value);
00843 
00844     /**
00845      * Alias for add(const matrix)
00846      * @param other the matrix to be added with
00847      * @return a reference to the actual matrix
00848      */
00849     inline matrix<T>& operator+=(const genericMatrix<T>& other);
00850 
00851     /**
00852      * Add \a other matrix to this matrix and leave the result in
00853      * a new matrix.  This object is not changed.
00854      * @param other the other matrix to be added with.
00855      * @return a matrix with the sum of this matrix and \a other
00856      *
00857      * \warning Note that the use of this operator is not as efficient
00858      * as the use of the add() methods, in which the programmer can
00859      * decide when to use temporal and when not...
00860      */
00861     matrix<T> operator+(const genericMatrix<T>& other) const;
00862 
00863     /**
00864      * Add constant value to this matrix, and leave result in a new
00865      * matrix. This object is not changed.
00866      * @param value a constant value to be added to all matrix elements
00867      * @return a new matrix with the result
00868      */
00869     matrix<T> operator+(const T value) const;
00870 
00871     /**
00872      * Subtract \a other matrix from this matrix and leave the result in
00873      * a new matrix.  This object is not changed.
00874      * @param other the other matrix to be added with.
00875      * @return a matrix with the sum of this matrix and \a other
00876      *
00877      * \warning Note that the use of this operator is not as efficient
00878      * as the use of the add() methods, in which the programmer can
00879      * decide when to use temporal and when not...
00880      */
00881     matrix<T> operator-(const genericMatrix<T>& other) const;
00882 
00883     /**
00884      * Subtract constant value from this matrix, and leave result in a new
00885      * matrix. This object is not changed.
00886      * @param value a constant value to be added to all matrix elements
00887      * @return a new matrix with the result
00888      */
00889     matrix<T> operator-(const T value) const;
00890 
00891     /**
00892      * Add another matrix scaled by \f$b\f$ to this matrix. The matrices
00893      * must be of the same types and dimensions. Let \f$A\f$ be this matrix
00894      * and \f$B\f$ the other matrix, then this method performs:<p>
00895      * \f$A=A+b\cdot B\f$
00896      * @param b scaling factor for <b>other</b>
00897      * @param other the matrix to be added after scaling
00898      * @return a reference to this matrix
00899      */
00900     matrix<T>& addScaled(const T b, const genericMatrix<T>& other);
00901 
00902     /**
00903      * Add a matrix A with a matrix B scaled by \f$b\f$, and leave the
00904      * result in this matrix. The matrices must be of the same types
00905      * and dimensions. This method performs:<p> \f$A+b\cdot B\f$
00906      * @param matA the first matrix
00907      * @param b scaling factor for <b>matB</b>
00908      * @param matB the matrix to be added after scaling
00909      * @return a reference to this matrix
00910      */
00911     matrix<T>& addScaled(const genericMatrix<T>& matA,
00912                          const T b,
00913                          const genericMatrix<T>& matB);
00914 
00915     /**
00916      * Leave the scaled %sum of two matrices in this matrix. The matrices
00917      * must be of the same types and dimensions. Let \f$A\f$ be the
00918      * first matrix and \f$B\f$ the second matrix with corresponding
00919      * scaling factors \f$a\f$ and \f$b\f$, further \f$C\f$ this
00920      * matrix, then this method performs:<p>
00921      * \f$C=a\cdot A+b\cdot B\f$
00922      * @param a scaling factor for <b>first</b>
00923      * @param first the first matrix to be added after scaling
00924      * @param b scaling factor for <b>second</b>
00925      * @param second the second matrix to be added after scaling
00926      * @return a reference to this matrix
00927      */
00928     matrix<T>& addScaled(const T a, const genericMatrix<T>& first,
00929                          const T b, const genericMatrix<T>& second);
00930 
00931 
00932     /**
00933      * Subtract \a other matrix from this matrix, and leave result
00934      * here.
00935      * @param other the matrix to be subtracted
00936      * @return a reference to this matrix.
00937      */
00938     matrix<T>& subtract(const genericMatrix<T>& other);
00939 
00940     /**
00941      * Subtract matrices \a b from \a a and write result
00942      * in this matrix.
00943      * @param a the first matrix
00944      * @param b the second matrix
00945      * @return a reference to this matrix, which is now a-b.
00946      */
00947     matrix<T>& subtract(const genericMatrix<T>& a, const genericMatrix<T>& b);
00948 
00949     /**
00950      * Subtract constant value from this matrix, and leave result here
00951      * @param value a constant value to be subtracted from all matrix elements
00952      * @return a reference to the actual matrix
00953      */
00954     matrix<T>& subtract(const T value);
00955 
00956     /**
00957      * Subtract constant value from the other matrix and leave result here.
00958      * @param other the other matrix
00959      * @param value a constant value to be subtracted from all matrix elements
00960      * @return a reference to the actual matrix
00961      */
00962     matrix<T>& subtract(const genericMatrix<T>& other,const T value);
00963 
00964     /**
00965      * Alias for subtract(const T value)
00966      * @param value a constant value to be subtracted from all matrix elements
00967      * @return a reference to the actual matrix
00968      */
00969     inline matrix<T>& operator-=(const T value);
00970 
00971     /**
00972      * Alias for subtract(const matrix)
00973      * @param other the matrix to be subtracted
00974      * @return a reference to the actual matrix
00975      */
00976     inline matrix<T>& operator-=(const genericMatrix<T>& other);
00977 
00978     /**
00979      * Multiply this matrix with \a other matrix, and leave result
00980      * here. The dimensions of this matrix will change if needed!
00981      * @param other the other matrix to be multiplied with.
00982      * @return a reference to this matrix.
00983      */
00984     matrix<T>& multiply(const genericMatrix<T>& other);
00985 
00986     /**
00987      * Multiply \a first with \a second and leave the
00988      * result in this object.
00989      * @param first the first matrix
00990      * @param second this second matrix will be multiplied with the first one.
00991      * @return a reference to this matrix, which has now the multiplication
00992      *         result.
00993      */
00994     matrix<T>& multiply(const matrix<T>& first,
00995                         const genericMatrix<T>& second);
00996 
00997     /**
00998      * Multiply this matrix with a vector and leave the result in
00999      * \a result.
01000      * A reference to \a result is returned.
01001      * @param other the vector to be multiplied with.  Its dimension must be
01002      *              equal to the number of columns of the matrix.
01003      * @param result the resulting vector.  It will have a number of
01004      *              dimensions equal to the number of rows of the matrix.
01005      * @return a reference to the result
01006      */
01007     row_type& multiply(const row_type& other,
01008                         row_type& result) const;
01009 
01010     /**
01011      * Multiply this matrix with a vector and leave the result in
01012      * same vector (In-Place Method)
01013      * A reference to the vector is returned.
01014      * @param srcdest the vector to be multiplied with.  Its dimension must be
01015      *              equal to the number of columns of the matrix.  The result
01016      *              vector will be left here too, and the resulting size
01017      *              will be the number of rows of the matrix.
01018      * @return a reference to the result
01019      */
01020     row_type& multiply(row_type& srcdest) const;
01021 
01022     /**
01023      * Multiply constant value with this matrix, and leave result here
01024      * @param value the constant value to be multiplied with
01025      * @return a reference to the actual matrix
01026      */
01027     matrix<T>& multiply(const T value);
01028 
01029     /**
01030      * Multiply constant value with the other matrix and leave result here.
01031      * @param other the other matrix
01032      * @param value the constant value to be multiplied with
01033      * @return a reference to the actual matrix
01034      */
01035     matrix<T>& multiply(const genericMatrix<T>& other,const T value);
01036 
01037     /**
01038      * Alias for multiply(const matrix)
01039      * @param other the other matrix to be multiplied with.
01040      * @return a reference to this matrix.
01041      */
01042     inline matrix<T>& operator*=(const genericMatrix<T>& other);
01043 
01044     /**
01045      * Alias for multiply(const T& value)
01046      * @param value the constant value to be multiplied with
01047      * @return a reference to this matrix.
01048      */
01049     inline matrix<T>& operator*=(const T value);
01050 
01051     /**
01052      * Multiplication operation
01053      *
01054      * \warning This overload is intended to be used sparselly, as the
01055      * creation of a new matrix has its cost (memory allocation,
01056      * specially).  Usually, you should prefer to work with the
01057      * multiply() methods and work on an already created instance.
01058      *
01059      * This method creates a new matrix that contains the result of multiplying
01060      * this matrix with the other one.
01061      *
01062      * @param other the other matrix to be multiplied with.
01063      * @return a new matrix containing the product.
01064      */
01065     inline matrix<T> operator*(const genericMatrix<T>& other) const;
01066 
01067 
01068     /**
01069      * Multiply the vector \a vct with this matrix, and save
01070      * the result in the vector \a result.
01071      *
01072      * The given vector will be interpreted as a row-vector (or a transposed
01073      * column vector).
01074      *
01075      * @param vct the vector to be multiplied with.
01076      * @param result the resulting vector.
01077      * @return a reference to the result vector
01078      */
01079     row_type& leftMultiply(const row_type& vct,
01080                                  row_type& result) const;
01081 
01082     /**
01083      * Multiply the given vector \a srcdest with this
01084      * matrix, and save the result in the same vector.
01085      *
01086      * The given vector will be interpreted as a row-vector (or a
01087      * transposed column vector).
01088      *
01089      * @param srcDest the vector to be multiplied with, and where the
01090      *                result must be written.
01091      * @return a reference to the result vector
01092      */
01093     row_type& leftMultiply(row_type& srcDest) const;
01094 
01095     /**
01096      * Multiply the matrix \a mat with \a this matrix, and leave
01097      * the result in \a this matrix.
01098      *
01099      * @param mat the matrix to be multiplied with.
01100      * @return a reference to \a this  matrix
01101      */
01102     matrix<T>& leftMultiply(const matrix<T>& mat);
01103 
01104     /**
01105      * Divide the elements of the matrix by a constant value, and leave
01106      * the result here
01107      * @param value the constant value (divisor)
01108      * @return a reference to the actual matrix
01109      */
01110     matrix<T>& divide(const T value);
01111 
01112     /**
01113      * Divide the elements of the other matrix by a constant value and leave
01114      * the result here.
01115      * @param other the other matrix
01116      * @param value the constant value (divisor)
01117      * @return a reference to the actual matrix
01118      */
01119     matrix<T>& divide(const genericMatrix<T>& other,const T value);
01120 
01121     /**
01122      * Alias for divide(const T& value)
01123      * @param value the constant value to be divided by
01124      * @return a reference to this matrix.
01125      */
01126     matrix<T>& operator/=(const T value);
01127 
01128     /**
01129      * Element-wise multiplication with other matrix
01130      * @param other the other matrix.  It should have the same dimensions of
01131      *              this matrix.
01132      * @return a reference to the actual matrix
01133      */
01134     matrix<T>& emultiply(const genericMatrix<T>& other);
01135 
01136     /**
01137      * Element-wise multiplication of \a a and \a b.
01138      * @param a the first matrix
01139      * @param b the second matrix (with same dimensions of a)
01140      * @return a reference to the actual matrix
01141      */
01142     matrix<T>& emultiply(const genericMatrix<T>& a, const genericMatrix<T>& b);
01143 
01144     /**
01145      * Element-wise division with other matrix
01146      * @param other the other matrix.  It should have the same dimensions of
01147      *              this matrix.
01148      * @return a reference to the actual matrix
01149      */
01150     matrix<T>& edivide(const genericMatrix<T>& other);
01151 
01152     /**
01153      * Element-wise division of \a a and \a b.
01154      * @param a the first matrix
01155      * @param b the second matrix (with same dimensions of a)
01156      * @return a reference to the actual matrix
01157      */
01158     matrix<T>& edivide(const genericMatrix<T>& a, const genericMatrix<T>& b);
01159 
01160     /**
01161      * Outer-product of two vectors.
01162      * The result will be left in this matrix.
01163      * The dimensions of this matrix will change if needed.
01164      * The outer product of two column vectors is defined as
01165      * \f$a \cdot b^T\f$
01166      * @param a first vector (will determine the number of rows)
01167      * @param b second vector (will determine the number of columns)
01168      * @return a reference to the actual matrix
01169      */
01170     matrix<T>& outerProduct(const row_type& a,
01171                             const row_type& b);
01172 
01173     /**
01174      * Transpose matrix and leave the result here.
01175      *
01176      * If the matrix is square this is actually a fast on-place
01177      * operation. However, if the matrix is not square a temporary
01178      * matrix is created with the new dimensions. This can be less
01179      * efficient than using transpose(const matrix&) directly on a
01180      * temporary matrix if many matrices of the same size need to be
01181      * transposed.
01182      *
01183      * @return a reference to the actual (now transposed) matrix
01184      */
01185     matrix<T>& transpose();
01186 
01187     /**
01188      * Transpose the other matrix and leave the result here.
01189      *
01190      * If U!=T each element of \a other is static_cast<T> in the
01191      * process.
01192      *
01193      * @return a reference to the actual (now transposed) matrix
01194      */
01195     template <typename U>
01196     matrix<T>& transpose(const genericMatrix<U>& other);
01197 
01198     /**
01199      * Calculate the sum of all elements of the matrix.
01200      * This member can be used with classes which define the operator '+='
01201      */
01202     T computeSumOfElements() const;
01203 
01204     /**
01205      * Calculate the product of all elements of the vector.
01206      * This member can be used with classes which define the operator '*='
01207      */
01208     T computeProductOfElements() const;
01209 
01210     /**
01211      * Returns the trace (i.e. the sum of the diagonal elements) of this
01212      * matrix. If the matrix is not symmetrical, it will return the sum
01213      * of all elements (i,i) with i from 0 to n-1;
01214      * n being min(rows(),columns())
01215      */
01216     T trace() const;
01217     //@}
01218 
01219     /**
01220      * @name Find extreme values
01221      */
01222     //@{
01223 
01224     /**
01225      * Get the smallest element of the matrix
01226      */
01227     T findMinimum() const;
01228 
01229     /**
01230      * Get the index of the smallest element of the matrix
01231      */
01232     size_type findIndexOfMinimum() const;
01233 
01234     /**
01235      * Get the biggest element of the matrix
01236      */
01237     T findMaximum() const;
01238 
01239     /**
01240      * Get the index of the biggest element of the matrix
01241      */
01242     size_type findIndexOfMaximum() const;
01243 
01244     /**
01245      * Get the extremes of the matrix (smallest and biggest elements)
01246      */
01247     void findExtremes(T& theMinimum, T& theMaximum) const;
01248 
01249     /**
01250      * Get the indices of the extremes of the matrix
01251      * (smallest and biggest elements)
01252      */
01253     void findIndexOfExtremes(size_type& theIdxMinimum,
01254                              size_type& theIdxMaximum) const;
01255 
01256     //@}
01257   protected:
01258 
01259     /**
01260      * @name Memory allocation and deallocation
01261      *
01262      * Restrict all memory allocation and deallocation to these functions.
01263      */
01264     //@{
01265     /**
01266      * Allocate \a n number of rows or the appropriate type.
01267      */
01268     inline virtual row_type* allocNewRows(const int n);
01269     //@}
01270 
01271   };
01272 }
01273 
01274 #include "cvrMatrix_inline.h"
01275 
01276 namespace cvr {
01277   /**
01278    * Matrix of double
01279    */
01280   typedef matrix<double> dmatrix;
01281   /**
01282    * Matrix of float
01283    */
01284   typedef matrix<float>  fmatrix;
01285   /**
01286    * Matrix of integer
01287    */
01288   typedef matrix<int32>  imatrix;
01289 
01290 }
01291 
01292 #endif
01293 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8