Medial Code Documentation
Loading...
Searching...
No Matches
BDCSVD.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// We used the "A Divide-And-Conquer Algorithm for the Bidiagonal SVD"
5// research report written by Ming Gu and Stanley C.Eisenstat
6// The code variable names correspond to the names they used in their
7// report
8//
9// Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
10// Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
11// Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
12// Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
13// Copyright (C) 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
14// Copyright (C) 2014-2017 Gael Guennebaud <gael.guennebaud@inria.fr>
15//
16// Source Code Form is subject to the terms of the Mozilla
17// Public License v. 2.0. If a copy of the MPL was not distributed
18// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
19
20#ifndef EIGEN_BDCSVD_H
21#define EIGEN_BDCSVD_H
22// #define EIGEN_BDCSVD_DEBUG_VERBOSE
23// #define EIGEN_BDCSVD_SANITY_CHECKS
24
25#ifdef EIGEN_BDCSVD_SANITY_CHECKS
26#undef eigen_internal_assert
27#define eigen_internal_assert(X) assert(X);
28#endif
29
30#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
31#include <iostream>
32#endif
33
34namespace Eigen {
35
36#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
37IOFormat bdcsvdfmt(8, 0, ", ", "\n", " [", "]");
38#endif
39
40template<typename _MatrixType> class BDCSVD;
41
42namespace internal {
43
44template<typename _MatrixType>
45struct traits<BDCSVD<_MatrixType> >
46 : traits<_MatrixType>
47{
48 typedef _MatrixType MatrixType;
49};
50
51} // end namespace internal
52
53
76template<typename _MatrixType>
77class BDCSVD : public SVDBase<BDCSVD<_MatrixType> >
78{
79 typedef SVDBase<BDCSVD> Base;
80
81public:
82 using Base::rows;
83 using Base::cols;
84 using Base::computeU;
85 using Base::computeV;
86
87 typedef _MatrixType MatrixType;
88 typedef typename MatrixType::Scalar Scalar;
90 typedef typename NumTraits<RealScalar>::Literal Literal;
91 enum {
92 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
93 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
94 DiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime, ColsAtCompileTime),
95 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
96 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
97 MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(MaxRowsAtCompileTime, MaxColsAtCompileTime),
98 MatrixOptions = MatrixType::Options
99 };
100
101 typedef typename Base::MatrixUType MatrixUType;
102 typedef typename Base::MatrixVType MatrixVType;
104
110 typedef Ref<ArrayXr> ArrayRef;
111 typedef Ref<ArrayXi> IndicesRef;
112
118 BDCSVD() : m_algoswap(16), m_isTranspose(false), m_compU(false), m_compV(false), m_numIters(0)
119 {}
120
121
128 BDCSVD(Index rows, Index cols, unsigned int computationOptions = 0)
129 : m_algoswap(16), m_numIters(0)
130 {
131 allocate(rows, cols, computationOptions);
132 }
133
144 BDCSVD(const MatrixType& matrix, unsigned int computationOptions = 0)
145 : m_algoswap(16), m_numIters(0)
146 {
148 }
149
150 ~BDCSVD()
151 {
152 }
153
164 BDCSVD& compute(const MatrixType& matrix, unsigned int computationOptions);
165
172 BDCSVD& compute(const MatrixType& matrix)
173 {
174 return compute(matrix, this->m_computationOptions);
175 }
176
177 void setSwitchSize(int s)
178 {
179 eigen_assert(s>=3 && "BDCSVD the size of the algo switch has to be at least 3.");
180 m_algoswap = s;
181 }
182
183private:
184 void allocate(Index rows, Index cols, unsigned int computationOptions);
185 void divide(Index firstCol, Index lastCol, Index firstRowW, Index firstColW, Index shift);
186 void computeSVDofM(Index firstCol, Index n, MatrixXr& U, VectorType& singVals, MatrixXr& V);
187 void computeSingVals(const ArrayRef& col0, const ArrayRef& diag, const IndicesRef& perm, VectorType& singVals, ArrayRef shifts, ArrayRef mus);
188 void perturbCol0(const ArrayRef& col0, const ArrayRef& diag, const IndicesRef& perm, const VectorType& singVals, const ArrayRef& shifts, const ArrayRef& mus, ArrayRef zhat);
189 void computeSingVecs(const ArrayRef& zhat, const ArrayRef& diag, const IndicesRef& perm, const VectorType& singVals, const ArrayRef& shifts, const ArrayRef& mus, MatrixXr& U, MatrixXr& V);
190 void deflation43(Index firstCol, Index shift, Index i, Index size);
191 void deflation44(Index firstColu , Index firstColm, Index firstRowW, Index firstColW, Index i, Index j, Index size);
192 void deflation(Index firstCol, Index lastCol, Index k, Index firstRowW, Index firstColW, Index shift);
193 template<typename HouseholderU, typename HouseholderV, typename NaiveU, typename NaiveV>
194 void copyUV(const HouseholderU &householderU, const HouseholderV &householderV, const NaiveU &naiveU, const NaiveV &naivev);
195 void structured_update(Block<MatrixXr,Dynamic,Dynamic> A, const MatrixXr &B, Index n1);
196 static RealScalar secularEq(RealScalar x, const ArrayRef& col0, const ArrayRef& diag, const IndicesRef &perm, const ArrayRef& diagShifted, RealScalar shift);
197
198protected:
199 MatrixXr m_naiveU, m_naiveV;
200 MatrixXr m_computed;
201 Index m_nRec;
202 ArrayXr m_workspace;
203 ArrayXi m_workspaceI;
204 int m_algoswap;
205 bool m_isTranspose, m_compU, m_compV;
206
207 using Base::m_singularValues;
208 using Base::m_diagSize;
209 using Base::m_computeFullU;
210 using Base::m_computeFullV;
211 using Base::m_computeThinU;
212 using Base::m_computeThinV;
213 using Base::m_matrixU;
214 using Base::m_matrixV;
215 using Base::m_info;
216 using Base::m_isInitialized;
217 using Base::m_nonzeroSingularValues;
218
219public:
220 int m_numIters;
221}; //end class BDCSVD
222
223
224// Method to allocate and initialize matrix and attributes
225template<typename MatrixType>
226void BDCSVD<MatrixType>::allocate(Eigen::Index rows, Eigen::Index cols, unsigned int computationOptions)
227{
228 m_isTranspose = (cols > rows);
229
230 if (Base::allocate(rows, cols, computationOptions))
231 return;
232
233 m_computed = MatrixXr::Zero(m_diagSize + 1, m_diagSize );
234 m_compU = computeV();
235 m_compV = computeU();
236 if (m_isTranspose)
237 std::swap(m_compU, m_compV);
238
239 if (m_compU) m_naiveU = MatrixXr::Zero(m_diagSize + 1, m_diagSize + 1 );
240 else m_naiveU = MatrixXr::Zero(2, m_diagSize + 1 );
241
242 if (m_compV) m_naiveV = MatrixXr::Zero(m_diagSize, m_diagSize);
243
244 m_workspace.resize((m_diagSize+1)*(m_diagSize+1)*3);
245 m_workspaceI.resize(3*m_diagSize);
246}// end allocate
247
248template<typename MatrixType>
250{
251#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
252 std::cout << "\n\n\n======================================================================================================================\n\n\n";
253#endif
254 allocate(matrix.rows(), matrix.cols(), computationOptions);
255 using std::abs;
256
257 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
258
259 //**** step -1 - If the problem is too small, directly falls back to JacobiSVD and return
260 if(matrix.cols() < m_algoswap)
261 {
262 // FIXME this line involves temporaries
264 m_isInitialized = true;
265 m_info = jsvd.info();
266 if (m_info == Success || m_info == NoConvergence) {
267 if(computeU()) m_matrixU = jsvd.matrixU();
268 if(computeV()) m_matrixV = jsvd.matrixV();
269 m_singularValues = jsvd.singularValues();
270 m_nonzeroSingularValues = jsvd.nonzeroSingularValues();
271 }
272 return *this;
273 }
274
275 //**** step 0 - Copy the input matrix and apply scaling to reduce over/under-flows
276 RealScalar scale = matrix.cwiseAbs().template maxCoeff<PropagateNaN>();
277 if (!(numext::isfinite)(scale)) {
278 m_isInitialized = true;
279 m_info = InvalidInput;
280 return *this;
281 }
282
283 if(scale==Literal(0)) scale = Literal(1);
284 MatrixX copy;
285 if (m_isTranspose) copy = matrix.adjoint()/scale;
286 else copy = matrix/scale;
287
288 //**** step 1 - Bidiagonalization
289 // FIXME this line involves temporaries
291
292 //**** step 2 - Divide & Conquer
293 m_naiveU.setZero();
294 m_naiveV.setZero();
295 // FIXME this line involves a temporary matrix
296 m_computed.topRows(m_diagSize) = bid.bidiagonal().toDenseMatrix().transpose();
297 m_computed.template bottomRows<1>().setZero();
298 divide(0, m_diagSize - 1, 0, 0, 0);
299 if (m_info != Success && m_info != NoConvergence) {
300 m_isInitialized = true;
301 return *this;
302 }
303
304 //**** step 3 - Copy singular values and vectors
305 for (int i=0; i<m_diagSize; i++)
306 {
307 RealScalar a = abs(m_computed.coeff(i, i));
308 m_singularValues.coeffRef(i) = a * scale;
309 if (a<considerZero)
310 {
311 m_nonzeroSingularValues = i;
312 m_singularValues.tail(m_diagSize - i - 1).setZero();
313 break;
314 }
315 else if (i == m_diagSize - 1)
316 {
317 m_nonzeroSingularValues = i + 1;
318 break;
319 }
320 }
321
322#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
323// std::cout << "m_naiveU\n" << m_naiveU << "\n\n";
324// std::cout << "m_naiveV\n" << m_naiveV << "\n\n";
325#endif
326 if(m_isTranspose) copyUV(bid.householderV(), bid.householderU(), m_naiveV, m_naiveU);
327 else copyUV(bid.householderU(), bid.householderV(), m_naiveU, m_naiveV);
328
329 m_isInitialized = true;
330 return *this;
331}// end compute
332
333
334template<typename MatrixType>
335template<typename HouseholderU, typename HouseholderV, typename NaiveU, typename NaiveV>
336void BDCSVD<MatrixType>::copyUV(const HouseholderU &householderU, const HouseholderV &householderV, const NaiveU &naiveU, const NaiveV &naiveV)
337{
338 // Note exchange of U and V: m_matrixU is set from m_naiveV and vice versa
339 if (computeU())
340 {
341 Index Ucols = m_computeThinU ? m_diagSize : householderU.cols();
342 m_matrixU = MatrixX::Identity(householderU.cols(), Ucols);
343 m_matrixU.topLeftCorner(m_diagSize, m_diagSize) = naiveV.template cast<Scalar>().topLeftCorner(m_diagSize, m_diagSize);
344 householderU.applyThisOnTheLeft(m_matrixU); // FIXME this line involves a temporary buffer
345 }
346 if (computeV())
347 {
348 Index Vcols = m_computeThinV ? m_diagSize : householderV.cols();
349 m_matrixV = MatrixX::Identity(householderV.cols(), Vcols);
350 m_matrixV.topLeftCorner(m_diagSize, m_diagSize) = naiveU.template cast<Scalar>().topLeftCorner(m_diagSize, m_diagSize);
351 householderV.applyThisOnTheLeft(m_matrixV); // FIXME this line involves a temporary buffer
352 }
353}
354
363template<typename MatrixType>
364void BDCSVD<MatrixType>::structured_update(Block<MatrixXr,Dynamic,Dynamic> A, const MatrixXr &B, Index n1)
365{
366 Index n = A.rows();
367 if(n>100)
368 {
369 // If the matrices are large enough, let's exploit the sparse structure of A by
370 // splitting it in half (wrt n1), and packing the non-zero columns.
371 Index n2 = n - n1;
372 Map<MatrixXr> A1(m_workspace.data() , n1, n);
373 Map<MatrixXr> A2(m_workspace.data()+ n1*n, n2, n);
374 Map<MatrixXr> B1(m_workspace.data()+ n*n, n, n);
375 Map<MatrixXr> B2(m_workspace.data()+2*n*n, n, n);
376 Index k1=0, k2=0;
377 for(Index j=0; j<n; ++j)
378 {
379 if( (A.col(j).head(n1).array()!=Literal(0)).any() )
380 {
381 A1.col(k1) = A.col(j).head(n1);
382 B1.row(k1) = B.row(j);
383 ++k1;
384 }
385 if( (A.col(j).tail(n2).array()!=Literal(0)).any() )
386 {
387 A2.col(k2) = A.col(j).tail(n2);
388 B2.row(k2) = B.row(j);
389 ++k2;
390 }
391 }
392
393 A.topRows(n1).noalias() = A1.leftCols(k1) * B1.topRows(k1);
394 A.bottomRows(n2).noalias() = A2.leftCols(k2) * B2.topRows(k2);
395 }
396 else
397 {
398 Map<MatrixXr,Aligned> tmp(m_workspace.data(),n,n);
399 tmp.noalias() = A*B;
400 A = tmp;
401 }
402}
403
404// The divide algorithm is done "in place", we are always working on subsets of the same matrix. The divide methods takes as argument the
405// place of the submatrix we are currently working on.
406
407//@param firstCol : The Index of the first column of the submatrix of m_computed and for m_naiveU;
408//@param lastCol : The Index of the last column of the submatrix of m_computed and for m_naiveU;
409// lastCol + 1 - firstCol is the size of the submatrix.
410//@param firstRowW : The Index of the first row of the matrix W that we are to change. (see the reference paper section 1 for more information on W)
411//@param firstColW : Same as firstRowW with the column.
412//@param shift : Each time one takes the left submatrix, one must add 1 to the shift. Why? Because! We actually want the last column of the U submatrix
413// to become the first column (*coeff) and to shift all the other columns to the right. There are more details on the reference paper.
414template<typename MatrixType>
415void BDCSVD<MatrixType>::divide(Eigen::Index firstCol, Eigen::Index lastCol, Eigen::Index firstRowW, Eigen::Index firstColW, Eigen::Index shift)
416{
417 // requires rows = cols + 1;
418 using std::pow;
419 using std::sqrt;
420 using std::abs;
421 const Index n = lastCol - firstCol + 1;
422 const Index k = n/2;
423 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
424 RealScalar alphaK;
425 RealScalar betaK;
426 RealScalar r0;
427 RealScalar lambda, phi, c0, s0;
428 VectorType l, f;
429 // We use the other algorithm which is more efficient for small
430 // matrices.
431 if (n < m_algoswap)
432 {
433 // FIXME this line involves temporaries
434 JacobiSVD<MatrixXr> b(m_computed.block(firstCol, firstCol, n + 1, n), ComputeFullU | (m_compV ? ComputeFullV : 0));
435 m_info = b.info();
436 if (m_info != Success && m_info != NoConvergence) return;
437 if (m_compU)
438 m_naiveU.block(firstCol, firstCol, n + 1, n + 1).real() = b.matrixU();
439 else
440 {
441 m_naiveU.row(0).segment(firstCol, n + 1).real() = b.matrixU().row(0);
442 m_naiveU.row(1).segment(firstCol, n + 1).real() = b.matrixU().row(n);
443 }
444 if (m_compV) m_naiveV.block(firstRowW, firstColW, n, n).real() = b.matrixV();
445 m_computed.block(firstCol + shift, firstCol + shift, n + 1, n).setZero();
446 m_computed.diagonal().segment(firstCol + shift, n) = b.singularValues().head(n);
447 return;
448 }
449 // We use the divide and conquer algorithm
450 alphaK = m_computed(firstCol + k, firstCol + k);
451 betaK = m_computed(firstCol + k + 1, firstCol + k);
452 // The divide must be done in that order in order to have good results. Divide change the data inside the submatrices
453 // and the divide of the right submatrice reads one column of the left submatrice. That's why we need to treat the
454 // right submatrix before the left one.
455 divide(k + 1 + firstCol, lastCol, k + 1 + firstRowW, k + 1 + firstColW, shift);
456 if (m_info != Success && m_info != NoConvergence) return;
457 divide(firstCol, k - 1 + firstCol, firstRowW, firstColW + 1, shift + 1);
458 if (m_info != Success && m_info != NoConvergence) return;
459
460 if (m_compU)
461 {
462 lambda = m_naiveU(firstCol + k, firstCol + k);
463 phi = m_naiveU(firstCol + k + 1, lastCol + 1);
464 }
465 else
466 {
467 lambda = m_naiveU(1, firstCol + k);
468 phi = m_naiveU(0, lastCol + 1);
469 }
470 r0 = sqrt((abs(alphaK * lambda) * abs(alphaK * lambda)) + abs(betaK * phi) * abs(betaK * phi));
471 if (m_compU)
472 {
473 l = m_naiveU.row(firstCol + k).segment(firstCol, k);
474 f = m_naiveU.row(firstCol + k + 1).segment(firstCol + k + 1, n - k - 1);
475 }
476 else
477 {
478 l = m_naiveU.row(1).segment(firstCol, k);
479 f = m_naiveU.row(0).segment(firstCol + k + 1, n - k - 1);
480 }
481 if (m_compV) m_naiveV(firstRowW+k, firstColW) = Literal(1);
482 if (r0<considerZero)
483 {
484 c0 = Literal(1);
485 s0 = Literal(0);
486 }
487 else
488 {
489 c0 = alphaK * lambda / r0;
490 s0 = betaK * phi / r0;
491 }
492
493#ifdef EIGEN_BDCSVD_SANITY_CHECKS
494 assert(m_naiveU.allFinite());
495 assert(m_naiveV.allFinite());
496 assert(m_computed.allFinite());
497#endif
498
499 if (m_compU)
500 {
501 MatrixXr q1 (m_naiveU.col(firstCol + k).segment(firstCol, k + 1));
502 // we shiftW Q1 to the right
503 for (Index i = firstCol + k - 1; i >= firstCol; i--)
504 m_naiveU.col(i + 1).segment(firstCol, k + 1) = m_naiveU.col(i).segment(firstCol, k + 1);
505 // we shift q1 at the left with a factor c0
506 m_naiveU.col(firstCol).segment( firstCol, k + 1) = (q1 * c0);
507 // last column = q1 * - s0
508 m_naiveU.col(lastCol + 1).segment(firstCol, k + 1) = (q1 * ( - s0));
509 // first column = q2 * s0
510 m_naiveU.col(firstCol).segment(firstCol + k + 1, n - k) = m_naiveU.col(lastCol + 1).segment(firstCol + k + 1, n - k) * s0;
511 // q2 *= c0
512 m_naiveU.col(lastCol + 1).segment(firstCol + k + 1, n - k) *= c0;
513 }
514 else
515 {
516 RealScalar q1 = m_naiveU(0, firstCol + k);
517 // we shift Q1 to the right
518 for (Index i = firstCol + k - 1; i >= firstCol; i--)
519 m_naiveU(0, i + 1) = m_naiveU(0, i);
520 // we shift q1 at the left with a factor c0
521 m_naiveU(0, firstCol) = (q1 * c0);
522 // last column = q1 * - s0
523 m_naiveU(0, lastCol + 1) = (q1 * ( - s0));
524 // first column = q2 * s0
525 m_naiveU(1, firstCol) = m_naiveU(1, lastCol + 1) *s0;
526 // q2 *= c0
527 m_naiveU(1, lastCol + 1) *= c0;
528 m_naiveU.row(1).segment(firstCol + 1, k).setZero();
529 m_naiveU.row(0).segment(firstCol + k + 1, n - k - 1).setZero();
530 }
531
532#ifdef EIGEN_BDCSVD_SANITY_CHECKS
533 assert(m_naiveU.allFinite());
534 assert(m_naiveV.allFinite());
535 assert(m_computed.allFinite());
536#endif
537
538 m_computed(firstCol + shift, firstCol + shift) = r0;
539 m_computed.col(firstCol + shift).segment(firstCol + shift + 1, k) = alphaK * l.transpose().real();
540 m_computed.col(firstCol + shift).segment(firstCol + shift + k + 1, n - k - 1) = betaK * f.transpose().real();
541
542#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
543 ArrayXr tmp1 = (m_computed.block(firstCol+shift, firstCol+shift, n, n)).jacobiSvd().singularValues();
544#endif
545 // Second part: try to deflate singular values in combined matrix
546 deflation(firstCol, lastCol, k, firstRowW, firstColW, shift);
547#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
548 ArrayXr tmp2 = (m_computed.block(firstCol+shift, firstCol+shift, n, n)).jacobiSvd().singularValues();
549 std::cout << "\n\nj1 = " << tmp1.transpose().format(bdcsvdfmt) << "\n";
550 std::cout << "j2 = " << tmp2.transpose().format(bdcsvdfmt) << "\n\n";
551 std::cout << "err: " << ((tmp1-tmp2).abs()>1e-12*tmp2.abs()).transpose() << "\n";
552 static int count = 0;
553 std::cout << "# " << ++count << "\n\n";
554 assert((tmp1-tmp2).matrix().norm() < 1e-14*tmp2.matrix().norm());
555// assert(count<681);
556// assert(((tmp1-tmp2).abs()<1e-13*tmp2.abs()).all());
557#endif
558
559 // Third part: compute SVD of combined matrix
560 MatrixXr UofSVD, VofSVD;
561 VectorType singVals;
562 computeSVDofM(firstCol + shift, n, UofSVD, singVals, VofSVD);
563
564#ifdef EIGEN_BDCSVD_SANITY_CHECKS
565 assert(UofSVD.allFinite());
566 assert(VofSVD.allFinite());
567#endif
568
569 if (m_compU)
570 structured_update(m_naiveU.block(firstCol, firstCol, n + 1, n + 1), UofSVD, (n+2)/2);
571 else
572 {
573 Map<Matrix<RealScalar,2,Dynamic>,Aligned> tmp(m_workspace.data(),2,n+1);
574 tmp.noalias() = m_naiveU.middleCols(firstCol, n+1) * UofSVD;
575 m_naiveU.middleCols(firstCol, n + 1) = tmp;
576 }
577
578 if (m_compV) structured_update(m_naiveV.block(firstRowW, firstColW, n, n), VofSVD, (n+1)/2);
579
580#ifdef EIGEN_BDCSVD_SANITY_CHECKS
581 assert(m_naiveU.allFinite());
582 assert(m_naiveV.allFinite());
583 assert(m_computed.allFinite());
584#endif
585
586 m_computed.block(firstCol + shift, firstCol + shift, n, n).setZero();
587 m_computed.block(firstCol + shift, firstCol + shift, n, n).diagonal() = singVals;
588}// end divide
589
590// Compute SVD of m_computed.block(firstCol, firstCol, n + 1, n); this block only has non-zeros in
591// the first column and on the diagonal and has undergone deflation, so diagonal is in increasing
592// order except for possibly the (0,0) entry. The computed SVD is stored U, singVals and V, except
593// that if m_compV is false, then V is not computed. Singular values are sorted in decreasing order.
594//
595// TODO Opportunities for optimization: better root finding algo, better stopping criterion, better
596// handling of round-off errors, be consistent in ordering
597// For instance, to solve the secular equation using FMM, see http://www.stat.uchicago.edu/~lekheng/courses/302/classics/greengard-rokhlin.pdf
598template <typename MatrixType>
599void BDCSVD<MatrixType>::computeSVDofM(Eigen::Index firstCol, Eigen::Index n, MatrixXr& U, VectorType& singVals, MatrixXr& V)
600{
601 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
602 using std::abs;
603 ArrayRef col0 = m_computed.col(firstCol).segment(firstCol, n);
604 m_workspace.head(n) = m_computed.block(firstCol, firstCol, n, n).diagonal();
605 ArrayRef diag = m_workspace.head(n);
606 diag(0) = Literal(0);
607
608 // Allocate space for singular values and vectors
609 singVals.resize(n);
610 U.resize(n+1, n+1);
611 if (m_compV) V.resize(n, n);
612
613#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
614 if (col0.hasNaN() || diag.hasNaN())
615 std::cout << "\n\nHAS NAN\n\n";
616#endif
617
618 // Many singular values might have been deflated, the zero ones have been moved to the end,
619 // but others are interleaved and we must ignore them at this stage.
620 // To this end, let's compute a permutation skipping them:
621 Index actual_n = n;
622 while(actual_n>1 && diag(actual_n-1)==Literal(0)) {--actual_n; eigen_internal_assert(col0(actual_n)==Literal(0)); }
623 Index m = 0; // size of the deflated problem
624 for(Index k=0;k<actual_n;++k)
625 if(abs(col0(k))>considerZero)
626 m_workspaceI(m++) = k;
627 Map<ArrayXi> perm(m_workspaceI.data(),m);
628
629 Map<ArrayXr> shifts(m_workspace.data()+1*n, n);
630 Map<ArrayXr> mus(m_workspace.data()+2*n, n);
631 Map<ArrayXr> zhat(m_workspace.data()+3*n, n);
632
633#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
634 std::cout << "computeSVDofM using:\n";
635 std::cout << " z: " << col0.transpose() << "\n";
636 std::cout << " d: " << diag.transpose() << "\n";
637#endif
638
639 // Compute singVals, shifts, and mus
640 computeSingVals(col0, diag, perm, singVals, shifts, mus);
641
642#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
643 std::cout << " j: " << (m_computed.block(firstCol, firstCol, n, n)).jacobiSvd().singularValues().transpose().reverse() << "\n\n";
644 std::cout << " sing-val: " << singVals.transpose() << "\n";
645 std::cout << " mu: " << mus.transpose() << "\n";
646 std::cout << " shift: " << shifts.transpose() << "\n";
647
648 {
649 std::cout << "\n\n mus: " << mus.head(actual_n).transpose() << "\n\n";
650 std::cout << " check1 (expect0) : " << ((singVals.array()-(shifts+mus)) / singVals.array()).head(actual_n).transpose() << "\n\n";
651 assert((((singVals.array()-(shifts+mus)) / singVals.array()).head(actual_n) >= 0).all());
652 std::cout << " check2 (>0) : " << ((singVals.array()-diag) / singVals.array()).head(actual_n).transpose() << "\n\n";
653 assert((((singVals.array()-diag) / singVals.array()).head(actual_n) >= 0).all());
654 }
655#endif
656
657#ifdef EIGEN_BDCSVD_SANITY_CHECKS
658 assert(singVals.allFinite());
659 assert(mus.allFinite());
660 assert(shifts.allFinite());
661#endif
662
663 // Compute zhat
664 perturbCol0(col0, diag, perm, singVals, shifts, mus, zhat);
665#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
666 std::cout << " zhat: " << zhat.transpose() << "\n";
667#endif
668
669#ifdef EIGEN_BDCSVD_SANITY_CHECKS
670 assert(zhat.allFinite());
671#endif
672
673 computeSingVecs(zhat, diag, perm, singVals, shifts, mus, U, V);
674
675#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
676 std::cout << "U^T U: " << (U.transpose() * U - MatrixXr(MatrixXr::Identity(U.cols(),U.cols()))).norm() << "\n";
677 std::cout << "V^T V: " << (V.transpose() * V - MatrixXr(MatrixXr::Identity(V.cols(),V.cols()))).norm() << "\n";
678#endif
679
680#ifdef EIGEN_BDCSVD_SANITY_CHECKS
681 assert(m_naiveU.allFinite());
682 assert(m_naiveV.allFinite());
683 assert(m_computed.allFinite());
684 assert(U.allFinite());
685 assert(V.allFinite());
686// assert((U.transpose() * U - MatrixXr(MatrixXr::Identity(U.cols(),U.cols()))).norm() < 100*NumTraits<RealScalar>::epsilon() * n);
687// assert((V.transpose() * V - MatrixXr(MatrixXr::Identity(V.cols(),V.cols()))).norm() < 100*NumTraits<RealScalar>::epsilon() * n);
688#endif
689
690 // Because of deflation, the singular values might not be completely sorted.
691 // Fortunately, reordering them is a O(n) problem
692 for(Index i=0; i<actual_n-1; ++i)
693 {
694 if(singVals(i)>singVals(i+1))
695 {
696 using std::swap;
697 swap(singVals(i),singVals(i+1));
698 U.col(i).swap(U.col(i+1));
699 if(m_compV) V.col(i).swap(V.col(i+1));
700 }
701 }
702
703#ifdef EIGEN_BDCSVD_SANITY_CHECKS
704 {
705 bool singular_values_sorted = (((singVals.segment(1,actual_n-1)-singVals.head(actual_n-1))).array() >= 0).all();
706 if(!singular_values_sorted)
707 std::cout << "Singular values are not sorted: " << singVals.segment(1,actual_n).transpose() << "\n";
708 assert(singular_values_sorted);
709 }
710#endif
711
712 // Reverse order so that singular values in increased order
713 // Because of deflation, the zeros singular-values are already at the end
714 singVals.head(actual_n).reverseInPlace();
715 U.leftCols(actual_n).rowwise().reverseInPlace();
716 if (m_compV) V.leftCols(actual_n).rowwise().reverseInPlace();
717
718#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
719 JacobiSVD<MatrixXr> jsvd(m_computed.block(firstCol, firstCol, n, n) );
720 std::cout << " * j: " << jsvd.singularValues().transpose() << "\n\n";
721 std::cout << " * sing-val: " << singVals.transpose() << "\n";
722// std::cout << " * err: " << ((jsvd.singularValues()-singVals)>1e-13*singVals.norm()).transpose() << "\n";
723#endif
724}
725
726template <typename MatrixType>
727typename BDCSVD<MatrixType>::RealScalar BDCSVD<MatrixType>::secularEq(RealScalar mu, const ArrayRef& col0, const ArrayRef& diag, const IndicesRef &perm, const ArrayRef& diagShifted, RealScalar shift)
728{
729 Index m = perm.size();
730 RealScalar res = Literal(1);
731 for(Index i=0; i<m; ++i)
732 {
733 Index j = perm(i);
734 // The following expression could be rewritten to involve only a single division,
735 // but this would make the expression more sensitive to overflow.
736 res += (col0(j) / (diagShifted(j) - mu)) * (col0(j) / (diag(j) + shift + mu));
737 }
738 return res;
739
740}
741
742template <typename MatrixType>
743void BDCSVD<MatrixType>::computeSingVals(const ArrayRef& col0, const ArrayRef& diag, const IndicesRef &perm,
744 VectorType& singVals, ArrayRef shifts, ArrayRef mus)
745{
746 using std::abs;
747 using std::swap;
748 using std::sqrt;
749
750 Index n = col0.size();
751 Index actual_n = n;
752 // Note that here actual_n is computed based on col0(i)==0 instead of diag(i)==0 as above
753 // because 1) we have diag(i)==0 => col0(i)==0 and 2) if col0(i)==0, then diag(i) is already a singular value.
754 while(actual_n>1 && col0(actual_n-1)==Literal(0)) --actual_n;
755
756 for (Index k = 0; k < n; ++k)
757 {
758 if (col0(k) == Literal(0) || actual_n==1)
759 {
760 // if col0(k) == 0, then entry is deflated, so singular value is on diagonal
761 // if actual_n==1, then the deflated problem is already diagonalized
762 singVals(k) = k==0 ? col0(0) : diag(k);
763 mus(k) = Literal(0);
764 shifts(k) = k==0 ? col0(0) : diag(k);
765 continue;
766 }
767
768 // otherwise, use secular equation to find singular value
769 RealScalar left = diag(k);
770 RealScalar right; // was: = (k != actual_n-1) ? diag(k+1) : (diag(actual_n-1) + col0.matrix().norm());
771 if(k==actual_n-1)
772 right = (diag(actual_n-1) + col0.matrix().norm());
773 else
774 {
775 // Skip deflated singular values,
776 // recall that at this stage we assume that z[j]!=0 and all entries for which z[j]==0 have been put aside.
777 // This should be equivalent to using perm[]
778 Index l = k+1;
779 while(col0(l)==Literal(0)) { ++l; eigen_internal_assert(l<actual_n); }
780 right = diag(l);
781 }
782
783 // first decide whether it's closer to the left end or the right end
784 RealScalar mid = left + (right-left) / Literal(2);
785 RealScalar fMid = secularEq(mid, col0, diag, perm, diag, Literal(0));
786#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
787 std::cout << "right-left = " << right-left << "\n";
788// std::cout << "fMid = " << fMid << " " << secularEq(mid-left, col0, diag, perm, ArrayXr(diag-left), left)
789// << " " << secularEq(mid-right, col0, diag, perm, ArrayXr(diag-right), right) << "\n";
790 std::cout << " = " << secularEq(left+RealScalar(0.000001)*(right-left), col0, diag, perm, diag, 0)
791 << " " << secularEq(left+RealScalar(0.1) *(right-left), col0, diag, perm, diag, 0)
792 << " " << secularEq(left+RealScalar(0.2) *(right-left), col0, diag, perm, diag, 0)
793 << " " << secularEq(left+RealScalar(0.3) *(right-left), col0, diag, perm, diag, 0)
794 << " " << secularEq(left+RealScalar(0.4) *(right-left), col0, diag, perm, diag, 0)
795 << " " << secularEq(left+RealScalar(0.49) *(right-left), col0, diag, perm, diag, 0)
796 << " " << secularEq(left+RealScalar(0.5) *(right-left), col0, diag, perm, diag, 0)
797 << " " << secularEq(left+RealScalar(0.51) *(right-left), col0, diag, perm, diag, 0)
798 << " " << secularEq(left+RealScalar(0.6) *(right-left), col0, diag, perm, diag, 0)
799 << " " << secularEq(left+RealScalar(0.7) *(right-left), col0, diag, perm, diag, 0)
800 << " " << secularEq(left+RealScalar(0.8) *(right-left), col0, diag, perm, diag, 0)
801 << " " << secularEq(left+RealScalar(0.9) *(right-left), col0, diag, perm, diag, 0)
802 << " " << secularEq(left+RealScalar(0.999999)*(right-left), col0, diag, perm, diag, 0) << "\n";
803#endif
804 RealScalar shift = (k == actual_n-1 || fMid > Literal(0)) ? left : right;
805
806 // measure everything relative to shift
807 Map<ArrayXr> diagShifted(m_workspace.data()+4*n, n);
808 diagShifted = diag - shift;
809
810 if(k!=actual_n-1)
811 {
812 // check that after the shift, f(mid) is still negative:
813 RealScalar midShifted = (right - left) / RealScalar(2);
814 if(shift==right)
815 midShifted = -midShifted;
816 RealScalar fMidShifted = secularEq(midShifted, col0, diag, perm, diagShifted, shift);
817 if(fMidShifted>0)
818 {
819 // fMid was erroneous, fix it:
820 shift = fMidShifted > Literal(0) ? left : right;
821 diagShifted = diag - shift;
822 }
823 }
824
825 // initial guess
826 RealScalar muPrev, muCur;
827 if (shift == left)
828 {
829 muPrev = (right - left) * RealScalar(0.1);
830 if (k == actual_n-1) muCur = right - left;
831 else muCur = (right - left) * RealScalar(0.5);
832 }
833 else
834 {
835 muPrev = -(right - left) * RealScalar(0.1);
836 muCur = -(right - left) * RealScalar(0.5);
837 }
838
839 RealScalar fPrev = secularEq(muPrev, col0, diag, perm, diagShifted, shift);
840 RealScalar fCur = secularEq(muCur, col0, diag, perm, diagShifted, shift);
841 if (abs(fPrev) < abs(fCur))
842 {
843 swap(fPrev, fCur);
844 swap(muPrev, muCur);
845 }
846
847 // rational interpolation: fit a function of the form a / mu + b through the two previous
848 // iterates and use its zero to compute the next iterate
849 bool useBisection = fPrev*fCur>Literal(0);
850 while (fCur!=Literal(0) && abs(muCur - muPrev) > Literal(8) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(abs(muCur), abs(muPrev)) && abs(fCur - fPrev)>NumTraits<RealScalar>::epsilon() && !useBisection)
851 {
852 ++m_numIters;
853
854 // Find a and b such that the function f(mu) = a / mu + b matches the current and previous samples.
855 RealScalar a = (fCur - fPrev) / (Literal(1)/muCur - Literal(1)/muPrev);
856 RealScalar b = fCur - a / muCur;
857 // And find mu such that f(mu)==0:
858 RealScalar muZero = -a/b;
859 RealScalar fZero = secularEq(muZero, col0, diag, perm, diagShifted, shift);
860
861#ifdef EIGEN_BDCSVD_SANITY_CHECKS
862 assert((numext::isfinite)(fZero));
863#endif
864
865 muPrev = muCur;
866 fPrev = fCur;
867 muCur = muZero;
868 fCur = fZero;
869
870 if (shift == left && (muCur < Literal(0) || muCur > right - left)) useBisection = true;
871 if (shift == right && (muCur < -(right - left) || muCur > Literal(0))) useBisection = true;
872 if (abs(fCur)>abs(fPrev)) useBisection = true;
873 }
874
875 // fall back on bisection method if rational interpolation did not work
876 if (useBisection)
877 {
878#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
879 std::cout << "useBisection for k = " << k << ", actual_n = " << actual_n << "\n";
880#endif
881 RealScalar leftShifted, rightShifted;
882 if (shift == left)
883 {
884 // to avoid overflow, we must have mu > max(real_min, |z(k)|/sqrt(real_max)),
885 // the factor 2 is to be more conservative
886 leftShifted = numext::maxi<RealScalar>( (std::numeric_limits<RealScalar>::min)(), Literal(2) * abs(col0(k)) / sqrt((std::numeric_limits<RealScalar>::max)()) );
887
888 // check that we did it right:
889 eigen_internal_assert( (numext::isfinite)( (col0(k)/leftShifted)*(col0(k)/(diag(k)+shift+leftShifted)) ) );
890 // I don't understand why the case k==0 would be special there:
891 // if (k == 0) rightShifted = right - left; else
892 rightShifted = (k==actual_n-1) ? right : ((right - left) * RealScalar(0.51)); // theoretically we can take 0.5, but let's be safe
893 }
894 else
895 {
896 leftShifted = -(right - left) * RealScalar(0.51);
897 if(k+1<n)
898 rightShifted = -numext::maxi<RealScalar>( (std::numeric_limits<RealScalar>::min)(), abs(col0(k+1)) / sqrt((std::numeric_limits<RealScalar>::max)()) );
899 else
900 rightShifted = -(std::numeric_limits<RealScalar>::min)();
901 }
902
903 RealScalar fLeft = secularEq(leftShifted, col0, diag, perm, diagShifted, shift);
904 eigen_internal_assert(fLeft<Literal(0));
905
906#if defined EIGEN_BDCSVD_DEBUG_VERBOSE || defined EIGEN_BDCSVD_SANITY_CHECKS || defined EIGEN_INTERNAL_DEBUGGING
907 RealScalar fRight = secularEq(rightShifted, col0, diag, perm, diagShifted, shift);
908#endif
909
910#ifdef EIGEN_BDCSVD_SANITY_CHECKS
911 if(!(numext::isfinite)(fLeft))
912 std::cout << "f(" << leftShifted << ") =" << fLeft << " ; " << left << " " << shift << " " << right << "\n";
913 assert((numext::isfinite)(fLeft));
914
915 if(!(numext::isfinite)(fRight))
916 std::cout << "f(" << rightShifted << ") =" << fRight << " ; " << left << " " << shift << " " << right << "\n";
917 // assert((numext::isfinite)(fRight));
918#endif
919
920#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
921 if(!(fLeft * fRight<0))
922 {
923 std::cout << "f(leftShifted) using leftShifted=" << leftShifted << " ; diagShifted(1:10):" << diagShifted.head(10).transpose() << "\n ; "
924 << "left==shift=" << bool(left==shift) << " ; left-shift = " << (left-shift) << "\n";
925 std::cout << "k=" << k << ", " << fLeft << " * " << fRight << " == " << fLeft * fRight << " ; "
926 << "[" << left << " .. " << right << "] -> [" << leftShifted << " " << rightShifted << "], shift=" << shift
927 << " , f(right)=" << secularEq(0, col0, diag, perm, diagShifted, shift)
928 << " == " << secularEq(right, col0, diag, perm, diag, 0) << " == " << fRight << "\n";
929 }
930#endif
931 eigen_internal_assert(fLeft * fRight < Literal(0));
932
933 if(fLeft<Literal(0))
934 {
935 while (rightShifted - leftShifted > Literal(2) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(abs(leftShifted), abs(rightShifted)))
936 {
937 RealScalar midShifted = (leftShifted + rightShifted) / Literal(2);
938 fMid = secularEq(midShifted, col0, diag, perm, diagShifted, shift);
939 eigen_internal_assert((numext::isfinite)(fMid));
940
941 if (fLeft * fMid < Literal(0))
942 {
943 rightShifted = midShifted;
944 }
945 else
946 {
947 leftShifted = midShifted;
948 fLeft = fMid;
949 }
950 }
951 muCur = (leftShifted + rightShifted) / Literal(2);
952 }
953 else
954 {
955 // We have a problem as shifting on the left or right give either a positive or negative value
956 // at the middle of [left,right]...
957 // Instead fo abbording or entering an infinite loop,
958 // let's just use the middle as the estimated zero-crossing:
959 muCur = (right - left) * RealScalar(0.5);
960 if(shift == right)
961 muCur = -muCur;
962 }
963 }
964
965 singVals[k] = shift + muCur;
966 shifts[k] = shift;
967 mus[k] = muCur;
968
969#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
970 if(k+1<n)
971 std::cout << "found " << singVals[k] << " == " << shift << " + " << muCur << " from " << diag(k) << " .. " << diag(k+1) << "\n";
972#endif
973#ifdef EIGEN_BDCSVD_SANITY_CHECKS
974 assert(k==0 || singVals[k]>=singVals[k-1]);
975 assert(singVals[k]>=diag(k));
976#endif
977
978 // perturb singular value slightly if it equals diagonal entry to avoid division by zero later
979 // (deflation is supposed to avoid this from happening)
980 // - this does no seem to be necessary anymore -
981 // if (singVals[k] == left) singVals[k] *= 1 + NumTraits<RealScalar>::epsilon();
982 // if (singVals[k] == right) singVals[k] *= 1 - NumTraits<RealScalar>::epsilon();
983 }
984}
985
986
987// zhat is perturbation of col0 for which singular vectors can be computed stably (see Section 3.1)
988template <typename MatrixType>
989void BDCSVD<MatrixType>::perturbCol0
990 (const ArrayRef& col0, const ArrayRef& diag, const IndicesRef &perm, const VectorType& singVals,
991 const ArrayRef& shifts, const ArrayRef& mus, ArrayRef zhat)
992{
993 using std::sqrt;
994 Index n = col0.size();
995 Index m = perm.size();
996 if(m==0)
997 {
998 zhat.setZero();
999 return;
1000 }
1001 Index lastIdx = perm(m-1);
1002 // The offset permits to skip deflated entries while computing zhat
1003 for (Index k = 0; k < n; ++k)
1004 {
1005 if (col0(k) == Literal(0)) // deflated
1006 zhat(k) = Literal(0);
1007 else
1008 {
1009 // see equation (3.6)
1010 RealScalar dk = diag(k);
1011 RealScalar prod = (singVals(lastIdx) + dk) * (mus(lastIdx) + (shifts(lastIdx) - dk));
1012#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1013 if(prod<0) {
1014 std::cout << "k = " << k << " ; z(k)=" << col0(k) << ", diag(k)=" << dk << "\n";
1015 std::cout << "prod = " << "(" << singVals(lastIdx) << " + " << dk << ") * (" << mus(lastIdx) << " + (" << shifts(lastIdx) << " - " << dk << "))" << "\n";
1016 std::cout << " = " << singVals(lastIdx) + dk << " * " << mus(lastIdx) + (shifts(lastIdx) - dk) << "\n";
1017 }
1018 assert(prod>=0);
1019#endif
1020
1021 for(Index l = 0; l<m; ++l)
1022 {
1023 Index i = perm(l);
1024 if(i!=k)
1025 {
1026#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1027 if(i>=k && (l==0 || l-1>=m))
1028 {
1029 std::cout << "Error in perturbCol0\n";
1030 std::cout << " " << k << "/" << n << " " << l << "/" << m << " " << i << "/" << n << " ; " << col0(k) << " " << diag(k) << " " << "\n";
1031 std::cout << " " <<diag(i) << "\n";
1032 Index j = (i<k /*|| l==0*/) ? i : perm(l-1);
1033 std::cout << " " << "j=" << j << "\n";
1034 }
1035#endif
1036 // Avoid index out of bounds.
1037 // Will end up setting zhat(k) = 0.
1038 if (i >= k && l == 0) {
1039 m_info = NumericalIssue;
1040 prod = 0;
1041 break;
1042 }
1043 Index j = i<k ? i : l > 0 ? perm(l-1) : i;
1044#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1045 if(!(dk!=Literal(0) || diag(i)!=Literal(0)))
1046 {
1047 std::cout << "k=" << k << ", i=" << i << ", l=" << l << ", perm.size()=" << perm.size() << "\n";
1048 }
1049 assert(dk!=Literal(0) || diag(i)!=Literal(0));
1050#endif
1051 prod *= ((singVals(j)+dk) / ((diag(i)+dk))) * ((mus(j)+(shifts(j)-dk)) / ((diag(i)-dk)));
1052#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1053 assert(prod>=0);
1054#endif
1055#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1056 if(i!=k && numext::abs(((singVals(j)+dk)*(mus(j)+(shifts(j)-dk)))/((diag(i)+dk)*(diag(i)-dk)) - 1) > 0.9 )
1057 std::cout << " " << ((singVals(j)+dk)*(mus(j)+(shifts(j)-dk)))/((diag(i)+dk)*(diag(i)-dk)) << " == (" << (singVals(j)+dk) << " * " << (mus(j)+(shifts(j)-dk))
1058 << ") / (" << (diag(i)+dk) << " * " << (diag(i)-dk) << ")\n";
1059#endif
1060 }
1061 }
1062#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1063 std::cout << "zhat(" << k << ") = sqrt( " << prod << ") ; " << (singVals(lastIdx) + dk) << " * " << mus(lastIdx) + shifts(lastIdx) << " - " << dk << "\n";
1064#endif
1065 RealScalar tmp = sqrt(prod);
1066#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1067 assert((numext::isfinite)(tmp));
1068#endif
1069 zhat(k) = col0(k) > Literal(0) ? RealScalar(tmp) : RealScalar(-tmp);
1070 }
1071 }
1072}
1073
1074// compute singular vectors
1075template <typename MatrixType>
1076void BDCSVD<MatrixType>::computeSingVecs
1077 (const ArrayRef& zhat, const ArrayRef& diag, const IndicesRef &perm, const VectorType& singVals,
1078 const ArrayRef& shifts, const ArrayRef& mus, MatrixXr& U, MatrixXr& V)
1079{
1080 Index n = zhat.size();
1081 Index m = perm.size();
1082
1083 for (Index k = 0; k < n; ++k)
1084 {
1085 if (zhat(k) == Literal(0))
1086 {
1087 U.col(k) = VectorType::Unit(n+1, k);
1088 if (m_compV) V.col(k) = VectorType::Unit(n, k);
1089 }
1090 else
1091 {
1092 U.col(k).setZero();
1093 for(Index l=0;l<m;++l)
1094 {
1095 Index i = perm(l);
1096 U(i,k) = zhat(i)/(((diag(i) - shifts(k)) - mus(k)) )/( (diag(i) + singVals[k]));
1097 }
1098 U(n,k) = Literal(0);
1099 U.col(k).normalize();
1100
1101 if (m_compV)
1102 {
1103 V.col(k).setZero();
1104 for(Index l=1;l<m;++l)
1105 {
1106 Index i = perm(l);
1107 V(i,k) = diag(i) * zhat(i) / (((diag(i) - shifts(k)) - mus(k)) )/( (diag(i) + singVals[k]));
1108 }
1109 V(0,k) = Literal(-1);
1110 V.col(k).normalize();
1111 }
1112 }
1113 }
1114 U.col(n) = VectorType::Unit(n+1, n);
1115}
1116
1117
1118// page 12_13
1119// i >= 1, di almost null and zi non null.
1120// We use a rotation to zero out zi applied to the left of M
1121template <typename MatrixType>
1122void BDCSVD<MatrixType>::deflation43(Eigen::Index firstCol, Eigen::Index shift, Eigen::Index i, Eigen::Index size)
1123{
1124 using std::abs;
1125 using std::sqrt;
1126 using std::pow;
1127 Index start = firstCol + shift;
1128 RealScalar c = m_computed(start, start);
1129 RealScalar s = m_computed(start+i, start);
1130 RealScalar r = numext::hypot(c,s);
1131 if (r == Literal(0))
1132 {
1133 m_computed(start+i, start+i) = Literal(0);
1134 return;
1135 }
1136 m_computed(start,start) = r;
1137 m_computed(start+i, start) = Literal(0);
1138 m_computed(start+i, start+i) = Literal(0);
1139
1140 JacobiRotation<RealScalar> J(c/r,-s/r);
1141 if (m_compU) m_naiveU.middleRows(firstCol, size+1).applyOnTheRight(firstCol, firstCol+i, J);
1142 else m_naiveU.applyOnTheRight(firstCol, firstCol+i, J);
1143}// end deflation 43
1144
1145
1146// page 13
1147// i,j >= 1, i!=j and |di - dj| < epsilon * norm2(M)
1148// We apply two rotations to have zj = 0;
1149// TODO deflation44 is still broken and not properly tested
1150template <typename MatrixType>
1151void BDCSVD<MatrixType>::deflation44(Eigen::Index firstColu , Eigen::Index firstColm, Eigen::Index firstRowW, Eigen::Index firstColW, Eigen::Index i, Eigen::Index j, Eigen::Index size)
1152{
1153 using std::abs;
1154 using std::sqrt;
1155 using std::conj;
1156 using std::pow;
1157 RealScalar c = m_computed(firstColm+i, firstColm);
1158 RealScalar s = m_computed(firstColm+j, firstColm);
1159 RealScalar r = sqrt(numext::abs2(c) + numext::abs2(s));
1160#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1161 std::cout << "deflation 4.4: " << i << "," << j << " -> " << c << " " << s << " " << r << " ; "
1162 << m_computed(firstColm + i-1, firstColm) << " "
1163 << m_computed(firstColm + i, firstColm) << " "
1164 << m_computed(firstColm + i+1, firstColm) << " "
1165 << m_computed(firstColm + i+2, firstColm) << "\n";
1166 std::cout << m_computed(firstColm + i-1, firstColm + i-1) << " "
1167 << m_computed(firstColm + i, firstColm+i) << " "
1168 << m_computed(firstColm + i+1, firstColm+i+1) << " "
1169 << m_computed(firstColm + i+2, firstColm+i+2) << "\n";
1170#endif
1171 if (r==Literal(0))
1172 {
1173 m_computed(firstColm + i, firstColm + i) = m_computed(firstColm + j, firstColm + j);
1174 return;
1175 }
1176 c/=r;
1177 s/=r;
1178 m_computed(firstColm + i, firstColm) = r;
1179 m_computed(firstColm + j, firstColm + j) = m_computed(firstColm + i, firstColm + i);
1180 m_computed(firstColm + j, firstColm) = Literal(0);
1181
1182 JacobiRotation<RealScalar> J(c,-s);
1183 if (m_compU) m_naiveU.middleRows(firstColu, size+1).applyOnTheRight(firstColu + i, firstColu + j, J);
1184 else m_naiveU.applyOnTheRight(firstColu+i, firstColu+j, J);
1185 if (m_compV) m_naiveV.middleRows(firstRowW, size).applyOnTheRight(firstColW + i, firstColW + j, J);
1186}// end deflation 44
1187
1188
1189// acts on block from (firstCol+shift, firstCol+shift) to (lastCol+shift, lastCol+shift) [inclusive]
1190template <typename MatrixType>
1191void BDCSVD<MatrixType>::deflation(Eigen::Index firstCol, Eigen::Index lastCol, Eigen::Index k, Eigen::Index firstRowW, Eigen::Index firstColW, Eigen::Index shift)
1192{
1193 using std::sqrt;
1194 using std::abs;
1195 const Index length = lastCol + 1 - firstCol;
1196
1197 Block<MatrixXr,Dynamic,1> col0(m_computed, firstCol+shift, firstCol+shift, length, 1);
1198 Diagonal<MatrixXr> fulldiag(m_computed);
1199 VectorBlock<Diagonal<MatrixXr>,Dynamic> diag(fulldiag, firstCol+shift, length);
1200
1201 const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
1202 RealScalar maxDiag = diag.tail((std::max)(Index(1),length-1)).cwiseAbs().maxCoeff();
1203 RealScalar epsilon_strict = numext::maxi<RealScalar>(considerZero,NumTraits<RealScalar>::epsilon() * maxDiag);
1204 RealScalar epsilon_coarse = Literal(8) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(col0.cwiseAbs().maxCoeff(), maxDiag);
1205
1206#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1207 assert(m_naiveU.allFinite());
1208 assert(m_naiveV.allFinite());
1209 assert(m_computed.allFinite());
1210#endif
1211
1212#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1213 std::cout << "\ndeflate:" << diag.head(k+1).transpose() << " | " << diag.segment(k+1,length-k-1).transpose() << "\n";
1214#endif
1215
1216 //condition 4.1
1217 if (diag(0) < epsilon_coarse)
1218 {
1219#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1220 std::cout << "deflation 4.1, because " << diag(0) << " < " << epsilon_coarse << "\n";
1221#endif
1222 diag(0) = epsilon_coarse;
1223 }
1224
1225 //condition 4.2
1226 for (Index i=1;i<length;++i)
1227 if (abs(col0(i)) < epsilon_strict)
1228 {
1229#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1230 std::cout << "deflation 4.2, set z(" << i << ") to zero because " << abs(col0(i)) << " < " << epsilon_strict << " (diag(" << i << ")=" << diag(i) << ")\n";
1231#endif
1232 col0(i) = Literal(0);
1233 }
1234
1235 //condition 4.3
1236 for (Index i=1;i<length; i++)
1237 if (diag(i) < epsilon_coarse)
1238 {
1239#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1240 std::cout << "deflation 4.3, cancel z(" << i << ")=" << col0(i) << " because diag(" << i << ")=" << diag(i) << " < " << epsilon_coarse << "\n";
1241#endif
1242 deflation43(firstCol, shift, i, length);
1243 }
1244
1245#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1246 assert(m_naiveU.allFinite());
1247 assert(m_naiveV.allFinite());
1248 assert(m_computed.allFinite());
1249#endif
1250#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1251 std::cout << "to be sorted: " << diag.transpose() << "\n\n";
1252 std::cout << " : " << col0.transpose() << "\n\n";
1253#endif
1254 {
1255 // Check for total deflation
1256 // If we have a total deflation, then we have to consider col0(0)==diag(0) as a singular value during sorting.
1257 const bool total_deflation = (col0.tail(length-1).array().abs()<considerZero).all();
1258
1259 // Sort the diagonal entries, since diag(1:k-1) and diag(k:length) are already sorted, let's do a sorted merge.
1260 // First, compute the respective permutation.
1261 Index *permutation = m_workspaceI.data();
1262 {
1263 permutation[0] = 0;
1264 Index p = 1;
1265
1266 // Move deflated diagonal entries at the end.
1267 for(Index i=1; i<length; ++i)
1268 if(abs(diag(i))<considerZero)
1269 permutation[p++] = i;
1270
1271 Index i=1, j=k+1;
1272 for( ; p < length; ++p)
1273 {
1274 if (i > k) permutation[p] = j++;
1275 else if (j >= length) permutation[p] = i++;
1276 else if (diag(i) < diag(j)) permutation[p] = j++;
1277 else permutation[p] = i++;
1278 }
1279 }
1280
1281 // If we have a total deflation, then we have to insert diag(0) at the right place
1282 if(total_deflation)
1283 {
1284 for(Index i=1; i<length; ++i)
1285 {
1286 Index pi = permutation[i];
1287 if(abs(diag(pi))<considerZero || diag(0)<diag(pi))
1288 permutation[i-1] = permutation[i];
1289 else
1290 {
1291 permutation[i-1] = 0;
1292 break;
1293 }
1294 }
1295 }
1296
1297 // Current index of each col, and current column of each index
1298 Index *realInd = m_workspaceI.data()+length;
1299 Index *realCol = m_workspaceI.data()+2*length;
1300
1301 for(int pos = 0; pos< length; pos++)
1302 {
1303 realCol[pos] = pos;
1304 realInd[pos] = pos;
1305 }
1306
1307 for(Index i = total_deflation?0:1; i < length; i++)
1308 {
1309 const Index pi = permutation[length - (total_deflation ? i+1 : i)];
1310 const Index J = realCol[pi];
1311
1312 using std::swap;
1313 // swap diagonal and first column entries:
1314 swap(diag(i), diag(J));
1315 if(i!=0 && J!=0) swap(col0(i), col0(J));
1316
1317 // change columns
1318 if (m_compU) m_naiveU.col(firstCol+i).segment(firstCol, length + 1).swap(m_naiveU.col(firstCol+J).segment(firstCol, length + 1));
1319 else m_naiveU.col(firstCol+i).segment(0, 2) .swap(m_naiveU.col(firstCol+J).segment(0, 2));
1320 if (m_compV) m_naiveV.col(firstColW + i).segment(firstRowW, length).swap(m_naiveV.col(firstColW + J).segment(firstRowW, length));
1321
1322 //update real pos
1323 const Index realI = realInd[i];
1324 realCol[realI] = J;
1325 realCol[pi] = i;
1326 realInd[J] = realI;
1327 realInd[i] = pi;
1328 }
1329 }
1330#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1331 std::cout << "sorted: " << diag.transpose().format(bdcsvdfmt) << "\n";
1332 std::cout << " : " << col0.transpose() << "\n\n";
1333#endif
1334
1335 //condition 4.4
1336 {
1337 Index i = length-1;
1338 while(i>0 && (abs(diag(i))<considerZero || abs(col0(i))<considerZero)) --i;
1339 for(; i>1;--i)
1340 if( (diag(i) - diag(i-1)) < NumTraits<RealScalar>::epsilon()*maxDiag )
1341 {
1342#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
1343 std::cout << "deflation 4.4 with i = " << i << " because " << diag(i) << " - " << diag(i-1) << " == " << (diag(i) - diag(i-1)) << " < " << NumTraits<RealScalar>::epsilon()*/*diag(i)*/maxDiag << "\n";
1344#endif
1345 eigen_internal_assert(abs(diag(i) - diag(i-1))<epsilon_coarse && " diagonal entries are not properly sorted");
1346 deflation44(firstCol, firstCol + shift, firstRowW, firstColW, i-1, i, length);
1347 }
1348 }
1349
1350#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1351 for(Index j=2;j<length;++j)
1352 assert(diag(j-1)<=diag(j) || abs(diag(j))<considerZero);
1353#endif
1354
1355#ifdef EIGEN_BDCSVD_SANITY_CHECKS
1356 assert(m_naiveU.allFinite());
1357 assert(m_naiveV.allFinite());
1358 assert(m_computed.allFinite());
1359#endif
1360}//end deflation
1361
1368template<typename Derived>
1369BDCSVD<typename MatrixBase<Derived>::PlainObject>
1374
1375} // end namespace Eigen
1376
1377#endif
class Bidiagonal Divide and Conquer SVD
Definition BDCSVD.h:78
BDCSVD(const MatrixType &matrix, unsigned int computationOptions=0)
Constructor performing the decomposition of given matrix.
Definition BDCSVD.h:144
BDCSVD()
Default Constructor.
Definition BDCSVD.h:118
BDCSVD(Index rows, Index cols, unsigned int computationOptions=0)
Default Constructor with memory preallocation.
Definition BDCSVD.h:128
BDCSVD & compute(const MatrixType &matrix, unsigned int computationOptions)
Method performing the decomposition of given matrix using custom options.
Definition BDCSVD.h:249
BDCSVD & compute(const MatrixType &matrix)
Method performing the decomposition of given matrix using current options.
Definition BDCSVD.h:172
const WithFormat< Derived > format(const IOFormat &fmt) const
Definition DenseBase.h:519
EIGEN_DEVICE_FUNC bool all() const
Definition BooleanRedux.h:81
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void swap(const DenseBase< OtherDerived > &other)
swaps *this with the expression other.
Definition DenseBase.h:420
EIGEN_DEVICE_FUNC Index count() const
Definition BooleanRedux.h:129
EIGEN_DEVICE_FUNC Scalar prod() const
Definition Redux.h:493
EIGEN_DEVICE_FUNC Derived & setZero()
Sets all coefficients in this expression to zero.
Definition CwiseNullaryOp.h:546
EIGEN_DEVICE_FUNC TransposeReturnType transpose()
Definition Transpose.h:182
EIGEN_DEVICE_FUNC ReverseReturnType reverse()
Definition Reverse.h:120
EIGEN_DEVICE_FUNC bool any() const
Definition BooleanRedux.h:105
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
JacobiSVD< PlainObject > jacobiSvd(unsigned int computationOptions=0) const
\svd_module
Definition JacobiSVD.h:806
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition Dot.h:103
A matrix or vector expression mapping an existing expression.
Definition Ref.h:283
Base class of SVD algorithms.
Definition SVDBase.h:64
bool computeV() const
Definition SVDBase.h:210
Eigen::Index Index
Definition SVDBase.h:74
bool computeU() const
Definition SVDBase.h:208
Definition UpperBidiagonalization.h:21
@ Aligned
Definition Constants.h:240
@ NumericalIssue
The provided data did not satisfy the prerequisites.
Definition Constants.h:444
@ InvalidInput
The inputs are invalid, or the algorithm has been improperly called.
Definition Constants.h:449
@ Success
Computation was successful.
Definition Constants.h:442
@ NoConvergence
Iterative procedure did not converge.
Definition Constants.h:446
@ ComputeFullV
Used in JacobiSVD to indicate that the square matrix V is to be computed.
Definition Constants.h:397
@ ComputeFullU
Used in JacobiSVD to indicate that the square matrix U is to be computed.
Definition Constants.h:393
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition Constants.h:22
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition EigenBase.h:67
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
Definition ForwardDeclarations.h:17
Definition Meta.h:96