170 const Matrix_& matrix,
171 const Eigen::Index number,
177 const Eigen::Index smaller = std::min(matrix.rows(), matrix.cols());
178 Eigen::Index requested_number = sanisizer::cast<Eigen::Index>(number);
179 if (requested_number > smaller) {
181 requested_number = smaller;
183 throw std::runtime_error(
"requested number of singular values cannot be greater than the smaller matrix dimension");
186 throw std::runtime_error(
"requested number of singular values must be less than the smaller matrix dimension for IRLBA iterations");
191 if (requested_number == 0) {
192 outD.resize(requested_number);
193 outU.resize(matrix.rows(), requested_number);
194 outV.resize(matrix.cols(), requested_number);
204 (options.
exact_for_large_number && requested_greater_than_or_equal_to_half_smaller(requested_number, smaller))
206 exact(matrix, requested_number, outU, outV, outD);
212 const Eigen::Index extra_work = choose_extra_work(requested_number, options.
extra_work);
216 const Eigen::Index work = choose_requested_plus_extra_work_or_smaller(requested_number, extra_work, smaller);
220 EigenMatrix_ V(matrix.cols(), work);
221 std::mt19937_64 eng(options.
seed);
222 if (options.
initial.has_value()) {
223 const auto& init = *(options.
initial);
224 if (init.size() != V.rows()) {
225 throw std::runtime_error(
"initialization vector does not have expected number of rows");
229 fill_with_random_normals(V, 0, eng);
231 V.col(0) /= V.col(0).norm();
233 bool converged =
false;
234 int iter = 0, mult = 0;
238 Eigen::BDCSVD<EigenMatrix_, Eigen::NoQRPreconditioner | Eigen::ComputeFullU | Eigen::ComputeFullV> svd(work, work);
240 LanczosWorkspace<EigenVector_, Matrix_> lpwork(matrix);
242 EigenMatrix_ W(matrix.rows(), work);
243 EigenMatrix_ Wtmp(matrix.rows(), work);
244 EigenMatrix_ Vtmp(matrix.cols(), work);
246 EigenMatrix_ B(work, work);
247 B.setZero(work, work);
248 EigenVector_ res(work);
249 EigenVector_ F(matrix.cols());
251 EigenVector_ prevS(work);
253 const typename EigenMatrix_::Scalar svtol_actual = choose_singular_value_tolerance(options);
254 const auto inv_eps = choose_invariant_tolerance<typename EigenMatrix_::Scalar>(options);
260 mult += run_lanczos_bidiagonalization(lpwork, W, V, B, eng, k, inv_eps);
269 const auto& BS = svd.singularValues();
270 const auto& BU = svd.matrixU();
271 const auto& BV = svd.matrixV();
274 if (B.coeff(work - 1, work - 1) == 0) {
279 const auto& F = lpwork.F;
287 res = R_F * BU.row(work - 1);
289 Eigen::Index n_converged = 0;
291 const auto Smax = *std::max_element(BS.begin(), BS.end());
292 const auto threshold = Smax * tol;
294 for (Eigen::Index j = 0; j < work; ++j) {
295 if (std::abs(res[j]) <= threshold) {
296 const auto ratio = std::abs(BS[j] - prevS[j]) / BS[j];
297 if (ratio <= svtol_actual) {
303 if (n_converged >= requested_number) {
310 k = update_k(k, requested_number, n_converged, work);
311 Vtmp.leftCols(k).noalias() = V * BV.leftCols(k);
322 Wtmp.leftCols(k).noalias() = W * BU.leftCols(k);
325 B.setZero(work, work);
326 for (I<
decltype(k)> l = 0; l < k; ++l) {
327 B.coeffRef(l, l) = BS[l];
335 B.coeffRef(l, k) = res[l];
341 outD.resize(requested_number);
342 outD = svd.singularValues().head(requested_number);
344 outU.resize(matrix.rows(), requested_number);
345 outU.noalias() = W * svd.matrixU().leftCols(requested_number);
347 outV.resize(matrix.cols(), requested_number);
348 outV.noalias() = V * svd.matrixV().leftCols(requested_number);
353 met.
iterations = (converged ? iter + 1 : iter);