Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
BDCSVD.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// We used the "A Divide-And-Conquer Algorithm for the Bidiagonal SVD"
5// research report written by Ming Gu and Stanley C.Eisenstat
6// The code variable names correspond to the names they used in their
7// report
8//
9// Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
10// Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
11// Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
12// Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
13// Copyright (C) 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
14// Copyright (C) 2014-2017 Gael Guennebaud <gael.guennebaud@inria.fr>
15//
16// Source Code Form is subject to the terms of the Mozilla
17// Public License v. 2.0. If a copy of the MPL was not distributed
18// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
19// SPDX-License-Identifier: MPL-2.0
20
21#ifndef EIGEN_BDCSVD_H
22#define EIGEN_BDCSVD_H
23
24// IWYU pragma: private
25#include "./InternalHeaderCheck.h"
26
27// Internal D&C implementation, templated only on RealScalar.
28#include "BDCSVDImpl.h"
29
30namespace Eigen {
31
32template <typename MatrixType_, int Options>
33class BDCSVD;
34
35namespace internal {
36
37template <typename MatrixType_, int Options>
38struct traits<BDCSVD<MatrixType_, Options> > : svd_traits<MatrixType_, Options> {
39 using MatrixType = MatrixType_;
40};
41
42} // end namespace internal
43
75template <typename MatrixType_, int Options_>
76class BDCSVD : public SVDBase<BDCSVD<MatrixType_, Options_> > {
77 using Base = SVDBase<BDCSVD>;
78
79 public:
80 using Base::cols;
81 using Base::computeU;
82 using Base::computeV;
83 using Base::diagSize;
84 using Base::rows;
85
86 using MatrixType = MatrixType_;
87 using Scalar = typename Base::Scalar;
88 using RealScalar = typename Base::RealScalar;
89 using Literal = typename NumTraits<RealScalar>::Literal;
90 using Index = typename Base::Index;
91 enum {
92 Options = Options_,
93 QRDecomposition = internal::get_qr_preconditioner(Options),
94 ComputationOptions = internal::get_computation_options(Options),
95 RowsAtCompileTime = Base::RowsAtCompileTime,
96 ColsAtCompileTime = Base::ColsAtCompileTime,
97 DiagSizeAtCompileTime = Base::DiagSizeAtCompileTime,
98 MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime,
99 MaxColsAtCompileTime = Base::MaxColsAtCompileTime,
100 MaxDiagSizeAtCompileTime = Base::MaxDiagSizeAtCompileTime,
101 MatrixOptions = Base::MatrixOptions
102 };
103
104 using MatrixUType = typename Base::MatrixUType;
105 using MatrixVType = typename Base::MatrixVType;
106 using SingularValuesType = typename Base::SingularValuesType;
107
110 using VectorType = Matrix<RealScalar, Dynamic, 1>;
111 using ArrayXr = Array<RealScalar, Dynamic, 1>;
112 using ArrayXi = Array<Index, 1, Dynamic>;
113 using ArrayRef = Ref<ArrayXr>;
114 using IndicesRef = Ref<ArrayXi>;
115
121 BDCSVD() : m_numIters(0) {}
122
129 BDCSVD(Index rows, Index cols) : m_numIters(0) { allocate(rows, cols, internal::get_computation_options(Options)); }
130
147 EIGEN_DEPRECATED_WITH_REASON("Options should be specified using the class template parameter.")
148 BDCSVD(Index rows, Index cols, unsigned int computationOptions) : m_numIters(0) {
149 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, rows, cols);
150 allocate(rows, cols, computationOptions);
151 }
152
158 template <typename Derived>
159 BDCSVD(const MatrixBase<Derived>& matrix) : m_numIters(0) {
160 compute_impl(matrix, internal::get_computation_options(Options));
161 }
162
172 template <typename DerivedD, typename DerivedE>
173 BDCSVD(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal) : m_numIters(0) {
174 compute_bidiagonal_impl(diagonal, superdiagonal, internal::get_computation_options(Options));
175 }
176
189 template <typename Derived>
190 EIGEN_DEPRECATED_WITH_REASON("Options should be specified using the class template parameter.")
191 BDCSVD(const MatrixBase<Derived>& matrix, unsigned int computationOptions) : m_numIters(0) {
192 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
193 compute_impl(matrix, computationOptions);
194 }
195
201 template <typename Derived>
203 return compute_impl(matrix, m_computationOptions);
204 }
205
215 template <typename Derived>
216 EIGEN_DEPRECATED_WITH_REASON("Options should be specified using the class template parameter.")
217 BDCSVD& compute(const MatrixBase<Derived>& matrix, unsigned int computationOptions) {
218 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
219 return compute_impl(matrix, computationOptions);
220 }
221
231 template <typename DerivedD, typename DerivedE>
232 BDCSVD& compute(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal) {
233 return compute_bidiagonal_impl(diagonal, superdiagonal, m_computationOptions);
234 }
235
236 void setSwitchSize(int s) {
237 eigen_assert(s >= 3 && "BDCSVD the size of the algo switch has to be at least 3.");
238 if (s == m_impl.algoSwap()) return;
239 m_impl.setAlgoSwap(s);
240 // smallSvd is only allocated for sizes below the switch, so the next compute must reallocate.
241 m_isAllocated = false;
242 }
243
244 private:
245 template <typename Derived>
246 BDCSVD& compute_impl(const MatrixBase<Derived>& matrix, unsigned int computationOptions);
247 template <typename Derived>
248 BDCSVD& compute_impl(const MatrixBase<Derived>& matrix, unsigned int computationOptions, internal::true_type);
249 template <typename Derived>
250 BDCSVD& compute_impl(const MatrixBase<Derived>& matrix, unsigned int computationOptions, internal::false_type);
251 template <typename DerivedD, typename DerivedE>
252 BDCSVD& compute_bidiagonal_impl(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal,
253 unsigned int computationOptions);
254 template <typename HouseholderU, typename HouseholderV, typename NaiveU, typename NaiveV>
255 void copyUV(const HouseholderU& householderU, const HouseholderV& householderV, const NaiveU& naiveU,
256 const NaiveV& naivev);
257
258 protected:
259 void allocate(Index rows, Index cols, unsigned int computationOptions);
260 void allocate_small(Index rows, Index cols, unsigned int computationOptions);
261 internal::bdcsvd_impl<RealScalar> m_impl;
262 bool m_isTranspose = false;
263 bool m_useQrDecomp = false;
264 // Only PreconditionSquareMatrix is forwarded: BDCSVD's QR bits configure its own R-bidiagonalization, and smallSvd
265 // needs its QR preconditioner to reduce non-square inputs. The unitaries are requested at runtime in allocate().
266 JacobiSVD<MatrixX, (Options & int(PreconditionSquareMatrix))> smallSvd;
267 HouseholderQR<MatrixX> qrDecomp;
268 internal::UpperBidiagonalization<MatrixX> bid;
269 MatrixX copyWorkspace;
270 MatrixX reducedTriangle;
271 // Reused workspace for HouseholderSequence::applyThisOnTheLeft in copyUV().
272 // Without this, each apply allocates a fresh row vector.
273 Matrix<Scalar, 1, Dynamic, RowMajor> m_householderWorkspace;
274
275 using Base::m_computationOptions;
276 using Base::m_computeThinU;
277 using Base::m_computeThinV;
278 using Base::m_info;
279 using Base::m_isAllocated;
280 using Base::m_isInitialized;
281 using Base::m_matrixU;
282 using Base::m_matrixV;
283 using Base::m_nonzeroSingularValues;
284 using Base::m_singularValues;
285
286 public:
287 int m_numIters;
288}; // end class BDCSVD
289
290// Method to allocate and initialize matrix and attributes
291template <typename MatrixType, int Options>
292void BDCSVD<MatrixType, Options>::allocate(Index rows, Index cols, unsigned int computationOptions) {
293 if (Base::allocate(rows, cols, computationOptions)) return;
294
295 if (cols < m_impl.algoSwap())
296 smallSvd.allocate(rows, cols, internal::get_computation_options(Options | computationOptions));
297
298 m_isTranspose = (cols > rows);
299
300 bool compU = computeV();
301 bool compV = computeU();
302 if (m_isTranspose) std::swap(compU, compV);
303
304 m_impl.allocate(diagSize(), compU, compV);
305
306 // kMinAspectRatio is the crossover point that determines if we perform R-Bidiagonalization
307 // or bidiagonalize the input matrix directly.
308 // It is based off of LAPACK's dgesdd routine, which uses 11.0/6.0
309 // we use a larger scalar to prevent a regression for relatively square matrices.
310 constexpr Index kMinAspectRatio = 4;
311 constexpr bool disableQrDecomp = static_cast<int>(QRDecomposition) == static_cast<int>(DisableQRDecomposition);
312 m_useQrDecomp = !disableQrDecomp && ((rows / kMinAspectRatio >= cols) || (cols / kMinAspectRatio >= rows));
313 if (m_useQrDecomp) {
314 qrDecomp = HouseholderQR<MatrixX>((std::max)(rows, cols), (std::min)(rows, cols));
315 reducedTriangle = MatrixX(diagSize(), diagSize());
316 }
317
318 copyWorkspace = MatrixX(m_isTranspose ? cols : rows, m_isTranspose ? rows : cols);
319 bid = internal::UpperBidiagonalization<MatrixX>(m_useQrDecomp ? diagSize() : copyWorkspace.rows(),
320 m_useQrDecomp ? diagSize() : copyWorkspace.cols());
321} // end allocate
322
323template <typename MatrixType, int Options>
324void BDCSVD<MatrixType, Options>::allocate_small(Index rows, Index cols, unsigned int computationOptions) {
325 if (Base::allocate(rows, cols, computationOptions)) return;
326
327 smallSvd.allocate(rows, cols, internal::get_computation_options(Options | computationOptions));
328 m_isTranspose = (cols > rows);
329}
330
331template <typename MatrixType, int Options>
332template <typename Derived>
333BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_impl(const MatrixBase<Derived>& matrix,
334 unsigned int computationOptions) {
335 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived, MatrixType);
336 EIGEN_STATIC_ASSERT((std::is_same<typename Derived::Scalar, typename MatrixType::Scalar>::value),
337 Input matrix must have the same Scalar type as the BDCSVD object.);
338
339 // setSwitchSize() enforces a minimum of 3, so these types can only use the JacobiSVD fallback.
340 using AlwaysUseSmallSvd = internal::bool_constant<(MaxColsAtCompileTime != Dynamic && MaxColsAtCompileTime < 3)>;
341 return compute_impl(matrix, computationOptions, AlwaysUseSmallSvd());
342}
343
344template <typename MatrixType, int Options>
345template <typename Derived>
346EIGEN_DONT_INLINE BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_impl(
347 const MatrixBase<Derived>& matrix, unsigned int computationOptions, internal::true_type) {
348 allocate_small(matrix.rows(), matrix.cols(), computationOptions);
349
350 smallSvd.compute(matrix);
351 m_isInitialized = true;
352 m_info = smallSvd.info();
353 if (m_info == Success || m_info == NoConvergence) {
354 if (computeU()) m_matrixU = smallSvd.matrixU();
355 if (computeV()) m_matrixV = smallSvd.matrixV();
356 m_singularValues = smallSvd.singularValues();
357 m_nonzeroSingularValues = smallSvd.nonzeroSingularValues();
358 }
359 return *this;
360}
361
362template <typename MatrixType, int Options>
363template <typename Derived>
364EIGEN_DONT_INLINE BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_impl(
365 const MatrixBase<Derived>& matrix, unsigned int computationOptions, internal::false_type) {
366 allocate(matrix.rows(), matrix.cols(), computationOptions);
367
368 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
369
370 //**** step -1 - If the problem is too small, directly falls back to JacobiSVD and return
371 if (matrix.cols() < m_impl.algoSwap()) {
372 smallSvd.compute(matrix);
373 m_isInitialized = true;
374 m_info = smallSvd.info();
375 if (m_info == Success || m_info == NoConvergence) {
376 if (computeU()) m_matrixU = smallSvd.matrixU();
377 if (computeV()) m_matrixV = smallSvd.matrixV();
378 m_singularValues = smallSvd.singularValues();
379 m_nonzeroSingularValues = smallSvd.nonzeroSingularValues();
380 }
381 return *this;
382 }
383
384 //**** step 0 - Copy the input matrix and apply scaling to reduce over/under-flows
385 // A SIMD unit that flushes subnormal inputs reads an all-subnormal matrix as zero; recover its maximum from the
386 // representation so that the scaling still brings it into the normal range.
387 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(matrix.derived());
388 if (!(numext::isfinite)(maxCoeff)) {
389 m_isInitialized = true;
390 m_info = InvalidInput;
391 return *this;
392 }
393
394 const auto factors = m_isTranspose
395 ? internal::safe_scaling<RealScalar>::scale_to(copyWorkspace, matrix.adjoint(), maxCoeff)
396 : internal::safe_scaling<RealScalar>::scale_to(copyWorkspace, matrix, maxCoeff);
397
398 //**** step 1 - Bidiagonalization.
399 // If the problem is sufficiently rectangular, we perform R-Bidiagonalization: compute A = Q(R/0)
400 // and then bidiagonalize R. Otherwise, if the problem is relatively square, we
401 // bidiagonalize the input matrix directly.
402 if (m_useQrDecomp) {
403 qrDecomp.compute(copyWorkspace);
404 reducedTriangle = qrDecomp.matrixQR().topRows(diagSize());
405 reducedTriangle.template triangularView<StrictlyLower>().setZero();
406 bid.compute(reducedTriangle);
407 } else {
408 bid.compute(copyWorkspace);
409 }
410
411 //**** step 2 - Divide & Conquer
412 m_impl.naiveU().setZero();
413 m_impl.naiveV().setZero();
414 // The transposed bidiagonal has only the main diagonal and one sub-diagonal;
415 // fill those directly instead of materializing a dense temporary.
416 // Note: BandMatrix::diagonal<N>() const has a latent type bug (returns
417 // Block<CoefficientsType, ...> instead of Block<const CoefficientsType, ...>),
418 // so use the index-based overload which is correctly const-qualified.
419 m_impl.computed().setZero();
420 m_impl.computed().topRows(diagSize()).diagonal() = bid.bidiagonal().diagonal();
421 m_impl.computed().topRows(diagSize()).template diagonal<-1>() = bid.bidiagonal().diagonal(1);
422 m_impl.splitNegligibleSuperdiagonal(diagSize());
423 m_impl.divide(0, diagSize() - 1, 0, 0, 0);
424 m_info = m_impl.info();
425 m_numIters = m_impl.numIters();
426 if (m_info != Success && m_info != NoConvergence) {
427 m_isInitialized = true;
428 return *this;
429 }
430
431 //**** step 3 - Copy singular values and vectors
432 for (int i = 0; i < diagSize(); i++) {
433 RealScalar a = numext::abs(m_impl.computed().coeff(i, i));
434 m_singularValues.coeffRef(i) = a;
435 if (a < considerZero) {
436 m_nonzeroSingularValues = i;
437 m_singularValues.tail(diagSize() - i - 1).setZero();
438 break;
439 } else if (i == diagSize() - 1) {
440 m_nonzeroSingularValues = i + 1;
441 break;
442 }
443 }
444 // Unscaling with maxCoeff keeps singular values that land in the subnormal range under FTZ.
445 internal::safe_scaling<RealScalar>::unscale_in_place(m_singularValues, maxCoeff, factors);
446
447 //**** step 4 - Finalize unitaries U and V
448 if (m_isTranspose)
449 copyUV(bid.householderV(), bid.householderU(), m_impl.naiveV(), m_impl.naiveU());
450 else
451 copyUV(bid.householderU(), bid.householderV(), m_impl.naiveU(), m_impl.naiveV());
452
453 if (m_useQrDecomp) {
454 if (m_isTranspose && computeV())
455 m_matrixV.applyOnTheLeft(qrDecomp.householderQ());
456 else if (!m_isTranspose && computeU())
457 m_matrixU.applyOnTheLeft(qrDecomp.householderQ());
458 }
459
460 m_isInitialized = true;
461 return *this;
462} // end compute
463
464template <typename MatrixType, int Options>
465template <typename HouseholderU, typename HouseholderV, typename NaiveU, typename NaiveV>
466EIGEN_DONT_INLINE void BDCSVD<MatrixType, Options>::copyUV(const HouseholderU& householderU,
467 const HouseholderV& householderV, const NaiveU& naiveU,
468 const NaiveV& naiveV) {
469 // Note exchange of U and V: m_matrixU is set from m_naiveV and vice versa.
470 // Cast the diagSize x diagSize block (rather than the full naive matrix) to avoid materializing
471 // a full-size temporary when Scalar != RealScalar; reuse m_householderWorkspace across the two
472 // applyThisOnTheLeft calls so each does not allocate a fresh row vector.
473 if (computeU()) {
474 Index Ucols = m_computeThinU ? diagSize() : rows();
475 m_matrixU = MatrixX::Identity(rows(), Ucols);
476 m_matrixU.topLeftCorner(diagSize(), diagSize()) =
477 naiveV.topLeftCorner(diagSize(), diagSize()).template cast<Scalar>();
478 if (m_useQrDecomp) {
479 auto sub = m_matrixU.topLeftCorner(householderU.cols(), diagSize());
480 householderU.applyThisOnTheLeft(sub, m_householderWorkspace);
481 } else {
482 householderU.applyThisOnTheLeft(m_matrixU, m_householderWorkspace);
483 }
484 }
485 if (computeV()) {
486 Index Vcols = m_computeThinV ? diagSize() : cols();
487 m_matrixV = MatrixX::Identity(cols(), Vcols);
488 m_matrixV.topLeftCorner(diagSize(), diagSize()) =
489 naiveU.topLeftCorner(diagSize(), diagSize()).template cast<Scalar>();
490 if (m_useQrDecomp) {
491 auto sub = m_matrixV.topLeftCorner(householderV.cols(), diagSize());
492 householderV.applyThisOnTheLeft(sub, m_householderWorkspace);
493 } else {
494 householderV.applyThisOnTheLeft(m_matrixV, m_householderWorkspace);
495 }
496 }
497}
498
499template <typename MatrixType, int Options>
500template <typename DerivedD, typename DerivedE>
501EIGEN_DONT_INLINE BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_bidiagonal_impl(
502 const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal, unsigned int computationOptions) {
503 EIGEN_STATIC_ASSERT(DerivedD::IsVectorAtCompileTime, THIS_METHOD_IS_ONLY_FOR_VECTORS);
504 EIGEN_STATIC_ASSERT(DerivedE::IsVectorAtCompileTime, THIS_METHOD_IS_ONLY_FOR_VECTORS);
505 EIGEN_STATIC_ASSERT((NumTraits<typename DerivedD::Scalar>::IsComplex == 0),
506 THIS_FUNCTION_IS_NOT_FOR_COMPLEX_VALUED_MATRICES);
507 EIGEN_STATIC_ASSERT((NumTraits<typename DerivedE::Scalar>::IsComplex == 0),
508 THIS_FUNCTION_IS_NOT_FOR_COMPLEX_VALUED_MATRICES);
509
510 const Index n = diagonal.size();
511 eigen_assert((n == 0 || superdiagonal.size() == n - 1) && "superdiagonal must have size diagonal.size() - 1");
512
513 // For a bidiagonal matrix, rows == cols == n.
514 allocate(n, n, computationOptions);
515
516 if (n == 0) {
517 m_isInitialized = true;
518 m_info = Success;
519 m_nonzeroSingularValues = 0;
520 return *this;
521 }
522
523 // Check for non-finite inputs. The rescan recovers an all-subnormal input that a flushing SIMD unit reads as zero.
524 const RealScalar diagScale = internal::safe_scaling<RealScalar>::recover_flushed_max_coeff(
525 diagonal.derived(), diagonal.cwiseAbs().template maxCoeff<PropagateNaN>());
526 const RealScalar superdiagScale =
527 n > 1 ? internal::safe_scaling<RealScalar>::recover_flushed_max_coeff(
528 superdiagonal.derived(), superdiagonal.cwiseAbs().template maxCoeff<PropagateNaN>())
529 : RealScalar(0);
530 const RealScalar maxCoeff = internal::max_preserving_subnormals(diagScale, superdiagScale);
531 if (!(numext::isfinite)(maxCoeff)) {
532 m_isInitialized = true;
533 m_info = InvalidInput;
534 return *this;
535 }
536
537 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
538 //**** Small problem: build dense bidiagonal and delegate to JacobiSVD.
539 if (n < m_impl.algoSwap()) {
540 // Build the dense upper bidiagonal matrix.
541 MatrixX B = MatrixX::Zero(n, n);
542 auto diagonalDest = B.diagonal();
543 const auto factors =
544 internal::safe_scaling<RealScalar>::scale_to(diagonalDest, diagonal.template cast<Scalar>(), maxCoeff);
545 if (n > 1) {
546 auto superdiagonalDest = B.diagonal(1);
547 internal::safe_scaling<RealScalar>::scale_to(superdiagonalDest, superdiagonal.template cast<Scalar>(), maxCoeff,
548 factors);
549 }
550 smallSvd.compute(B);
551 m_isInitialized = true;
552 m_info = smallSvd.info();
553 if (m_info == Success || m_info == NoConvergence) {
554 internal::safe_scaling<RealScalar>::unscale_to(m_singularValues, smallSvd.singularValues(), maxCoeff, factors);
555 m_nonzeroSingularValues = smallSvd.nonzeroSingularValues();
556 if (computeU()) m_matrixU = smallSvd.matrixU();
557 if (computeV()) m_matrixV = smallSvd.matrixV();
558 }
559 return *this;
560 }
561
562 //**** Fill m_computed with transposed bidiagonal format.
563 // D&C operates on B^T: m_computed(i,i) = d_i, m_computed(i+1,i) = e_i.
564 m_impl.naiveU().setZero();
565 m_impl.naiveV().setZero();
566 m_impl.computed().setZero();
567 auto diagonalDest = m_impl.computed().diagonal();
568 const auto factors =
569 internal::safe_scaling<RealScalar>::scale_to(diagonalDest, diagonal.template cast<RealScalar>(), maxCoeff);
570 if (n > 1) {
571 auto superdiagonalDest = m_impl.computed().template diagonal<-1>().head(n - 1);
572 internal::safe_scaling<RealScalar>::scale_to(superdiagonalDest, superdiagonal.template cast<RealScalar>(), maxCoeff,
573 factors);
574 }
575
576 m_isTranspose = false;
577
578 //**** Run D&C.
579 m_impl.splitNegligibleSuperdiagonal(n);
580 m_impl.divide(0, n - 1, 0, 0, 0);
581 m_info = m_impl.info();
582 m_numIters = m_impl.numIters();
583 if (m_info != Success && m_info != NoConvergence) {
584 m_isInitialized = true;
585 return *this;
586 }
587
588 //**** Extract singular values.
589 for (int i = 0; i < diagSize(); i++) {
590 RealScalar a = numext::abs(m_impl.computed().coeff(i, i));
591 m_singularValues.coeffRef(i) = a;
592 if (a < considerZero) {
593 m_nonzeroSingularValues = i;
594 m_singularValues.tail(diagSize() - i - 1).setZero();
595 break;
596 } else if (i == diagSize() - 1) {
597 m_nonzeroSingularValues = i + 1;
598 break;
599 }
600 }
601 // Unscaling with maxCoeff keeps singular values that land in the subnormal range under FTZ.
602 internal::safe_scaling<RealScalar>::unscale_in_place(m_singularValues, maxCoeff, factors);
603
604 //**** Copy U and V directly (no Householder to apply).
605 // D&C computes B^T = naiveU * S * naiveV^T, so B = naiveV * S * naiveU^T.
606 // Thus U_of_B = naiveV, V_of_B = naiveU.
607 if (computeU()) {
608 Index Ucols = m_computeThinU ? diagSize() : rows();
609 m_matrixU = MatrixX::Identity(rows(), Ucols);
610 m_matrixU.topLeftCorner(diagSize(), diagSize()) =
611 m_impl.naiveV().template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
612 }
613 if (computeV()) {
614 Index Vcols = m_computeThinV ? diagSize() : cols();
615 m_matrixV = MatrixX::Identity(cols(), Vcols);
616 m_matrixV.topLeftCorner(diagSize(), diagSize()) =
617 m_impl.naiveU().template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
618 }
619
620 m_isInitialized = true;
621 return *this;
622}
623
630template <typename Derived>
631template <int Options>
632BDCSVD<typename MatrixBase<Derived>::PlainObject, Options> MatrixBase<Derived>::bdcSvd() const {
633 return BDCSVD<PlainObject, Options>(*this);
634}
635
642template <typename Derived>
643template <int Options>
644BDCSVD<typename MatrixBase<Derived>::PlainObject, Options> MatrixBase<Derived>::bdcSvd(
645 unsigned int computationOptions) const {
646 return BDCSVD<PlainObject, Options>(*this, computationOptions);
647}
648
649} // end namespace Eigen
650
651#endif
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:55
class Bidiagonal Divide and Conquer SVD
Definition BDCSVD.h:76
BDCSVD(const MatrixBase< DerivedD > &diagonal, const MatrixBase< DerivedE > &superdiagonal)
Constructor performing the SVD of an upper bidiagonal matrix given its diagonal and superdiagonal.
Definition BDCSVD.h:173
BDCSVD()
Default Constructor.
Definition BDCSVD.h:121
BDCSVD(const MatrixBase< Derived > &matrix)
Constructor performing the decomposition of given matrix, using the custom options specified with the...
Definition BDCSVD.h:159
BDCSVD & compute(const MatrixBase< Derived > &matrix)
Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified us...
Definition BDCSVD.h:202
BDCSVD & compute(const MatrixBase< DerivedD > &diagonal, const MatrixBase< DerivedE > &superdiagonal)
Compute the SVD of an upper bidiagonal matrix given its diagonal and superdiagonal.
Definition BDCSVD.h:232
BDCSVD(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition BDCSVD.h:129
Householder QR decomposition of a matrix.
Definition HouseholderQR.h:77
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
A matrix or vector expression mapping an existing expression.
Definition Ref.h:261
bool computeV() const
Definition SVDBase.h:277
bool computeU() const
Definition SVDBase.h:275
@ DisableQRDecomposition
Definition Constants.h:434
@ PreconditionSquareMatrix
Definition Constants.h:437
@ InvalidInput
Definition Constants.h:464
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Matrix< Type, Dynamic, Dynamic > MatrixX
Dynamic×Dynamic matrix of type Type.
Definition Matrix.h:524
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44