svd_fill.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) 2014-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 template <typename T>
11 Array<T, 4, 1> four_denorms();
12 
13 template <>
14 Array4f four_denorms() {
15  return Array4f(5.60844e-39f, -5.60844e-39f, 4.94e-44f, -4.94e-44f);
16 }
17 template <>
18 Array4d four_denorms() {
19  return Array4d(5.60844e-313, -5.60844e-313, 4.94e-324, -4.94e-324);
20 }
21 template <typename T>
22 Array<T, 4, 1> four_denorms() {
23  return four_denorms<double>().cast<T>();
24 }
25 
26 template <typename MatrixType>
27 void svd_fill_random(MatrixType &m, int Option = 0) {
28  using std::pow;
29  typedef typename MatrixType::Scalar Scalar;
30  typedef typename MatrixType::RealScalar RealScalar;
31  Index diagSize = (std::min)(m.rows(), m.cols());
32  RealScalar s = std::numeric_limits<RealScalar>::max_exponent10 / 4;
33  s = internal::random<RealScalar>(1, s);
34  Matrix<RealScalar, Dynamic, 1> d = Matrix<RealScalar, Dynamic, 1>::Random(diagSize);
35  for (Index k = 0; k < diagSize; ++k) d(k) = d(k) * pow(RealScalar(10), internal::random<RealScalar>(-s, s));
36 
37  bool dup = internal::random<int>(0, 10) < 3;
38  bool unit_uv =
39  internal::random<int>(0, 10) < (dup ? 7 : 3); // if we duplicate some diagonal entries, then increase the chance
40  // to preserve them using unitary U and V factors
41 
42  // duplicate some singular values
43  if (dup) {
44  Index n = internal::random<Index>(0, d.size() - 1);
45  for (Index i = 0; i < n; ++i)
46  d(internal::random<Index>(0, d.size() - 1)) = d(internal::random<Index>(0, d.size() - 1));
47  }
48 
49  Matrix<Scalar, Dynamic, Dynamic> U(m.rows(), diagSize);
50  Matrix<Scalar, Dynamic, Dynamic> VT(diagSize, m.cols());
51  if (unit_uv) {
52  // in very rare cases let's try with a pure diagonal matrix
53  if (internal::random<int>(0, 10) < 1) {
54  U.setIdentity();
55  VT.setIdentity();
56  } else {
57  createRandomPIMatrixOfRank(diagSize, U.rows(), U.cols(), U);
58  createRandomPIMatrixOfRank(diagSize, VT.rows(), VT.cols(), VT);
59  }
60  } else {
61  U.setRandom();
62  VT.setRandom();
63  }
64 
65  Matrix<Scalar, Dynamic, 1> samples(9);
66  samples << Scalar(0), four_denorms<RealScalar>(), -RealScalar(1) / NumTraits<RealScalar>::highest(),
67  RealScalar(1) / NumTraits<RealScalar>::highest(), (std::numeric_limits<RealScalar>::min)(),
69 
70  if (Option == Symmetric) {
71  m = U * d.asDiagonal() * U.transpose();
72 
73  // randomly nullify some rows/columns
74  {
75  Index count = internal::random<Index>(-diagSize, diagSize);
76  for (Index k = 0; k < count; ++k) {
77  Index i = internal::random<Index>(0, diagSize - 1);
78  m.row(i).setZero();
79  m.col(i).setZero();
80  }
81  if (count < 0)
82  // (partly) cancel some coeffs
83  if (!(dup && unit_uv)) {
84  Index n = internal::random<Index>(0, m.size() - 1);
85  for (Index k = 0; k < n; ++k) {
86  Index i = internal::random<Index>(0, m.rows() - 1);
87  Index j = internal::random<Index>(0, m.cols() - 1);
88  m(j, i) = m(i, j) = samples(internal::random<Index>(0, samples.size() - 1));
90  *(&numext::real_ref(m(j, i)) + 1) = *(&numext::real_ref(m(i, j)) + 1) =
91  samples.real()(internal::random<Index>(0, samples.size() - 1));
92  }
93  }
94  }
95  } else {
96  m = U * d.asDiagonal() * VT;
97  // (partly) cancel some coeffs
98  if (!(dup && unit_uv)) {
99  Index n = internal::random<Index>(0, m.size() - 1);
100  for (Index k = 0; k < n; ++k) {
101  Index i = internal::random<Index>(0, m.rows() - 1);
102  Index j = internal::random<Index>(0, m.cols() - 1);
103  m(i, j) = samples(internal::random<Index>(0, samples.size() - 1));
105  *(&numext::real_ref(m(i, j)) + 1) = samples.real()(internal::random<Index>(0, samples.size() - 1));
106  }
107  }
108  }
109 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
DataMatrix samples
Definition: LLT_solve.cpp:3
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137
#define min(a, b)
Definition: datatypes.h:22
@ Symmetric
Definition: Constants.h:229
RealScalar s
Definition: level1_cplx_impl.h:130
int * m
Definition: level2_cplx_impl.h:294
char char char int int * k
Definition: level2_impl.h:374
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
EIGEN_DEVICE_FUNC internal::add_const_on_value_type_t< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar)> real_ref(const Scalar &x)
Definition: MathFunctions.h:1051
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: random_matrix_helper.h:60
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53
@ IsComplex
Definition: NumTraits.h:176
void svd_fill_random(MatrixType &m, int Option=0)
Definition: svd_fill.h:27
Array< T, 4, 1 > four_denorms()
Definition: svd_fill.h:22
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2