/* Main program */ int main() { /* Locals */ lapack_int m = M, n = N, lda = LDA, ldu = LDU, ldvt = LDVT, info; double superb[min(M,N)-1]; /* Local arrays */ double s[N], u[LDU*M], vt[LDVT*N]; double a[LDA*N] = { 8.79, 6.11, -9.15, 9.57, -3.49, 9.84, 9.93, 6.91, -7.93, 1.64, 4.02, 0.15, 9.83, 5.04, 4.86, 8.83, 9.80, -8.99, 5.45, -0.27, 4.85, 0.74, 10.00, -6.02, 3.16, 7.98, 3.01, 5.80, 4.27, -5.31 }; /* Executable statements */ printf( "LAPACKE_dgesvd (column-major, high-level) Example Program Results\n" ); /* Compute SVD */ info = LAPACKE_dgesvd( LAPACK_COL_MAJOR, 'A', 'A', m, n, a, lda, s, u, ldu, vt, ldvt, superb ); /* Check for convergence */ if( info > 0 ) { printf( "The algorithm computing SVD failed to converge.\n" ); exit( 1 ); } /* Print singular values */ print_matrix( "Singular values", 1, n, s, 1 ); /* Print left singular vectors */ print_matrix( "Left singular vectors (stored columnwise)", m, n, u, ldu ); /* Print right singular vectors */ print_matrix( "Right singular vectors (stored rowwise)", n, n, vt, ldvt ); return 0; } /* End of LAPACKE_dgesvd Example */
int phonopy_pinv(double *data_out, const double *data_in, const int m, const int n, const double cutoff) { int i, j, k; lapack_int info; double *s, *a, *u, *vt, *superb; a = (double*)malloc(sizeof(double) * m * n); s = (double*)malloc(sizeof(double) * min(m,n)); u = (double*)malloc(sizeof(double) * m * m); vt = (double*)malloc(sizeof(double) * n * n); superb = (double*)malloc(sizeof(double) * (min(m,n) - 1)); for (i = 0; i < m * n; i++) { a[i] = data_in[i]; } info = LAPACKE_dgesvd(LAPACK_ROW_MAJOR, 'A', 'A', (lapack_int)m, (lapack_int)n, a, (lapack_int)n, s, u, (lapack_int)m, vt, (lapack_int)n, superb); for (i = 0; i < n * m; i++) { data_out[i] = 0; } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { for (k = 0; k < min(m,n); k++) { if (s[k] > cutoff) { data_out[j * m + i] += u[i * m + k] / s[k] * vt[k * n + j]; } } } } free(a); free(s); free(u); free(vt); free(superb); return (int)info; }
double* HFMatrix<T>::pinv(double *matrix, int rows, int cols) { lapack_int m, n, lda, ldu, ldvt, info; char jobu = 'A'; char jobvt = 'A'; m = rows; n = cols; lda = rows; ldu = rows; ldvt = cols; int ssize = HFMath::min(m, n); double *s = (double*)malloc(sizeof(double)*ssize); double *u = (double*)malloc(sizeof(double)*m*m); double *vt = (double*)malloc(sizeof(double)*n*n); double *sb = (double*)malloc(sizeof(double)*(ssize-1)); info = LAPACKE_dgesvd(LAPACK_COL_MAJOR, jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, sb); ASSERT(info == 0); print_matrix("Singular values", 1, n, s, 1); print_matrix("Left singular vectors(stored columnwise)", m, n, u, ldu); print_matrix("Left singular vectors(stored rowwise)", n, n, vt, ldvt); for(int i = 0; i < n; ++i) { for(int j = 0; j < ssize; ++j) vt[i*n+j] = vt[i*n+j]/s[j]; } double *result = (double*)malloc(m*n*sizeof(double)); cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, n, m, n, 1.0, vt, ldvt, u, ldu, 0, result, n); free(s); free(u); free(vt); free(sb); return result; }
static void bench_gesvd(int size, double *A, double *U, double *VT, double *S, double *superb) { int i, iter = 1; double start, stop; double gflops, peak; if ((double) size * (double) size * (double) size <= 1.e12) iter = 2; if ((double) size * (double) size * (double) size <= 1.e10) iter = 3; if ((double) size * (double) size * (double) size <= 1.e8) iter = 5; peak = 0.; for (i = 0; i < iter; i++) { start = gettime(); LAPACKE_dgesvd(LAPACK_COL_MAJOR, 'A', 'A', size, size, A, size, S, U, size, VT, size, superb); stop = gettime(); gflops = getgflops(size, stop - start); if (peak < gflops) peak = gflops; } printf("%4d, %10.3f\n", size, peak); fflush(stdout); return; }
int main() { lapack_int m = 3, n = 4; lapack_int ret; double *A = new double[m * n]; double *U = new double[m * m]; double *VT = new double[n * n]; double *S = new double[std::min(m, n)]; double *superb = new double[std::min(m, n)]; //setting A matrix A[0 + 0 * m] = 1; A[0 + 1 * m] = 2; A[0 + 2 * m] = 3; A[0 + 3 * m] = 4; A[1 + 0 * m] = 5; A[1 + 1 * m] = 6; A[1 + 2 * m] = 7; A[1 + 3 * m] = 8; A[2 + 0 * m] = 9; A[2 + 1 * m] = 10; A[2 + 2 * m] = 11; A[2 + 3 * m] = 12; printf("A ="); printmat(m, n, A, m); printf("\n"); ret = LAPACKE_dgesvd(LAPACK_COL_MAJOR, 'A', 'A', m, n, A, m, S, U, m, VT, n, superb); //print out some results. printf("#singular values\n"); printf("S ="); printmat(std::min(m, n), 1, S, 1); printf("\n"); printf("#U matrix\n"); printf("U ="); printmat(m, m, U, m); printf("\n"); printf("#V^t matrix\n"); printf("Vt ="); printmat(n, n, VT, n); printf("\n"); printf("#You can check result by octave/matlab by\n"); printf("[u, s, v] = svd (A) \n"); printf("#u*s*v' should be equal to A\n"); printf("u*s*v' \n"); printf("U*diag(S,%d,%d)*Vt \n",m,n); delete[]superb; delete[]S; delete[]VT; delete[]U; delete[]A; }
int svd(int m, int n, scalar* A,scalar* U,scalar* S,scalar* VT,scalar* superb){ return LAPACKE_dgesvd( LAPACK_ROW_MAJOR, 'A','A',m,n,A,n,S,U,m,VT,n,superb); }