LAPACKを使用してSVDを計算する例を知っていますか?
12248 次
1 に答える
13
このルーチンdgesdd
は、倍精度行列の SVD を計算します。使用方法の例だけが必要ですか? ドキュメントを読んでみましたか?
C LAPACK バインディングを使用した例 (これはちょうど今書いたものであり、実際にはテストしていないことに注意してください。また、clapack への引数の正確な型はプラットフォームによって多少異なるため、int
別のものに変更する必要がある場合があることに注意してください):
#include <clapack.h>
void SingularValueDecomposition(int m, // number of rows in matrix
int n, // number of columns in matrix
int lda, // leading dimension of matrix
double *a) // pointer to top-left corner
{
// Setup a buffer to hold the singular values:
int numberOfSingularValues = m < n ? m : n;
double *s = malloc(numberOfSingularValues * sizeof s[0]);
// Setup buffers to hold the matrices U and Vt:
double *u = malloc(m*m * sizeof u[0]);
double *vt = malloc(n*n * sizeof vt[0]);
// Workspace and status variables:
double workSize;
double *work = &workSize;
int lwork = -1;
int *iwork = malloc(8 * numberOfSingularValues * sizeof iwork[0]);
int info = 0;
// Call dgesdd_ with lwork = -1 to query optimal workspace size:
dgesdd_("A", &m, &n, a, &lda, s, u, &m, vt, &n, work, &lwork, iwork, &info);
if (info) // handle error conditions here
// Optimal workspace size is returned in work[0].
lwork = workSize;
work = malloc(lwork * sizeof work[0]);
// Call dgesdd_ to do the actual computation:
dgesdd_("A", &m, &n, a, &lda, s, u, &m, vt, &n, work, &lwork, iwork, &info);
if (info) // handle error conditions here
// Cleanup workspace:
free(work);
free(iwork);
// do something useful with U, S, Vt ...
// and then clean them up too:
free(s);
free(u);
free(vt);
}
于 2011-02-18T23:19:54.907 に答える