svd_fill.h File Reference

Go to the source code of this file.

Functions

template<typename T >
Array< T, 4, 1 > four_denorms ()
 
template<>
Array4f four_denorms ()
 
template<typename MatrixType >
void svd_fill_random (MatrixType &m, int Option=0)
 

Function Documentation

◆ four_denorms() [1/2]

template<>
Array4d four_denorms ( )
14  {
15  return Array4f(5.60844e-39f, -5.60844e-39f, 4.94e-44f, -4.94e-44f);
16 }

◆ four_denorms() [2/2]

template<typename T >
Array< T, 4, 1 > four_denorms ( )
22  {
23  return four_denorms<double>().cast<T>();
24 }

◆ svd_fill_random()

template<typename MatrixType >
void svd_fill_random ( MatrixType m,
int  Option = 0 
)
27  {
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
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
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::createRandomPIMatrixOfRank(), i, Eigen::GenericNumTraits< T >::IsComplex, j, k, m, min, n, Eigen::bfloat16_impl::pow(), Eigen::ArrayBase< Derived >::pow(), Eigen::numext::real_ref(), s, samples, Eigen::Symmetric, and RachelsAdvectionDiffusion::U.

Referenced by EIGEN_DECLARE_TEST(), jacobisvd_full_options(), jacobisvd_thin_options(), jacobisvd_verify_assert(), jacobisvd_verify_inputs(), selfadjointeigensolver(), svd_check_max_size_matrix(), svd_option_checks_full_only(), svd_thin_option_checks(), svd_verify_assert(), svd_verify_assert_full_only(), and svd_verify_constructor_options_assert().