svd_common.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef SVD_DEFAULT
12 #error a macro SVD_DEFAULT(MatrixType) must be defined prior to including svd_common.h
13 #endif
14 
15 #ifndef SVD_FOR_MIN_NORM
16 #error a macro SVD_FOR_MIN_NORM(MatrixType) must be defined prior to including svd_common.h
17 #endif
18 
19 #ifndef SVD_STATIC_OPTIONS
20 #error a macro SVD_STATIC_OPTIONS(MatrixType, Options) must be defined prior to including svd_common.h
21 #endif
22 
23 #include "svd_fill.h"
24 #include "solverbase.h"
25 
26 // Check that the matrix m is properly reconstructed and that the U and V factors are unitary
27 // The SVD must have already been computed.
28 template <typename SvdType, typename MatrixType>
29 void svd_check_full(const MatrixType& m, const SvdType& svd) {
30  Index rows = m.rows();
31  Index cols = m.cols();
32 
33  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime };
34 
35  typedef typename MatrixType::Scalar Scalar;
36  typedef typename MatrixType::RealScalar RealScalar;
37  typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixUType;
38  typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime> MatrixVType;
39 
41  sigma.diagonal() = svd.singularValues().template cast<Scalar>();
42  MatrixUType u = svd.matrixU();
43  MatrixVType v = svd.matrixV();
44  RealScalar scaling = m.cwiseAbs().maxCoeff();
45  if (scaling < (std::numeric_limits<RealScalar>::min)()) {
46  VERIFY(sigma.cwiseAbs().maxCoeff() <= (std::numeric_limits<RealScalar>::min)());
47  } else {
48  VERIFY_IS_APPROX(m / scaling, u * (sigma / scaling) * v.adjoint());
49  }
52 }
53 
54 // Compare partial SVD defined by computationOptions to a full SVD referenceSvd
55 template <typename MatrixType, typename SvdType, int Options>
56 void svd_compare_to_full(const MatrixType& m, const SvdType& referenceSvd) {
57  typedef typename MatrixType::RealScalar RealScalar;
58  Index rows = m.rows();
59  Index cols = m.cols();
60  Index diagSize = (std::min)(rows, cols);
61  RealScalar prec = test_precision<RealScalar>();
62 
64 
65  VERIFY_IS_APPROX(svd.singularValues(), referenceSvd.singularValues());
66 
67  if (Options & (ComputeFullV | ComputeThinV)) {
68  VERIFY((svd.matrixV().adjoint() * svd.matrixV()).isIdentity(prec));
69  VERIFY_IS_APPROX(svd.matrixV().leftCols(diagSize) * svd.singularValues().asDiagonal() *
70  svd.matrixV().leftCols(diagSize).adjoint(),
71  referenceSvd.matrixV().leftCols(diagSize) * referenceSvd.singularValues().asDiagonal() *
72  referenceSvd.matrixV().leftCols(diagSize).adjoint());
73  }
74 
75  if (Options & (ComputeFullU | ComputeThinU)) {
76  VERIFY((svd.matrixU().adjoint() * svd.matrixU()).isIdentity(prec));
77  VERIFY_IS_APPROX(svd.matrixU().leftCols(diagSize) * svd.singularValues().cwiseAbs2().asDiagonal() *
78  svd.matrixU().leftCols(diagSize).adjoint(),
79  referenceSvd.matrixU().leftCols(diagSize) *
80  referenceSvd.singularValues().cwiseAbs2().asDiagonal() *
81  referenceSvd.matrixU().leftCols(diagSize).adjoint());
82  }
83 
84  // The following checks are not critical.
85  // For instance, with Dived&Conquer SVD, if only the factor 'V' is computed then different matrix-matrix product
86  // implementation will be used and the resulting 'V' factor might be significantly different when the SVD
87  // decomposition is not unique, especially with single precision float.
88  ++g_test_level;
89  if (Options & ComputeFullU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU());
90  if (Options & ComputeThinU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU().leftCols(diagSize));
91  if (Options & ComputeFullV) VERIFY_IS_APPROX(svd.matrixV().cwiseAbs(), referenceSvd.matrixV().cwiseAbs());
92  if (Options & ComputeThinV) VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize));
93  --g_test_level;
94 }
95 
96 template <typename SvdType, typename MatrixType>
98  typedef typename MatrixType::Scalar Scalar;
99  typedef typename MatrixType::RealScalar RealScalar;
100  Index rows = m.rows();
101  Index cols = m.cols();
102 
103  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime };
104 
105  typedef Matrix<Scalar, RowsAtCompileTime, Dynamic> RhsType;
106  typedef Matrix<Scalar, ColsAtCompileTime, Dynamic> SolutionType;
107 
108  RhsType rhs = RhsType::Random(rows, internal::random<Index>(1, cols));
109  SvdType svd(m);
110 
112  svd.setThreshold(RealScalar(1e-8));
114  svd.setThreshold(RealScalar(2e-4));
115 
116  SolutionType x = svd.solve(rhs);
117 
118  RealScalar residual = (m * x - rhs).norm();
119  RealScalar rhs_norm = rhs.norm();
120  if (!test_isMuchSmallerThan(residual, rhs.norm())) {
121  // ^^^ If the residual is very small, then we have an exact solution, so we are already good.
122 
123  // evaluate normal equation which works also for least-squares solutions
124  if (internal::is_same<RealScalar, double>::value || svd.rank() == m.diagonal().size()) {
125  using std::sqrt;
126  // This test is not stable with single precision.
127  // This is probably because squaring m signicantly affects the precision.
129 
130  VERIFY_IS_APPROX(m.adjoint() * (m * x), m.adjoint() * rhs);
131 
133  }
134 
135  // Check that there is no significantly better solution in the neighborhood of x
136  for (Index k = 0; k < x.rows(); ++k) {
137  using std::abs;
138 
139  SolutionType y(x);
140  y.row(k) = (RealScalar(1) + 2 * NumTraits<RealScalar>::epsilon()) * x.row(k);
141  RealScalar residual_y = (m * y - rhs).norm();
142  VERIFY(test_isMuchSmallerThan(abs(residual_y - residual), rhs_norm) || residual < residual_y);
144  VERIFY(test_isApprox(residual_y, residual) || residual < residual_y);
146 
147  y.row(k) = (RealScalar(1) - 2 * NumTraits<RealScalar>::epsilon()) * x.row(k);
148  residual_y = (m * y - rhs).norm();
149  VERIFY(test_isMuchSmallerThan(abs(residual_y - residual), rhs_norm) || residual < residual_y);
151  VERIFY(test_isApprox(residual_y, residual) || residual < residual_y);
153  }
154  }
155 }
156 
157 // check minimal norm solutions, the input matrix m is only used to recover problem size
158 template <typename MatrixType, int Options>
159 void svd_min_norm(const MatrixType& m) {
160  typedef typename MatrixType::Scalar Scalar;
161  Index cols = m.cols();
162 
163  enum { ColsAtCompileTime = MatrixType::ColsAtCompileTime };
164 
165  typedef Matrix<Scalar, ColsAtCompileTime, Dynamic> SolutionType;
166 
167  // generate a full-rank m x n problem with m<n
168  enum {
169  RankAtCompileTime2 = ColsAtCompileTime == Dynamic ? Dynamic : (ColsAtCompileTime) / 2 + 1,
170  RowsAtCompileTime3 = ColsAtCompileTime == Dynamic ? Dynamic : ColsAtCompileTime + 1
171  };
172  typedef Matrix<Scalar, RankAtCompileTime2, ColsAtCompileTime> MatrixType2;
173  typedef Matrix<Scalar, RankAtCompileTime2, 1> RhsType2;
174  typedef Matrix<Scalar, ColsAtCompileTime, RankAtCompileTime2> MatrixType2T;
175  Index rank = RankAtCompileTime2 == Dynamic ? internal::random<Index>(1, cols) : Index(RankAtCompileTime2);
176  MatrixType2 m2(rank, cols);
177  int guard = 0;
178  do {
179  m2.setRandom();
180  } while (SVD_FOR_MIN_NORM(MatrixType2)(m2).setThreshold(test_precision<Scalar>()).rank() != rank && (++guard) < 10);
181  VERIFY(guard < 10);
182 
183  RhsType2 rhs2 = RhsType2::Random(rank);
184  // use QR to find a reference minimal norm solution
185  HouseholderQR<MatrixType2T> qr(m2.adjoint());
186  Matrix<Scalar, Dynamic, 1> tmp =
187  qr.matrixQR().topLeftCorner(rank, rank).template triangularView<Upper>().adjoint().solve(rhs2);
189  tmp.tail(cols - rank).setZero();
190  SolutionType x21 = qr.householderQ() * tmp;
191  // now check with SVD
192  SVD_STATIC_OPTIONS(MatrixType2, Options) svd2(m2);
193  SolutionType x22 = svd2.solve(rhs2);
194  VERIFY_IS_APPROX(m2 * x21, rhs2);
195  VERIFY_IS_APPROX(m2 * x22, rhs2);
196  VERIFY_IS_APPROX(x21, x22);
197 
198  // Now check with a rank deficient matrix
199  typedef Matrix<Scalar, RowsAtCompileTime3, ColsAtCompileTime> MatrixType3;
200  typedef Matrix<Scalar, RowsAtCompileTime3, 1> RhsType3;
201  Index rows3 = RowsAtCompileTime3 == Dynamic ? internal::random<Index>(rank + 1, 2 * cols) : Index(RowsAtCompileTime3);
202  Matrix<Scalar, RowsAtCompileTime3, Dynamic> C = Matrix<Scalar, RowsAtCompileTime3, Dynamic>::Random(rows3, rank);
203  MatrixType3 m3 = C * m2;
204  RhsType3 rhs3 = C * rhs2;
205  SVD_STATIC_OPTIONS(MatrixType3, Options) svd3(m3);
206  SolutionType x3 = svd3.solve(rhs3);
207  VERIFY_IS_APPROX(m3 * x3, rhs3);
208  VERIFY_IS_APPROX(m3 * x21, rhs3);
209  VERIFY_IS_APPROX(m2 * x3, rhs2);
210  VERIFY_IS_APPROX(x21, x3);
211 }
212 
213 template <typename MatrixType, typename SolverType>
214 void svd_test_solvers(const MatrixType& m, const SolverType& solver) {
215  Index rows, cols, cols2;
216 
217  rows = m.rows();
218  cols = m.cols();
219 
220  if (MatrixType::ColsAtCompileTime == Dynamic) {
221  cols2 = internal::random<int>(2, EIGEN_TEST_MAX_SIZE);
222  } else {
223  cols2 = cols;
224  }
225  typedef Matrix<typename MatrixType::Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> CMatrixType;
226  check_solverbase<CMatrixType, MatrixType>(m, solver, rows, cols, cols2);
227 }
228 
229 // work around stupid msvc error when constructing at compile time an expression that involves
230 // a division by zero, even if the numeric type has floating point
231 template <typename Scalar>
233  return Scalar(0);
234 }
235 
236 // workaround aggressive optimization in ICC
237 template <typename T>
239  return a - b;
240 }
241 
242 // This function verifies we don't iterate infinitely on nan/inf values,
243 // and that info() returns InvalidInput.
244 template <typename MatrixType>
245 void svd_inf_nan() {
247  typedef typename MatrixType::Scalar Scalar;
248  Scalar some_inf = Scalar(1) / zero<Scalar>();
249  VERIFY(sub(some_inf, some_inf) != sub(some_inf, some_inf));
250  svd.compute(MatrixType::Constant(10, 10, some_inf));
251  VERIFY(svd.info() == InvalidInput);
252 
253  Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
254  VERIFY(nan != nan);
255  svd.compute(MatrixType::Constant(10, 10, nan));
256  VERIFY(svd.info() == InvalidInput);
257 
258  MatrixType m = MatrixType::Zero(10, 10);
259  m(internal::random<int>(0, 9), internal::random<int>(0, 9)) = some_inf;
260  svd.compute(m);
261  VERIFY(svd.info() == InvalidInput);
262 
263  m = MatrixType::Zero(10, 10);
264  m(internal::random<int>(0, 9), internal::random<int>(0, 9)) = nan;
265  svd.compute(m);
266  VERIFY(svd.info() == InvalidInput);
267 
268  // regression test for bug 791
269  m.resize(3, 3);
270  m << 0, 2 * NumTraits<Scalar>::epsilon(), 0.5, 0, -0.5, 0, nan, 0, 0;
271  svd.compute(m);
272  VERIFY(svd.info() == InvalidInput);
273 
275  m.resize(4, 4);
276  m << 1, 0, 0, 0, 0, 3, 1, min, 1, 0, 1, nan, 0, nan, nan, 0;
277  svd.compute(m);
278  VERIFY(svd.info() == InvalidInput);
279 }
280 
281 // Regression test for bug 286: JacobiSVD loops indefinitely with some
282 // matrices containing denormal numbers.
283 template <typename>
285 #if defined __INTEL_COMPILER
286 // shut up warning #239: floating point underflow
287 #pragma warning push
288 #pragma warning disable 239
289 #endif
290  Matrix2d M;
291  M << -7.90884e-313, -4.94e-324, 0, 5.60844e-313;
293  svd.compute(M);
295 
296  // Check all 2x2 matrices made with the following coefficients:
297  VectorXd value_set(9);
298  value_set << 0, 1, -1, 5.60844e-313, -5.60844e-313, 4.94e-324, -4.94e-324, -4.94e-223, 4.94e-223;
299  Array4i id(0, 0, 0, 0);
300  int k = 0;
301  do {
302  M << value_set(id(0)), value_set(id(1)), value_set(id(2)), value_set(id(3));
303  svd.compute(M);
305 
306  id(k)++;
307  if (id(k) >= value_set.size()) {
308  while (k < 3 && id(k) >= value_set.size()) id(++k)++;
309  id.head(k).setZero();
310  k = 0;
311  }
312 
313  } while ((id < int(value_set.size())).all());
314 
315 #if defined __INTEL_COMPILER
316 #pragma warning pop
317 #endif
318 
319  // Check for overflow:
320  Matrix3d M3;
321  M3 << 4.4331978442502944e+307, -5.8585363752028680e+307, 6.4527017443412964e+307, 3.7841695601406358e+307,
322  2.4331702789740617e+306, -3.5235707140272905e+307, -8.7190887618028355e+307, -7.3453213709232193e+307,
323  -2.4367363684472105e+307;
324 
325  SVD_STATIC_OPTIONS(Matrix3d, ComputeFullU | ComputeFullV) svd3;
326  svd3.compute(M3); // just check we don't loop indefinitely
328 }
329 
330 template <typename MatrixType>
331 void svd_all_trivial_2x2(void (*cb)(const MatrixType&)) {
332  MatrixType M;
333  VectorXd value_set(3);
334  value_set << 0, 1, -1;
335  Array4i id(0, 0, 0, 0);
336  int k = 0;
337  do {
338  M << value_set(id(0)), value_set(id(1)), value_set(id(2)), value_set(id(3));
339 
340  cb(M);
341 
342  id(k)++;
343  if (id(k) >= value_set.size()) {
344  while (k < 3 && id(k) >= value_set.size()) id(++k)++;
345  id.head(k).setZero();
346  k = 0;
347  }
348 
349  } while ((id < int(value_set.size())).all());
350 }
351 
352 template <typename>
354  Vector3f v(3.f, 2.f, 1.f);
355  MatrixXf m = v.asDiagonal();
356 
357  internal::set_is_malloc_allowed(false);
358  VERIFY_RAISES_ASSERT(VectorXf tmp(10);)
359  SVD_DEFAULT(MatrixXf) svd;
360  internal::set_is_malloc_allowed(true);
361  svd.compute(m);
362  VERIFY_IS_APPROX(svd.singularValues(), v);
363  VERIFY_RAISES_ASSERT(svd.matrixU());
364  VERIFY_RAISES_ASSERT(svd.matrixV());
365 
366  SVD_STATIC_OPTIONS(MatrixXf, ComputeFullU | ComputeFullV) svd2(3, 3);
367  internal::set_is_malloc_allowed(false);
368  svd2.compute(m);
369  internal::set_is_malloc_allowed(true);
370  VERIFY_IS_APPROX(svd2.singularValues(), v);
371  VERIFY_IS_APPROX(svd2.matrixU(), Matrix3f::Identity());
372  VERIFY_IS_APPROX(svd2.matrixV(), Matrix3f::Identity());
373  internal::set_is_malloc_allowed(false);
374  svd2.compute(m);
375  internal::set_is_malloc_allowed(true);
376 }
377 
378 template <typename MatrixType, int QRPreconditioner = 0>
380  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime };
381 
382  typedef Matrix<typename MatrixType::Scalar, RowsAtCompileTime, 1> RhsType;
383  RhsType rhs = RhsType::Zero(input.rows());
384  MatrixType m(input.rows(), input.cols());
386 
387  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner) svd0;
388  VERIFY_RAISES_ASSERT((svd0.matrixU()));
389  VERIFY_RAISES_ASSERT((svd0.singularValues()));
390  VERIFY_RAISES_ASSERT((svd0.matrixV()));
391  VERIFY_RAISES_ASSERT((svd0.solve(rhs)));
392  VERIFY_RAISES_ASSERT((svd0.transpose().solve(rhs)));
393  VERIFY_RAISES_ASSERT((svd0.adjoint().solve(rhs)));
394 
395  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner) svd1(m);
396  VERIFY_RAISES_ASSERT((svd1.matrixU()));
397  VERIFY_RAISES_ASSERT((svd1.matrixV()));
398  VERIFY_RAISES_ASSERT((svd1.solve(rhs)));
399 
400  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU) svdFullU(m);
401  VERIFY_RAISES_ASSERT((svdFullU.matrixV()));
402  VERIFY_RAISES_ASSERT((svdFullU.solve(rhs)));
403  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullV) svdFullV(m);
404  VERIFY_RAISES_ASSERT((svdFullV.matrixU()));
405  VERIFY_RAISES_ASSERT((svdFullV.solve(rhs)));
406 }
407 
408 template <typename MatrixType, int QRPreconditioner = 0>
409 void svd_verify_assert(const MatrixType& input = MatrixType()) {
410  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime };
411  typedef Matrix<typename MatrixType::Scalar, RowsAtCompileTime, 1> RhsType;
412  RhsType rhs = RhsType::Zero(input.rows());
413  MatrixType m(input.rows(), input.cols());
415 
416  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU) svdThinU(m);
417  VERIFY_RAISES_ASSERT((svdThinU.matrixV()));
418  VERIFY_RAISES_ASSERT((svdThinU.solve(rhs)));
419  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinV) svdThinV(m);
420  VERIFY_RAISES_ASSERT((svdThinV.matrixU()));
421  VERIFY_RAISES_ASSERT((svdThinV.solve(rhs)));
422 
423  svd_verify_assert_full_only<MatrixType, QRPreconditioner>(m);
424 }
425 
426 template <typename MatrixType, int Options>
428  typedef SVD_STATIC_OPTIONS(MatrixType, Options) SVDType;
429 
430  enum {
431  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
432  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
433  DiagAtCompileTime = internal::min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime),
434  MatrixURowsAtCompileTime = SVDType::MatrixUType::RowsAtCompileTime,
435  MatrixUColsAtCompileTime = SVDType::MatrixUType::ColsAtCompileTime,
436  MatrixVRowsAtCompileTime = SVDType::MatrixVType::RowsAtCompileTime,
437  MatrixVColsAtCompileTime = SVDType::MatrixVType::ColsAtCompileTime
438  };
439 
440  SVDType staticSvd(m);
441 
442  VERIFY(MatrixURowsAtCompileTime == RowsAtCompileTime);
443  VERIFY(MatrixVRowsAtCompileTime == ColsAtCompileTime);
444  if (Options & ComputeThinU) VERIFY(MatrixUColsAtCompileTime == DiagAtCompileTime);
445  if (Options & ComputeFullU) VERIFY(MatrixUColsAtCompileTime == RowsAtCompileTime);
446  if (Options & ComputeThinV) VERIFY(MatrixVColsAtCompileTime == DiagAtCompileTime);
447  if (Options & ComputeFullV) VERIFY(MatrixVColsAtCompileTime == ColsAtCompileTime);
448 
449  if (Options & (ComputeThinU | ComputeFullU))
450  VERIFY(staticSvd.computeU());
451  else
452  VERIFY(!staticSvd.computeU());
453  if (Options & (ComputeThinV | ComputeFullV))
454  VERIFY(staticSvd.computeV());
455  else
456  VERIFY(!staticSvd.computeV());
457 
458  if (staticSvd.computeU()) VERIFY(staticSvd.matrixU().isUnitary());
459  if (staticSvd.computeV()) VERIFY(staticSvd.matrixV().isUnitary());
460 
461  if (staticSvd.computeU() && staticSvd.computeV()) {
462  svd_test_solvers(m, staticSvd);
463  svd_least_square<SVDType, MatrixType>(m);
464  // svd_min_norm generates non-square matrices so it can't be used with NoQRPreconditioner
465  if ((Options & internal::QRPreconditionerBits) != NoQRPreconditioner) svd_min_norm<MatrixType, Options>(m);
466  }
467 }
468 
469 template <typename MatrixType, int QRPreconditioner = 0>
470 void svd_thin_option_checks(const MatrixType& input) {
471  MatrixType m(input.rows(), input.cols());
473 
474  svd_compute_checks<MatrixType, QRPreconditioner>(m);
475  svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU>(m);
476  svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinV>(m);
477  svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU | ComputeThinV>(m);
478 
479  svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU | ComputeFullV>(m);
480  svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU | ComputeThinV>(m);
481 
482  typedef SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) FullSvdType;
483  FullSvdType fullSvd(m);
484  svd_check_full(m, fullSvd);
485  svd_compare_to_full<MatrixType, FullSvdType, QRPreconditioner | ComputeFullU | ComputeFullV>(m, fullSvd);
486 }
487 
488 template <typename MatrixType, int QRPreconditioner = 0>
490  MatrixType m(input.rows(), input.cols());
492  svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU>(m);
493  svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullV>(m);
494  svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV>(m);
495 
496  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) fullSvd(m);
497  svd_check_full(m, fullSvd);
498 }
499 
500 template <typename MatrixType, int QRPreconditioner = 0>
501 void svd_check_max_size_matrix(int initialRows, int initialCols) {
502  enum {
503  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
504  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
505  };
506 
507  int rows = MaxRowsAtCompileTime == Dynamic ? initialRows : (std::min)(initialRows, (int)MaxRowsAtCompileTime);
508  int cols = MaxColsAtCompileTime == Dynamic ? initialCols : (std::min)(initialCols, (int)MaxColsAtCompileTime);
509 
510  MatrixType m(rows, cols);
512  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU | ComputeThinV) thinSvd(m);
513  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU | ComputeFullV) mixedSvd1(m);
514  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeThinV) mixedSvd2(m);
515  SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) fullSvd(m);
516 
517  MatrixType n(MaxRowsAtCompileTime, MaxColsAtCompileTime);
519  thinSvd.compute(n);
520  mixedSvd1.compute(n);
521  mixedSvd2.compute(n);
522  fullSvd.compute(n);
523 
524  MatrixX<typename MatrixType::Scalar> dynamicMatrix(MaxRowsAtCompileTime + 1, MaxColsAtCompileTime + 1);
525 
526  VERIFY_RAISES_ASSERT(thinSvd.compute(dynamicMatrix));
527  VERIFY_RAISES_ASSERT(mixedSvd1.compute(dynamicMatrix));
528  VERIFY_RAISES_ASSERT(mixedSvd2.compute(dynamicMatrix));
529  VERIFY_RAISES_ASSERT(fullSvd.compute(dynamicMatrix));
530 }
531 
532 template <typename SvdType, typename MatrixType>
534  typedef typename MatrixType::Scalar Scalar;
535  Index rows = m.rows();
536 
537  enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime };
538 
539  typedef Matrix<Scalar, RowsAtCompileTime, 1> RhsType;
540  RhsType rhs(rows);
541  svd_fill_random(rhs);
542  SvdType svd;
543  VERIFY_RAISES_ASSERT(svd.matrixU())
544  VERIFY_RAISES_ASSERT(svd.singularValues())
545  VERIFY_RAISES_ASSERT(svd.matrixV())
546  VERIFY_RAISES_ASSERT(svd.solve(rhs))
547  VERIFY_RAISES_ASSERT(svd.transpose().solve(rhs))
548  VERIFY_RAISES_ASSERT(svd.adjoint().solve(rhs))
549 }
550 
551 #undef SVD_DEFAULT
552 #undef SVD_FOR_MIN_NORM
553 #undef SVD_STATIC_OPTIONS
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition: AnnoyingScalar.h:196
bool test_isMuchSmallerThan(const AnnoyingScalar &a, const AnnoyingScalar &b)
Definition: AnnoyingScalar.h:200
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
BiCGSTAB< SparseMatrix< double > > solver
Definition: BiCGSTAB_simple.cpp:5
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Array< double, 1, 3 > e(1./3., 0.5, 2.)
HouseholderQR< MatrixXf > qr(A)
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf, ComputeThinU|ComputeThinV > svd(m)
#define EIGEN_DONT_INLINE
Definition: Macros.h:853
MatrixType m2(n_dims)
RowMajorMatrixXf M3(M1)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
#define SVD_DEFAULT(M)
Definition: bdcsvd.cpp:20
#define SVD_STATIC_OPTIONS(M, O)
Definition: bdcsvd.cpp:22
#define SVD_FOR_MIN_NORM(M)
Definition: bdcsvd.cpp:21
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:50
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void conservativeResize(Index rows, Index cols)
Definition: PlainObjectBase.h:398
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
Definition: matrices.h:74
#define min(a, b)
Definition: datatypes.h:22
@ NoQRPreconditioner
Definition: Constants.h:423
@ InvalidInput
Definition: Constants.h:447
@ ComputeFullV
Definition: Constants.h:393
@ ComputeThinV
Definition: Constants.h:395
@ ComputeFullU
Definition: Constants.h:389
@ ComputeThinU
Definition: Constants.h:391
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
Scalar * y
Definition: level1_cplx_impl.h:128
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
char char char int int * k
Definition: level2_impl.h:374
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
#define VERIFY(a)
Definition: main.h:362
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
#define VERIFY_IS_UNITARY(a)
Definition: main.h:378
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
@ QRPreconditionerBits
Definition: SVDBase.h:27
constexpr int min_size_prefer_dynamic(A a, B b)
Definition: Meta.h:668
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
static int g_test_level
Definition: main.h:190
const int Dynamic
Definition: Constants.h:25
int sigma
Definition: calibrate.py:179
double Zero
Definition: pseudosolid_node_update_elements.cc:35
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43
list x
Definition: plotDoE.py:28
void svd_option_checks_full_only(const MatrixType &input)
Definition: svd_common.h:489
void svd_inf_nan()
Definition: svd_common.h:245
void svd_compute_checks(const MatrixType &m)
Definition: svd_common.h:427
void svd_all_trivial_2x2(void(*cb)(const MatrixType &))
Definition: svd_common.h:331
void svd_thin_option_checks(const MatrixType &input)
Definition: svd_common.h:470
EIGEN_DONT_INLINE Scalar zero()
Definition: svd_common.h:232
void svd_min_norm(const MatrixType &m)
Definition: svd_common.h:159
void svd_verify_assert_full_only(const MatrixType &input=MatrixType())
Definition: svd_common.h:379
void svd_underoverflow()
Definition: svd_common.h:284
void svd_verify_constructor_options_assert(const MatrixType &m)
Definition: svd_common.h:533
void svd_test_solvers(const MatrixType &m, const SolverType &solver)
Definition: svd_common.h:214
void svd_compare_to_full(const MatrixType &m, const SvdType &referenceSvd)
Definition: svd_common.h:56
EIGEN_DONT_INLINE T sub(T a, T b)
Definition: svd_common.h:238
void svd_least_square(const MatrixType &m)
Definition: svd_common.h:97
void svd_check_max_size_matrix(int initialRows, int initialCols)
Definition: svd_common.h:501
void svd_preallocate()
Definition: svd_common.h:353
void svd_check_full(const MatrixType &m, const SvdType &svd)
Definition: svd_common.h:29
void svd_verify_assert(const MatrixType &input=MatrixType())
Definition: svd_common.h:409
void svd_fill_random(MatrixType &m, int Option=0)
Definition: svd_fill.h:27