25#ifdef EIGEN_BDCSVD_SANITY_CHECKS
26#undef eigen_internal_assert
27#define eigen_internal_assert(X) assert(X);
31#include "./InternalHeaderCheck.h"
33#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
39#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
40IOFormat bdcsvdfmt(8, 0,
", ",
"\n",
" [",
"]");
43template <
typename MatrixType_,
int Options>
48template <
typename MatrixType_,
int Options>
49struct traits<BDCSVD<MatrixType_, Options> > : svd_traits<MatrixType_, Options> {
50 typedef MatrixType_ MatrixType;
53template <
typename MatrixType,
int Options>
54struct allocate_small_svd {
55 static void run(JacobiSVD<MatrixType, Options>& smallSvd, Index rows, Index cols,
unsigned int computationOptions) {
56 (void)computationOptions;
57 smallSvd = JacobiSVD<MatrixType, Options>(rows, cols);
61EIGEN_DIAGNOSTICS(push)
62EIGEN_DISABLE_DEPRECATED_WARNING
64template <
typename MatrixType>
65struct allocate_small_svd<MatrixType, 0> {
66 static void run(JacobiSVD<MatrixType>& smallSvd, Index rows, Index cols,
unsigned int computationOptions) {
67 smallSvd = JacobiSVD<MatrixType>(rows, cols, computationOptions);
104template <
typename MatrixType_,
int Options_>
112 using Base::diagSize;
115 typedef MatrixType_ MatrixType;
116 typedef typename Base::Scalar Scalar;
117 typedef typename Base::RealScalar RealScalar;
118 typedef typename NumTraits<RealScalar>::Literal Literal;
122 QRDecomposition = Options & internal::QRPreconditionerBits,
123 ComputationOptions = Options & internal::ComputationOptionsBits,
124 RowsAtCompileTime = Base::RowsAtCompileTime,
125 ColsAtCompileTime = Base::ColsAtCompileTime,
126 DiagSizeAtCompileTime = Base::DiagSizeAtCompileTime,
127 MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime,
128 MaxColsAtCompileTime = Base::MaxColsAtCompileTime,
129 MaxDiagSizeAtCompileTime = Base::MaxDiagSizeAtCompileTime,
130 MatrixOptions = Base::MatrixOptions
135 typedef typename Base::SingularValuesType SingularValuesType;
150 BDCSVD() : m_algoswap(16), m_isTranspose(false), m_compU(false), m_compV(false), m_numIters(0) {}
158 BDCSVD(Index rows, Index cols) : m_algoswap(16), m_numIters(0) {
159 allocate(rows, cols, internal::get_computation_options(Options));
176 EIGEN_DEPRECATED
BDCSVD(Index rows, Index cols,
unsigned int computationOptions) : m_algoswap(16), m_numIters(0) {
177 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, rows, cols);
178 allocate(rows, cols, computationOptions);
186 BDCSVD(
const MatrixType& matrix) : m_algoswap(16), m_numIters(0) {
187 compute_impl(matrix, internal::get_computation_options(Options));
202 EIGEN_DEPRECATED
BDCSVD(
const MatrixType& matrix,
unsigned int computationOptions) : m_algoswap(16), m_numIters(0) {
203 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
204 compute_impl(matrix, computationOptions);
214 BDCSVD&
compute(
const MatrixType& matrix) {
return compute_impl(matrix, m_computationOptions); }
225 EIGEN_DEPRECATED
BDCSVD&
compute(
const MatrixType& matrix,
unsigned int computationOptions) {
226 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
227 return compute_impl(matrix, computationOptions);
230 void setSwitchSize(
int s) {
231 eigen_assert(s >= 3 &&
"BDCSVD the size of the algo switch has to be at least 3.");
236 BDCSVD& compute_impl(
const MatrixType& matrix,
unsigned int computationOptions);
237 void divide(Index firstCol, Index lastCol, Index firstRowW, Index firstColW, Index shift);
238 void computeSVDofM(Index firstCol, Index n, MatrixXr& U, VectorType& singVals, MatrixXr& V);
239 void computeSingVals(
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm, VectorType& singVals,
240 ArrayRef shifts, ArrayRef mus);
241 void perturbCol0(
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm,
const VectorType& singVals,
242 const ArrayRef& shifts,
const ArrayRef& mus, ArrayRef zhat);
243 void computeSingVecs(
const ArrayRef& zhat,
const ArrayRef& diag,
const IndicesRef& perm,
const VectorType& singVals,
244 const ArrayRef& shifts,
const ArrayRef& mus, MatrixXr& U, MatrixXr& V);
245 void deflation43(Index firstCol, Index shift, Index i, Index
size);
246 void deflation44(Index firstColu, Index firstColm, Index firstRowW, Index firstColW, Index i, Index j, Index
size);
247 void deflation(Index firstCol, Index lastCol, Index k, Index firstRowW, Index firstColW, Index shift);
248 template <
typename HouseholderU,
typename HouseholderV,
typename NaiveU,
typename NaiveV>
249 void copyUV(
const HouseholderU& householderU,
const HouseholderV& householderV,
const NaiveU& naiveU,
250 const NaiveV& naivev);
251 void structured_update(Block<MatrixXr, Dynamic, Dynamic> A,
const MatrixXr& B, Index n1);
252 static RealScalar secularEq(RealScalar x,
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm,
253 const ArrayRef& diagShifted, RealScalar shift);
254 template <
typename SVDType>
255 void computeBaseCase(SVDType& svd, Index n, Index firstCol, Index firstRowW, Index firstColW, Index shift);
258 void allocate(Index rows, Index cols,
unsigned int computationOptions);
259 MatrixXr m_naiveU, m_naiveV;
263 ArrayXi m_workspaceI;
265 bool m_isTranspose, m_compU, m_compV, m_useQrDecomp;
266 JacobiSVD<MatrixType, ComputationOptions> smallSvd;
267 HouseholderQR<MatrixX> qrDecomp;
268 internal::UpperBidiagonalization<MatrixX> bid;
269 MatrixX copyWorkspace;
270 MatrixX reducedTriangle;
272 using Base::m_computationOptions;
273 using Base::m_computeThinU;
274 using Base::m_computeThinV;
276 using Base::m_isInitialized;
277 using Base::m_matrixU;
278 using Base::m_matrixV;
279 using Base::m_nonzeroSingularValues;
280 using Base::m_singularValues;
287template <
typename MatrixType,
int Options>
288void BDCSVD<MatrixType, Options>::allocate(Index rows, Index cols,
unsigned int computationOptions) {
289 if (Base::allocate(rows, cols, computationOptions))
return;
291 if (cols < m_algoswap)
292 internal::allocate_small_svd<MatrixType, ComputationOptions>::run(smallSvd, rows, cols, computationOptions);
294 m_computed = MatrixXr::Zero(diagSize() + 1, diagSize());
295 m_compU = computeV();
296 m_compV = computeU();
297 m_isTranspose = (cols > rows);
298 if (m_isTranspose) std::swap(m_compU, m_compV);
304 constexpr Index kMinAspectRatio = 4;
305 constexpr bool disableQrDecomp =
static_cast<int>(QRDecomposition) ==
static_cast<int>(DisableQRDecomposition);
306 m_useQrDecomp = !disableQrDecomp && ((rows / kMinAspectRatio > cols) || (cols / kMinAspectRatio > rows));
308 qrDecomp = HouseholderQR<MatrixX>((std::max)(rows, cols), (std::min)(rows, cols));
309 reducedTriangle =
MatrixX(diagSize(), diagSize());
312 copyWorkspace =
MatrixX(m_isTranspose ? cols : rows, m_isTranspose ? rows : cols);
313 bid = internal::UpperBidiagonalization<MatrixX>(m_useQrDecomp ? diagSize() : copyWorkspace.rows(),
314 m_useQrDecomp ? diagSize() : copyWorkspace.cols());
317 m_naiveU = MatrixXr::Zero(diagSize() + 1, diagSize() + 1);
319 m_naiveU = MatrixXr::Zero(2, diagSize() + 1);
321 if (m_compV) m_naiveV = MatrixXr::Zero(diagSize(), diagSize());
323 m_workspace.resize((diagSize() + 1) * (diagSize() + 1) * 3);
324 m_workspaceI.resize(3 * diagSize());
327template <
typename MatrixType,
int Options>
328BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_impl(
const MatrixType& matrix,
329 unsigned int computationOptions) {
330#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
331 std::cout <<
"\n\n\n================================================================================================="
332 "=====================\n\n\n";
336 allocate(matrix.rows(), matrix.cols(), computationOptions);
338 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
341 if (matrix.cols() < m_algoswap) {
342 smallSvd.compute(matrix);
343 m_isInitialized =
true;
344 m_info = smallSvd.info();
345 if (m_info == Success || m_info == NoConvergence) {
346 if (computeU()) m_matrixU = smallSvd.matrixU();
347 if (computeV()) m_matrixV = smallSvd.matrixV();
348 m_singularValues = smallSvd.singularValues();
349 m_nonzeroSingularValues = smallSvd.nonzeroSingularValues();
355 RealScalar scale = matrix.cwiseAbs().template maxCoeff<PropagateNaN>();
356 if (!(numext::isfinite)(scale)) {
357 m_isInitialized =
true;
362 if (numext::is_exactly_zero(scale)) scale = Literal(1);
365 copyWorkspace = matrix.adjoint() / scale;
367 copyWorkspace = matrix / scale;
374 qrDecomp.compute(copyWorkspace);
375 reducedTriangle = qrDecomp.matrixQR().topRows(diagSize());
376 reducedTriangle.template triangularView<StrictlyLower>().setZero();
377 bid.compute(reducedTriangle);
379 bid.compute(copyWorkspace);
386 m_computed.topRows(diagSize()) = bid.bidiagonal().toDenseMatrix().transpose();
387 m_computed.template bottomRows<1>().setZero();
388 divide(0, diagSize() - 1, 0, 0, 0);
389 if (m_info != Success && m_info != NoConvergence) {
390 m_isInitialized =
true;
395 for (
int i = 0; i < diagSize(); i++) {
396 RealScalar a = abs(m_computed.coeff(i, i));
397 m_singularValues.coeffRef(i) = a * scale;
398 if (a < considerZero) {
399 m_nonzeroSingularValues = i;
400 m_singularValues.tail(diagSize() - i - 1).setZero();
402 }
else if (i == diagSize() - 1) {
403 m_nonzeroSingularValues = i + 1;
410 copyUV(bid.householderV(), bid.householderU(), m_naiveV, m_naiveU);
412 copyUV(bid.householderU(), bid.householderV(), m_naiveU, m_naiveV);
415 if (m_isTranspose && computeV())
416 m_matrixV.applyOnTheLeft(qrDecomp.householderQ());
417 else if (!m_isTranspose && computeU())
418 m_matrixU.applyOnTheLeft(qrDecomp.householderQ());
421 m_isInitialized =
true;
425template <
typename MatrixType,
int Options>
426template <
typename HouseholderU,
typename HouseholderV,
typename NaiveU,
typename NaiveV>
427void BDCSVD<MatrixType, Options>::copyUV(
const HouseholderU& householderU,
const HouseholderV& householderV,
428 const NaiveU& naiveU,
const NaiveV& naiveV) {
431 Index Ucols = m_computeThinU ? diagSize() : rows();
432 m_matrixU = MatrixX::Identity(rows(), Ucols);
433 m_matrixU.topLeftCorner(diagSize(), diagSize()) =
434 naiveV.template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
437 m_matrixU.topLeftCorner(householderU.cols(), diagSize()).applyOnTheLeft(householderU);
439 m_matrixU.applyOnTheLeft(householderU);
442 Index Vcols = m_computeThinV ? diagSize() : cols();
443 m_matrixV = MatrixX::Identity(cols(), Vcols);
444 m_matrixV.topLeftCorner(diagSize(), diagSize()) =
445 naiveU.template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
448 m_matrixV.topLeftCorner(householderV.cols(), diagSize()).applyOnTheLeft(householderV);
450 m_matrixV.applyOnTheLeft(householderV);
462template <
typename MatrixType,
int Options>
463void BDCSVD<MatrixType, Options>::structured_update(Block<MatrixXr, Dynamic, Dynamic> A,
const MatrixXr& B, Index n1) {
469 Map<MatrixXr> A1(m_workspace.data(), n1, n);
470 Map<MatrixXr> A2(m_workspace.data() + n1 * n, n2, n);
471 Map<MatrixXr> B1(m_workspace.data() + n * n, n, n);
472 Map<MatrixXr> B2(m_workspace.data() + 2 * n * n, n, n);
473 Index k1 = 0, k2 = 0;
474 for (Index j = 0; j < n; ++j) {
475 if ((A.col(j).head(n1).array() != Literal(0)).any()) {
476 A1.col(k1) = A.col(j).head(n1);
477 B1.row(k1) = B.row(j);
480 if ((A.col(j).tail(n2).array() != Literal(0)).any()) {
481 A2.col(k2) = A.col(j).tail(n2);
482 B2.row(k2) = B.row(j);
487 A.topRows(n1).noalias() = A1.leftCols(k1) * B1.topRows(k1);
488 A.bottomRows(n2).noalias() = A2.leftCols(k2) * B2.topRows(k2);
490 Map<MatrixXr, Aligned> tmp(m_workspace.data(), n, n);
491 tmp.noalias() = A * B;
496template <
typename MatrixType,
int Options>
497template <
typename SVDType>
498void BDCSVD<MatrixType, Options>::computeBaseCase(SVDType& svd, Index n, Index firstCol, Index firstRowW,
499 Index firstColW, Index shift) {
500 svd.compute(m_computed.block(firstCol, firstCol, n + 1, n));
502 if (m_info != Success && m_info != NoConvergence)
return;
504 m_naiveU.block(firstCol, firstCol, n + 1, n + 1).real() = svd.matrixU();
506 m_naiveU.row(0).segment(firstCol, n + 1).real() = svd.matrixU().row(0);
507 m_naiveU.row(1).segment(firstCol, n + 1).real() = svd.matrixU().row(n);
509 if (m_compV) m_naiveV.block(firstRowW, firstColW, n, n).real() = svd.matrixV();
510 m_computed.block(firstCol + shift, firstCol + shift, n + 1, n).setZero();
511 m_computed.diagonal().segment(firstCol + shift, n) = svd.singularValues().head(n);
527template <
typename MatrixType,
int Options>
528void BDCSVD<MatrixType, Options>::divide(Index firstCol, Index lastCol, Index firstRowW, Index firstColW, Index shift) {
533 const Index n = lastCol - firstCol + 1;
534 const Index k = n / 2;
535 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
539 RealScalar lambda, phi, c0, s0;
543 if (n < m_algoswap) {
546 JacobiSVD<MatrixXr, ComputeFullU | ComputeFullV> baseSvd;
547 computeBaseCase(baseSvd, n, firstCol, firstRowW, firstColW, shift);
549 JacobiSVD<MatrixXr, ComputeFullU> baseSvd;
550 computeBaseCase(baseSvd, n, firstCol, firstRowW, firstColW, shift);
555 alphaK = m_computed(firstCol + k, firstCol + k);
556 betaK = m_computed(firstCol + k + 1, firstCol + k);
560 divide(k + 1 + firstCol, lastCol, k + 1 + firstRowW, k + 1 + firstColW, shift);
561 if (m_info != Success && m_info != NoConvergence)
return;
562 divide(firstCol, k - 1 + firstCol, firstRowW, firstColW + 1, shift + 1);
563 if (m_info != Success && m_info != NoConvergence)
return;
566 lambda = m_naiveU(firstCol + k, firstCol + k);
567 phi = m_naiveU(firstCol + k + 1, lastCol + 1);
569 lambda = m_naiveU(1, firstCol + k);
570 phi = m_naiveU(0, lastCol + 1);
572 r0 = sqrt((abs(alphaK * lambda) * abs(alphaK * lambda)) + abs(betaK * phi) * abs(betaK * phi));
574 l = m_naiveU.row(firstCol + k).segment(firstCol, k);
575 f = m_naiveU.row(firstCol + k + 1).segment(firstCol + k + 1, n - k - 1);
577 l = m_naiveU.row(1).segment(firstCol, k);
578 f = m_naiveU.row(0).segment(firstCol + k + 1, n - k - 1);
580 if (m_compV) m_naiveV(firstRowW + k, firstColW) = Literal(1);
581 if (r0 < considerZero) {
585 c0 = alphaK * lambda / r0;
586 s0 = betaK * phi / r0;
589#ifdef EIGEN_BDCSVD_SANITY_CHECKS
590 eigen_internal_assert(m_naiveU.allFinite());
591 eigen_internal_assert(m_naiveV.allFinite());
592 eigen_internal_assert(m_computed.allFinite());
596 MatrixXr q1(m_naiveU.col(firstCol + k).segment(firstCol, k + 1));
598 for (Index i = firstCol + k - 1; i >= firstCol; i--)
599 m_naiveU.col(i + 1).segment(firstCol, k + 1) = m_naiveU.col(i).segment(firstCol, k + 1);
601 m_naiveU.col(firstCol).segment(firstCol, k + 1) = (q1 * c0);
603 m_naiveU.col(lastCol + 1).segment(firstCol, k + 1) = (q1 * (-s0));
605 m_naiveU.col(firstCol).segment(firstCol + k + 1, n - k) =
606 m_naiveU.col(lastCol + 1).segment(firstCol + k + 1, n - k) * s0;
608 m_naiveU.col(lastCol + 1).segment(firstCol + k + 1, n - k) *= c0;
610 RealScalar q1 = m_naiveU(0, firstCol + k);
612 for (Index i = firstCol + k - 1; i >= firstCol; i--) m_naiveU(0, i + 1) = m_naiveU(0, i);
614 m_naiveU(0, firstCol) = (q1 * c0);
616 m_naiveU(0, lastCol + 1) = (q1 * (-s0));
618 m_naiveU(1, firstCol) = m_naiveU(1, lastCol + 1) * s0;
620 m_naiveU(1, lastCol + 1) *= c0;
621 m_naiveU.row(1).segment(firstCol + 1, k).setZero();
622 m_naiveU.row(0).segment(firstCol + k + 1, n - k - 1).setZero();
625#ifdef EIGEN_BDCSVD_SANITY_CHECKS
626 eigen_internal_assert(m_naiveU.allFinite());
627 eigen_internal_assert(m_naiveV.allFinite());
628 eigen_internal_assert(m_computed.allFinite());
631 m_computed(firstCol + shift, firstCol + shift) = r0;
632 m_computed.col(firstCol + shift).segment(firstCol + shift + 1, k) = alphaK * l.transpose().real();
633 m_computed.col(firstCol + shift).segment(firstCol + shift + k + 1, n - k - 1) = betaK * f.transpose().real();
635#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
636 ArrayXr tmp1 = (m_computed.block(firstCol + shift, firstCol + shift, n, n)).jacobiSvd().singularValues();
639 deflation(firstCol, lastCol, k, firstRowW, firstColW, shift);
640#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
641 ArrayXr tmp2 = (m_computed.block(firstCol + shift, firstCol + shift, n, n)).jacobiSvd().singularValues();
642 std::cout <<
"\n\nj1 = " << tmp1.transpose().format(bdcsvdfmt) <<
"\n";
643 std::cout <<
"j2 = " << tmp2.transpose().format(bdcsvdfmt) <<
"\n\n";
644 std::cout <<
"err: " << ((tmp1 - tmp2).abs() > 1e-12 * tmp2.abs()).transpose() <<
"\n";
645 static int count = 0;
646 std::cout <<
"# " << ++count <<
"\n\n";
647 eigen_internal_assert((tmp1 - tmp2).matrix().norm() < 1e-14 * tmp2.matrix().norm());
653 MatrixXr UofSVD, VofSVD;
655 computeSVDofM(firstCol + shift, n, UofSVD, singVals, VofSVD);
657#ifdef EIGEN_BDCSVD_SANITY_CHECKS
658 eigen_internal_assert(UofSVD.allFinite());
659 eigen_internal_assert(VofSVD.allFinite());
663 structured_update(m_naiveU.block(firstCol, firstCol, n + 1, n + 1), UofSVD, (n + 2) / 2);
665 Map<Matrix<RealScalar, 2, Dynamic>,
Aligned> tmp(m_workspace.data(), 2, n + 1);
666 tmp.noalias() = m_naiveU.middleCols(firstCol, n + 1) * UofSVD;
667 m_naiveU.middleCols(firstCol, n + 1) = tmp;
670 if (m_compV) structured_update(m_naiveV.block(firstRowW, firstColW, n, n), VofSVD, (n + 1) / 2);
672#ifdef EIGEN_BDCSVD_SANITY_CHECKS
673 eigen_internal_assert(m_naiveU.allFinite());
674 eigen_internal_assert(m_naiveV.allFinite());
675 eigen_internal_assert(m_computed.allFinite());
678 m_computed.block(firstCol + shift, firstCol + shift, n, n).setZero();
679 m_computed.block(firstCol + shift, firstCol + shift, n, n).diagonal() = singVals;
691template <
typename MatrixType,
int Options>
692void BDCSVD<MatrixType, Options>::computeSVDofM(Index firstCol, Index n, MatrixXr& U, VectorType& singVals,
694 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
696 ArrayRef col0 = m_computed.col(firstCol).segment(firstCol, n);
697 m_workspace.head(n) = m_computed.block(firstCol, firstCol, n, n).diagonal();
698 ArrayRef diag = m_workspace.head(n);
699 diag(0) = Literal(0);
703 U.resize(n + 1, n + 1);
704 if (m_compV) V.resize(n, n);
706#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
707 if (col0.hasNaN() || diag.hasNaN()) std::cout <<
"\n\nHAS NAN\n\n";
714 while (actual_n > 1 && numext::is_exactly_zero(diag(actual_n - 1))) {
716 eigen_internal_assert(numext::is_exactly_zero(col0(actual_n)));
719 for (Index k = 0; k < actual_n; ++k)
720 if (abs(col0(k)) > considerZero) m_workspaceI(m++) = k;
721 Map<ArrayXi> perm(m_workspaceI.data(), m);
723 Map<ArrayXr> shifts(m_workspace.data() + 1 * n, n);
724 Map<ArrayXr> mus(m_workspace.data() + 2 * n, n);
725 Map<ArrayXr> zhat(m_workspace.data() + 3 * n, n);
727#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
728 std::cout <<
"computeSVDofM using:\n";
729 std::cout <<
" z: " << col0.transpose() <<
"\n";
730 std::cout <<
" d: " << diag.transpose() <<
"\n";
734 computeSingVals(col0, diag, perm, singVals, shifts, mus);
736#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
738 << (m_computed.block(firstCol, firstCol, n, n)).jacobiSvd().singularValues().transpose().reverse()
740 std::cout <<
" sing-val: " << singVals.transpose() <<
"\n";
741 std::cout <<
" mu: " << mus.transpose() <<
"\n";
742 std::cout <<
" shift: " << shifts.transpose() <<
"\n";
745 std::cout <<
"\n\n mus: " << mus.head(actual_n).transpose() <<
"\n\n";
746 std::cout <<
" check1 (expect0) : "
747 << ((singVals.array() - (shifts + mus)) / singVals.array()).head(actual_n).transpose() <<
"\n\n";
748 eigen_internal_assert((((singVals.array() - (shifts + mus)) / singVals.array()).head(actual_n) >= 0).all());
749 std::cout <<
" check2 (>0) : " << ((singVals.array() - diag) / singVals.array()).head(actual_n).transpose()
751 eigen_internal_assert((((singVals.array() - diag) / singVals.array()).head(actual_n) >= 0).all());
755#ifdef EIGEN_BDCSVD_SANITY_CHECKS
756 eigen_internal_assert(singVals.allFinite());
757 eigen_internal_assert(mus.allFinite());
758 eigen_internal_assert(shifts.allFinite());
762 perturbCol0(col0, diag, perm, singVals, shifts, mus, zhat);
763#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
764 std::cout <<
" zhat: " << zhat.transpose() <<
"\n";
767#ifdef EIGEN_BDCSVD_SANITY_CHECKS
768 eigen_internal_assert(zhat.allFinite());
771 computeSingVecs(zhat, diag, perm, singVals, shifts, mus, U, V);
773#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
774 std::cout <<
"U^T U: " << (U.transpose() * U - MatrixXr(MatrixXr::Identity(U.cols(), U.cols()))).norm() <<
"\n";
775 std::cout <<
"V^T V: " << (V.transpose() * V - MatrixXr(MatrixXr::Identity(V.cols(), V.cols()))).norm() <<
"\n";
778#ifdef EIGEN_BDCSVD_SANITY_CHECKS
779 eigen_internal_assert(m_naiveU.allFinite());
780 eigen_internal_assert(m_naiveV.allFinite());
781 eigen_internal_assert(m_computed.allFinite());
782 eigen_internal_assert(U.allFinite());
783 eigen_internal_assert(V.allFinite());
791 for (Index i = 0; i < actual_n - 1; ++i) {
792 if (singVals(i) > singVals(i + 1)) {
794 swap(singVals(i), singVals(i + 1));
795 U.col(i).swap(U.col(i + 1));
796 if (m_compV) V.col(i).swap(V.col(i + 1));
800#ifdef EIGEN_BDCSVD_SANITY_CHECKS
802 bool singular_values_sorted =
803 (((singVals.segment(1, actual_n - 1) - singVals.head(actual_n - 1))).array() >= 0).all();
804 if (!singular_values_sorted)
805 std::cout <<
"Singular values are not sorted: " << singVals.segment(1, actual_n).transpose() <<
"\n";
806 eigen_internal_assert(singular_values_sorted);
812 singVals.head(actual_n).reverseInPlace();
813 U.leftCols(actual_n).rowwise().reverseInPlace();
814 if (m_compV) V.leftCols(actual_n).rowwise().reverseInPlace();
816#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
817 JacobiSVD<MatrixXr> jsvd(m_computed.block(firstCol, firstCol, n, n));
818 std::cout <<
" * j: " << jsvd.singularValues().transpose() <<
"\n\n";
819 std::cout <<
" * sing-val: " << singVals.transpose() <<
"\n";
824template <
typename MatrixType,
int Options>
825typename BDCSVD<MatrixType, Options>::RealScalar BDCSVD<MatrixType, Options>::secularEq(
826 RealScalar mu,
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm,
const ArrayRef& diagShifted,
828 Index m = perm.size();
829 RealScalar res = Literal(1);
830 for (Index i = 0; i < m; ++i) {
834 res += (col0(j) / (diagShifted(j) - mu)) * (col0(j) / (diag(j) + shift + mu));
839template <
typename MatrixType,
int Options>
840void BDCSVD<MatrixType, Options>::computeSingVals(
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm,
841 VectorType& singVals, ArrayRef shifts, ArrayRef mus) {
846 Index n = col0.size();
850 while (actual_n > 1 && numext::is_exactly_zero(col0(actual_n - 1))) --actual_n;
852 for (Index k = 0; k < n; ++k) {
853 if (numext::is_exactly_zero(col0(k)) || actual_n == 1) {
856 singVals(k) = k == 0 ? col0(0) : diag(k);
858 shifts(k) = k == 0 ? col0(0) : diag(k);
863 RealScalar left = diag(k);
865 if (k == actual_n - 1)
866 right = (diag(actual_n - 1) + col0.matrix().norm());
872 while (numext::is_exactly_zero(col0(l))) {
874 eigen_internal_assert(l < actual_n);
880 RealScalar mid = left + (right - left) / Literal(2);
881 RealScalar fMid = secularEq(mid, col0, diag, perm, diag, Literal(0));
882#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
883 std::cout <<
"right-left = " << right - left <<
"\n";
887 std::cout <<
" = " << secularEq(left + RealScalar(0.000001) * (right - left), col0, diag, perm, diag, 0) <<
" "
888 << secularEq(left + RealScalar(0.1) * (right - left), col0, diag, perm, diag, 0) <<
" "
889 << secularEq(left + RealScalar(0.2) * (right - left), col0, diag, perm, diag, 0) <<
" "
890 << secularEq(left + RealScalar(0.3) * (right - left), col0, diag, perm, diag, 0) <<
" "
891 << secularEq(left + RealScalar(0.4) * (right - left), col0, diag, perm, diag, 0) <<
" "
892 << secularEq(left + RealScalar(0.49) * (right - left), col0, diag, perm, diag, 0) <<
" "
893 << secularEq(left + RealScalar(0.5) * (right - left), col0, diag, perm, diag, 0) <<
" "
894 << secularEq(left + RealScalar(0.51) * (right - left), col0, diag, perm, diag, 0) <<
" "
895 << secularEq(left + RealScalar(0.6) * (right - left), col0, diag, perm, diag, 0) <<
" "
896 << secularEq(left + RealScalar(0.7) * (right - left), col0, diag, perm, diag, 0) <<
" "
897 << secularEq(left + RealScalar(0.8) * (right - left), col0, diag, perm, diag, 0) <<
" "
898 << secularEq(left + RealScalar(0.9) * (right - left), col0, diag, perm, diag, 0) <<
" "
899 << secularEq(left + RealScalar(0.999999) * (right - left), col0, diag, perm, diag, 0) <<
"\n";
901 RealScalar shift = (k == actual_n - 1 || fMid > Literal(0)) ? left : right;
904 Map<ArrayXr> diagShifted(m_workspace.data() + 4 * n, n);
905 diagShifted = diag - shift;
907 if (k != actual_n - 1) {
909 RealScalar midShifted = (right - left) / RealScalar(2);
911 if (numext::equal_strict(shift, right)) midShifted = -midShifted;
912 RealScalar fMidShifted = secularEq(midShifted, col0, diag, perm, diagShifted, shift);
913 if (fMidShifted > 0) {
915 shift = fMidShifted > Literal(0) ? left : right;
916 diagShifted = diag - shift;
921 RealScalar muPrev, muCur;
923 if (numext::equal_strict(shift, left)) {
924 muPrev = (right - left) * RealScalar(0.1);
925 if (k == actual_n - 1)
926 muCur = right - left;
928 muCur = (right - left) * RealScalar(0.5);
930 muPrev = -(right - left) * RealScalar(0.1);
931 muCur = -(right - left) * RealScalar(0.5);
934 RealScalar fPrev = secularEq(muPrev, col0, diag, perm, diagShifted, shift);
935 RealScalar fCur = secularEq(muCur, col0, diag, perm, diagShifted, shift);
936 if (abs(fPrev) < abs(fCur)) {
943 bool useBisection = fPrev * fCur > Literal(0);
944 while (!numext::is_exactly_zero(fCur) &&
945 abs(muCur - muPrev) >
946 Literal(8) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(abs(muCur), abs(muPrev)) &&
947 abs(fCur - fPrev) > NumTraits<RealScalar>::epsilon() && !useBisection) {
951 RealScalar a = (fCur - fPrev) / (Literal(1) / muCur - Literal(1) / muPrev);
952 RealScalar b = fCur - a / muCur;
954 RealScalar muZero = -a / b;
955 RealScalar fZero = secularEq(muZero, col0, diag, perm, diagShifted, shift);
957#ifdef EIGEN_BDCSVD_SANITY_CHECKS
958 eigen_internal_assert((numext::isfinite)(fZero));
967 if (numext::equal_strict(shift, left) && (muCur < Literal(0) || muCur > right - left)) useBisection =
true;
968 if (numext::equal_strict(shift, right) && (muCur < -(right - left) || muCur > Literal(0))) useBisection =
true;
969 if (abs(fCur) > abs(fPrev)) useBisection =
true;
974#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
975 std::cout <<
"useBisection for k = " << k <<
", actual_n = " << actual_n <<
"\n";
977 RealScalar leftShifted, rightShifted;
979 if (numext::equal_strict(shift, left)) {
983 numext::maxi<RealScalar>((std::numeric_limits<RealScalar>::min)(),
984 Literal(2) * abs(col0(k)) / sqrt((std::numeric_limits<RealScalar>::max)()));
987 eigen_internal_assert(
988 (numext::isfinite)((col0(k) / leftShifted) * (col0(k) / (diag(k) + shift + leftShifted))));
991 rightShifted = (k == actual_n - 1)
993 : ((right - left) * RealScalar(0.51));
995 leftShifted = -(right - left) * RealScalar(0.51);
997 rightShifted = -numext::maxi<RealScalar>((std::numeric_limits<RealScalar>::min)(),
998 abs(col0(k + 1)) / sqrt((std::numeric_limits<RealScalar>::max)()));
1000 rightShifted = -(std::numeric_limits<RealScalar>::min)();
1003 RealScalar fLeft = secularEq(leftShifted, col0, diag, perm, diagShifted, shift);
1004 eigen_internal_assert(fLeft < Literal(0));
1006#if defined EIGEN_BDCSVD_DEBUG_VERBOSE || defined EIGEN_BDCSVD_SANITY_CHECKS || defined EIGEN_INTERNAL_DEBUGGING
1007 RealScalar fRight = secularEq(rightShifted, col0, diag, perm, diagShifted, shift);
1010#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1011 if (!(numext::isfinite)(fLeft))
1012 std::cout <<
"f(" << leftShifted <<
") =" << fLeft <<
" ; " << left <<
" " << shift <<
" " << right <<
"\n";
1013 eigen_internal_assert((numext::isfinite)(fLeft));
1015 if (!(numext::isfinite)(fRight))
1016 std::cout <<
"f(" << rightShifted <<
") =" << fRight <<
" ; " << left <<
" " << shift <<
" " << right <<
"\n";
1020#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1021 if (!(fLeft * fRight < 0)) {
1022 std::cout <<
"f(leftShifted) using leftShifted=" << leftShifted
1023 <<
" ; diagShifted(1:10):" << diagShifted.head(10).transpose() <<
"\n ; "
1024 <<
"left==shift=" << bool(left == shift) <<
" ; left-shift = " << (left - shift) <<
"\n";
1025 std::cout <<
"k=" << k <<
", " << fLeft <<
" * " << fRight <<
" == " << fLeft * fRight <<
" ; "
1026 <<
"[" << left <<
" .. " << right <<
"] -> [" << leftShifted <<
" " << rightShifted
1027 <<
"], shift=" << shift <<
" , f(right)=" << secularEq(0, col0, diag, perm, diagShifted, shift)
1028 <<
" == " << secularEq(right, col0, diag, perm, diag, 0) <<
" == " << fRight <<
"\n";
1031 eigen_internal_assert(fLeft * fRight < Literal(0));
1033 if (fLeft < Literal(0)) {
1034 while (rightShifted - leftShifted > Literal(2) * NumTraits<RealScalar>::epsilon() *
1035 numext::maxi<RealScalar>(abs(leftShifted), abs(rightShifted))) {
1036 RealScalar midShifted = (leftShifted + rightShifted) / Literal(2);
1037 fMid = secularEq(midShifted, col0, diag, perm, diagShifted, shift);
1038 eigen_internal_assert((numext::isfinite)(fMid));
1040 if (fLeft * fMid < Literal(0)) {
1041 rightShifted = midShifted;
1043 leftShifted = midShifted;
1047 muCur = (leftShifted + rightShifted) / Literal(2);
1053 muCur = (right - left) * RealScalar(0.5);
1055 if (numext::equal_strict(shift, right)) muCur = -muCur;
1059 singVals[k] = shift + muCur;
1063#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1065 std::cout <<
"found " << singVals[k] <<
" == " << shift <<
" + " << muCur <<
" from " << diag(k) <<
" .. "
1066 << diag(k + 1) <<
"\n";
1068#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1069 eigen_internal_assert(k == 0 || singVals[k] >= singVals[k - 1]);
1070 eigen_internal_assert(singVals[k] >= diag(k));
1082template <
typename MatrixType,
int Options>
1083void BDCSVD<MatrixType, Options>::perturbCol0(
const ArrayRef& col0,
const ArrayRef& diag,
const IndicesRef& perm,
1084 const VectorType& singVals,
const ArrayRef& shifts,
const ArrayRef& mus,
1087 Index n = col0.size();
1088 Index m = perm.size();
1093 Index lastIdx = perm(m - 1);
1095 for (Index k = 0; k < n; ++k) {
1096 if (numext::is_exactly_zero(col0(k)))
1097 zhat(k) = Literal(0);
1100 RealScalar dk = diag(k);
1101 RealScalar prod = (singVals(lastIdx) + dk) * (mus(lastIdx) + (shifts(lastIdx) - dk));
1102#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1104 std::cout <<
"k = " << k <<
" ; z(k)=" << col0(k) <<
", diag(k)=" << dk <<
"\n";
1105 std::cout <<
"prod = "
1106 <<
"(" << singVals(lastIdx) <<
" + " << dk <<
") * (" << mus(lastIdx) <<
" + (" << shifts(lastIdx)
1107 <<
" - " << dk <<
"))"
1109 std::cout <<
" = " << singVals(lastIdx) + dk <<
" * " << mus(lastIdx) + (shifts(lastIdx) - dk) <<
"\n";
1111 eigen_internal_assert(prod >= 0);
1114 for (Index l = 0; l < m; ++l) {
1117#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1118 if (i >= k && (l == 0 || l - 1 >= m)) {
1119 std::cout <<
"Error in perturbCol0\n";
1120 std::cout <<
" " << k <<
"/" << n <<
" " << l <<
"/" << m <<
" " << i <<
"/" << n <<
" ; " << col0(k)
1121 <<
" " << diag(k) <<
" "
1123 std::cout <<
" " << diag(i) <<
"\n";
1124 Index j = (i < k ) ? i : perm(l - 1);
1126 <<
"j=" << j <<
"\n";
1129 Index j = i < k ? i : l > 0 ? perm(l - 1) : i;
1130#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1131 if (!(dk != Literal(0) || diag(i) != Literal(0))) {
1132 std::cout <<
"k=" << k <<
", i=" << i <<
", l=" << l <<
", perm.size()=" << perm.size() <<
"\n";
1134 eigen_internal_assert(dk != Literal(0) || diag(i) != Literal(0));
1136 prod *= ((singVals(j) + dk) / ((diag(i) + dk))) * ((mus(j) + (shifts(j) - dk)) / ((diag(i) - dk)));
1137#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1138 eigen_internal_assert(prod >= 0);
1140#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1142 numext::abs(((singVals(j) + dk) * (mus(j) + (shifts(j) - dk))) / ((diag(i) + dk) * (diag(i) - dk)) - 1) >
1145 << ((singVals(j) + dk) * (mus(j) + (shifts(j) - dk))) / ((diag(i) + dk) * (diag(i) - dk))
1146 <<
" == (" << (singVals(j) + dk) <<
" * " << (mus(j) + (shifts(j) - dk)) <<
") / ("
1147 << (diag(i) + dk) <<
" * " << (diag(i) - dk) <<
")\n";
1151#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1152 std::cout <<
"zhat(" << k <<
") = sqrt( " << prod <<
") ; " << (singVals(lastIdx) + dk) <<
" * "
1153 << mus(lastIdx) + shifts(lastIdx) <<
" - " << dk <<
"\n";
1155 RealScalar tmp = sqrt(prod);
1156#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1157 eigen_internal_assert((numext::isfinite)(tmp));
1159 zhat(k) = col0(k) > Literal(0) ? RealScalar(tmp) : RealScalar(-tmp);
1165template <
typename MatrixType,
int Options>
1166void BDCSVD<MatrixType, Options>::computeSingVecs(
const ArrayRef& zhat,
const ArrayRef& diag,
const IndicesRef& perm,
1167 const VectorType& singVals,
const ArrayRef& shifts,
1168 const ArrayRef& mus, MatrixXr& U, MatrixXr& V) {
1169 Index n = zhat.size();
1170 Index m = perm.size();
1172 for (Index k = 0; k < n; ++k) {
1173 if (numext::is_exactly_zero(zhat(k))) {
1174 U.col(k) = VectorType::Unit(n + 1, k);
1175 if (m_compV) V.col(k) = VectorType::Unit(n, k);
1178 for (Index l = 0; l < m; ++l) {
1180 U(i, k) = zhat(i) / (((diag(i) - shifts(k)) - mus(k))) / ((diag(i) + singVals[k]));
1182 U(n, k) = Literal(0);
1183 U.col(k).normalize();
1187 for (Index l = 1; l < m; ++l) {
1189 V(i, k) = diag(i) * zhat(i) / (((diag(i) - shifts(k)) - mus(k))) / ((diag(i) + singVals[k]));
1191 V(0, k) = Literal(-1);
1192 V.col(k).normalize();
1196 U.col(n) = VectorType::Unit(n + 1, n);
1202template <
typename MatrixType,
int Options>
1203void BDCSVD<MatrixType, Options>::deflation43(Index firstCol, Index shift, Index i, Index size) {
1207 Index start = firstCol + shift;
1208 RealScalar c = m_computed(start, start);
1209 RealScalar s = m_computed(start + i, start);
1210 RealScalar r = numext::hypot(c, s);
1211 if (numext::is_exactly_zero(r)) {
1212 m_computed(start + i, start + i) = Literal(0);
1215 m_computed(start, start) = r;
1216 m_computed(start + i, start) = Literal(0);
1217 m_computed(start + i, start + i) = Literal(0);
1219 JacobiRotation<RealScalar> J(c / r, -s / r);
1221 m_naiveU.middleRows(firstCol, size + 1).applyOnTheRight(firstCol, firstCol + i, J);
1223 m_naiveU.applyOnTheRight(firstCol, firstCol + i, J);
1229template <
typename MatrixType,
int Options>
1230void BDCSVD<MatrixType, Options>::deflation44(Index firstColu, Index firstColm, Index firstRowW, Index firstColW,
1231 Index i, Index j, Index size) {
1237 RealScalar s = m_computed(firstColm + i, firstColm);
1238 RealScalar c = m_computed(firstColm + j, firstColm);
1239 RealScalar r = numext::hypot(c, s);
1240#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1241 std::cout <<
"deflation 4.4: " << i <<
"," << j <<
" -> " << c <<
" " << s <<
" " << r <<
" ; "
1242 << m_computed(firstColm + i - 1, firstColm) <<
" " << m_computed(firstColm + i, firstColm) <<
" "
1243 << m_computed(firstColm + i + 1, firstColm) <<
" " << m_computed(firstColm + i + 2, firstColm) <<
"\n";
1244 std::cout << m_computed(firstColm + i - 1, firstColm + i - 1) <<
" " << m_computed(firstColm + i, firstColm + i)
1245 <<
" " << m_computed(firstColm + i + 1, firstColm + i + 1) <<
" "
1246 << m_computed(firstColm + i + 2, firstColm + i + 2) <<
"\n";
1248 if (numext::is_exactly_zero(r)) {
1249 m_computed(firstColm + j, firstColm + j) = m_computed(firstColm + i, firstColm + i);
1254 m_computed(firstColm + j, firstColm) = r;
1255 m_computed(firstColm + j, firstColm + j) = m_computed(firstColm + i, firstColm + i);
1256 m_computed(firstColm + i, firstColm) = Literal(0);
1258 JacobiRotation<RealScalar> J(c, -s);
1260 m_naiveU.middleRows(firstColu, size + 1).applyOnTheRight(firstColu + j, firstColu + i, J);
1262 m_naiveU.applyOnTheRight(firstColu + j, firstColu + i, J);
1263 if (m_compV) m_naiveV.middleRows(firstRowW, size).applyOnTheRight(firstColW + j, firstColW + i, J);
1267template <
typename MatrixType,
int Options>
1268void BDCSVD<MatrixType, Options>::deflation(Index firstCol, Index lastCol, Index k, Index firstRowW, Index firstColW,
1272 const Index length = lastCol + 1 - firstCol;
1274 Block<MatrixXr, Dynamic, 1> col0(m_computed, firstCol + shift, firstCol + shift, length, 1);
1275 Diagonal<MatrixXr> fulldiag(m_computed);
1276 VectorBlock<Diagonal<MatrixXr>,
Dynamic> diag(fulldiag, firstCol + shift, length);
1278 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
1279 RealScalar maxDiag = diag.tail((std::max)(Index(1), length - 1)).cwiseAbs().maxCoeff();
1280 RealScalar epsilon_strict = numext::maxi<RealScalar>(considerZero, NumTraits<RealScalar>::epsilon() * maxDiag);
1281 RealScalar epsilon_coarse =
1282 Literal(8) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(col0.cwiseAbs().maxCoeff(), maxDiag);
1284#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1285 eigen_internal_assert(m_naiveU.allFinite());
1286 eigen_internal_assert(m_naiveV.allFinite());
1287 eigen_internal_assert(m_computed.allFinite());
1290#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1291 std::cout <<
"\ndeflate:" << diag.head(k + 1).transpose() <<
" | "
1292 << diag.segment(k + 1, length - k - 1).transpose() <<
"\n";
1296 if (diag(0) < epsilon_coarse) {
1297#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1298 std::cout <<
"deflation 4.1, because " << diag(0) <<
" < " << epsilon_coarse <<
"\n";
1300 diag(0) = epsilon_coarse;
1304 for (Index i = 1; i < length; ++i)
1305 if (abs(col0(i)) < epsilon_strict) {
1306#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1307 std::cout <<
"deflation 4.2, set z(" << i <<
") to zero because " << abs(col0(i)) <<
" < " << epsilon_strict
1308 <<
" (diag(" << i <<
")=" << diag(i) <<
")\n";
1310 col0(i) = Literal(0);
1314 for (Index i = 1; i < length; i++)
1315 if (diag(i) < epsilon_coarse) {
1316#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1317 std::cout <<
"deflation 4.3, cancel z(" << i <<
")=" << col0(i) <<
" because diag(" << i <<
")=" << diag(i)
1318 <<
" < " << epsilon_coarse <<
"\n";
1320 deflation43(firstCol, shift, i, length);
1323#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1324 eigen_internal_assert(m_naiveU.allFinite());
1325 eigen_internal_assert(m_naiveV.allFinite());
1326 eigen_internal_assert(m_computed.allFinite());
1328#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1329 std::cout <<
"to be sorted: " << diag.transpose() <<
"\n\n";
1330 std::cout <<
" : " << col0.transpose() <<
"\n\n";
1335 const bool total_deflation = (col0.tail(length - 1).array().abs() < considerZero).all();
1339 Index* permutation = m_workspaceI.data();
1345 for (Index i = 1; i < length; ++i)
1346 if (diag(i) < considerZero) permutation[p++] = i;
1348 Index i = 1, j = k + 1;
1349 for (; p < length; ++p) {
1351 permutation[p] = j++;
1352 else if (j >= length)
1353 permutation[p] = i++;
1354 else if (diag(i) < diag(j))
1355 permutation[p] = j++;
1357 permutation[p] = i++;
1362 if (total_deflation) {
1363 for (Index i = 1; i < length; ++i) {
1364 Index pi = permutation[i];
1365 if (diag(pi) < considerZero || diag(0) < diag(pi))
1366 permutation[i - 1] = permutation[i];
1368 permutation[i - 1] = 0;
1375 Index* realInd = m_workspaceI.data() + length;
1376 Index* realCol = m_workspaceI.data() + 2 * length;
1378 for (
int pos = 0; pos < length; pos++) {
1383 for (Index i = total_deflation ? 0 : 1; i < length; i++) {
1384 const Index pi = permutation[length - (total_deflation ? i + 1 : i)];
1385 const Index J = realCol[pi];
1389 swap(diag(i), diag(J));
1390 if (i != 0 && J != 0) swap(col0(i), col0(J));
1394 m_naiveU.col(firstCol + i)
1395 .segment(firstCol, length + 1)
1396 .swap(m_naiveU.col(firstCol + J).segment(firstCol, length + 1));
1398 m_naiveU.col(firstCol + i).segment(0, 2).swap(m_naiveU.col(firstCol + J).segment(0, 2));
1400 m_naiveV.col(firstColW + i)
1401 .segment(firstRowW, length)
1402 .swap(m_naiveV.col(firstColW + J).segment(firstRowW, length));
1405 const Index realI = realInd[i];
1412#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1413 std::cout <<
"sorted: " << diag.transpose().format(bdcsvdfmt) <<
"\n";
1414 std::cout <<
" : " << col0.transpose() <<
"\n\n";
1419 Index i = length - 1;
1421 while (i > 0 && (diag(i) < considerZero || abs(col0(i)) < considerZero)) --i;
1424 if ((diag(i) - diag(i - 1)) < epsilon_strict) {
1425#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1426 std::cout <<
"deflation 4.4 with i = " << i <<
" because " << diag(i) <<
" - " << diag(i - 1)
1427 <<
" == " << (diag(i) - diag(i - 1)) <<
" < " << epsilon_strict <<
"\n";
1429 eigen_internal_assert(abs(diag(i) - diag(i - 1)) < epsilon_coarse &&
1430 " diagonal entries are not properly sorted");
1431 deflation44(firstCol, firstCol + shift, firstRowW, firstColW, i, i - 1, length);
1435#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1436 for (Index j = 2; j < length; ++j) eigen_internal_assert(diag(j - 1) <= diag(j) || abs(diag(j)) < considerZero);
1439#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1440 eigen_internal_assert(m_naiveU.allFinite());
1441 eigen_internal_assert(m_naiveV.allFinite());
1442 eigen_internal_assert(m_computed.allFinite());
1452template <
typename Derived>
1453template <
int Options>
1464template <
typename Derived>
1465template <
int Options>
1467 unsigned int computationOptions)
const {
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:48
class Bidiagonal Divide and Conquer SVD
Definition BDCSVD.h:105
EIGEN_DEPRECATED BDCSVD(const MatrixType &matrix, unsigned int computationOptions)
Constructor performing the decomposition of given matrix using specified options for computing unitar...
Definition BDCSVD.h:202
BDCSVD(const MatrixType &matrix)
Constructor performing the decomposition of given matrix, using the custom options specified with the...
Definition BDCSVD.h:186
EIGEN_DEPRECATED BDCSVD(Index rows, Index cols, unsigned int computationOptions)
Default Constructor with memory preallocation.
Definition BDCSVD.h:176
BDCSVD()
Default Constructor.
Definition BDCSVD.h:150
EIGEN_DEPRECATED BDCSVD & compute(const MatrixType &matrix, unsigned int computationOptions)
Method performing the decomposition of given matrix, as specified by the computationOptions parameter...
Definition BDCSVD.h:225
BDCSVD & compute(const MatrixType &matrix)
Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified us...
Definition BDCSVD.h:214
BDCSVD(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition BDCSVD.h:158
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
A matrix or vector expression mapping an existing expression.
Definition Ref.h:264
Base class of SVD algorithms.
Definition SVDBase.h:119
bool computeV() const
Definition SVDBase.h:275
bool computeU() const
Definition SVDBase.h:273
@ Aligned
Definition Constants.h:242
@ InvalidInput
Definition Constants.h:447
Matrix< Type, Dynamic, Dynamic > MatrixX
[c++11] Dynamic×Dynamic matrix of type Type.
Definition Matrix.h:508
Namespace containing all symbols from the Eigen library.
Definition Core:137
const int Dynamic
Definition Constants.h:25
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition EigenBase.h:64