Eigen  5.0.1-dev+master
 
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 <jacob.benoit.1@gmail.com>
5// Copyright (C) 2013-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_JACOBISVD_H
13#define EIGEN_JACOBISVD_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22// forward declaration (needed by ICC)
23// the empty body is required by MSVC
24template <typename MatrixType, int Options, bool IsComplex = NumTraits<typename MatrixType::Scalar>::IsComplex>
25struct svd_precondition_2x2_block_to_be_real {};
26
27/*** QR preconditioners (R-SVD)
28 ***
29 *** Their role is to reduce the problem of computing the SVD to the case of a square matrix.
30 *** This approach, known as R-SVD, is an optimization for rectangular-enough matrices, and is a requirement for
31 *** JacobiSVD which by itself is only able to work on square matrices.
32 ***/
33
34enum { PreconditionIfMoreColsThanRows, PreconditionIfMoreRowsThanCols };
35
36// If option PreconditionSquareMatrix is set, square matrices take the PreconditionIfMoreRowsThanCols path: A = Q R P^*.
37constexpr bool svd_precondition_more_rows(int options, Index rows, Index cols) {
38 return rows > cols || (should_svd_precondition_square_matrix(options) && rows == cols);
39}
40
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)))> {};
51
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 {};
56
57template <typename MatrixType, int Options, int QRPreconditioner, int Case>
58class qr_preconditioner_impl<MatrixType, Options, QRPreconditioner, Case, false> {
59 public:
60 void allocate(const JacobiSVD<MatrixType, Options>&) {}
61 template <typename Xpr>
62 bool run(JacobiSVD<MatrixType, Options>&, const Xpr&) {
63 return false;
64 }
65};
66
67/*** preconditioner using FullPivHouseholderQR ***/
68
69template <typename MatrixType, int Options>
70class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
71 true> {
72 public:
73 using Scalar = typename MatrixType::Scalar;
74 using SVDType = JacobiSVD<MatrixType, Options>;
75
76 enum { WorkspaceSize = MatrixType::RowsAtCompileTime, MaxWorkspaceSize = MatrixType::MaxRowsAtCompileTime };
77
78 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
79
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());
84 }
85 if (svd.m_computeFullU) m_workspace.resize(svd.rows());
86 }
87 template <typename Xpr>
88 bool run(SVDType& svd, const Xpr& matrix) {
89 if (svd_precondition_more_rows(Options, matrix.rows(), matrix.cols())) {
90 m_qr.compute(matrix);
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();
94 return true;
95 }
96 return false;
97 }
98
99 private:
100 using QRType = FullPivHouseholderQR<MatrixType>;
101 QRType m_qr;
102 WorkspaceType m_workspace;
103};
104
105template <typename MatrixType, int Options>
106class qr_preconditioner_impl<MatrixType, Options, FullPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
107 true> {
108 public:
109 using Scalar = typename MatrixType::Scalar;
110 using SVDType = JacobiSVD<MatrixType, Options>;
111
112 enum {
113 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
114 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
115 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
116 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
117 MatrixOptions = traits<MatrixType>::Options
118 };
119
120 using TransposeTypeWithSameStorageOrder =
121 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
122 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
123
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());
128 }
129 if (svd.m_computeFullV) m_workspace.resize(svd.cols());
130 }
131 template <typename Xpr>
132 bool run(SVDType& svd, const Xpr& matrix) {
133 if (matrix.cols() > matrix.rows()) {
134 m_qr.compute(matrix.adjoint());
135 svd.m_workMatrix =
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();
139 return true;
140 } else
141 return false;
142 }
143
144 private:
145 using QRType = FullPivHouseholderQR<TransposeTypeWithSameStorageOrder>;
146 QRType m_qr;
147 typename plain_row_type<MatrixType>::type m_workspace;
148};
149
150/*** preconditioner using ColPivHouseholderQR ***/
151
152template <typename MatrixType, int Options>
153class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols,
154 true> {
155 public:
156 using Scalar = typename MatrixType::Scalar;
157 using SVDType = JacobiSVD<MatrixType, Options>;
158
159 enum {
160 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
161 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
162 };
163
164 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
165
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());
170 }
171 if (svd.m_computeFullU)
172 m_workspace.resize(svd.rows());
173 else if (svd.m_computeThinU)
174 m_workspace.resize(svd.cols());
175 }
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);
186 }
187 if (svd.computeV()) svd.m_matrixV = m_qr.colsPermutation();
188 return true;
189 }
190 return false;
191 }
192
193 private:
194 using QRType = ColPivHouseholderQR<MatrixType>;
195 QRType m_qr;
196 WorkspaceType m_workspace;
197};
198
199template <typename MatrixType, int Options>
200class qr_preconditioner_impl<MatrixType, Options, ColPivHouseholderQRPreconditioner, PreconditionIfMoreColsThanRows,
201 true> {
202 public:
203 using Scalar = typename MatrixType::Scalar;
204 using SVDType = JacobiSVD<MatrixType, Options>;
205
206 enum {
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
214 };
215
216 using WorkspaceType = Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1>;
217
218 using TransposeTypeWithSameStorageOrder =
219 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
220 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
221
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());
226 }
227 if (svd.m_computeFullV)
228 m_workspace.resize(svd.cols());
229 else if (svd.m_computeThinV)
230 m_workspace.resize(svd.rows());
231 }
232 template <typename Xpr>
233 bool run(SVDType& svd, const Xpr& matrix) {
234 if (matrix.cols() > matrix.rows()) {
235 m_qr.compute(matrix.adjoint());
236
237 svd.m_workMatrix =
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);
244 }
245 if (svd.computeU()) svd.m_matrixU = m_qr.colsPermutation();
246 return true;
247 } else
248 return false;
249 }
250
251 private:
252 using QRType = ColPivHouseholderQR<TransposeTypeWithSameStorageOrder>;
253 QRType m_qr;
254 WorkspaceType m_workspace;
255};
256
257/*** preconditioner using HouseholderQR ***/
258
259template <typename MatrixType, int Options>
260class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreRowsThanCols, true> {
261 public:
262 using Scalar = typename MatrixType::Scalar;
263 using SVDType = JacobiSVD<MatrixType, Options>;
264
265 enum {
266 WorkspaceSize = internal::traits<SVDType>::MatrixUColsAtCompileTime,
267 MaxWorkspaceSize = internal::traits<SVDType>::MatrixUMaxColsAtCompileTime
268 };
269
270 using WorkspaceType = Matrix<Scalar, 1, WorkspaceSize, RowMajor, 1, MaxWorkspaceSize>;
271
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());
276 }
277 if (svd.m_computeFullU)
278 m_workspace.resize(svd.rows());
279 else if (svd.m_computeThinU)
280 m_workspace.resize(svd.cols());
281 }
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);
292 }
293 if (svd.computeV()) svd.m_matrixV.setIdentity(matrix.cols(), matrix.cols());
294 return true;
295 }
296 return false;
297 }
298
299 private:
300 using QRType = HouseholderQR<MatrixType>;
301 QRType m_qr;
302 WorkspaceType m_workspace;
303};
304
305template <typename MatrixType, int Options>
306class qr_preconditioner_impl<MatrixType, Options, HouseholderQRPreconditioner, PreconditionIfMoreColsThanRows, true> {
307 public:
308 using Scalar = typename MatrixType::Scalar;
309 using SVDType = JacobiSVD<MatrixType, Options>;
310
311 enum {
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
319 };
320
321 using WorkspaceType = Matrix<Scalar, WorkspaceSize, 1, ColMajor, MaxWorkspaceSize, 1>;
322
323 using TransposeTypeWithSameStorageOrder =
324 typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, RowsAtCompileTime, MatrixOptions,
325 MaxColsAtCompileTime, MaxRowsAtCompileTime>::type;
326
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());
331 }
332 if (svd.m_computeFullV)
333 m_workspace.resize(svd.cols());
334 else if (svd.m_computeThinV)
335 m_workspace.resize(svd.rows());
336 }
337
338 template <typename Xpr>
339 bool run(SVDType& svd, const Xpr& matrix) {
340 if (matrix.cols() > matrix.rows()) {
341 m_qr.compute(matrix.adjoint());
342
343 svd.m_workMatrix =
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);
350 }
351 if (svd.computeU()) svd.m_matrixU.setIdentity(matrix.rows(), matrix.rows());
352 return true;
353 } else
354 return false;
355 }
356
357 private:
359 QRType m_qr;
360 WorkspaceType m_workspace;
361};
362
363/*** 2x2 SVD implementation
364 ***
365 *** JacobiSVD consists in performing a series of 2x2 SVD subproblems
366 ***/
367
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; }
373};
374
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) {
381 Scalar z;
382 JacobiRotation<Scalar> rot;
383 RealScalar n =
384 numext::sqrt<RealScalar>(numext::abs2(work_matrix.coeff(p, p)) + numext::abs2(work_matrix.coeff(q, p)));
385
386 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
387 const RealScalar precision = NumTraits<Scalar>::epsilon();
388
389 if (numext::is_exactly_zero(n)) {
390 // make sure first column is zero
391 work_matrix.coeffRef(p, p) = work_matrix.coeffRef(q, p) = Scalar(0);
392
393 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
394 // work_matrix.coeff(p,q) can be zero if work_matrix.coeff(q,p) is not zero but small enough to underflow when
395 // computing n
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);
399 }
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);
404 }
405 // otherwise the second row is already zero, so we have nothing to do.
406 } else {
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;
415 }
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);
420 }
421 }
422
423 // update largest diagonal entry
424 maxDiagEntry = numext::maxi<RealScalar>(
425 maxDiagEntry,
426 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
427 // and check whether the 2x2 block is already diagonal
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;
430 }
431};
432
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;
443
444 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
445 // Complex non-blocking sweep: condition each 2x2 block to be real before diagonalizing.
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) {
450 notFinished = true;
451 Scalar z;
452 bool doRealSvd = true;
453 RealScalar nn =
454 numext::sqrt<RealScalar>(numext::abs2(work_matrix.coeff(p, p)) + numext::abs2(work_matrix.coeff(q, p)));
455
456 if (numext::is_exactly_zero(nn)) {
457 // Make sure first column is zero.
458 work_matrix.coeffRef(p, p) = work_matrix.coeffRef(q, p) = Scalar(0);
459
460 if (numext::abs(numext::imag(work_matrix.coeff(p, q))) > considerAsZero) {
461 // work_matrix.coeff(p, q) can be zero if work_matrix.coeff(q, p) is not zero but small enough to
462 // underflow when computing nn.
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);
466 }
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);
471 }
472 } else {
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;
482 }
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);
487 }
488 }
489
490 maxDiagEntry = numext::maxi<RealScalar>(
491 maxDiagEntry,
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);
494 doRealSvd =
495 numext::abs(work_matrix.coeff(p, q)) > threshold || numext::abs(work_matrix.coeff(q, p)) > threshold;
496
497 if (doRealSvd) {
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>(
505 maxDiagEntry,
506 numext::maxi<RealScalar>(numext::abs(work_matrix.coeff(p, p)), numext::abs(work_matrix.coeff(q, q))));
507 }
508 }
509 }
510 }
511 } else {
512 // Real non-blocking sweep: diagonalize each 2x2 block directly.
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) {
517 notFinished = true;
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>(
525 maxDiagEntry,
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);
528 }
529 }
530 }
531 }
532
533 return notFinished;
534}
535
536template <typename MatrixType_, int Options>
537struct traits<JacobiSVD<MatrixType_, Options>> : svd_traits<MatrixType_, Options> {
538 using MatrixType = MatrixType_;
539};
540
541} // end namespace internal
542
615template <typename MatrixType_, int Options_>
616class JacobiSVD : public SVDBase<JacobiSVD<MatrixType_, Options_>> {
617 using Base = SVDBase<JacobiSVD>;
618
619 public:
620 using MatrixType = MatrixType_;
621 using Scalar = typename Base::Scalar;
622 using RealScalar = typename Base::RealScalar;
623 enum : int {
624 Options = Options_,
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
633 };
634
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>;
640
647
655 JacobiSVD(Index rows, Index cols) { allocate(rows, cols, internal::get_computation_options(Options)); }
656
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);
677 }
678
684 template <typename Derived>
685 explicit JacobiSVD(const MatrixBase<Derived>& matrix) {
686 compute_impl(matrix, internal::get_computation_options(Options));
687 }
688
689 template <typename Derived>
690 explicit JacobiSVD(const TriangularBase<Derived>& matrix) {
691 compute_impl(matrix, internal::get_computation_options(Options));
692 }
693
706 // EIGEN_DEPRECATED // TODO(cantonios): re-enable after fixing a few 3p libraries that error on deprecation warnings.
707 template <typename Derived>
708 JacobiSVD(const MatrixBase<Derived>& matrix, unsigned int computationOptions) {
709 internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
710 compute_impl(matrix, computationOptions);
711 }
712
718 template <typename Derived>
720 return compute_impl(matrix, m_computationOptions);
721 }
722
723 template <typename Derived>
724 JacobiSVD& compute(const TriangularBase<Derived>& matrix) {
725 return compute_impl(matrix, m_computationOptions);
726 }
727
737 template <typename Derived>
738 EIGEN_DEPRECATED_WITH_REASON("Options should be specified using the class template parameter.")
739 JacobiSVD& compute(const MatrixBase<Derived>& matrix, unsigned int computationOptions) {
740 internal::check_svd_options_assertions<MatrixType, Options>(m_computationOptions, matrix.rows(), matrix.cols());
741 return compute_impl(matrix, computationOptions);
742 }
743
744 using Base::cols;
745 using Base::computeU;
746 using Base::computeV;
747 using Base::diagSize;
748 using Base::rank;
749 using Base::rows;
750
751 void allocate(Index rows_, Index cols_, unsigned int computationOptions) {
752 if (Base::allocate(rows_, cols_, computationOptions)) return;
753 eigen_assert(
754 !((m_computeThinU || m_computeThinV) && int(QRPreconditioner) == int(FullPivHouseholderQRPreconditioner)) &&
755 "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
756 "Use the ColPivHouseholderQR preconditioner instead.");
757
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);
761 }
762
763 private:
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);
768
769 // Blocked sweep for the Jacobi SVD (works for both real and complex scalars).
770 // Extracted into a separate EIGEN_DONT_INLINE method to prevent the blocking
771 // code from interfering with the compiler's optimization of the non-blocking
772 // scalar sweep.
773 EIGEN_DONT_INLINE bool blocked_sweep(RealScalar considerAsZero, RealScalar precision, RealScalar& maxDiagEntry);
774
775 protected:
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;
781 using Base::m_info;
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;
792
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.")
800
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;
805
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;
811
812 // Blocking parameters for the Jacobi SVD sweep.
813#ifdef EIGEN_JACOBI_SVD_BLOCK_SIZE
814 static constexpr Index kDefaultBlockSize = EIGEN_JACOBI_SVD_BLOCK_SIZE;
815#else
816 static constexpr Index kDefaultBlockSize = 32;
817#endif
818
819 // Use the lower of the default block size and static maximum matrix dimensions.
820 static constexpr Index kBlockSize = internal::min_size_prefer_fixed(kDefaultBlockSize, MaxDiagSizeAtCompileTime);
821};
822
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);
828}
829
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.);
837
838 allocate(matrix.rows(), matrix.cols(), computationOptions);
839
840 // currently we stop when we reach precision 2*epsilon as the last bit of precision can require an unreasonable number
841 // of iterations, only worsening the precision of U and V as we accumulate more rotations
842 const RealScalar precision = RealScalar(2) * NumTraits<Scalar>::epsilon();
843
844 // limit for denormal numbers to be considered zero in order to avoid infinite loops (see bug 286)
845 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
846
847 // Scaling factor to reduce over/under-flows. A SIMD unit that flushes subnormal inputs reads an all-subnormal matrix
848 // as zero; recover its maximum from the representation so that the scaling still brings it into the normal range.
849 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(matrix.derived());
850 if (!(numext::isfinite)(maxCoeff)) {
851 m_isInitialized = true;
852 m_info = InvalidInput;
853 m_nonzeroSingularValues = 0;
854 m_singularValues.setZero();
855 return *this;
856 }
857 internal::safe_scaling_factors<RealScalar> factors;
858
859 /*** step 1. The R-SVD step: we use a QR decomposition to reduce to the case of a square matrix */
860
861 if (rows() != cols() || internal::should_svd_precondition_square_matrix(Options)) {
862 factors =
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);
866 });
867 } else {
868 factors = internal::safe_scaling<RealScalar>::scale_to(
869 m_workMatrix,
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());
875 }
876
877 /*** step 2. The main Jacobi SVD iteration. ***/
878 RealScalar maxDiagEntry = diagSize() == 0 ? RealScalar(0) : m_workMatrix.cwiseAbs().diagonal().maxCoeff();
879
880 bool finished = false;
881 while (!finished) {
882 finished = true;
883
884 EIGEN_IF_CONSTEXPR (MaxDiagSizeAtCompileTime == Dynamic || MaxDiagSizeAtCompileTime > kBlockSize) {
885 // Sweep with optional blocking for large matrices.
886 // Use blocking when the matrix is large enough that individual left rotations
887 // (strided row operations on column-major data) cause significant cache misses.
888 // The threshold is derived from the L2 cache size: blocking becomes worthwhile
889 // when n exceeds sqrt(L2 / 4). We divide by sizeof(float) rather than sizeof(RealScalar)
890 // because the cache miss pattern depends on the number of columns accessed (one cache
891 // line per column), not the scalar size. This also makes the threshold appropriately
892 // more conservative for larger types where GEMM overhead is higher.
893 const Index n = diagSize();
894#ifdef EIGEN_JACOBI_SVD_BLOCKING_THRESHOLD
895 const Index blockingThreshold = EIGEN_JACOBI_SVD_BLOCKING_THRESHOLD;
896#else
897 const Index blockingThreshold =
898 static_cast<Index>(numext::sqrt(static_cast<double>(l2CacheSize() / sizeof(float))));
899#endif
900
901 if (n >= blockingThreshold) {
902 // The blocked sweep is in a separate EIGEN_DONT_INLINE method to prevent
903 // the blocking code from interfering with the compiler's optimization of
904 // the non-blocking scalar sweep below.
905 finished = !blocked_sweep(considerAsZero, precision, maxDiagEntry);
906 continue;
907 }
908 }
909 finished = !internal::jacobi_svd_nonblocking_sweep(m_workMatrix, m_matrixU, m_matrixV, computeU(), computeV(),
910 considerAsZero, precision, maxDiagEntry);
911 }
912
913 /*** step 3. The work matrix is now diagonal, so ensure it's positive so its diagonal entries are the singular values
914 * ***/
915
916 for (Index i = 0; i < diagSize(); ++i) {
917 // For a complex matrix, some diagonal coefficients might not have been
918 // treated by svd_precondition_2x2_block_to_be_real, and the imaginary part
919 // of some diagonal entry might not be null.
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;
923 }
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;
928 } else {
929 // m_workMatrix.coeff(i,i) is already real. Its magnitude and sign are read from the representation for float
930 // and double: a subnormal entry compares as zero under DAZ, and an abs() that widens flushes it under FTZ.
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);
934 }
935 }
936
937 /*** step 4. Sort singular values in descending order and compute the number of nonzero singular values ***/
938
939 // Sort in the scaled frame, where the largest values are normal. A tail whose maximum reads zero or subnormal
940 // holds zeros and subnormals, which FTZ/DAZ hardware compares as zero: order it from the representation.
941 for (Index i = 0; i < diagSize(); i++) {
942 Index pos;
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),
946 pos);
947 if (numext::is_exactly_zero_no_flush(m_singularValues.coeff(i + pos))) break;
948 }
949 if (pos) {
950 pos += i;
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));
954 }
955 }
956 // Unscaling with maxCoeff keeps singular values that land in the subnormal range under FTZ; the count then reads
957 // them from the representation.
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;
963 break;
964 }
965 }
966
967 m_isInitialized = true;
968 return *this;
969}
970
971// Blocked Jacobi SVD sweep for both real and complex scalar types. For large n,
972// applying left rotations (row operations on column-major data) causes cache
973// misses due to strided access. To mitigate this, we accumulate kBlockSize left
974// rotations into a small dense matrix and apply them via a single GEMM to the
975// contiguous row block q..q+kBlockSize-1 and the (possibly distant) row p.
976// Right rotations and column scalings act on columns (contiguous in column-major)
977// and are applied individually.
978//
979// For complex types, the 2x2 preconditioning (making the block real) involves
980// complex left rotations and row scalings, which are also accumulated into the
981// block matrix. Column scalings from preconditioning are applied directly.
982//
983// The accumulated rotation matrix has lower-triangular structure in its top-left
984// kBlockSize x kBlockSize corner, which we exploit with triangularView.
985//
986// Returns true if any off-diagonal element exceeded the threshold (i.e. sweep
987// is not yet converged).
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);
998
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,
1001 kBlockSize + 1);
1002 Matrix<Scalar, 1, Dynamic, RowMajor, 1, MaxDiagSizeAtCompileTime> Mp_save;
1003
1004 for (Index p = 1; p < n; ++p) {
1005 Index q = 0;
1006
1007 // Blocked loop: process kBlockSize pairs (p,q+qq) for qq=0..kBlockSize-1.
1008 // We extract the relevant (kBlockSize+1) x (kBlockSize+1) submatrix of W
1009 // into a small buffer, compute all rotations on the buffer, accumulate the
1010 // left transformations into `accum`, and apply them in one GEMM at the end.
1011 for (; q + kBlockSize <= p; q += kBlockSize) {
1012 // Buffer = [ W(q:q+k, q:q+k) W(q:q+k, p) ]
1013 // [ W(p, q:q+k) W(p, p) ]
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);
1019
1020 // Accumulator for left transformations: W <- accum * W.
1021 // After processing qq pairs, accum's top-left kBlockSize x kBlockSize
1022 // block is lower-triangular (each rotation only mixes row qq with row
1023 // kBlockSize, so rows 0..qq-1 are unchanged).
1024 accum.setIdentity(kBlockSize + 1, kBlockSize + 1);
1025 bool blockDirty = false;
1026
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) {
1030 notFinished = true;
1031 blockDirty = true;
1032
1033 // Complex preconditioning: transform the 2x2 block
1034 // [w_pp w_pq] = [buffer(kBlockSize, kBlockSize) buffer(kBlockSize, qq)]
1035 // [w_qp w_qq] [buffer(qq, kBlockSize) buffer(qq, qq) ]
1036 // to have real entries via unitary row/column operations, so
1037 // real_2x2_jacobi_svd can be applied.
1038 //
1039 // Left operations (complex rotation, row scaling by e^{i*theta}) are
1040 // accumulated into `accum` for deferred GEMM application.
1041 // Right operations (column scaling) are applied directly since column
1042 // ops are contiguous in column-major layout.
1043 bool doRealSvd = true;
1044 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
1045 Scalar z;
1046 // nn = ||(w_pp, w_qp)||_2, the norm of the first column of the 2x2 block.
1047 RealScalar nn = numext::sqrt<RealScalar>(numext::abs2(blockBuffer.coeff(kBlockSize, kBlockSize)) +
1048 numext::abs2(blockBuffer.coeff(qq, kBlockSize)));
1049
1050 if (numext::is_exactly_zero(nn)) {
1051 // First column is zero => block is already upper triangular.
1052 blockBuffer.coeffRef(kBlockSize, kBlockSize) = Scalar(0);
1053 blockBuffer.coeffRef(qq, kBlockSize) = Scalar(0);
1054
1055 // Scale rows by z = e^{-i*arg(w)} to make remaining entries real.
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; // accumulate left op
1060 if (computeU()) m_matrixU.col(p) *= numext::conj(z);
1061 }
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;
1065 accum.row(qq) *= z; // accumulate left op
1066 if (computeU()) m_matrixU.col(q + qq) *= numext::conj(z);
1067 }
1068 } else {
1069 // Apply complex Givens rotation to zero out w_qp:
1070 // [c s] [w_pp] [nn] conj(w_pp) w_qp
1071 // [-s c] [w_qp] = [0 ] c = ----------, s = ------
1072 // nn nn
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); // accumulate left op
1078 if (computeU()) m_matrixU.applyOnTheRight(p, q + qq, rot.adjoint());
1079
1080 // Scale column qq by z = e^{-i*arg(w_pq)} to make w_pq real.
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; // right op: apply directly
1085 if (computeV()) m_matrixV.col(q + qq) *= z;
1086 }
1087 // Scale row qq by z = e^{-i*arg(w_qq)} to make w_qq real.
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;
1091 accum.row(qq) *= z; // accumulate left op
1092 if (computeU()) m_matrixU.col(q + qq) *= numext::conj(z);
1093 }
1094 }
1095 // Update maxDiagEntry from preconditioning.
1096 maxDiagEntry = numext::maxi<RealScalar>(
1097 maxDiagEntry, numext::maxi<RealScalar>(numext::abs(blockBuffer.coeff(kBlockSize, kBlockSize)),
1098 numext::abs(blockBuffer.coeff(qq, qq))));
1099 // Check if 2x2 block still needs diagonalizing.
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;
1103 }
1104
1105 if (doRealSvd) {
1106 // Compute real 2x2 SVD: buffer_2x2 = j_left * diag * j_right^T.
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);
1111
1112 // Accumulate left rotation for deferred GEMM.
1113 accum.applyOnTheLeft(kBlockSize, qq, j_left);
1114
1115 // Right rotation is a column op (contiguous): apply directly.
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);
1119
1120 maxDiagEntry = numext::maxi<RealScalar>(
1121 maxDiagEntry, numext::maxi<RealScalar>(numext::abs(blockBuffer.coeff(kBlockSize, kBlockSize)),
1122 numext::abs(blockBuffer.coeff(qq, qq))));
1123 }
1124 threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
1125 }
1126 }
1127
1128 // Apply accumulated left rotations: W <- accum * W, via GEMM.
1129 // When p == q + kBlockSize, all kBlockSize+1 rows are contiguous.
1130 // Otherwise, rows q..q+k-1 and row p are non-adjacent; we split:
1131 // [Mq] [L11 l12] [Mq]
1132 // [Mp] <- [l21 l22] [Mp]
1133 // L11 is lower-triangular (exploited via triangularView).
1134 if (blockDirty) {
1135 if (p == q + kBlockSize) {
1136 m_workMatrix.template middleRows<kBlockSize + 1>(q) =
1137 accum * m_workMatrix.template middleRows<kBlockSize + 1>(q);
1138 } else {
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);
1145 Mp_save = Mp;
1146 Mp.noalias() = l21 * Mq + l22 * Mp_save;
1147 Mq = L11.template triangularView<Lower>() * Mq + l12 * Mp_save;
1148 }
1149 }
1150 }
1151
1152 // Scalar loop for remaining pairs after blocked processing.
1153 for (; q < p; ++q) {
1154 if (numext::abs(m_workMatrix.coeff(p, q)) > threshold || numext::abs(m_workMatrix.coeff(q, p)) > threshold) {
1155 notFinished = true;
1156
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,
1160 q, maxDiagEntry);
1161 }
1162
1163 if (doRealSvd) {
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>(
1171 maxDiagEntry,
1172 numext::maxi<RealScalar>(numext::abs(m_workMatrix.coeff(p, p)), numext::abs(m_workMatrix.coeff(q, q))));
1173 }
1174 threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
1175 }
1176 }
1177 }
1178
1179 return notFinished;
1180}
1181
1189template <typename Derived>
1190template <int Options>
1191JacobiSVD<typename MatrixBase<Derived>::PlainObject, Options> MatrixBase<Derived>::jacobiSvd() const {
1192 return JacobiSVD<PlainObject, Options>(*this);
1193}
1194
1195template <typename Derived>
1196template <int Options>
1197JacobiSVD<typename MatrixBase<Derived>::PlainObject, Options> MatrixBase<Derived>::jacobiSvd(
1198 unsigned int computationOptions) const {
1199 return JacobiSVD<PlainObject, Options>(*this, computationOptions);
1200}
1201
1202} // end namespace Eigen
1203
1204#endif // EIGEN_JACOBISVD_H
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
Base class for triangular part in a matrix.
Definition TriangularMatrix.h:68
@ FullPivHouseholderQRPreconditioner
Definition Constants.h:432
@ InvalidInput
Definition Constants.h:464