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