Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
JacobiSVD.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009-2010 Benoit Jacob <[email protected]>
5// Copyright (C) 2013-2014 Gael Guennebaud <[email protected]>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_JACOBISVD_H
12#define EIGEN_JACOBISVD_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// forward declaration (needed by ICC)
22// the empty body is required by MSVC
23template <typename MatrixType, int Options, bool IsComplex = NumTraits<typename MatrixType::Scalar>::IsComplex>
24struct svd_precondition_2x2_block_to_be_real {};
25
26/*** QR preconditioners (R-SVD)
27 ***
28 *** Their role is to reduce the problem of computing the SVD to the case of a square matrix.
29 *** This approach, known as R-SVD, is an optimization for rectangular-enough matrices, and is a requirement for
30 *** JacobiSVD which by itself is only able to work on square matrices.
31 ***/
32
33enum { PreconditionIfMoreColsThanRows, PreconditionIfMoreRowsThanCols };
34
35template <typename MatrixType, int QRPreconditioner, int Case>
36struct qr_preconditioner_should_do_anything {
37 enum {
38 a = MatrixType::RowsAtCompileTime != Dynamic && MatrixType::ColsAtCompileTime != Dynamic &&
39 MatrixType::ColsAtCompileTime <= MatrixType::RowsAtCompileTime,
40 b = MatrixType::RowsAtCompileTime != Dynamic && MatrixType::ColsAtCompileTime != Dynamic &&
41 MatrixType::RowsAtCompileTime <= MatrixType::ColsAtCompileTime,
42 ret = !((QRPreconditioner == NoQRPreconditioner) || (Case == PreconditionIfMoreColsThanRows && bool(a)) ||
43 (Case == PreconditionIfMoreRowsThanCols && bool(b)))
44 };
45};
46
47template <typename MatrixType, int Options, int QRPreconditioner, int Case,
48 bool DoAnything = qr_preconditioner_should_do_anything<MatrixType, QRPreconditioner, Case>::ret>
49struct qr_preconditioner_impl {};
50
51template <typename MatrixType, int Options, int QRPreconditioner, int Case>
52class qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, Case, false> {
53 public:
54 void allocate(const JacobiSVD<MatrixType, Options>&) {}
55 template <typename Xpr>
56 bool run(JacobiSVD<MatrixType, Options>&, const Xpr&) {
57 return false;
58 }
59};
60
61/*** preconditioner using FullPivHouseholderQR ***/
62
63template <typename MatrixType, int Options>
64class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
65 true> {
66 public:
67 typedef typename MatrixType::Scalar Scalar;
68 typedef JacobiSVD<MatrixType, Options> SVDType;
69
70 enum { WorkspaceSize = MatrixType::RowsAtCompileTime, MaxWorkspaceSize = MatrixType::MaxRowsAtCompileTime };
71
72 typedef Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize> WorkspaceType;
73
74 void allocate(const SVDType& svd) {
75 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
76 internal::destroy_at(&m_qr);
77 internal::construct_at(&m_qr, svd.rows(), svd.cols());
78 }
79 if (svd.m_computeFullU) m_workspace.resize(svd.rows());
80 }
81 template <typename Xpr>
82 bool run(SVDType& svd, const Xpr& matrix) {
83 if (matrix.rows() > matrix.cols()) {
84 m_qr.compute(matrix);
85 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
86 if (svd.m_computeFullU) m_qr.matrixQ().evalTo(svd.m_matrixU, m_workspace);
87 if (svd.computeV()) svd.m_matrixV = m_qr.colsPermutation();
88 return true;
89 }
90 return false;
91 }
92
93 private:
94 typedef FullPivHouseholderQR<MatrixType> QRType;
95 QRType m_qr;
96 WorkspaceType m_workspace;
97};
98
99template <typename MatrixType, int Options>
100class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
101 true> {
102 public:
103 typedef typename MatrixType::Scalar Scalar;
104 typedef JacobiSVD<MatrixType, Options> SVDType;
105
106 enum {
107 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
108 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
109 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
110 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
111 MatrixOptions = traits<MatrixType>::Options
112 };
113
114 typedef typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
115 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type
116 TransposeTypeWithSameStorageOrder;
117
118 void allocate(const SVDType& svd) {
119 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
120 internal::destroy_at(&m_qr);
121 internal::construct_at(&m_qr, svd.cols(), svd.rows());
122 }
123 if (svd.m_computeFullV) m_workspace.resize(svd.cols());
124 }
125 template <typename Xpr>
126 bool run(SVDType& svd, const Xpr& matrix) {
127 if (matrix.cols() > matrix.rows()) {
128 m_qr.compute(matrix.adjoint());
129 svd.m_workMatrix =
130 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
131 if (svd.m_computeFullV) m_qr.matrixQ().evalTo(svd.m_matrixV, m_workspace);
132 if (svd.computeU()) svd.m_matrixU = m_qr.colsPermutation();
133 return true;
134 } else
135 return false;
136 }
137
138 private:
139 typedef FullPivHouseholderQR<TransposeTypeWithSameStorageOrder> QRType;
140 QRType m_qr;
141 typename plain_row_type<MatrixType>::type m_workspace;
142};
143
144/*** preconditioner using ColPivHouseholderQR ***/
145
146template <typename MatrixType, int Options>
147class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
148 true> {
149 public:
150 typedef typename MatrixType::Scalar Scalar;
151 typedef JacobiSVD<MatrixType, Options> SVDType;
152
153 enum {
154 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
155 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
156 };
157
158 typedef Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize> WorkspaceType;
159
160 void allocate(const SVDType& svd) {
161 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
162 internal::destroy_at(&m_qr);
163 internal::construct_at(&m_qr, svd.rows(), svd.cols());
164 }
165 if (svd.m_computeFullU)
166 m_workspace.resize(svd.rows());
167 else if (svd.m_computeThinU)
168 m_workspace.resize(svd.cols());
169 }
170 template <typename Xpr>
171 bool run(SVDType& svd, const Xpr& matrix) {
172 if (matrix.rows() > matrix.cols()) {
173 m_qr.compute(matrix);
174 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
175 if (svd.m_computeFullU)
176 m_qr.householderQ().evalTo(svd.m_matrixU, m_workspace);
177 else if (svd.m_computeThinU) {
178 svd.m_matrixU.setIdentity(matrix.rows(), matrix.cols());
179 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixU, m_workspace);
180 }
181 if (svd.computeV()) svd.m_matrixV = m_qr.colsPermutation();
182 return true;
183 }
184 return false;
185 }
186
187 private:
188 typedef ColPivHouseholderQR<MatrixType> QRType;
189 QRType m_qr;
190 WorkspaceType m_workspace;
191};
192
193template <typename MatrixType, int Options>
194class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
195 true> {
196 public:
197 typedef typename MatrixType::Scalar Scalar;
198 typedef JacobiSVD<MatrixType, Options> SVDType;
199
200 enum {
201 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
202 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
203 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
204 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
205 MatrixOptions = internal::traits<MatrixType>::Options,
206 WorkspaceSize = internal::traits<SVDType>::MatrixVColsAtCompileTime,
207 MaxWorkspaceSize = internal::traits<SVDType>::MatrixVMaxColsAtCompileTime
208 };
209
210 typedef Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1> WorkspaceType;
211
212 typedef typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
213 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type
214 TransposeTypeWithSameStorageOrder;
215
216 void allocate(const SVDType& svd) {
217 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
218 internal::destroy_at(&m_qr);
219 internal::construct_at(&m_qr, svd.cols(), svd.rows());
220 }
221 if (svd.m_computeFullV)
222 m_workspace.resize(svd.cols());
223 else if (svd.m_computeThinV)
224 m_workspace.resize(svd.rows());
225 }
226 template <typename Xpr>
227 bool run(SVDType& svd, const Xpr& matrix) {
228 if (matrix.cols() > matrix.rows()) {
229 m_qr.compute(matrix.adjoint());
230
231 svd.m_workMatrix =
232 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
233 if (svd.m_computeFullV)
234 m_qr.householderQ().evalTo(svd.m_matrixV, m_workspace);
235 else if (svd.m_computeThinV) {
236 svd.m_matrixV.setIdentity(matrix.cols(), matrix.rows());
237 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixV, m_workspace);
238 }
239 if (svd.computeU()) svd.m_matrixU = m_qr.colsPermutation();
240 return true;
241 } else
242 return false;
243 }
244
245 private:
246 typedef ColPivHouseholderQR<TransposeTypeWithSameStorageOrder> QRType;
247 QRType m_qr;
248 WorkspaceType m_workspace;
249};
250
251/*** preconditioner using HouseholderQR ***/
252
253template <typename MatrixType, int Options>
254class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols, true> {
255 public:
256 typedef typename MatrixType::Scalar Scalar;
257 typedef JacobiSVD<MatrixType, Options> SVDType;
258
259 enum {
260 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
261 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
262 };
263
264 typedef Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize> WorkspaceType;
265
266 void allocate(const SVDType& svd) {
267 if (svd.rows() != m_qr.rows() || svd.cols() != m_qr.cols()) {
268 internal::destroy_at(&m_qr);
269 internal::construct_at(&m_qr, svd.rows(), svd.cols());
270 }
271 if (svd.m_computeFullU)
272 m_workspace.resize(svd.rows());
273 else if (svd.m_computeThinU)
274 m_workspace.resize(svd.cols());
275 }
276 template <typename Xpr>
277 bool run(SVDType& svd, const Xpr& matrix) {
278 if (matrix.rows() > matrix.cols()) {
279 m_qr.compute(matrix);
280 svd.m_workMatrix = m_qr.matrixQR().block(0, 0, matrix.cols(), matrix.cols()).template triangularView<Upper>();
281 if (svd.m_computeFullU)
282 m_qr.householderQ().evalTo(svd.m_matrixU, m_workspace);
283 else if (svd.m_computeThinU) {
284 svd.m_matrixU.setIdentity(matrix.rows(), matrix.cols());
285 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixU, m_workspace);
286 }
287 if (svd.computeV()) svd.m_matrixV.setIdentity(matrix.cols(), matrix.cols());
288 return true;
289 }
290 return false;
291 }
292
293 private:
294 typedef HouseholderQR<MatrixType> QRType;
295 QRType m_qr;
296 WorkspaceType m_workspace;
297};
298
299template <typename MatrixType, int Options>
300class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreColsThanRows, true> {
301 public:
302 typedef typename MatrixType::Scalar Scalar;
303 typedef JacobiSVD<MatrixType, Options> SVDType;
304
305 enum {
306 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
307 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
308 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
309 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
310 MatrixOptions = internal::traits<MatrixType>::Options,
311 WorkspaceSize = internal::traits<SVDType>::MatrixVColsAtCompileTime,
312 MaxWorkspaceSize = internal::traits<SVDType>::MatrixVMaxColsAtCompileTime
313 };
314
315 typedef Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1> WorkspaceType;
316
317 typedef typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
318 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type
319 TransposeTypeWithSameStorageOrder;
320
321 void allocate(const SVDType& svd) {
322 if (svd.cols() != m_qr.rows() || svd.rows() != m_qr.cols()) {
323 internal::destroy_at(&m_qr);
324 internal::construct_at(&m_qr, svd.cols(), svd.rows());
325 }
326 if (svd.m_computeFullV)
327 m_workspace.resize(svd.cols());
328 else if (svd.m_computeThinV)
329 m_workspace.resize(svd.rows());
330 }
331
332 template <typename Xpr>
333 bool run(SVDType& svd, const Xpr& matrix) {
334 if (matrix.cols() > matrix.rows()) {
335 m_qr.compute(matrix.adjoint());
336
337 svd.m_workMatrix =
338 m_qr.matrixQR().block(0, 0, matrix.rows(), matrix.rows()).template triangularView<Upper>().adjoint();
339 if (svd.m_computeFullV)
340 m_qr.householderQ().evalTo(svd.m_matrixV, m_workspace);
341 else if (svd.m_computeThinV) {
342 svd.m_matrixV.setIdentity(matrix.cols(), matrix.rows());
343 m_qr.householderQ().applyThisOnTheLeft(svd.m_matrixV, m_workspace);
344 }
345 if (svd.computeU()) svd.m_matrixU.setIdentity(matrix.rows(), matrix.rows());
346 return true;
347 } else
348 return false;
349 }
350
351 private:
352 typedef HouseholderQR<TransposeTypeWithSameStorageOrder> QRType;
353 QRType m_qr;
354 WorkspaceType m_workspace;
355};
356
357/*** 2x2 SVD implementation
358 ***
359 *** JacobiSVD consists in performing a series of 2x2 SVD subproblems
360 ***/
361
362template <typename MatrixType, int Options>
363struct svd_precondition_2x2_block_to_be_real<MatrixType, Options, false> {
364 typedef JacobiSVD<MatrixType, Options> SVD;
365 typedef typename MatrixType::RealScalar RealScalar;
366 static bool run(typename SVD::WorkMatrixType&, SVD&, Index, Index, RealScalar&) { return true; }
367};
368
369template <typename MatrixType, int Options>
370struct svd_precondition_2x2_block_to_be_real<MatrixType, Options, true> {
371 typedef JacobiSVD<MatrixType, Options> SVD;
372 typedef typename MatrixType::Scalar Scalar;
373 typedef typename MatrixType::RealScalar RealScalar;
374 static bool run(typename SVD::WorkMatrixType& work_matrix, SVD& svd, Index p, Index q, RealScalar& maxDiagEntry) {
375 using std::abs;
376 using std::sqrt;
377 Scalar z;
378 JacobiRotation<Scalar> rot;
379 RealScalar n = sqrt(numext::abs2(work_matrix.coeff(p, p)) + numext::abs2(work_matrix.coeff(q, p)));
380
381 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
382 const RealScalar precision = NumTraits<Scalar>::epsilon();
383
384 if (numext::is_exactly_zero(n)) {
385 // make sure first column is zero
386 work_matrix.coeffRef(p, p) = work_matrix.coeffRef(q, p) = Scalar(0);
387
388 if (abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
389 // work_matrix.coeff(p,q) can be zero if work_matrix.coeff(q,p) is not zero but small enough to underflow when
390 // computing n
391 z = abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
392 work_matrix.row(p) *= z;
393 if (svd.computeU()) svd.m_matrixU.col(p) *= conj(z);
394 }
395 if (abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
396 z = abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
397 work_matrix.row(q) *= z;
398 if (svd.computeU()) svd.m_matrixU.col(q) *= conj(z);
399 }
400 // otherwise the second row is already zero, so we have nothing to do.
401 } else {
402 rot.c() = conj(work_matrix.coeff(p, p)) / n;
403 rot.s() = work_matrix.coeff(q, p) / n;
404 work_matrix.applyOnTheLeft(p, q, rot);
405 if (svd.computeU()) svd.m_matrixU.applyOnTheRight(p, q, rot.adjoint());
406 if (abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
407 z = abs(work_matrix.coeff(p, q)) / work_matrix.coeff(p, q);
408 work_matrix.col(q) *= z;
409 if (svd.computeV()) svd.m_matrixV.col(q) *= z;
410 }
411 if (abs(numext::imag(work_matrix.coeff(q, q))) > considerAsZero) {
412 z = abs(work_matrix.coeff(q, q)) / work_matrix.coeff(q, q);
413 work_matrix.row(q) *= z;
414 if (svd.computeU()) svd.m_matrixU.col(q) *= conj(z);
415 }
416 }
417
418 // update largest diagonal entry
419 maxDiagEntry = numext::maxi<RealScalar>(
420 maxDiagEntry, numext::maxi<RealScalar>(abs(work_matrix.coeff(p, p)), abs(work_matrix.coeff(q, q))));
421 // and check whether the 2x2 block is already diagonal
422 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
423 return abs(work_matrix.coeff(p, q)) > threshold || abs(work_matrix.coeff(q, p)) > threshold;
424 }
425};
426
427template <typename MatrixType_, int Options>
428struct traits<JacobiSVD<MatrixType_, Options> > : svd_traits<MatrixType_, Options> {
429 typedef MatrixType_ MatrixType;
430};
431
432} // end namespace internal
433
499template <typename MatrixType_, int Options_>
500class JacobiSVD : public SVDBase<JacobiSVD<MatrixType_, Options_> > {
501 typedef SVDBase<JacobiSVD> Base;
502
503 public:
504 typedef MatrixType_ MatrixType;
505 typedef typename Base::Scalar Scalar;
506 typedef typename Base::RealScalar RealScalar;
507 enum : int {
508 Options = Options_,
509 QRPreconditioner = internal::get_qr_preconditioner(Options),
510 RowsAtCompileTime = Base::RowsAtCompileTime,
511 ColsAtCompileTime = Base::ColsAtCompileTime,
512 DiagSizeAtCompileTime = Base::DiagSizeAtCompileTime,
513 MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime,
514 MaxColsAtCompileTime = Base::MaxColsAtCompileTime,
515 MaxDiagSizeAtCompileTime = Base::MaxDiagSizeAtCompileTime,
516 MatrixOptions = Base::MatrixOptions
517 };
518
519 typedef typename Base::MatrixUType MatrixUType;
520 typedef typename Base::MatrixVType MatrixVType;
521 typedef typename Base::SingularValuesType SingularValuesType;
522 typedef Matrix<Scalar, DiagSizeAtCompileTime, DiagSizeAtCompileTime, MatrixOptions, MaxDiagSizeAtCompileTime,
523 MaxDiagSizeAtCompileTime>
525
532
540 JacobiSVD(Index rows, Index cols) { allocate(rows, cols, internal::get_computation_options(Options)); }
541
556 EIGEN_DEPRECATED JacobiSVD(Index rows, Index cols, unsigned int computationOptions) {
557 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, rows, cols);
558 allocate(rows, cols, computationOptions);
559 }
560
566 explicit JacobiSVD(const MatrixType& matrix) { compute_impl(matrix, internal::get_computation_options(Options)); }
567
580 // EIGEN_DEPRECATED // TODO(cantonios): re-enable after fixing a few 3p libraries that error on deprecation warnings.
581 JacobiSVD(const MatrixType& matrix, unsigned int computationOptions) {
582 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
583 compute_impl(matrix, computationOptions);
584 }
585
591 JacobiSVD& compute(const MatrixType& matrix) { return compute_impl(matrix, m_computationOptions); }
592
602 EIGEN_DEPRECATED JacobiSVD& compute(const MatrixType& matrix, unsigned int computationOptions) {
603 internal::check_svd_options_assertions<MatrixType, Options>(m_computationOptions, matrix.rows(), matrix.cols());
604 return compute_impl(matrix, computationOptions);
605 }
606
607 using Base::cols;
608 using Base::computeU;
609 using Base::computeV;
610 using Base::diagSize;
611 using Base::rank;
612 using Base::rows;
613
614 private:
615 void allocate(Index rows_, Index cols_, unsigned int computationOptions) {
616 if (Base::allocate(rows_, cols_, computationOptions)) return;
617 eigen_assert(!(ShouldComputeThinU && int(QRPreconditioner) == int(FullPivHouseholderQRPreconditioner)) &&
618 !(ShouldComputeThinU && int(QRPreconditioner) == int(FullPivHouseholderQRPreconditioner)) &&
619 "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
620 "Use the ColPivHouseholderQR preconditioner instead.");
621
622 m_workMatrix.resize(diagSize(), diagSize());
623 if (cols() > rows()) m_qr_precond_morecols.allocate(*this);
624 if (rows() > cols()) m_qr_precond_morerows.allocate(*this);
625 }
626
627 JacobiSVD& compute_impl(const MatrixType& matrix, unsigned int computationOptions);
628
629 protected:
630 using Base::m_computationOptions;
631 using Base::m_computeFullU;
632 using Base::m_computeFullV;
633 using Base::m_computeThinU;
634 using Base::m_computeThinV;
635 using Base::m_info;
636 using Base::m_isAllocated;
637 using Base::m_isInitialized;
638 using Base::m_matrixU;
639 using Base::m_matrixV;
640 using Base::m_nonzeroSingularValues;
641 using Base::m_prescribedThreshold;
642 using Base::m_singularValues;
643 using Base::m_usePrescribedThreshold;
644 using Base::ShouldComputeThinU;
645 using Base::ShouldComputeThinV;
646
647 EIGEN_STATIC_ASSERT(!(ShouldComputeThinU && int(QRPreconditioner) == int(FullPivHouseholderQRPreconditioner)) &&
648 !(ShouldComputeThinU && int(QRPreconditioner) == int(FullPivHouseholderQRPreconditioner)),
649 "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
650 "Use the ColPivHouseholderQR preconditioner instead.")
651
652 template <typename MatrixType__, int Options__, bool IsComplex_>
653 friend struct internal::svd_precondition_2x2_block_to_be_real;
654 template <typename MatrixType__, int Options__, int QRPreconditioner_, int Case_, bool DoAnything_>
655 friend struct internal::qr_preconditioner_impl;
656
657 internal::qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreColsThanRows>
658 m_qr_precond_morecols;
659 internal::qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreRowsThanCols>
660 m_qr_precond_morerows;
661 WorkMatrixType m_workMatrix;
662};
663
664template <typename MatrixType, int Options>
665JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute_impl(const MatrixType& matrix,
666 unsigned int computationOptions) {
667 using std::abs;
668
669 allocate(matrix.rows(), matrix.cols(), computationOptions);
670
671 // currently we stop when we reach precision 2*epsilon as the last bit of precision can require an unreasonable number
672 // of iterations, only worsening the precision of U and V as we accumulate more rotations
673 const RealScalar precision = RealScalar(2) * NumTraits<Scalar>::epsilon();
674
675 // limit for denormal numbers to be considered zero in order to avoid infinite loops (see bug 286)
676 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
677
678 // Scaling factor to reduce over/under-flows
679 RealScalar scale = matrix.cwiseAbs().template maxCoeff<PropagateNaN>();
680 if (!(numext::isfinite)(scale)) {
681 m_isInitialized = true;
682 m_info = InvalidInput;
683 m_nonzeroSingularValues = 0;
684 return *this;
685 }
686 if (numext::is_exactly_zero(scale)) scale = RealScalar(1);
687
688 /*** step 1. The R-SVD step: we use a QR decomposition to reduce to the case of a square matrix */
689
690 if (rows() != cols()) {
691 m_qr_precond_morecols.run(*this, matrix / scale);
692 m_qr_precond_morerows.run(*this, matrix / scale);
693 } else {
694 m_workMatrix =
695 matrix.template topLeftCorner<DiagSizeAtCompileTime, DiagSizeAtCompileTime>(diagSize(), diagSize()) / scale;
696 if (m_computeFullU) m_matrixU.setIdentity(rows(), rows());
697 if (m_computeThinU) m_matrixU.setIdentity(rows(), diagSize());
698 if (m_computeFullV) m_matrixV.setIdentity(cols(), cols());
699 if (m_computeThinV) m_matrixV.setIdentity(cols(), diagSize());
700 }
701
702 /*** step 2. The main Jacobi SVD iteration. ***/
703 RealScalar maxDiagEntry = m_workMatrix.cwiseAbs().diagonal().maxCoeff();
704
705 bool finished = false;
706 while (!finished) {
707 finished = true;
708
709 // do a sweep: for all index pairs (p,q), perform SVD of the corresponding 2x2 sub-matrix
710
711 for (Index p = 1; p < diagSize(); ++p) {
712 for (Index q = 0; q < p; ++q) {
713 // if this 2x2 sub-matrix is not diagonal already...
714 // notice that this comparison will evaluate to false if any NaN is involved, ensuring that NaN's don't
715 // keep us iterating forever. Similarly, small denormal numbers are considered zero.
716 RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
717 if (abs(m_workMatrix.coeff(p, q)) > threshold || abs(m_workMatrix.coeff(q, p)) > threshold) {
718 finished = false;
719 // perform SVD decomposition of 2x2 sub-matrix corresponding to indices p,q to make it diagonal
720 // the complex to real operation returns true if the updated 2x2 block is not already diagonal
721 if (internal::svd_precondition_2x2_block_to_be_real<MatrixType, Options>::run(m_workMatrix, *this, p, q,
722 maxDiagEntry)) {
723 JacobiRotation<RealScalar> j_left, j_right;
724 internal::real_2x2_jacobi_svd(m_workMatrix, p, q, &j_left, &j_right);
725
726 // accumulate resulting Jacobi rotations
727 m_workMatrix.applyOnTheLeft(p, q, j_left);
728 if (computeU()) m_matrixU.applyOnTheRight(p, q, j_left.transpose());
729
730 m_workMatrix.applyOnTheRight(p, q, j_right);
731 if (computeV()) m_matrixV.applyOnTheRight(p, q, j_right);
732
733 // keep track of the largest diagonal coefficient
734 maxDiagEntry = numext::maxi<RealScalar>(
735 maxDiagEntry, numext::maxi<RealScalar>(abs(m_workMatrix.coeff(p, p)), abs(m_workMatrix.coeff(q, q))));
736 }
737 }
738 }
739 }
740 }
741
742 /*** step 3. The work matrix is now diagonal, so ensure it's positive so its diagonal entries are the singular values
743 * ***/
744
745 for (Index i = 0; i < diagSize(); ++i) {
746 // For a complex matrix, some diagonal coefficients might note have been
747 // treated by svd_precondition_2x2_block_to_be_real, and the imaginary part
748 // of some diagonal entry might not be null.
749 if (NumTraits<Scalar>::IsComplex && abs(numext::imag(m_workMatrix.coeff(i, i))) > considerAsZero) {
750 RealScalar a = abs(m_workMatrix.coeff(i, i));
751 m_singularValues.coeffRef(i) = abs(a);
752 if (computeU()) m_matrixU.col(i) *= m_workMatrix.coeff(i, i) / a;
753 } else {
754 // m_workMatrix.coeff(i,i) is already real, no difficulty:
755 RealScalar a = numext::real(m_workMatrix.coeff(i, i));
756 m_singularValues.coeffRef(i) = abs(a);
757 if (computeU() && (a < RealScalar(0))) m_matrixU.col(i) = -m_matrixU.col(i);
758 }
759 }
760
761 m_singularValues *= scale;
762
763 /*** step 4. Sort singular values in descending order and compute the number of nonzero singular values ***/
764
765 m_nonzeroSingularValues = diagSize();
766 for (Index i = 0; i < diagSize(); i++) {
767 Index pos;
768 RealScalar maxRemainingSingularValue = m_singularValues.tail(diagSize() - i).maxCoeff(&pos);
769 if (numext::is_exactly_zero(maxRemainingSingularValue)) {
770 m_nonzeroSingularValues = i;
771 break;
772 }
773 if (pos) {
774 pos += i;
775 std::swap(m_singularValues.coeffRef(i), m_singularValues.coeffRef(pos));
776 if (computeU()) m_matrixU.col(pos).swap(m_matrixU.col(i));
777 if (computeV()) m_matrixV.col(pos).swap(m_matrixV.col(i));
778 }
779 }
780
781 m_isInitialized = true;
782 return *this;
783}
784
792template <typename Derived>
793template <int Options>
797
798template <typename Derived>
799template <int Options>
801 unsigned int computationOptions) const {
802 return JacobiSVD<PlainObject, Options>(*this, computationOptions);
803}
804
805} // end namespace Eigen
806
807#endif // EIGEN_JACOBISVD_H
void swap(const DenseBase< OtherDerived > &other)
Definition DenseBase.h:390
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition JacobiSVD.h:500
JacobiSVD()
Default Constructor.
Definition JacobiSVD.h:531
EIGEN_DEPRECATED JacobiSVD(Index rows, Index cols, unsigned int computationOptions)
Default Constructor with memory preallocation.
Definition JacobiSVD.h:556
bool computeV() const
Definition SVDBase.h:275
JacobiSVD(const MatrixType &matrix, unsigned int computationOptions)
Constructor performing the decomposition of given matrix using specified options for computing unitar...
Definition JacobiSVD.h:581
bool computeU() const
Definition SVDBase.h:273
EIGEN_DEPRECATED JacobiSVD & compute(const MatrixType &matrix, unsigned int computationOptions)
Method performing the decomposition of given matrix, as specified by the computationOptions parameter...
Definition JacobiSVD.h:602
JacobiSVD(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition JacobiSVD.h:540
JacobiSVD(const MatrixType &matrix)
Constructor performing the decomposition of given matrix, using the custom options specified with the...
Definition JacobiSVD.h:566
JacobiSVD & compute(const MatrixType &matrix)
Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified us...
Definition JacobiSVD.h:591
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Derived & setIdentity()
Definition CwiseNullaryOp.h:839
void applyOnTheRight(const EigenBase< OtherDerived > &other)
Definition MatrixBase.h:521
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:198
constexpr void resize(Index rows, Index cols)
Definition PlainObjectBase.h:294
Base class of SVD algorithms.
Definition SVDBase.h:119
Index rank() const
Definition SVDBase.h:217
bool computeV() const
Definition SVDBase.h:275
bool computeU() const
Definition SVDBase.h:273
RealScalar threshold() const
Definition SVDBase.h:265
@ NoQRPreconditioner
Definition Constants.h:423
@ HouseholderQRPreconditioner
Definition Constants.h:425
@ ColPivHouseholderQRPreconditioner
Definition Constants.h:421
@ FullPivHouseholderQRPreconditioner
Definition Constants.h:427
@ InvalidInput
Definition Constants.h:447
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int Dynamic
Definition Constants.h:25