27 std::cerr <<
"svds: X must be 2D arma::matrix" << std::endl;
31 arma::uword m = X.n_rows;
32 arma::uword n = X.n_cols;
33 arma::uword p = (m < n ? m : n);
39 std::cerr <<
"svds: value of k " << k <<
"is greater than number of rows " << m << std::endl;
41 std::cerr <<
"svds: value of k" << k <<
"is greater than number of columns " << n << std::endl;
47 arma::sp_mat B(m+n, m+n);
48 B.submat(arma::span(0, m-1), arma::span(m, m+n-1)) = X;
49 B.submat(arma::span(m, m+n-1), arma::span(0, m-1)) = X.t();
51 if (B.n_nonzero == 0){
53 s = arma::zeros(k, k);
55 std::cerr <<
"svds: input arma::matrix has no non-zero elements" << std::endl;
65 bool eigs_success = eigs_sym(eigval, eigvec, B, 2*k);
68 std::cerr <<
"svds: arma::eigs_sym did not converge!" << std::endl;
74 eigvec = eigvec.cols(sort_index(eigval,
"descend"));
81 arma::uvec return_indices = find(eigval >= 0, k,
"first");
83 U = sqrt(2) * (eigvec.rows(0, m-1));
84 U = U.cols(return_indices);
86 s = eigval.rows(return_indices);
88 V = sqrt(2) * eigvec.rows(m, m+n-1);
89 V = V.cols(return_indices);
91 arma::uvec indices = sort_index(s,
"descend");
92 s = sort(s,
"descend");
VESPUCCI_EXPORT bool svds(const arma::mat &X, arma::uword k, arma::mat &U, arma::vec &s, arma::mat &V)
Vespucci::Math::DimensionReduction::svds Finds a few largest singular values of the arma::matrix X...