uLAPACK.hpp

Go to the documentation of this file.
00001 /*
00002  * Bayes++ the Bayesian Filtering Library
00003  * Copyright (c) 2002 Michael Stevens
00004  * See accompanying Bayes++.htm for terms and conditions of use.
00005  *
00006  * $Id: uLAPACK.hpp 562 2006-04-05 20:46:23 +0200 (Wed, 05 Apr 2006) mistevens $
00007  */
00008 
00009 /*
00010  * uBLAS to LAPACK Interface
00011  *  Very basic we only functions for information_root_filter supported
00012  */
00013 
00014 /* Filter Matrix Namespace */
00015 namespace Bayesian_filter_matrix
00016 {
00017 namespace LAPACK {
00018 
00019 namespace rawLAPACK {
00020     // LAPACK routines as C callable functions
00021     extern "C" {
00022 
00023     void dgeqrf_(
00024             const int & m,
00025             const int & n,
00026             double da[],
00027             const int & lda,
00028             double dtau[],
00029             double dwork[],
00030             const int& ldwork,
00031             int& info);
00032     void sgeqrf_(
00033             const int & m,
00034             const int & n,
00035             float da[],
00036             const int & lda,
00037             float dtau[],
00038             float dwork[],
00039             const int& ldwork,
00040             int& info);
00041 
00042     void dgetrs_(
00043             const char& transa,
00044             const int& n,
00045             const int& nrhs,
00046             const double da[],
00047             const int& lda,
00048             int ipivot[],
00049             double db[],
00050             const int& ldb,
00051             int& info);
00052     void sgetrs_(
00053             const char& transa,
00054             const int& n,
00055             const int& nrhs,
00056             const float da[],
00057             const int& lda,
00058             int ipivot[],
00059             float db[],
00060             const int& ldb,
00061             int& info);
00062 
00063     void dgetrf_(
00064             const int& m,
00065             const int& n,
00066             double da[],
00067             const int& lda,
00068             int ipivot[],
00069             int& info);
00070     void sgetrf_(
00071             const int& m,
00072             const int& n,
00073             float da[],
00074             const int& lda,
00075             int ipivot[],
00076             int& info);
00077 
00078     }// extern "C"
00079 
00080     // Type overloads for C++
00081     inline void geqrf( const int & m, const int & n, double da[], const int & lda, double dtau[], double dwork[], const int& ldwork, int& info)
00082     {
00083         dgeqrf_(m,n,da,lda,dtau,dwork,ldwork,info);
00084     }
00085     inline void geqrf( const int & m, const int & n, float da[], const int & lda, float dtau[], float dwork[], const int& ldwork, int& info)
00086     {
00087         sgeqrf_(m,n,da,lda,dtau,dwork,ldwork,info);
00088     }
00089     inline void getrs( const char& transa, const int& n, const int& nrhs, const double da[], const int& lda, int ipivot[], double db[], const int& ldb, int& info)
00090     {
00091         dgetrs_(transa,n,nrhs,da,lda,ipivot,db,ldb,info);
00092     }
00093     inline void getrs( const char& transa, const int& n, const int& nrhs, const float da[], const int& lda, int ipivot[], float db[], const int& ldb, int& info)
00094     {
00095         sgetrs_(transa,n,nrhs,da,lda,ipivot,db,ldb,info);
00096     }
00097     inline void getrf( const int& m, const int& n, double da[], const int& lda, int ipivot[], int& info)
00098     {
00099         dgetrf_(m,n,da,lda,ipivot,info);
00100     }
00101     inline void getrf( const int& m, const int& n, float da[], const int& lda, int ipivot[], int& info)
00102     {
00103         sgetrf_(m,n,da,lda,ipivot,info);
00104     }
00105 }// namespace rawLAPACK
00106 
00107 
00108 /* Support types */
00109 typedef boost::numeric::ublas::vector<int> pivot_t;
00110 typedef Bayesian_filter_matrix::DenseVec vector_t;
00111 typedef Bayesian_filter_matrix::DenseColMatrix matrix_t;
00112 
00113 /* LAPACK Interface*/
00114 
00115 
00116 
00117 // QR Factorization of a MxN General Matrix A.
00118 //    a       (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A. On exit , the upper triangle and diagonal is the min(M,N) by N upper triangular matrix R.  The lower triangle, together with the tau vector, is the orthogonal matrix Q as a product of min(M,N) elementary reflectors.
00119 //    tau     (OUT - vector (min(M,N))) Vector of the same numerical type as A. The scalar factors of the elementary reflectors.
00120 //    info    (OUT - int)
00121 //   0   : function completed normally
00122 //   < 0 : The ith argument, where i = abs(return value) had an illegal value.
00123 int geqrf (matrix_t& a, vector_t& tau)
00124 {
00125     int              _m = int(a.size1());
00126     int              _n = int(a.size2());
00127     int              _lda = int(a.size1());
00128     int              _info;
00129 
00130     // make_sure tau's size is greater than or equal to min(m,n)
00131     if (int(tau.size()) < (_n<_m ? _n : _m) )
00132         return -104;
00133 
00134     int ldwork = _n*_n;
00135     vector_t dwork(ldwork);
00136     rawLAPACK::geqrf (_m, _n, a.data().begin(), _lda, tau.data().begin(), dwork.data().begin(), ldwork, _info);
00137 
00138     return _info;
00139 }
00140 
00141 // LU factorization of a general matrix A.  
00142 //    Computes an LU factorization of a general M-by-N matrix A using
00143 //    partial pivoting with row interchanges. Factorization has the form
00144 //    A = P*L*U.
00145 //    a       (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A to be factored. On exit, the factors L and U from the factorization A = P*L*U.
00146 //    ipivot  (OUT - vector(min(M,N))) Integer vector. The row i of A was interchanged with row IPIV(i).
00147 //    info    (OUT - int)
00148 //   0   :  successful exit
00149 //   < 0 :  If INFO = -i, then the i-th argument had an illegal value.
00150 //   > 0 :  If INFO = i, then U(i,i) is exactly zero. The  factorization has been completed, but the factor U is exactly singular, and division by zero will occur if it is used to solve a system of equations.
00151 int getrf (matrix_t& a, pivot_t& ipivot)
00152 {
00153     matrix_t::value_type* _a = a.data().begin();
00154     int _m = int(a.size1());
00155     int _n = int(a.size2());
00156     int _lda = _m;  // minor size
00157     int _info;
00158 
00159     rawLAPACK::getrf (_m, _n,   _a, _lda, ipivot.data().begin(), _info);
00160 
00161     return _info;
00162 }
00163 
00164 // Solution to a system using LU factorization 
00165 //   Solves a system of linear equations A*X = B with a general NxN
00166 //   matrix A using the LU factorization computed by GETRF.
00167 //   transa  (IN - char)  'T' for the transpose of A, 'N' otherwise.
00168 //   a       (IN - matrix(M,N)) The factors L and U from the factorization A = P*L*U as computed by GETRF.
00169 //   ipivot  (IN - vector(min(M,N))) Integer vector. The pivot indices from GETRF; row i of A was interchanged with row IPIV(i).
00170 //   b       (IN/OUT - matrix(ldb,NRHS)) Matrix of same numerical type as A. On entry, the right hand side matrix B. On exit, the solution matrix X.
00171 //
00172 //   info    (OUT - int)
00173 //   0   : function completed normally
00174 //   < 0 : The ith argument, where i = abs(return value) had an illegal value.
00175 //   > 0 : if INFO =  i,  U(i,i)  is  exactly  zero;  the  matrix is singular and its inverse could not be computed.
00176 int getrs (char transa, matrix_t& a,
00177         pivot_t& ipivot, matrix_t& b)
00178 {
00179     matrix_t::value_type* _a = a.data().begin();
00180     int a_n = int(a.size1());
00181     int _lda = a_n;
00182     int p_n = int(ipivot.size());
00183 
00184     matrix_t::value_type* _b = b.data().begin();
00185     int b_n = int(b.size1());
00186     int _ldb = b_n;
00187     int _nrhs = int(b.size2()); /* B's size2 is the # of vectors on rhs */
00188 
00189     if (a_n != b_n) /*Test to see if AX=B has correct dimensions */
00190         return -101;
00191     if (p_n < a_n)     /*Check to see if ipivot is big enough */
00192         return -102;
00193 
00194     int _info;
00195     rawLAPACK::getrs (transa, a_n, _nrhs, _a,   _lda, ipivot.data().begin(), 
00196                 _b, _ldb, _info);
00197 
00198     return _info;
00199 } 
00200 
00201 }//namespace LAPACK
00202 }//namespace Bayesian_filter_matrix

Generated on Wed Oct 4 22:57:23 2006 for Bayes++ Bayesian Filtering Classes by  doxygen 1.4.6