Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Lapack.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /** @file Lapack.hh
27  * @ingroup g_mathalgo
28  BIAS c++ wrapper class for lapack fortran77 functions
29  @author Daniel Grest, Jan Woetzel, Felix Woelk
30  @status tested
31 */
32 #ifndef BIASLAPACK_H
33 #define BIASLAPACK_H
34 
35 #include "bias_config.h"
36 
37 #include <Base/Math/Matrix.hh>
38 #include <Base/Math/Vector.hh>
39 
40 #include <vector>
41 
42 
43 /** @addtogroup g_mathalgo
44  \brief C++ Wrappers for important Lapack fortran77 functions
45 
46  Functions from Lapack, e.g. solving systems of linear equations
47  or computing eigenvalues
48  For further information have a look here
49  <a href="http://www.netlib.org/lapack/lug/">
50  http://www.netlib.org/lapack/lug/</a>.
51 
52  @{
53 */
54 
55 /** solve linear equations using LU factorization */
56 BIASMathAlgo_EXPORT
59  const BIAS::Vector<double> &b);
60 
61 /** linear least squares solves |Ax-b|=min
62  res=0 on success, anything else
63  rudimentary tested (woelk)
64  @author woetzel */
65 BIASMathAlgo_EXPORT
68  const BIAS::Vector<double> &b, int &res);
69 
70 /** as above but ignores success info (res) */
71 BIASMathAlgo_EXPORT
74  const BIAS::Vector<double> &b)
75 { int res; return Lapack_LLS_QR_linear_solve(A, b, res); }
76 
77 
78 /** weighted linear least squares
79  solves |B^-1(b-Ax)|=min
80  res=0 on success, anything else
81  rudimentary tested
82  @author woelk 04 2003 */
83 BIASMathAlgo_EXPORT
86  const BIAS::Vector<double> &b,
87  const BIAS::Matrix<double> &B, int &res);
88 
89 
90 /** @brief Coputes the Cholesky decomposition of a symmetric positive definit
91  matrix A.
92 
93  When ul='U', the factorization has the form
94  A = UL^T * UL
95  where UL is a upper triangular matrix.
96  When ul='L',the factorization has the form
97  A = UL * UL^T
98  where UL is a lower triangular matrix.
99  Wrapper for lapacks dpotf2.
100 
101 
102  @author woelk 09/2007 */
103 BIASMathAlgo_EXPORT
106  const char ul='U');
107 
108 
109 /** @brief computes Mahalanobis distance V^T Sigma^-1 V efficiently using
110  * cholesky decomposition and exploitation of symmetry
111  *
112  * Sigma must be square and symmetric positive definite, same dim as V
113  *
114  * About 10 times faster (for 6x6) than V^T svd.invert(Sigma) V,
115  * but it still has potential for optimization, see doc in code.
116  *
117  * @author koeser 11/2007 */
118 BIASMathAlgo_EXPORT
120  const BIAS::Vector<double> &d);
121 
122 /** @brief computes squared Mahalanobis distance V^T Sigma^-1 V efficiently
123  * using cholesky decomposition and exploitation of symmetry
124  *
125  * Sigma must be square and symmetric positive definite, same dim as V
126  *
127  * About 10 times faster (for 6x6) than V^T svd.invert(Sigma) V,
128  * but it still has potential for optimization, see doc in code.
129  *
130  * @author koeser 11/2007 */
131 BIASMathAlgo_EXPORT
132 double MahalanobisDistance(const BIAS::Matrix<double> &Sigma,
133  const BIAS::Vector<double> &d);
134 
135 
136 
137 // *********************** Eigenvalue problems *******************
138 
139 /** @brief Solve symmetric eigenvalue problem (eigenvalues only)
140  @note Wrapper for LAPACK routine dsyev.
141  @status untested
142  @author grest
143 */
144 BIASMathAlgo_EXPORT
147 
148 /** @brief Compute eigenvalues and eigenvectors of a symmetric
149  real square matrix.
150  @note Wrapper for LAPACK routine dsyevd.
151  @author woelk 11/2006
152 */
153 BIASMathAlgo_EXPORT
155  BIAS::Vector<double>& eigenvalues,
156  std::vector<BIAS::Vector<double> >& eigenvectors);
157 
158 /** @brief Solve symmetric eigenvalue problem for matrix in packed storage.
159  @param[in] N Number of rows/cols of N x N matrix A
160  @param[in] A Data vector of A in packed storage of length N*(N+1)/2.
161  If upper triangle matrix is given, vector contains matrix rows
162  above the diagonal (i.e. A11, A12, A13, ..., A1N, A22, A23, ...),
163  otherweise vector contains matrix columns below the diagonal
164  (i.e. A11, A21, A31, ..., AN1, A22, A32, ...).
165  @param[out] eigVals Returns eigenvalues in ascending order
166  @param[out] eigVecs Return eigenvectors for eigenvalues
167  @param[in] upperTriangle Specifies if packed vector for matrix A is given
168  as upper (row-wise) or lower (column-wise) triangle.
169  @note Wrapper for LAPACK routine dspev.
170  @author esquivel 08/2013
171  */
172 BIASMathAlgo_EXPORT
173 int Packed_symmetric_eigenvalue_solve(long int N,
174  const BIAS::Vector<double> &A,
175  BIAS::Vector<double>& eigVals,
176  std::vector<BIAS::Vector<double> >& eigVecs,
177  const bool upperTriangle = true);
178 
179 /** solve unsymmetric eigenvalue problems (for a rectangular matrix)
180  @status untested, grest
181 */
182 BIASMathAlgo_EXPORT
183 int
186 
187 /** solve general eigenproblem for a general quadratix (n x n) matrix.
188  computes eigenvalues and eigenvectors of A.
189  (Jan Woetzel), 03/2002
190 **/
191 BIASMathAlgo_EXPORT
192 int
194  BIAS::Vector<double> &ret_EigenValuesReal,
195  BIAS::Vector<double> &ret_EigenValuesImag,
196  BIAS::Matrix<double> &eigenVectors);
197 
198 /** solve general eigenproblem.
199  computes singular value decomposition of a rectangular m x n matrix A
200  calls extern liblapack routine.
201  better use SVD.hh
202  @params ret_S vector with singular values of A with s(i) >= s(i+1)
203  @params ret_U m x m orthogonal/unitary matrix U
204  @params ret_VT n x n orthogonal/unitary matrx transpose(V)
205  Jan Woetzel 03/2002, changed for Matrix by Daniel Grest, July 2002
206  woelk: added jobu and jobvt
207  jobu: 'A' - calculate U
208  'N' - do not calculate U (much faster)
209  not tested
210  see man dgesvd*/
211 BIASMathAlgo_EXPORT
212 int
213 General_singular_value_decomposition(char jobu, char jobvt,
214  const BIAS::Matrix<double> &A,
215  BIAS::Vector<double> &ret_S,
216  BIAS::Matrix<double> &ret_U,
217  BIAS::Matrix<double> &ret_VT);
218 
219 BIASMathAlgo_EXPORT
220 inline int
222  BIAS::Vector<double> &ret_S,
223  BIAS::Matrix<double> &ret_U,
224  BIAS::Matrix<double> &ret_VT)
225 { return General_singular_value_decomposition('A', 'A', A, ret_S, ret_U,
226  ret_VT); }
227 
228 BIASMathAlgo_EXPORT
229 inline int
231  BIAS::Vector<double> &ret_S,
232  BIAS::Matrix<double> &ret_VT)
233 { BIAS::Matrix<double> ret_U;
234  return General_singular_value_decomposition('N', 'A', A, ret_S, ret_U,
235  ret_VT); }
236 
237 
238 /** converts the argument Fortran_Matrix to a Matrix and returns it.
239  it mainly tranposes the data array because Fortran-Matrices
240  different row/col-order
241  @author Jan Woetzel
242  @status bug fixed by Daniel Grest (04/2003) */
243 BIASMathAlgo_EXPORT
246 
247 /** converts the argument Fortran_Matrix to a Matrix and returns it.
248  it mainly tranposes the data array because Fortran-Matrices
249  different row/col-order, same as above but now copying necessary
250  @author Daniel Grest
251  @status beta April 2003*/
252 BIASMathAlgo_EXPORT
253 void
255  BIAS::Matrix<double> &res);
256 
257 
258 BIASMathAlgo_EXPORT
260  BIAS::Matrix<double> &A_, /* m x m matrix */
261  BIAS::Matrix<double> &B_ /* m x m matrx */ );
262 
263 //@}
264 
265 
266 #endif
267 // BIASLAPACK_H
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
BIAS::Matrix< double > generalised_eigenvalue_matrix_solve(BIAS::Matrix< double > &A_, BIAS::Matrix< double > &B_)
Definition: Lapack.cpp:826
BIAS::Vector< double > Lapack_LLS_QR_linear_solve(const BIAS::Matrix< double > &A, const BIAS::Vector< double > &b, int &res)
linear least squares solves |Ax-b|=min res=0 on success, anything else rudimentary tested (woelk) ...
Definition: Lapack.cpp:311
int Eigenvalue_solve(const BIAS::Matrix< double > &A, BIAS::Vector< double > &wr, BIAS::Vector< double > &wi)
solve unsymmetric eigenvalue problems (for a rectangular matrix) untested, grest ...
Definition: Lapack.cpp:580
BIAS::Vector< double > Lapack_WLLS_solve(const BIAS::Matrix< double > &A, const BIAS::Vector< double > &b, const BIAS::Matrix< double > &B, int &res)
weighted linear least squares solves |B^-1(b-Ax)|=min res=0 on success, anything else rudimentary tes...
Definition: Lapack.cpp:376
double SquaredMahalanobisDistance(const BIAS::Matrix< double > &Sigma, const BIAS::Vector< double > &V)
computes Mahalanobis distance V^T Sigma^-1 V efficiently using cholesky decomposition and exploitatio...
Definition: Lapack.cpp:787
BIAS::Vector< double > Lapack_LU_linear_solve(const BIAS::Matrix< double > &A, const BIAS::Vector< double > &b)
solve linear equations using LU factorization
Definition: Lapack.cpp:269
int Lapack_Cholesky_SymmetricPositiveDefinit(const BIAS::Matrix< double > &A, BIAS::Matrix< double > &UL, const char ul)
Coputes the Cholesky decomposition of a symmetric positive definit matrix A.
Definition: Lapack.cpp:212
BIAS::Matrix< double > Fortran_Matrix_to_Matrix(const Fortran_Matrix< double > &FMat)
converts the argument Fortran_Matrix to a Matrix and returns it.
Definition: Lapack.cpp:169
int General_singular_value_decomposition(char jobu, char jobvt, const BIAS::Matrix< double > &A, BIAS::Vector< double > &ret_S, BIAS::Matrix< double > &ret_U, BIAS::Matrix< double > &ret_VT)
solve general eigenproblem.
Definition: Lapack.cpp:688
int Eigenproblem_quadratic_matrix(const BIAS::Matrix< double > &A, BIAS::Vector< double > &ret_EigenValuesReal, BIAS::Vector< double > &ret_EigenValuesImag, BIAS::Matrix< double > &eigenVectors)
solve general eigenproblem for a general quadratix (n x n) matrix.
Definition: Lapack.cpp:626
double MahalanobisDistance(const BIAS::Matrix< double > &Sigma, const BIAS::Vector< double > &V)
computes squared Mahalanobis distance V^T Sigma^-1 V efficiently using cholesky decomposition and exp...
Definition: Lapack.cpp:780
int Packed_symmetric_eigenvalue_solve(long int N, const BIAS::Vector< double > &A, BIAS::Vector< double > &eigVals, std::vector< BIAS::Vector< double > > &eigVecs, const bool upperTriangle)
Solve symmetric eigenvalue problem for matrix in packed storage.
Definition: Lapack.cpp:535
BIAS::Vector< double > Upper_symmetric_eigenvalue_solve(const BIAS::Matrix< double > &A)
Solve symmetric eigenvalue problem (eigenvalues only)
Definition: Lapack.cpp:438