linear algebra - LAPACK SVD (Singular Value Decomposition) -
do yo know example use lapack calculate svd?
the routine dgesdd
computes svd double precision matrix. need example of how use it? have tried reading documentation?
an example using c lapack bindings (note wrote now, , haven't tested it. note exact types arguments clapack vary between platforms may need change int
else):
#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 top-left corner { // setup buffer hold singular values: int numberofsingularvalues = m < n ? m : n; double *s = malloc(numberofsingularvalues * sizeof s[0]); // setup buffers hold matrices u , vt: double *u = malloc(m*m * sizeof u[0]); double *vt = malloc(n*n * sizeof vt[0]); // workspace , status variables: double worksize; double *work = &worksize; int lwork = -1; int *iwork = malloc(8 * numberofsingularvalues * sizeof iwork[0]); int info = 0; // call dgesdd_ lwork = -1 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 returned in work[0]. lwork = worksize; work = malloc(lwork * sizeof work[0]); // call dgesdd_ 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); // useful u, s, vt ... // , clean them too: free(s); free(u); free(vt); }
Comments
Post a Comment