18 #ifndef __utlVNLLapack_h 19 #define __utlVNLLapack_h 70 template <
class T>
inline void 71 syev_VnlMatrix (
const vnl_matrix<T>& mat, vnl_vector<T>& eigenValues, vnl_matrix<T>& eigenVectors)
73 utlException(mat.rows()!=mat.columns(),
"The matrix should be symmetric");
74 char JOBZ=
'V', UPLO=
'U';
75 int N = mat.rows(),INFO=0, LWORK=-1;
78 eigenValues.set_size(N);
89 INFO = utl::syev<T>(LAPACK_COL_MAJOR, JOBZ,UPLO,N,eigenVectors.data_block(), N, eigenValues.data_block());
90 utlGlobalException(INFO,
"LAPACK library function dsyev_() returned error code INFO=" << INFO);
102 template <
class T>
inline void 103 syevd_VnlMatrix (
const vnl_matrix<T>& mat, vnl_vector<T>& eigenValues, vnl_matrix<T>& eigenVectors)
105 utlException(mat.rows()!=mat.columns(),
"The matrix should be symmetric");
106 char JOBZ=
'V', UPLO=
'U';
107 int N = mat.rows(),INFO=0, LWORK=-1, LIWORK=-1;
110 eigenValues.set_size(N);
125 INFO = utl::syevd<T>(LAPACK_COL_MAJOR, JOBZ,UPLO,N,eigenVectors.data_block(), N, eigenValues.data_block());
126 utlGlobalException(INFO,
"LAPACK library function dsyevd_() returned error code INFO=" << INFO);
139 template <
class T>
inline void 140 gesvd_VnlMatrix(
const vnl_matrix<T>& mat, vnl_matrix<T>& U, vnl_vector<T>& s, vnl_matrix<T>& V,
char format=
'S' )
142 utlException(format!=
'S' && format!=
'A',
"wrong format. format should be 'A' or 'S' ");
143 int M=mat.rows(), N=mat.columns(), INFO=0;
148 U.set_size(min_MN, M);
149 V.set_size(N, min_MN);
158 char formatU=format, formatV=format;
159 int LDA=M, LDU=M, LWORK=-1;
160 vnl_matrix<T> matTmp;
173 T* superb =
new T[min_MN];
174 INFO=utl::gesvd<T>(LAPACK_COL_MAJOR, formatU, formatV, M, N, matTmp.data_block(), LDA, s.data_block(), U.data_block(), LDU, V.data_block(), LDVT, superb);
176 U.inplace_transpose();
178 utlGlobalException(INFO,
"LAPACK library function dgesvd_() returned error code INFO=" << INFO);
193 template <
class T>
inline void 194 gesdd_VnlMatrix(
const vnl_matrix<T>& mat, vnl_matrix<T>& U, vnl_vector<T>& s, vnl_matrix<T>& V,
char format=
'S' )
196 utlException(format!=
'S' && format!=
'A',
"wrong format. format should be 'A' or 'S' ");
197 int M=mat.rows(), N=mat.columns(), INFO=0;
202 U.set_size(min_MN, M);
203 V.set_size(N, min_MN);
213 vnl_matrix<T> matTmp;
215 char formatU=format, formatV=format;
216 int LDA=M, LDU=M, LWORK;
232 INFO=utl::gesdd<T>(LAPACK_COL_MAJOR, format, M, N, matTmp.data_block(), LDA, s.data_block(), U.data_block(), LDU, V.data_block(), LDVT);
233 U.inplace_transpose();
235 utlGlobalException(INFO,
"LAPACK library function dgesdd_() returned error code INFO=" << INFO);
248 template <
class T>
inline void 251 #ifdef UTL_USE_FASTLAPACK 253 syevd_VnlMatrix<T>(mat, eigenValues, eigenVectors);
256 syev_VnlMatrix<T>(mat, eigenValues, eigenVectors);
269 template <
class T>
inline void 270 SVDVnlMatrix (
const vnl_matrix<T>& mat, vnl_matrix<T>& U, vnl_vector<T>& s, vnl_matrix<T>& V,
char format=
'S' )
272 #ifdef UTL_USE_FASTLAPACK 274 utl::gesdd_VnlMatrix<T>(mat, U, s, V, format);
277 utl::gesvd_VnlMatrix<T>(mat, U, s, V, format);
283 template <
class T>
inline void 290 int lwork=-1, INFO=0;
291 int* ipiv=
new int[n];
305 utl::sytrf<T>(LAPACK_COL_MAJOR, uplo,n,result.data_block(),n,ipiv);
306 INFO=utl::sytri<T>(LAPACK_COL_MAJOR, uplo,n,result.data_block(),n,ipiv);
309 utlGlobalException(INFO>0,
"The matrix is singular and its inverse could not be computed. \ 310 LAPACK library function sytrid_() returned error code INFO=" << INFO);
311 utlGlobalException(INFO<0,
"LAPACK library function sytrid_() returned error code INFO=" << INFO);
313 T* data = result.data_block();
314 for (
int i = 0; i < n; ++i )
315 for (
int j = 0; j < i; ++j )
316 data[j*n+i] = data[i*n+j];
320 template <
class T>
inline void 324 vnl_matrix<T> eigenVectors, tmp, S(N,N,0.0);
325 vnl_vector<T> eigenValues, v;
327 for (
unsigned i=0; i<N; ++i)
329 if ( eigenValues[i]>eps || eigenValues[i]<-eps )
330 S(i,i) = 1.0/eigenValues[i];
339 template <
class T>
inline void 342 vnl_matrix<T> U,V, tmp;
343 vnl_vector<T> S, uVec, vVec;
346 vnl_matrix<T> diag(S.size(), S.size(),0.0);
347 for (
int i = 0; i < S.size(); i += 1 )
349 if ( S[i]>eps || S[i]<-eps )
350 diag(i,i) = 1.0/S[i];
375 vnl_matrix<T>& projMatrix, vnl_vector<T>& projVector )
377 int n = QInverse.rows();
378 utlException(Aeq.rows()!=n,
"wrong size! Aeq.rows()="<<Aeq.rows()<<
", n="<<n);
379 vnl_matrix<T> AeqT=Aeq.transpose();
380 projMatrix.set_size(n,n);
381 projMatrix.set_identity();
382 vnl_matrix<T> temp, tmp2, tmp3;
void EigenDecompositionSymmetricVnlMatrix(const vnl_matrix< T > &mat, vnl_vector< T > &eigenValues, vnl_matrix< T > &eigenVectors)
EigenDecompositionSymmetricVnlMatrix eigen-decomposition for symmetric matrix.
#define __utl_geev_Matrix(T, FuncName, RowMajorMatrixName, GetRowsFuncName, GetColsFuncName, MatrixGetDataFuncName, VectorName, VectorGetDataFuncName, ReSizeFuncName)
geev_VnlMatrix Calculate non-symmetric eigen-decomposition. Define 3 functions. 1) calculate only eig...
void geev_VnlMatrix(const vnl_matrix< T > &mat, vnl_vector< T > &valReal, vnl_vector< T > &valImg, vnl_matrix< T > &vecRealR, vnl_matrix< T > &vecImgR, vnl_matrix< T > &vecRealL, vnl_matrix< T > &vecImgL)
geev_VnlMatrix calculate non-symmetric eigen-decomposition.
void PInverseSymmericVnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &result, const T eps=1e-8)
#define utlException(cond, expout)
void gesdd_VnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &U, vnl_vector< T > &s, vnl_matrix< T > &V, char format='S')
dgesdd_VnlMatrix dgesdd is faster than dgesvd. http://www.netlib.org/lapack/explore-html/db/db4/dgesd...
void PInverseVnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &result, const T eps=1e-8)
void MatrixCopy(const vnl_matrix< T > &mat, vnl_matrix< T > &matOut, const T alpha, const char trans='N')
MatrixCopy. A := alpha * op(A)
const T & min(const T &a, const T &b)
Return the minimum between a and b.
void ProductVnlMv(const vnl_matrix< T > &A, const vnl_vector< T > &b, vnl_vector< T > &c, const double alpha=1.0, const double beta=0.0)
#define utlGlobalException(cond, expout)
void InverseSymmericVnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &result, const T eps=1e-8)
void syevd_VnlMatrix(const vnl_matrix< T > &mat, vnl_vector< T > &eigenValues, vnl_matrix< T > &eigenVectors)
syevd_VnlMatrix eigen-decomposition for symmetric matrix. dsyevd is faster than dsyev http://www...
void syev_VnlMatrix(const vnl_matrix< T > &mat, vnl_vector< T > &eigenValues, vnl_matrix< T > &eigenVectors)
syev_VnlMatrix eigen-decomposition for symmetric matrix. http://www.netlib.org/lapack/explore-html/dd...
void GetEqualityConstraintProjection(const NDArray< T, 2 > &Aeq, const NDArray< T, 1 > &beq, const NDArray< T, 2 > &QInverse, NDArray< T, 2 > &projMatrix, NDArray< T, 1 > &projVector)
void ProductVnlMMt(const vnl_matrix< T > &A, const vnl_matrix< T > &B, vnl_matrix< T > &C, const double alpha=1.0, const double beta=0.0)
void gesvd_VnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &U, vnl_vector< T > &s, vnl_matrix< T > &V, char format='S')
dgesvd_VnlMatrix
void ProductVnlMM(const vnl_matrix< T > &A, const vnl_matrix< T > &B, vnl_matrix< T > &C, const double alpha=1.0, const double beta=0.0)
void ProductVnlMtM(const vnl_matrix< T > &A, const vnl_matrix< T > &B, vnl_matrix< T > &C, const double alpha=1.0, const double beta=0.0)
void SVDVnlMatrix(const vnl_matrix< T > &mat, vnl_matrix< T > &U, vnl_vector< T > &s, vnl_matrix< T > &V, char format='S')
SVDVnlMatrix.