595 |
jab |
1 |
/* ----------------------------------------------------------------------- *
|
|
|
2 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
3 |
* Copyright (C) the authors and DTU Informatics
|
|
|
4 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
5 |
* ----------------------------------------------------------------------- */
|
|
|
6 |
|
39 |
bj |
7 |
#if !defined(LAPACKFUNC_H_HAA_AGUST_2001)
|
|
|
8 |
#define LAPACKFUNC_H_HAA_AGUST_2001
|
|
|
9 |
|
|
|
10 |
#if defined(_MSC_VER)
|
595 |
jab |
11 |
#if defined(_DEBUG)
|
|
|
12 |
#pragma message("Note: including lib: lapackd.lib and ignoring defaultlib : LIBC\n")
|
|
|
13 |
#pragma comment(lib, "lapackd.lib")
|
|
|
14 |
#else
|
|
|
15 |
#pragma message("Note: including lib: lapack.lib and ignoring defaultlib : LIBC\n")
|
|
|
16 |
#pragma comment(lib, "lapack.lib")
|
|
|
17 |
#endif
|
|
|
18 |
#pragma comment(linker, "/NODEFAULTLIB:LIBC.LIB")
|
39 |
bj |
19 |
#endif
|
|
|
20 |
|
|
|
21 |
#include "Matrix.h"
|
|
|
22 |
#include "Vector.h"
|
|
|
23 |
|
|
|
24 |
namespace LinAlg
|
|
|
25 |
{
|
|
|
26 |
|
|
|
27 |
/*!
|
|
|
28 |
\file LapackFunc.h
|
|
|
29 |
\brief Interface to some of the LAPACK functionality.
|
|
|
30 |
|
|
|
31 |
These are functions which more or less directly interface with the
|
|
|
32 |
Lapack provided algorithms.
|
|
|
33 |
|
|
|
34 |
For indepth reference to the LAPACK functions see:
|
|
|
35 |
LAPACK Users' Guide - 3rd Edition,
|
|
|
36 |
by E. Anderson et al.,
|
|
|
37 |
ISBN 0-89871-447-8,
|
|
|
38 |
Published by SIAM,
|
|
|
39 |
|
|
|
40 |
This book is also available at: \URL{http://www.netlib.org/lapack/lug/lapack_lug.html}
|
|
|
41 |
|
|
|
42 |
The official LAPACK sites where from the source can be downloaded are:
|
|
|
43 |
\URL{http://www.netlib.org/clapack/} and
|
|
|
44 |
\URL{http://www.netlib.org/lapack/}
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
NB: When running this in MS Visual C++ it is usually required to set the
|
|
|
48 |
multithread "\MD" compiler option. This is to ensure correct linkage to the
|
|
|
49 |
precompiled library "clapack.lib" and/or "clapackDB.lib".
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
\author Henrik Aanęs
|
|
|
53 |
\version Aug 2001
|
|
|
54 |
*/
|
|
|
55 |
|
|
|
56 |
/*!
|
|
|
57 |
\name Singular Value Decomposition SVD
|
|
|
58 |
|
|
|
59 |
These functions perform the Singular Value Decomposition SVD of
|
|
|
60 |
the MxN matrix A. The SVD is defined by:
|
|
|
61 |
|
|
|
62 |
A=U*S*V^T
|
|
|
63 |
|
|
|
64 |
where:
|
|
|
65 |
- U is a M by M orthogonal matrix
|
|
|
66 |
- V is a N by N orthogonal matrix
|
|
|
67 |
- S is a M by N diaggonal matrix. The values in the diagonal are the singular values
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
\param A the matrix to perform SVD on
|
|
|
71 |
\return U will be resized if it is does not have the correct dimensions
|
|
|
72 |
\return V will be resized if it is does not have the correct dimensions
|
|
|
73 |
\return S will be resized if it is does not have the correct dimensions.
|
|
|
74 |
\exception assert(info==0) for Lapack. Add a throw statement later.
|
|
|
75 |
\version Aug 2001
|
|
|
76 |
\author Henrik Aanęs
|
|
|
77 |
*/
|
|
|
78 |
//@{
|
|
|
79 |
///SVD of A, where the singular values are returned in a Vector.
|
|
|
80 |
void SVD(const CMatrix& A,CMatrix& U,CVector& s,CMatrix& V);
|
|
|
81 |
///SVD of A, where the singular values are returned in a 'diagonal' Matrix.
|
|
|
82 |
void SVD(const CMatrix& A,CMatrix& U,CMatrix& S,CMatrix& V);
|
|
|
83 |
///SVD of A, returning only the singular values in a Vector.
|
|
|
84 |
CVector SVD(const CMatrix& A);
|
|
|
85 |
//@}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
/*!
|
|
|
89 |
\name Linear Equations
|
|
|
90 |
These functions solve the system of linear equations
|
|
|
91 |
|
|
|
92 |
A*x=b
|
|
|
93 |
|
|
|
94 |
for x, where:
|
|
|
95 |
- A is a N by N matrix
|
|
|
96 |
- b is a N vector
|
|
|
97 |
- x is a N vector
|
|
|
98 |
|
|
|
99 |
There a speceilaized functions for symetric positive definite (SPD)
|
|
|
100 |
matrices yeilding better performance. These are denote by SPD in
|
|
|
101 |
there function name.
|
|
|
102 |
|
|
|
103 |
\param A the NxN square matrix
|
|
|
104 |
\param b the N vector
|
|
|
105 |
\return x will be resized if it is does not have the correct dimensions
|
|
|
106 |
\exception assert(info==0) for Lapack. Add a throw statement later.
|
|
|
107 |
\exception assert(A.Row()==A.Col()). Add a throw statement later.
|
|
|
108 |
\exception assert(A.Row()==b.Length()). Add a throw statement later.
|
|
|
109 |
\version Aug 2001
|
|
|
110 |
\author Henrik Aanęs
|
|
|
111 |
*/
|
|
|
112 |
//@{
|
|
|
113 |
///Solves Ax=b for x.
|
|
|
114 |
void LinearSolve(const CMatrix& A,const CVector&b,CVector& x);
|
|
|
115 |
///Solves Ax=b for x and returns x.
|
|
|
116 |
CVector LinearSolve(const CMatrix& A,const CVector&b);
|
|
|
117 |
///Solves Ax=b for x, where A is SPD.
|
|
|
118 |
void LinearSolveSPD(const CMatrix& A,const CVector&b,CVector& x);
|
|
|
119 |
///Solves Ax=b for x and returns x, where A is SPD.
|
|
|
120 |
CVector LinearSolveSPD(const CMatrix& A,const CVector&b);
|
|
|
121 |
//@}
|
|
|
122 |
|
|
|
123 |
void LinearSolveSym(const CMatrix& A,
|
|
|
124 |
const CVector&b,
|
|
|
125 |
CVector& x);
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
\name Linear Least Squares
|
|
|
129 |
These functions solve the Linear Least Squares problem:
|
|
|
130 |
|
|
|
131 |
min_x ||Ax-b||^2
|
|
|
132 |
|
|
|
133 |
for x, where:
|
|
|
134 |
- || || denotes the 2-norm
|
|
|
135 |
- A is a M by N matrix. For a well formed M>=N and rank (A)=N. See below.
|
|
|
136 |
- b is a M vector.
|
|
|
137 |
- x is a N vector
|
|
|
138 |
|
|
|
139 |
If the solution is not \em well \em formed the algorithm provided will find a
|
|
|
140 |
solution, x, which is not unique, but which sets the objective function
|
|
|
141 |
to 0. The reson being that the underlining algorithm works by SVD.
|
|
|
142 |
|
|
|
143 |
\param A the MxN matrix
|
|
|
144 |
\param b the M vector
|
|
|
145 |
\return x will be resized if it is does not have the correct dimensions
|
|
|
146 |
\exception assert(info==0) for Lapack. Add a throw statement later.
|
|
|
147 |
\exception assert(A.Rows()==b.Length());. Add a throw statement later.
|
|
|
148 |
\version Aug 2001
|
|
|
149 |
\author Henrik Aanęs
|
|
|
150 |
*/
|
|
|
151 |
//@{
|
|
|
152 |
///Solves the Linear Least Squares problem min_x ||Ax=b||^2 for x.
|
|
|
153 |
void LinearLSSolve(const CMatrix& A,const CVector&b,CVector& x);
|
|
|
154 |
///Solves the Linear Least Squares problem min_x ||Ax=b||^2 for x, and returnes x.
|
|
|
155 |
CVector LinearLSSolve(const CMatrix& A,const CVector&b);
|
|
|
156 |
//@}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
\name Matrix Inversion
|
|
|
160 |
These functions inverts the square matrix A. This matrix A must have
|
|
|
161 |
full rank.
|
|
|
162 |
\param A square matrix
|
|
|
163 |
\return InvA the invers of A for one instance.
|
|
|
164 |
\exception assert(info==0) for Lapack. This wil among others happen if A is rank deficient. Add a throw statement later.
|
|
|
165 |
\exception assert(A.Rows()==A.Cols()). Add a throw statement later.
|
|
|
166 |
\version Aug 2001
|
|
|
167 |
\author Henrik Aanęs
|
|
|
168 |
*/
|
|
|
169 |
//@{
|
|
|
170 |
///Invertes the square matrix A. That is here A is altered as opposed to the other Invert functions.
|
|
|
171 |
void Invert(CMatrix& A);
|
|
|
172 |
/// Returns the inverse of the square matrix A in InvA.
|
|
|
173 |
void Inverted(const CMatrix& A,CMatrix& InvA);
|
|
|
174 |
/// Returns the inverse of the square matrix A.
|
|
|
175 |
CMatrix Inverted(const CMatrix& A);
|
|
|
176 |
//@}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
\name QR Factorization
|
|
|
181 |
This function returns the QR factorization of A, such that Q*R=A where
|
|
|
182 |
Q is a orthonormal matrix and R is an upper triangular matrix. However,
|
|
|
183 |
in the case of A.Col()>A.Row(), the last A.Col-A.Row columns of Q are
|
|
|
184 |
'carbage' and as such not part of a orthonormal matrix.
|
|
|
185 |
\param A the input matrix
|
|
|
186 |
\return Q an orthonormal matrix. (See above)
|
|
|
187 |
\return R an upper triangular matrix.
|
|
|
188 |
\exception assert(info==0) for Lapack. This wil among others happen if A is rank deficient. Add a throw statement later.
|
|
|
189 |
\exception assert(A.Rows()>0 && A.Cols()>0). Add a throw statement later.
|
|
|
190 |
\version Aug 2001
|
|
|
191 |
\author Henrik Aanęs
|
|
|
192 |
*/
|
|
|
193 |
//@{
|
|
|
194 |
void QRfact(const CMatrix& A,CMatrix& Q, CMatrix& R);
|
|
|
195 |
//@}
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
\name RQ Factorization
|
|
|
200 |
This function returns the RQ factorization of A, such that R*Q=A where
|
|
|
201 |
Q is a orthonormal matrix and R is an upper triangular matrix. However,
|
|
|
202 |
in the case of A not beeing a square matrix, there might be some fuck up of Q.
|
|
|
203 |
\param A the input matrix
|
|
|
204 |
\return Q an orthonormal matrix. (See above)
|
|
|
205 |
\return R an upper triangular matrix.
|
|
|
206 |
\exception assert(info==0) for Lapack. This wil among others happen if A is rank deficient. Add a throw statement later.
|
|
|
207 |
\exception assert(A.Rows()>0 && A.Cols()>0). Add a throw statement later.
|
|
|
208 |
\version Aug 2001
|
|
|
209 |
\author Henrik Aanęs
|
|
|
210 |
*/
|
|
|
211 |
//@{
|
|
|
212 |
void RQfact(const CMatrix& A,CMatrix& R, CMatrix& Q);
|
|
|
213 |
//@}
|
|
|
214 |
|
379 |
jab |
215 |
/**
|
|
|
216 |
\name Find eigensolutions of a symmetric real matrix.
|
|
|
217 |
This function accepts a real symmetric matrix Q and a vector b.
|
|
|
218 |
When the function returns, the eigenvalues of the matrix Q will be stored in b and
|
|
|
219 |
the eigenvectors form the columns of Q. This function is based on the
|
|
|
220 |
Lapack function dsyev, and returns its info code. A code of 0 indicates
|
|
|
221 |
success, and a code < 0 indicates an error. Probably Q is not a real symmetric matrix.
|
|
|
222 |
If the code is > 0 "the algorithm failed to converge; code off-diagonal elements
|
|
|
223 |
of an intermediate tridiagonal form did not converge to zero." Presumably this means that
|
|
|
224 |
code contains the number of eigenvalues which are ok.
|
|
|
225 |
\author Andreas B¾rentzen.
|
|
|
226 |
*/
|
|
|
227 |
//@{
|
|
|
228 |
int EigenSolutionsSym(CMatrix& Q, CVector& b);
|
|
|
229 |
//@}
|
39 |
bj |
230 |
}
|
|
|
231 |
|
|
|
232 |
#endif // !defined(LAPACKFUNC_H_HAA_AGUST_2001)
|