last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 /** 00042 * \file cvrLuDecomposition.h 00043 * Compute the LU decomposition of a given matrix. 00044 * \author Thomas Rusert 00045 * \author Gustavo Quiros 00046 * \date 23.06.1999 00047 * 00048 * $Id: cvrLuDecomposition.h,v 1.8 2006/09/07 13:38:37 doerfler Exp $ 00049 */ 00050 00051 #ifndef _CVR_LU_DECOMPOSITON_H_ 00052 #define _CVR_LU_DECOMPOSITON_H_ 00053 00054 #include "cvrLinearAlgebraFunctor.h" 00055 #include "cvrMatrix.h" 00056 #include "cvrVector.h" 00057 00058 #ifdef HAVE_LAPACK 00059 #include "cvrLapackInterface.h" 00060 #endif 00061 00062 namespace cvr { 00063 00064 #ifdef HAVE_LAPACK 00065 namespace internal { 00066 00067 /** 00068 * Offers a direct interface to the LAPACK function getrf for 00069 * computing an LU decomposition with partial pivoting. 00070 * 00071 * This function is needed by other classes that use it as part of 00072 * the LAPACK processing chain. 00073 */ 00074 template <typename T> 00075 class luDecompositionLapackInterface : public lapackInterface { 00076 public: 00077 /** 00078 * GETRF computes an LU factorization of a general M-by-N matrix A 00079 * using partial pivoting with row interchanges. 00080 * 00081 * The factorization has the form 00082 * \code 00083 * A = P * L * U 00084 * \endcode 00085 * where P is a permutation matrix, L is lower triangular with unit 00086 * diagonal elements (lower trapezoidal if m > n), and U is upper 00087 * triangular (upper trapezoidal if m < n). 00088 */ 00089 int getrf(integer* rows, integer* cols, T* a, 00090 integer* lda, integer *ipiv, integer* info) const; 00091 }; 00092 } 00093 #endif 00094 00095 00096 /** 00097 * LU decomposition functor. 00098 * Computes the LU decomposition of a square matrix. 00099 * 00100 * This class uses LAPACK if it is available. Note that if LAPACK 00101 * is not used or not available, A <b>must</b> be of full rank! 00102 * 00103 * @ingroup gLinearAlgebra 00104 */ 00105 template<typename T> 00106 class luDecomposition 00107 : public linearAlgebraFunctor 00108 #ifdef HAVE_LAPACK 00109 , protected internal::luDecompositionLapackInterface<T> 00110 #endif 00111 { 00112 00113 public: 00114 00115 /** 00116 * The parameters for the class luDecomposition 00117 */ 00118 class parameters : public linearAlgebraFunctor::parameters { 00119 public: 00120 00121 /** 00122 * Default constructor 00123 */ 00124 parameters(); 00125 00126 /** 00127 * Copy constructor 00128 * @param other the parameters object to be copied 00129 */ 00130 parameters(const parameters& other); 00131 00132 /** 00133 * Destructor 00134 */ 00135 ~parameters(); 00136 00137 /** 00138 * Copy the contents of a parameters object 00139 * @param other the parameters object to be copied 00140 * @return a reference to this parameters object 00141 */ 00142 parameters& copy(const parameters& other); 00143 00144 /** 00145 * Copy the contents of a parameters object 00146 * @param other the parameters object to be copied 00147 * @return a reference to this parameters object 00148 */ 00149 parameters& operator=(const parameters& other); 00150 00151 /** 00152 * Returns the name of this class. 00153 */ 00154 const std::string& name() const; 00155 00156 /** 00157 * Returns a pointer to a clone of the parameters 00158 */ 00159 virtual parameters* clone() const; 00160 00161 /** 00162 * Returns a pointer to a new instance of the parameters 00163 */ 00164 virtual parameters* newInstance() const; 00165 00166 /** 00167 * Write the parameters in the given ioHandler 00168 * @param handler the ioHandler to be used 00169 * @param complete if true (the default) the enclosing begin/end will 00170 * be also written, otherwise only the data block will be written. 00171 * @return true if write was successful 00172 */ 00173 virtual bool write(ioHandler& handler, const bool complete=true) const; 00174 00175 /** 00176 * Read the parameters from the given ioHandler 00177 * @param handler the ioHandler to be used 00178 * @param complete if true (the default) the enclosing begin/end will 00179 * be also written, otherwise only the data block will be written. 00180 * @return true if write was successful 00181 */ 00182 virtual bool read(ioHandler& handler,const bool complete=true); 00183 00184 // ------------------------------------------------ 00185 // the parameters 00186 // ------------------------------------------------ 00187 00188 }; 00189 00190 /** 00191 * Default constructor 00192 */ 00193 luDecomposition(); 00194 00195 /** 00196 * Construct a functor using the given parameters 00197 */ 00198 luDecomposition(const parameters& par); 00199 00200 /** 00201 * Copy constructor 00202 * @param other the object to be copied 00203 */ 00204 luDecomposition(const luDecomposition& other); 00205 00206 /** 00207 * Destructor 00208 */ 00209 virtual ~luDecomposition(); 00210 00211 /** 00212 * onPlace version of apply. 00213 * 00214 * Given a matrix a[0..n-1][0..n-1], this routine replaces it by 00215 * the LU decomposition of a rowwise permutation of itself. 00216 * permutation[0..n-1] is an output vector that records the row 00217 * permutation effected by the partial pivoting. 'pivot' is 00218 * +/-1 depending on whether the number of row interchanges was even or 00219 * odd, respectively. 00220 * @return true, if the decomposition could be computed, false 00221 * otherwise (typically, the matrix was singular). 00222 */ 00223 bool apply(matrix<T>& a, vector<integer>& permutation, int& pivot) const; 00224 00225 /** 00226 * onCopy version of apply. 00227 * 00228 * Given a matrix a[0..n-1][0..n-1], this routine returns a matrix 00229 * decomposition[0..n-1][0..n-1] which contains the LU decomposition of a 00230 * rowwise permutation of theMatrix. permutation[0..n-1] is an output 00231 * vector that records the row permutation effected by the partial 00232 * pivoting. 'pivot' is +/-1 depending on whether the number of 00233 * row interchanges was even or odd, respectively. 00234 * @return true, if the decomposition could be computed, false 00235 * otherwise (typically, the matrix was singular). 00236 */ 00237 bool apply(const matrix<T>& a, matrix<T>& lu, 00238 vector<integer>& permutation, int& pivot) const; 00239 00240 /** 00241 * Copy data of "other" functor. 00242 */ 00243 luDecomposition<T>& copy(const luDecomposition<T>& other); 00244 00245 /** 00246 * Returns the name of this class. 00247 */ 00248 const std::string& name() const; 00249 00250 /** 00251 * Returns a pointer to a clone of the functor. 00252 */ 00253 virtual luDecomposition<T>* clone() const; 00254 00255 /** 00256 * Returns a pointer to a new instance of the functor. 00257 */ 00258 virtual luDecomposition<T>* newInstance() const; 00259 00260 /** 00261 * Returns used parameters 00262 */ 00263 const parameters& getParameters() const; 00264 00265 /** 00266 * Returns a new Matrix which contains the "L" part of the given 00267 * LU decomposition 00268 */ 00269 matrix<T> extractL(const matrix<T>& lu) const; 00270 00271 /** 00272 * Returns a new Matrix which contains the "U" part of the given 00273 * LU decomposition 00274 */ 00275 matrix<T> extractU(const matrix<T>& lu) const; 00276 00277 /** 00278 * Returns a new Matrix which contains the "L" part of the given 00279 * LU decomposition. This is much faster than the on copy 00280 * version. 00281 */ 00282 void extractL(const matrix<T>& lu, matrix<T>& l) const; 00283 00284 /** 00285 * Returns a new Matrix which contains the "U" part of the given 00286 * LU decomposition. This is much faster than the on copy 00287 * version. 00288 */ 00289 void extractU(const matrix<T>& lu, matrix<T>& u) const; 00290 00291 /** 00292 * Builds the permutation matrix for the given permutation vector 00293 * 00294 * @param pv The row permutation vector on input 00295 * @param pm The row permutation matrix on output 00296 */ 00297 void buildPermutationMatrix(const vector<integer>& pv, 00298 matrix<T>& pm) const; 00299 00300 protected: 00301 00302 static const T epsilon; 00303 00304 #ifdef HAVE_LAPACK 00305 bool applyLapack(matrix<T>& theMatrix, 00306 vector<integer>& permutation, 00307 int& pivot) const; 00308 #endif 00309 00310 }; 00311 } 00312 00313 #endif 00314