12#ifndef EIGEN_JACOBISVD_H
13#define EIGEN_JACOBISVD_H
16#include "./InternalHeaderCheck.h"
24template <typename MatrixType, int Options, bool IsComplex = NumTraits<typename MatrixType::Scalar>::IsComplex>
25struct svd_precondition_2x2_block_to_be_real {};
34enum { PreconditionIfMoreColsThanRows, PreconditionIfMoreRowsThanCols };
37constexpr bool svd_precondition_more_rows(
int options, Index rows, Index cols) {
38 return rows > cols || (should_svd_precondition_square_matrix(options) && rows == cols);
41template <
typename MatrixType,
int QRPreconditioner,
int Case,
bool PreconditionSquare>
42struct qr_preconditioner_should_do_anything
43 : bool_constant<!((QRPreconditioner == NoQRPreconditioner) ||
44 (Case == PreconditionIfMoreColsThanRows && MatrixType::RowsAtCompileTime != Dynamic &&
45 MatrixType::ColsAtCompileTime != Dynamic &&
46 MatrixType::ColsAtCompileTime <= MatrixType::RowsAtCompileTime) ||
47 (Case == PreconditionIfMoreRowsThanCols && MatrixType::RowsAtCompileTime != Dynamic &&
48 MatrixType::ColsAtCompileTime != Dynamic &&
49 (PreconditionSquare ? MatrixType::RowsAtCompileTime < MatrixType::ColsAtCompileTime
50 : MatrixType::RowsAtCompileTime <= MatrixType::ColsAtCompileTime)))> {};
52template <typename MatrixType, int Options, int QRPreconditioner, int Case,
53 bool DoAnything = qr_preconditioner_should_do_anything<MatrixType, QRPreconditioner, Case,
54 should_svd_precondition_square_matrix(Options)>::value>
55struct qr_preconditioner_impl {};
57template <typename MatrixType, int Options, int QRPreconditioner, int Case>
58class qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, Case, false> {
60 void allocate(const JacobiSVD<MatrixType, Options>&) {}
61 template <typename Xpr>
62 bool run(JacobiSVD<MatrixType, Options>&, const Xpr&) {
69template <typename MatrixType, int Options>
70class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
73 using Scalar = typename MatrixType::Scalar;
74 using SVDType = JacobiSVD<MatrixType, Options>;
76 enum { WorkspaceSize = MatrixType::RowsAtCompileTime, MaxWorkspaceSize = MatrixType::MaxRowsAtCompileTime };
78 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
80 void allocate(const SVDType& svd) {
81 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
82 internal::destroy_at(&m_qr);
83 internal::construct_at(&m_qr, svd.rows(), svd.cols());
85 if (svd.m_computeFullU) m_workspace.resize(svd.rows());
87 template <typename Xpr>
88 bool run(SVDType& svd, const Xpr& matrix) {
89 if (svd_precondition_more_rows(Options, matrix.rows(), matrix.cols())) {
91 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
92 if (svd.m_computeFullU) m_qr.matrixQ().evalTo(svd.m_matrixU, m_workspace);
93 if (svd.computeV()) svd.m_matrixV = m_qr.colsPermutation();
100 using QRType = FullPivHouseholderQR<MatrixType>;
102 WorkspaceType m_workspace;
105template <typename MatrixType, int Options>
106class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
109 using Scalar = typename MatrixType::Scalar;
110 using SVDType = JacobiSVD<MatrixType, Options>;
113 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
114 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
115 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
116 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
117 MatrixOptions = traits<MatrixType>::Options
120 using TransposeTypeWithSameStorageOrder =
121 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
122 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
124 void allocate(const SVDType& svd) {
125 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
126 internal::destroy_at(&m_qr);
127 internal::construct_at(&m_qr, svd.cols(), svd.rows());
129 if (svd.m_computeFullV) m_workspace.resize(svd.cols());
131 template <typename Xpr>
132 bool run(SVDType& svd, const Xpr& matrix) {
133 if (matrix.cols() > matrix.rows()) {
134 m_qr.compute(matrix.adjoint());
136 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
137 if (svd.m_computeFullV) m_qr.matrixQ().evalTo(svd.m_matrixV, m_workspace);
138 if (svd.computeU()) svd.m_matrixU = m_qr.colsPermutation();
145 using QRType = FullPivHouseholderQR<TransposeTypeWithSameStorageOrder>;
147 typename plain_row_type<MatrixType>::type m_workspace;
152template <typename MatrixType, int Options>
153class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
156 using Scalar = typename MatrixType::Scalar;
157 using SVDType = JacobiSVD<MatrixType, Options>;
160 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
161 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
164 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
166 void allocate(const SVDType& svd) {
167 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
168 internal::destroy_at(&m_qr);
169 internal::construct_at(&m_qr, svd.rows(), svd.cols());
171 if (svd.m_computeFullU)
172 m_workspace.resize(svd.rows());
173 else if (svd.m_computeThinU)
174 m_workspace.resize(svd.cols());
176 template <typename Xpr>
177 bool run(SVDType& svd, const Xpr& matrix) {
178 if (svd_precondition_more_rows(Options, matrix.rows(), matrix.cols())) {
179 m_qr.compute(matrix);
180 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
181 if (svd.m_computeFullU)
182 m_qr.householderQ().evalTo(svd.m_matrixU, m_workspace);
183 else if (svd.m_computeThinU) {
184 svd.m_matrixU.setIdentity(matrix.rows(), matrix.cols());
185 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixU, m_workspace);
187 if (svd.computeV()) svd.m_matrixV = m_qr.colsPermutation();
194 using QRType = ColPivHouseholderQR<MatrixType>;
196 WorkspaceType m_workspace;
199template <typename MatrixType, int Options>
200class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
203 using Scalar = typename MatrixType::Scalar;
204 using SVDType = JacobiSVD<MatrixType, Options>;
207 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
208 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
209 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
210 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
211 MatrixOptions = internal::traits<MatrixType>::Options,
212 WorkspaceSize = internal::traits<SVDType>::MatrixVColsAtCompileTime,
213 MaxWorkspaceSize = internal::traits<SVDType>::MatrixVMaxColsAtCompileTime
216 using WorkspaceType = Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1>;
218 using TransposeTypeWithSameStorageOrder =
219 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
220 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
222 void allocate(const SVDType& svd) {
223 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
224 internal::destroy_at(&m_qr);
225 internal::construct_at(&m_qr, svd.cols(), svd.rows());
227 if (svd.m_computeFullV)
228 m_workspace.resize(svd.cols());
229 else if (svd.m_computeThinV)
230 m_workspace.resize(svd.rows());
232 template <typename Xpr>
233 bool run(SVDType& svd, const Xpr& matrix) {
234 if (matrix.cols() > matrix.rows()) {
235 m_qr.compute(matrix.adjoint());
238 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
239 if (svd.m_computeFullV)
240 m_qr.householderQ().evalTo(svd.m_matrixV, m_workspace);
241 else if (svd.m_computeThinV) {
242 svd.m_matrixV.setIdentity(matrix.cols(), matrix.rows());
243 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixV, m_workspace);
245 if (svd.computeU()) svd.m_matrixU = m_qr.colsPermutation();
252 using QRType = ColPivHouseholderQR<TransposeTypeWithSameStorageOrder>;
254 WorkspaceType m_workspace;
259template <typename MatrixType, int Options>
260class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols, true> {
262 using Scalar = typename MatrixType::Scalar;
263 using SVDType = JacobiSVD<MatrixType, Options>;
266 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
267 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
270 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
272 void allocate(const SVDType& svd) {
273 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
274 internal::destroy_at(&m_qr);
275 internal::construct_at(&m_qr, svd.rows(), svd.cols());
277 if (svd.m_computeFullU)
278 m_workspace.resize(svd.rows());
279 else if (svd.m_computeThinU)
280 m_workspace.resize(svd.cols());
282 template <typename Xpr>
283 bool run(SVDType& svd, const Xpr& matrix) {
284 if (svd_precondition_more_rows(Options, matrix.rows(), matrix.cols())) {
285 m_qr.compute(matrix);
286 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
287 if (svd.m_computeFullU)
288 m_qr.householderQ().evalTo(svd.m_matrixU, m_workspace);
289 else if (svd.m_computeThinU) {
290 svd.m_matrixU.setIdentity(matrix.rows(), matrix.cols());
291 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixU, m_workspace);
293 if (svd.computeV()) svd.m_matrixV.setIdentity(matrix.cols(), matrix.cols());
300 using QRType = HouseholderQR<MatrixType>;
302 WorkspaceType m_workspace;
305template <typename MatrixType, int Options>
306class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreColsThanRows, true> {
308 using Scalar = typename MatrixType::Scalar;
309 using SVDType = JacobiSVD<MatrixType, Options>;
312 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
313 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
314 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
315 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
316 MatrixOptions = internal::traits<MatrixType>::Options,
317 WorkspaceSize = internal::traits<SVDType>::MatrixVColsAtCompileTime,
318 MaxWorkspaceSize = internal::traits<SVDType>::MatrixVMaxColsAtCompileTime
321 using WorkspaceType = Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1>;
323 using TransposeTypeWithSameStorageOrder =
324 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
325 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
327 void allocate(const SVDType& svd) {
328 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
329 internal::destroy_at(&m_qr);
330 internal::construct_at(&m_qr, svd.cols(), svd.rows());
332 if (svd.m_computeFullV)
333 m_workspace.resize(svd.cols());
334 else if (svd.m_computeThinV)
335 m_workspace.resize(svd.rows());
338 template <typename Xpr>
339 bool run(SVDType& svd, const Xpr& matrix) {
340 if (matrix.cols() > matrix.rows()) {
341 m_qr.compute(matrix.adjoint());
344 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
345 if (svd.m_computeFullV)
346 m_qr.householderQ().evalTo(svd.m_matrixV, m_workspace);
347 else if (svd.m_computeThinV) {
348 svd.m_matrixV.setIdentity(matrix.cols(), matrix.rows());
349 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixV, m_workspace);
351 if (svd.computeU()) svd.m_matrixU.setIdentity(matrix.rows(), matrix.rows());
360 WorkspaceType m_workspace;
368template <
typename MatrixType,
int Options>
369struct svd_precondition_2x2_block_to_be_real<MatrixType, Options, false> {
370 using SVD = JacobiSVD<MatrixType, Options>;
371 using RealScalar =
typename MatrixType::RealScalar;
372 static bool run(
typename SVD::WorkMatrixType&, SVD&, Index, Index, RealScalar&) {
return true; }
375template <
typename MatrixType,
int Options>
376struct svd_precondition_2x2_block_to_be_real<MatrixType, Options, true> {
377 using SVD = JacobiSVD<MatrixType, Options>;
378 using Scalar =
typename MatrixType::Scalar;
379 using RealScalar =
typename MatrixType::RealScalar;
380 static bool run(
typename SVD::WorkMatrixType& work_matrix, SVD& svd, Index p, Index q, RealScalar& maxDiagEntry) {
382 JacobiRotation<Scalar> rot;
384 numext::sqrt<RealScalar>(numext::abs2(work_matrix.coeff(p, p)) + numext::abs2(work_matrix.coeff(q, p)));
386 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
387 const RealScalar precision = NumTraits<Scalar>::epsilon();
389 if (numext::is_exactly_zero(n)) {
391 work_matrix.coeffRef(p, p) = work_matrix.coeffRef(q, p) = Scalar(0);
393 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
396 z = numext::abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
397 work_matrix.row(p) *= z;
398 if (svd.computeU()) svd.m_matrixU.col(p) *= numext::conj(z);
400 if (numext::abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
401 z = numext::abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
402 work_matrix.row(q) *= z;
403 if (svd.computeU()) svd.m_matrixU.col(q) *= numext::conj(z);
407 rot.c() = numext::conj(work_matrix.coeff(p, p)) / n;
408 rot.s() = work_matrix.coeff(q, p) / n;
409 work_matrix.applyOnTheLeft(p, q, rot);
410 if (svd.computeU()) svd.m_matrixU.applyOnTheRight(p, q, rot.adjoint());
411 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
412 z = numext::abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
413 work_matrix.col(q) *= z;
414 if (svd.computeV()) svd.m_matrixV.col(q) *= z;
416 if (numext::abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
417 z = numext::abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
418 work_matrix.row(q) *= z;
419 if (svd.computeU()) svd.m_matrixU.col(q) *= numext::conj(z);
424 maxDiagEntry = numext::maxi<RealScalar>(
426 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
428 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
429 return numext::abs(work_matrix.coeff(p, q)) > threshold || numext::abs(work_matrix.coeff(q, p)) > threshold;
433template <
typename WorkMatrixType,
typename MatrixUType,
typename MatrixVType>
434EIGEN_DONT_INLINE
bool jacobi_svd_nonblocking_sweep(WorkMatrixType& work_matrix, MatrixUType& matrix_u,
435 MatrixVType& matrix_v,
bool compute_u,
bool compute_v,
436 typename WorkMatrixType::RealScalar considerAsZero,
437 typename WorkMatrixType::RealScalar precision,
438 typename WorkMatrixType::RealScalar& maxDiagEntry) {
439 using Scalar =
typename WorkMatrixType::Scalar;
440 using RealScalar =
typename WorkMatrixType::RealScalar;
441 const Index n = work_matrix.rows();
442 bool notFinished =
false;
444 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
446 for (Index p = 1; p < n; ++p) {
447 for (Index q = 0; q < p; ++q) {
448 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
449 if (numext::abs(work_matrix.coeff(p, q)) > threshold || numext::abs(work_matrix.coeff(q, p)) > threshold) {
452 bool doRealSvd =
true;
454 numext::sqrt<RealScalar>(numext::abs2(work_matrix.coeff(p, p)) + numext::abs2(work_matrix.coeff(q, p)));
456 if (numext::is_exactly_zero(nn)) {
458 work_matrix.coeffRef(p, p) = work_matrix.coeffRef(q, p) = Scalar(0);
460 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
463 z = numext::abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
464 work_matrix.row(p) *= z;
465 if (compute_u) matrix_u.col(p) *= numext::conj(z);
467 if (numext::abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
468 z = numext::abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
469 work_matrix.row(q) *= z;
470 if (compute_u) matrix_u.col(q) *= numext::conj(z);
473 JacobiRotation<Scalar> rot;
474 rot.c() = numext::conj(work_matrix.coeff(p, p)) / nn;
475 rot.s() = work_matrix.coeff(q, p) / nn;
476 work_matrix.applyOnTheLeft(p, q, rot);
477 if (compute_u) matrix_u.applyOnTheRight(p, q, rot.adjoint());
478 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
479 z = numext::abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
480 work_matrix.col(q) *= z;
481 if (compute_v) matrix_v.col(q) *= z;
483 if (numext::abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
484 z = numext::abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
485 work_matrix.row(q) *= z;
486 if (compute_u) matrix_u.col(q) *= numext::conj(z);
490 maxDiagEntry = numext::maxi<RealScalar>(
492 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
493 threshold = numext::maxi<RealScalar>(considerAsZero, NumTraits<Scalar>::epsilon() * maxDiagEntry);
495 numext::abs(work_matrix.coeff(p, q)) > threshold || numext::abs(work_matrix.coeff(q, p)) > threshold;
498 JacobiRotation<RealScalar> j_left, j_right;
499 internal::real_2x2_jacobi_svd(work_matrix, p, q, &j_left, &j_right);
500 work_matrix.applyOnTheLeft(p, q, j_left);
501 if (compute_u) matrix_u.applyOnTheRight(p, q, j_left.transpose());
502 work_matrix.applyOnTheRight(p, q, j_right);
503 if (compute_v) matrix_v.applyOnTheRight(p, q, j_right);
504 maxDiagEntry = numext::maxi<RealScalar>(
506 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
513 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
514 for (Index p = 1; p < n; ++p) {
515 for (Index q = 0; q < p; ++q) {
516 if (numext::abs(work_matrix.coeff(p, q)) > threshold || numext::abs(work_matrix.coeff(q, p)) > threshold) {
518 JacobiRotation<RealScalar> j_left, j_right;
519 internal::real_2x2_jacobi_svd(work_matrix, p, q, &j_left, &j_right);
520 work_matrix.applyOnTheLeft(p, q, j_left);
521 if (compute_u) matrix_u.applyOnTheRight(p, q, j_left.transpose());
522 work_matrix.applyOnTheRight(p, q, j_right);
523 if (compute_v) matrix_v.applyOnTheRight(p, q, j_right);
524 maxDiagEntry = numext::maxi<RealScalar>(
526 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
527 threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
536template <
typename MatrixType_,
int Options>
537struct traits<JacobiSVD<MatrixType_, Options>> : svd_traits<MatrixType_, Options> {
538 using MatrixType = MatrixType_;
615template <
typename MatrixType_,
int Options_>
620 using MatrixType = MatrixType_;
621 using Scalar =
typename Base::Scalar;
622 using RealScalar =
typename Base::RealScalar;
625 QRPreconditioner = internal::get_qr_preconditioner(Options),
626 RowsAtCompileTime = Base::RowsAtCompileTime,
627 ColsAtCompileTime = Base::ColsAtCompileTime,
628 DiagSizeAtCompileTime = Base::DiagSizeAtCompileTime,
629 MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime,
630 MaxColsAtCompileTime = Base::MaxColsAtCompileTime,
631 MaxDiagSizeAtCompileTime = Base::MaxDiagSizeAtCompileTime,
632 MatrixOptions = Base::MatrixOptions
635 using MatrixUType =
typename Base::MatrixUType;
636 using MatrixVType =
typename Base::MatrixVType;
637 using SingularValuesType =
typename Base::SingularValuesType;
638 using WorkMatrixType =
Matrix<Scalar, DiagSizeAtCompileTime, DiagSizeAtCompileTime, MatrixOptions,
639 MaxDiagSizeAtCompileTime, MaxDiagSizeAtCompileTime>;
655 JacobiSVD(Index rows, Index cols) { allocate(rows, cols, internal::get_computation_options(Options)); }
673 EIGEN_DEPRECATED_WITH_REASON(
"Options should be specified using the class template parameter.")
674 JacobiSVD(Index rows, Index cols,
unsigned int computationOptions) {
675 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, rows, cols);
676 allocate(rows, cols, computationOptions);
684 template <
typename Derived>
686 compute_impl(matrix, internal::get_computation_options(Options));
689 template <
typename Derived>
691 compute_impl(matrix, internal::get_computation_options(Options));
707 template <
typename Derived>
709 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
710 compute_impl(matrix, computationOptions);
718 template <
typename Derived>
720 return compute_impl(matrix, m_computationOptions);
723 template <
typename Derived>
725 return compute_impl(matrix, m_computationOptions);
737 template <
typename Derived>
738 EIGEN_DEPRECATED_WITH_REASON(
"Options should be specified using the class template parameter.")
740 internal::check_svd_options_assertions<MatrixType, Options>(m_computationOptions, matrix.rows(), matrix.cols());
741 return compute_impl(matrix, computationOptions);
745 using Base::computeU;
746 using Base::computeV;
747 using Base::diagSize;
751 void allocate(Index rows_, Index cols_,
unsigned int computationOptions) {
752 if (Base::allocate(rows_, cols_, computationOptions))
return;
755 "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
756 "Use the ColPivHouseholderQR preconditioner instead.");
758 m_workMatrix.resize(diagSize(), diagSize());
759 if (cols() > rows()) m_qr_precond_morecols.allocate(*
this);
760 if (internal::svd_precondition_more_rows(Options, rows(), cols())) m_qr_precond_morerows.allocate(*
this);
764 template <
typename Derived>
765 JacobiSVD& compute_impl(
const TriangularBase<Derived>& matrix,
unsigned int computationOptions);
766 template <
typename Derived>
767 JacobiSVD& compute_impl(
const MatrixBase<Derived>& matrix,
unsigned int computationOptions);
773 EIGEN_DONT_INLINE
bool blocked_sweep(RealScalar considerAsZero, RealScalar precision, RealScalar& maxDiagEntry);
776 using Base::m_computationOptions;
777 using Base::m_computeFullU;
778 using Base::m_computeFullV;
779 using Base::m_computeThinU;
780 using Base::m_computeThinV;
782 using Base::m_isAllocated;
783 using Base::m_isInitialized;
784 using Base::m_matrixU;
785 using Base::m_matrixV;
786 using Base::m_nonzeroSingularValues;
787 using Base::m_prescribedThreshold;
788 using Base::m_singularValues;
789 using Base::m_usePrescribedThreshold;
790 using Base::ShouldComputeThinU;
791 using Base::ShouldComputeThinV;
793 EIGEN_STATIC_ASSERT(!(ShouldComputeThinU &&
int(QRPreconditioner) ==
int(FullPivHouseholderQRPreconditioner)) &&
794 !(ShouldComputeThinV &&
int(QRPreconditioner) ==
int(FullPivHouseholderQRPreconditioner)),
795 "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
796 "Use the ColPivHouseholderQR preconditioner instead.")
797 EIGEN_STATIC_ASSERT(!(internal::should_svd_precondition_square_matrix(Options) &&
798 int(QRPreconditioner) ==
int(NoQRPreconditioner)),
799 "JacobiSVD: PreconditionSquareMatrix requires a QR preconditioner other than NoQRPreconditioner.")
801 template <typename MatrixType__,
int Options__,
bool IsComplex_>
802 friend struct internal::svd_precondition_2x2_block_to_be_real;
803 template <typename MatrixType__,
int Options__,
int QRPreconditioner_,
int Case_,
bool DoAnything_>
804 friend struct internal::qr_preconditioner_impl;
806 internal::qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreColsThanRows>
807 m_qr_precond_morecols;
808 internal::qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreRowsThanCols>
809 m_qr_precond_morerows;
810 WorkMatrixType m_workMatrix;
813#ifdef EIGEN_JACOBI_SVD_BLOCK_SIZE
814 static constexpr Index kDefaultBlockSize = EIGEN_JACOBI_SVD_BLOCK_SIZE;
816 static constexpr Index kDefaultBlockSize = 32;
820 static constexpr Index kBlockSize = internal::min_size_prefer_fixed(kDefaultBlockSize, MaxDiagSizeAtCompileTime);
823template <
typename MatrixType,
int Options>
824template <
typename Derived>
825JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute_impl(
const TriangularBase<Derived>& matrix,
826 unsigned int computationOptions) {
827 return compute_impl(matrix.toDenseMatrix(), computationOptions);
830template <
typename MatrixType,
int Options>
831template <
typename Derived>
832JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute_impl(
const MatrixBase<Derived>& matrix,
833 unsigned int computationOptions) {
834 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived, MatrixType);
835 EIGEN_STATIC_ASSERT((std::is_same<typename Derived::Scalar, typename MatrixType::Scalar>::value),
836 Input matrix must have the same Scalar type as the JacobiSVD
object.);
838 allocate(matrix.rows(), matrix.cols(), computationOptions);
842 const RealScalar precision = RealScalar(2) * NumTraits<Scalar>::epsilon();
845 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
849 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(matrix.derived());
850 if (!(numext::isfinite)(maxCoeff)) {
851 m_isInitialized =
true;
853 m_nonzeroSingularValues = 0;
854 m_singularValues.setZero();
857 internal::safe_scaling_factors<RealScalar> factors;
861 if (rows() != cols() || internal::should_svd_precondition_square_matrix(Options)) {
863 internal::safe_scaling<RealScalar>::with_scaled(matrix.derived(), maxCoeff, [&](
const auto& scaledMatrix) {
864 m_qr_precond_morecols.run(*this, scaledMatrix);
865 m_qr_precond_morerows.run(*this, scaledMatrix);
868 factors = internal::safe_scaling<RealScalar>::scale_to(
870 matrix.template topLeftCorner<DiagSizeAtCompileTime, DiagSizeAtCompileTime>(diagSize(), diagSize()), maxCoeff);
871 if (m_computeFullU) m_matrixU.setIdentity(rows(), rows());
872 if (m_computeThinU) m_matrixU.setIdentity(rows(), diagSize());
873 if (m_computeFullV) m_matrixV.setIdentity(cols(), cols());
874 if (m_computeThinV) m_matrixV.setIdentity(cols(), diagSize());
878 RealScalar maxDiagEntry = diagSize() == 0 ? RealScalar(0) : m_workMatrix.cwiseAbs().diagonal().maxCoeff();
880 bool finished =
false;
884 EIGEN_IF_CONSTEXPR (MaxDiagSizeAtCompileTime == Dynamic || MaxDiagSizeAtCompileTime > kBlockSize) {
893 const Index n = diagSize();
894#ifdef EIGEN_JACOBI_SVD_BLOCKING_THRESHOLD
895 const Index blockingThreshold = EIGEN_JACOBI_SVD_BLOCKING_THRESHOLD;
897 const Index blockingThreshold =
898 static_cast<Index
>(numext::sqrt(
static_cast<double>(l2CacheSize() /
sizeof(
float))));
901 if (n >= blockingThreshold) {
905 finished = !blocked_sweep(considerAsZero, precision, maxDiagEntry);
909 finished = !internal::jacobi_svd_nonblocking_sweep(m_workMatrix, m_matrixU, m_matrixV, computeU(), computeV(),
910 considerAsZero, precision, maxDiagEntry);
916 for (Index i = 0; i < diagSize(); ++i) {
920 bool diagonal_has_imaginary_part =
false;
921 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
922 diagonal_has_imaginary_part = numext::abs(numext::imag(m_workMatrix.coeff(i, i))) > considerAsZero;
924 if (diagonal_has_imaginary_part) {
925 RealScalar a = numext::abs(m_workMatrix.coeff(i, i));
926 m_singularValues.coeffRef(i) = numext::abs(a);
927 if (computeU()) m_matrixU.col(i) *= m_workMatrix.coeff(i, i) / a;
931 RealScalar a = numext::real(m_workMatrix.coeff(i, i));
932 m_singularValues.coeffRef(i) = internal::abs_preserving_subnormals(a);
933 if (computeU() && internal::is_negative_preserving_subnormals(a)) m_matrixU.col(i) = -m_matrixU.col(i);
941 for (Index i = 0; i < diagSize(); i++) {
943 RealScalar maxRemainingSingularValue = m_singularValues.tail(diagSize() - i).maxCoeff(&pos);
944 if (internal::is_zero_or_subnormal_magnitude(maxRemainingSingularValue)) {
945 pos = internal::safe_scaling<RealScalar>::recover_flushed_max_coeff_index(m_singularValues.tail(diagSize() - i),
947 if (numext::is_exactly_zero_no_flush(m_singularValues.coeff(i + pos)))
break;
951 std::swap(m_singularValues.coeffRef(i), m_singularValues.coeffRef(pos));
952 if (computeU()) m_matrixU.col(pos).swap(m_matrixU.col(i));
953 if (computeV()) m_matrixV.col(pos).swap(m_matrixV.col(i));
958 internal::safe_scaling<RealScalar>::unscale_in_place(m_singularValues, maxCoeff, factors);
959 m_nonzeroSingularValues = diagSize();
960 for (Index i = 0; i < diagSize(); i++) {
961 if (numext::is_exactly_zero_no_flush(m_singularValues.coeff(i))) {
962 m_nonzeroSingularValues = i;
967 m_isInitialized =
true;
988template <
typename MatrixType,
int Options>
989EIGEN_DONT_INLINE
bool JacobiSVD<MatrixType, Options>::blocked_sweep(RealScalar considerAsZero, RealScalar precision,
990 RealScalar& maxDiagEntry) {
991 const Index n = diagSize();
992 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
993 bool notFinished =
false;
994 static constexpr Index kBlockBufferSize = (kBlockSize + 1) * (kBlockSize + 1);
995 ei_declare_aligned_stack_constructed_variable(Scalar, blockBufferPtr, kBlockBufferSize, 0);
996 Map<Matrix<Scalar, kBlockSize + 1, kBlockSize + 1, MatrixOptions>, AlignedMax> blockBuffer(
997 blockBufferPtr, kBlockSize + 1, kBlockSize + 1);
999 ei_declare_aligned_stack_constructed_variable(Scalar, accumPtr, kBlockBufferSize, 0);
1000 Map<Matrix<Scalar, kBlockSize + 1, kBlockSize + 1, MatrixOptions>, AlignedMax> accum(accumPtr, kBlockSize + 1,
1002 Matrix<Scalar, 1, Dynamic, RowMajor, 1, MaxDiagSizeAtCompileTime> Mp_save;
1004 for (Index p = 1; p < n; ++p) {
1011 for (; q + kBlockSize <= p; q += kBlockSize) {
1014 blockBuffer.template topLeftCorner<kBlockSize, kBlockSize>() =
1015 m_workMatrix.template block<kBlockSize, kBlockSize>(q, q);
1016 blockBuffer.col(kBlockSize).template head<kBlockSize>() = m_workMatrix.col(p).template segment<kBlockSize>(q);
1017 blockBuffer.row(kBlockSize).template head<kBlockSize>() = m_workMatrix.row(p).template segment<kBlockSize>(q);
1018 blockBuffer(kBlockSize, kBlockSize) = m_workMatrix(p, p);
1024 accum.setIdentity(kBlockSize + 1, kBlockSize + 1);
1025 bool blockDirty =
false;
1027 for (Index qq = 0; qq < kBlockSize; ++qq) {
1028 if (numext::abs(blockBuffer.coeff(kBlockSize, qq)) > threshold ||
1029 numext::abs(blockBuffer.coeff(qq, kBlockSize)) > threshold) {
1043 bool doRealSvd =
true;
1044 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
1047 RealScalar nn = numext::sqrt<RealScalar>(numext::abs2(blockBuffer.coeff(kBlockSize, kBlockSize)) +
1048 numext::abs2(blockBuffer.coeff(qq, kBlockSize)));
1050 if (numext::is_exactly_zero(nn)) {
1052 blockBuffer.coeffRef(kBlockSize, kBlockSize) = Scalar(0);
1053 blockBuffer.coeffRef(qq, kBlockSize) = Scalar(0);
1056 if (numext::abs(numext::imag(blockBuffer.coeff(kBlockSize, qq))) > considerAsZero) {
1057 z = numext::abs(blockBuffer.coeff(kBlockSize, qq)) / blockBuffer.coeff(kBlockSize, qq);
1058 blockBuffer.row(kBlockSize) *= z;
1059 accum.row(kBlockSize) *= z;
1060 if (computeU()) m_matrixU.col(p) *= numext::conj(z);
1062 if (numext::abs(numext::imag(blockBuffer.coeff(qq, qq))) > considerAsZero) {
1063 z = numext::abs(blockBuffer.coeff(qq, qq)) / blockBuffer.coeff(qq, qq);
1064 blockBuffer.row(qq) *= z;
1066 if (computeU()) m_matrixU.col(q + qq) *= numext::conj(z);
1073 JacobiRotation<Scalar> rot;
1074 rot.c() = numext::conj(blockBuffer.coeff(kBlockSize, kBlockSize)) / nn;
1075 rot.s() = blockBuffer.coeff(qq, kBlockSize) / nn;
1076 blockBuffer.applyOnTheLeft(kBlockSize, qq, rot);
1077 accum.applyOnTheLeft(kBlockSize, qq, rot);
1078 if (computeU()) m_matrixU.applyOnTheRight(p, q + qq, rot.adjoint());
1081 if (numext::abs(numext::imag(blockBuffer.coeff(kBlockSize, qq))) > considerAsZero) {
1082 z = numext::abs(blockBuffer.coeff(kBlockSize, qq)) / blockBuffer.coeff(kBlockSize, qq);
1083 blockBuffer.col(qq) *= z;
1084 m_workMatrix.col(q + qq) *= z;
1085 if (computeV()) m_matrixV.col(q + qq) *= z;
1088 if (numext::abs(numext::imag(blockBuffer.coeff(qq, qq))) > considerAsZero) {
1089 z = numext::abs(blockBuffer.coeff(qq, qq)) / blockBuffer.coeff(qq, qq);
1090 blockBuffer.row(qq) *= z;
1092 if (computeU()) m_matrixU.col(q + qq) *= numext::conj(z);
1096 maxDiagEntry = numext::maxi<RealScalar>(
1097 maxDiagEntry, numext::maxi<RealScalar>(numext::abs(blockBuffer.coeff(kBlockSize, kBlockSize)),
1098 numext::abs(blockBuffer.coeff(qq, qq))));
1100 RealScalar precondThreshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
1101 doRealSvd = numext::abs(blockBuffer.coeff(kBlockSize, qq)) > precondThreshold ||
1102 numext::abs(blockBuffer.coeff(qq, kBlockSize)) > precondThreshold;
1107 JacobiRotation<RealScalar> j_left, j_right;
1108 internal::real_2x2_jacobi_svd(blockBuffer, kBlockSize, qq, &j_left, &j_right);
1109 blockBuffer.applyOnTheLeft(kBlockSize, qq, j_left);
1110 blockBuffer.applyOnTheRight(kBlockSize, qq, j_right);
1113 accum.applyOnTheLeft(kBlockSize, qq, j_left);
1116 m_workMatrix.applyOnTheRight(p, q + qq, j_right);
1117 if (computeU()) m_matrixU.applyOnTheRight(p, q + qq, j_left.transpose());
1118 if (computeV()) m_matrixV.applyOnTheRight(p, q + qq, j_right);
1120 maxDiagEntry = numext::maxi<RealScalar>(
1121 maxDiagEntry, numext::maxi<RealScalar>(numext::abs(blockBuffer.coeff(kBlockSize, kBlockSize)),
1122 numext::abs(blockBuffer.coeff(qq, qq))));
1124 threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
1135 if (p == q + kBlockSize) {
1136 m_workMatrix.template middleRows<kBlockSize + 1>(q) =
1137 accum * m_workMatrix.template middleRows<kBlockSize + 1>(q);
1139 const auto L11 = accum.template topLeftCorner<kBlockSize, kBlockSize>();
1140 const auto l12 = accum.col(kBlockSize).template head<kBlockSize>();
1141 const auto l21 = accum.row(kBlockSize).template head<kBlockSize>();
1142 const Scalar l22 = accum(kBlockSize, kBlockSize);
1143 auto Mq = m_workMatrix.template middleRows<kBlockSize>(q);
1144 auto Mp = m_workMatrix.row(p);
1146 Mp.noalias() = l21 * Mq + l22 * Mp_save;
1147 Mq = L11.template triangularView<Lower>() * Mq + l12 * Mp_save;
1153 for (; q < p; ++q) {
1154 if (numext::abs(m_workMatrix.coeff(p, q)) > threshold || numext::abs(m_workMatrix.coeff(q, p)) > threshold) {
1157 bool doRealSvd =
true;
1158 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
1159 doRealSvd = internal::svd_precondition_2x2_block_to_be_real<MatrixType, Options>::run(m_workMatrix, *
this, p,
1164 JacobiRotation<RealScalar> j_left, j_right;
1165 internal::real_2x2_jacobi_svd(m_workMatrix, p, q, &j_left, &j_right);
1166 m_workMatrix.applyOnTheLeft(p, q, j_left);
1167 if (computeU()) m_matrixU.applyOnTheRight(p, q, j_left.transpose());
1168 m_workMatrix.applyOnTheRight(p, q, j_right);
1169 if (computeV()) m_matrixV.applyOnTheRight(p, q, j_right);
1170 maxDiagEntry = numext::maxi<RealScalar>(
1172 numext::maxi<RealScalar>(numext::abs(m_workMatrix.coeff(p, p)), numext::abs(m_workMatrix.coeff(q, q))));
1174 threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
1189template <
typename Derived>
1190template <
int Options>
1195template <
typename Derived>
1196template <
int Options>
1198 unsigned int computationOptions)
const {
Householder QR decomposition of a matrix.
Definition HouseholderQR.h:77
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition JacobiSVD.h:616
JacobiSVD()
Default Constructor.
Definition JacobiSVD.h:646
JacobiSVD & compute(const MatrixBase< Derived > &matrix)
Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified us...
Definition JacobiSVD.h:719
JacobiSVD(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition JacobiSVD.h:655
JacobiSVD(const MatrixBase< Derived > &matrix)
Constructor performing the decomposition of given matrix, using the custom options specified with the...
Definition JacobiSVD.h:685
JacobiSVD(const MatrixBase< Derived > &matrix, unsigned int computationOptions)
Constructor performing the decomposition of given matrix using specified options for computing unitar...
Definition JacobiSVD.h:708
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
SVDBase()
Definition SVDBase.h:350
Base class for triangular part in a matrix.
Definition TriangularMatrix.h:68
@ FullPivHouseholderQRPreconditioner
Definition Constants.h:432
@ InvalidInput
Definition Constants.h:464