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

ltiEquationSystem.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 .......: ltiEquationSystem.h
00027  * authors ....: Thomas Rusert
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 02.06.99
00030  * revisions ..: $Id: ltiEquationSystem.h,v 1.8 2006/02/08 12:19:54 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_EQUATION_SYSTEM_H_
00034 #define _LTI_EQUATION_SYSTEM_H_
00035 
00036 #include "ltiLinearAlgebraFunctor.h"
00037 #include "ltiMatrix.h"
00038 #include "ltiVector.h"
00039 
00040 namespace lti {
00041   /** Abstract base class for all lti linear equation system solution functors
00042       to solve Ax=b. */
00043   template<class T>
00044   class linearEquationSystemSolutionMethod : public linearAlgebraFunctor {
00045   public:
00046     /**
00047      * returns the name of this type
00048      */
00049     virtual const char* getTypeName() const
00050     {return "linearEquationSystemSolutionMethod";};
00051   };
00052 
00053   /**
00054    * Abstract base class for all lti linear equation system solution functors
00055    * using matrix decomposition to solve Ax=b.
00056    */
00057   template<class T>
00058   class decompositionSolution : public linearEquationSystemSolutionMethod<T> {
00059   public:
00060     /**
00061      * decompositionSolution parameter class
00062      */
00063     class parameters
00064       : public linearEquationSystemSolutionMethod<T>::parameters {
00065     public:
00066       /**
00067        * default constructor
00068        */
00069       parameters() : linearEquationSystemSolutionMethod<T>::parameters() {};
00070       /**
00071        * copy constructor
00072        */
00073       parameters(const parameters& other)
00074         : linearEquationSystemSolutionMethod<T>::parameters() {
00075         copy(other);
00076       };
00077 
00078       /**
00079        * returns the name of this type
00080        */
00081       virtual const char* getTypeName() const
00082       {return "decompositionSolution::parameters";};
00083 
00084       /**
00085        * copy member.
00086        *
00087        * if the system-matrix of the other object is a normal matrix,
00088        * its contents will be copied to the system-matrix of this
00089        * parameters-object.  If the other system-matrix contains just
00090        * a reference to external data, then the system-matrix of this
00091        * object will contain the same reference to the data!  The
00092        * reason for this is to allow the use of this functor with huge
00093        * matrices without the need of duplication.  Use this data
00094        * referencing option carefully!
00095        */
00096       parameters& copy(const parameters& other) {
00097 #ifndef _LTI_MSC_6
00098         // MS Visual C++ 6 is not able to compile this...
00099         linearEquationSystemSolutionMethod<T>::parameters::copy(other);
00100 #else
00101         // ...so we have to use this workaround.
00102         // Conditional on that, copy may not be virtual.
00103         functor::parameters&
00104           (functor::parameters::* p_copy)
00105           (const functor::parameters&) =
00106           functor::parameters::copy;
00107         (this->*p_copy)(other);
00108 #endif
00109 
00110         // if other has its own data, make a copy of it
00111         if (other.systemMatrix.ownsData()) {
00112           systemMatrix.copy(other.systemMatrix);
00113         } else {
00114           // if other does not own its data, continue referencing the original
00115           // data (this is used in case of huge matrices (> 1GB!)
00116           systemMatrix.useExternData(other.systemMatrix.rows(),
00117                                      other.systemMatrix.columns(),
00118                                      const_cast<T*>(&other.systemMatrix.at(0,0)));
00119         }
00120 
00121         return (*this);
00122       };
00123       /**
00124        * returns a pointer to a clone of the parameters.
00125        */
00126       virtual functor::parameters* clone() const {
00127         return (new parameters(*this));
00128       };
00129 
00130       // -------------------------------------------------------
00131       // the parameters
00132       // -------------------------------------------------------
00133 
00134       /**
00135        * matrix A
00136        */
00137       matrix<T> systemMatrix;
00138 
00139     };
00140 
00141     /**
00142      * default constructor
00143      */
00144     decompositionSolution();
00145 
00146     /**
00147      * constructor, sets the parameters
00148      */
00149     decompositionSolution(const parameters& theParams);
00150 
00151     /**
00152      * constructor, sets the matrix A
00153      */
00154     decompositionSolution(const matrix<T>& theMatrix);
00155 
00156     /**
00157      * destructor
00158      */
00159     virtual ~decompositionSolution() {};
00160 
00161     /**
00162      * sets the functor's parameters.
00163      * @return true if successful, false otherwise.
00164      */
00165     virtual bool updateParameters();
00166 
00167     /**
00168      * returns the current parameters.
00169      */
00170     const parameters& getParameters() const;
00171 
00172     /**
00173      * copy data of "other" functor.
00174      */
00175     decompositionSolution<T>& copy(const decompositionSolution<T>& other);
00176     /**
00177      * returns the name of this type
00178      */
00179     virtual const char* getTypeName() const {return "decompositionSolution";};
00180 
00181   protected:
00182     //decompositionSolution<T>& copy(
00183     //  const decompositionSolution<T>& other);
00184 
00185     bool decomposed;
00186     matrix<T> dcmpMat;
00187   };
00188 
00189   /**
00190    * QR solution functor.
00191    * Solves the linear equation Ax=b as a least-squares problem using QR
00192    * decomposition A=QR  (Householder transformation) of the given
00193    * (m,n)-matrix A.
00194    * @see decompositionSolution::parameters
00195    */
00196   template<class T>
00197   class qrSolution : public decompositionSolution<T> {
00198   public:
00199     /**
00200      * qrSolution parameter class
00201      */
00202     class parameters : public decompositionSolution<T>::parameters {
00203     public:
00204       /**
00205        * default constructor
00206        */
00207       parameters()
00208         : decompositionSolution<T>::parameters(),computeResiduum(false) {};
00209 
00210      /**
00211        * copy constructor
00212        */
00213       parameters(const parameters& other)
00214         : decompositionSolution<T>::parameters() {
00215         copy(other);
00216       };
00217 
00218       /**
00219        * compute residuum?
00220        */
00221       bool computeResiduum;
00222 
00223       /**
00224        * returns the name of this type
00225        */
00226       virtual const char* getTypeName() const {
00227         return "qrSolution::parameters";
00228       };
00229 
00230       /**
00231        * copy member.
00232        */
00233       parameters& copy(const parameters& other) {
00234 #     ifndef _LTI_MSC_6
00235         // MS Visual C++ 6 is not able to compile this...
00236         decompositionSolution<T>::parameters::copy(other);
00237 #     else
00238         // ...so we have to use this workaround.
00239         // Conditional on that, copy may not be virtual.
00240         decompositionSolution<T>::parameters&
00241           (decompositionSolution<T>::parameters::* p_copy)
00242           (const decompositionSolution<T>::parameters&) =
00243           decompositionSolution<T>::parameters::copy;
00244         (this->*p_copy)(other);
00245 #     endif
00246 
00247         computeResiduum = other.computeResiduum;
00248 
00249         return (*this);
00250       };
00251       /**
00252        * returns a pointer to a clone of the parameters.
00253        */
00254       virtual functor::parameters* clone() const {
00255         return (new parameters(*this));
00256       };
00257     };
00258 
00259     /**
00260      * default constructor
00261      */
00262     qrSolution() : decompositionSolution<T>() {
00263       parameters params;
00264       setParameters(params);
00265     };
00266 
00267     /**
00268      * constructor, sets the parameters
00269      */
00270     qrSolution(const parameters& theParams) :
00271       decompositionSolution<T>(theParams) {};
00272 
00273     /**
00274      * constructor, sets the matrix A
00275      */
00276     qrSolution(const matrix<T>& theMatrix) :
00277       decompositionSolution<T>(theMatrix) {
00278       parameters tmpParam;
00279       tmpParam.systemMatrix = theMatrix;
00280       setParameters(tmpParam);
00281     };
00282 
00283     /**
00284      * returns the current parameters.
00285      */
00286     const parameters& getParameters() const;
00287 
00288     /** onPlace version of apply.
00289         Solves the least-squares problem Ax=b and returns the residuum if
00290         computeResiduum==true.
00291 
00292         WARNING: For use with multiple right sides b of a set of
00293         equation systems Ax=b, the matrix decomposition is computed
00294         only on calling <em>apply()</em> the first time. After that
00295         the existing decomposition will be used until calling
00296         <em>setParameters()</em>.
00297     */
00298     double apply(vector<T>& b);
00299 
00300     /** onCopy version of apply.
00301         Solves the least-squares problem Ax=b and returns the residuum if
00302         computeResiduum==true.
00303 
00304         WARNING: For use with multiple right sides b of a set of
00305         equation systems Ax=b, the matrix decomposition is computed
00306         only on calling <em>apply()</em> the first time. After that
00307         the existing decomposition will be used until calling
00308         <em>setParameters()</em>.
00309     */
00310     double apply(const vector<T>& b,vector<T>& x);
00311 
00312     /**
00313      * copy data of "other" functor.
00314      */
00315     qrSolution& copy(const qrSolution& other);
00316 
00317     /**
00318      * returns a pointer to a clone of the functor.
00319      */
00320     virtual functor* clone() const {
00321       return (new qrSolution<T>(*this));
00322     };
00323 
00324     /**
00325      * returns the name of this type
00326      */
00327     virtual const char* getTypeName() const {
00328       return "qrSolution";
00329     };
00330 
00331   protected:
00332     //qrSolution<T>& copy(const qrSolution<T>& other);
00333 
00334     vector<double> dcmpVec;
00335     vector<double> helpVec;
00336   };
00337 
00338   /**
00339    * LU solution functor.
00340    * Solves the linear equation Ax=b using LU decomposition.
00341    */
00342   template<class T>
00343   class luSolution : public decompositionSolution<T> {
00344   public:
00345     typedef typename decompositionSolution<T>::parameters parameters;
00346 
00347     /**
00348      * default constructor
00349      */
00350     luSolution() : decompositionSolution<T>() {};
00351 
00352     /**
00353      * constructor, sets the parameters
00354      * @see decompositionSolution::parameters
00355      */
00356     luSolution(const parameters& theParams) :
00357       decompositionSolution<T>(theParams) {};
00358 
00359     /**
00360      * constructor, sets the matrix A
00361      */
00362     luSolution(const matrix<T>& theMatrix) :
00363       decompositionSolution<T>(theMatrix) {};
00364 
00365     /**
00366      * onPlace version of apply.
00367      * Solves the set of n linear equations Ax=b. For use with multiple right
00368      * sides b of a set of equation systems Ax=b, the matrix decomposition is
00369      * computed only on calling apply the first time.
00370      * After that the existing decomposition will be used until calling
00371      * setParameters.*/
00372     bool apply(vector<T>& b);
00373 
00374     /**
00375      * onCopy version of apply.
00376      * Solves the set of n linear equations Ax=b. For use with multiple right
00377      * sides b of a set of equation systems Ax=b, the matrix decomposition is
00378      * computed only on calling apply the first time.
00379      * After that the existing decomposition will be used until calling
00380      * setParameters.
00381      */
00382     bool apply(const vector<T>& b,vector<T>& x);
00383 
00384     /**
00385      * onPlace version of apply.  Solves the set of n linear equations
00386      * A x=b where x is the i-th _column_ vector of X and b the i-th
00387      * _column_ vector of B. For use with multiple right sides b of a
00388      * set of equation systems Ax=b, the matrix decomposition is
00389      * computed only on calling apply the first time.  After that the
00390      * existing decomposition will be used until calling
00391      * setParameters.
00392      */
00393     bool apply(matrix<T>& B);
00394 
00395     /**
00396      * onCopy version of apply.  Solves the set of n linear equations
00397      * A x=b where x is the i-th _column_ vector of X and b the i-th
00398      * _column_ vector of B. For use with multiple right sides b of a
00399      * set of equation systems Ax=b, the matrix decomposition is
00400      * computed only on calling apply the first time.  After that the
00401      * existing decomposition will be used until calling
00402      * setParameters.
00403      */
00404     bool apply(const matrix<T>& B,matrix<T>& X);
00405 
00406     /**
00407      * copy data of "other" functor.
00408      */
00409     luSolution& copy(const luSolution& other);
00410 
00411     /**
00412      * returns a pointer to a clone of the functor.
00413      */
00414     virtual functor* clone() const { return (new luSolution<T>(*this));};
00415 
00416     /**
00417      * returns the name of this type
00418      */
00419     virtual const char* getTypeName() const {return "luSolution";};
00420 
00421   protected:
00422     //luSolution<T>& copy(const luSolution<T>& other);
00423 
00424     vector<int> dcmpVec;
00425   };
00426 }
00427 
00428 #endif

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