basicstuff.cpp File Reference
#include "main.h"
#include "random_without_cast_overflow.h"

Classes

struct  casting_test< SrcScalar, TgtScalar >
 
struct  casting_test_runner< SrcScalar, EnableIf >
 
struct  casting_test_runner< SrcScalar, std::enable_if_t<(NumTraits< SrcScalar >::IsComplex)> >
 

Functions

template<typename MatrixType >
std::enable_if_t<(MatrixType::RowsAtCompileTime==1||MatrixType::ColsAtCompileTime==1), void > check_index (const MatrixType &m)
 
template<typename MatrixType >
std::enable_if_t<!(MatrixType::RowsAtCompileTime==1||MatrixType::ColsAtCompileTime==1), void > check_index (const MatrixType &)
 
template<typename MatrixType >
void basicStuff (const MatrixType &m)
 
template<typename MatrixType >
void basicStuffComplex (const MatrixType &m)
 
void casting_all ()
 
template<typename Scalar >
void fixedSizeMatrixConstruction ()
 
 EIGEN_DECLARE_TEST (basicstuff)
 

Function Documentation

◆ basicStuff()

template<typename MatrixType >
void basicStuff ( const MatrixType m)
25  {
26  typedef typename MatrixType::Scalar Scalar;
29 
30  Index rows = m.rows();
31  Index cols = m.cols();
32 
33  // this test relies a lot on Random.h, and there's not much more that we can do
34  // to test it, hence I consider that we will have tested Random.h
35  MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols),
36  mzero = MatrixType::Zero(rows, cols),
38  VectorType v1 = VectorType::Random(rows), vzero = VectorType::Zero(rows);
39  SquareMatrixType sm1 = SquareMatrixType::Random(rows, rows), sm2(rows, rows);
40 
41  Scalar x = 0;
42  while (x == Scalar(0)) x = internal::random<Scalar>();
43 
44  Index r = internal::random<Index>(0, rows - 1), c = internal::random<Index>(0, cols - 1);
45 
46  m1.coeffRef(r, c) = x;
47  VERIFY_IS_APPROX(x, m1.coeff(r, c));
48  m1(r, c) = x;
49  VERIFY_IS_APPROX(x, m1(r, c));
50  v1.coeffRef(r) = x;
51  VERIFY_IS_APPROX(x, v1.coeff(r));
52  v1(r) = x;
54  v1[r] = x;
56 
57  // test fetching with various index types.
58  Index r1 = internal::random<Index>(0, numext::mini(Index(127), rows - 1));
59  x = v1(static_cast<char>(r1));
60  x = v1(static_cast<signed char>(r1));
61  x = v1(static_cast<unsigned char>(r1));
62  x = v1(static_cast<signed short>(r1));
63  x = v1(static_cast<unsigned short>(r1));
64  x = v1(static_cast<signed int>(r1));
65  x = v1(static_cast<unsigned int>(r1));
66  x = v1(static_cast<signed long>(r1));
67  x = v1(static_cast<unsigned long>(r1));
68  if (sizeof(Index) >= sizeof(long long int)) x = v1(static_cast<long long int>(r1));
69  if (sizeof(Index) >= sizeof(unsigned long long int)) x = v1(static_cast<unsigned long long int>(r1));
70 
74  VERIFY_IS_MUCH_SMALLER_THAN(vzero, v1.squaredNorm());
76  VERIFY_IS_APPROX(vzero, v1 - v1);
81  VERIFY_IS_APPROX(mzero, m1 - m1);
82 
83  // always test operator() on each read-only expression class,
84  // in order to check const-qualifiers.
85  // indeed, if an expression class (here Zero) is meant to be read-only,
86  // hence has no _write() method, the corresponding MatrixBase method (here zero())
87  // should return a const-qualified object so that it is the const-qualified
88  // operator() that gets called, which in turn calls _read().
90 
91  // now test copying a row-vector into a (column-)vector and conversely.
92  square.col(r) = square.row(r).eval();
95  rv = square.row(r);
96  cv = square.col(r);
97 
98  VERIFY_IS_APPROX(rv, cv.transpose());
99 
100  if (cols != 1 && rows != 1 && MatrixType::SizeAtCompileTime != Dynamic) {
101  VERIFY_RAISES_ASSERT(m1 = (m2.block(0, 0, rows - 1, cols - 1)));
102  }
103 
104  if (cols != 1 && rows != 1) {
105  check_index(m1);
106  }
107 
108  VERIFY_IS_APPROX(m3 = m1, m1);
109  MatrixType m4;
110  VERIFY_IS_APPROX(m4 = m1, m1);
111 
112  m3.real() = m1.real();
113  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
114  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
115 
116  // check == / != operators
117  VERIFY(m1 == m1);
118  VERIFY(m1 != m2);
119  VERIFY(!(m1 == m2));
120  VERIFY(!(m1 != m1));
121  m1 = m2;
122  VERIFY(m1 == m2);
123  VERIFY(!(m1 != m2));
124 
125  // check automatic transposition
126  sm2.setZero();
127  for (Index i = 0; i < rows; ++i) sm2.col(i) = sm1.row(i);
128  VERIFY_IS_APPROX(sm2, sm1.transpose());
129 
130  sm2.setZero();
131  for (Index i = 0; i < rows; ++i) sm2.col(i).noalias() = sm1.row(i);
132  VERIFY_IS_APPROX(sm2, sm1.transpose());
133 
134  sm2.setZero();
135  for (Index i = 0; i < rows; ++i) sm2.col(i).noalias() += sm1.row(i);
136  VERIFY_IS_APPROX(sm2, sm1.transpose());
137 
138  sm2.setZero();
139  for (Index i = 0; i < rows; ++i) sm2.col(i).noalias() -= sm1.row(i);
140  VERIFY_IS_APPROX(sm2, -sm1.transpose());
141 
142  // check ternary usage
143  {
144  bool b = internal::random<int>(0, 10) > 5;
145  m3 = b ? m1 : m2;
146  if (b)
147  VERIFY_IS_APPROX(m3, m1);
148  else
149  VERIFY_IS_APPROX(m3, m2);
150  m3 = b ? -m1 : m2;
151  if (b)
152  VERIFY_IS_APPROX(m3, -m1);
153  else
154  VERIFY_IS_APPROX(m3, m2);
155  m3 = b ? m1 : -m2;
156  if (b)
157  VERIFY_IS_APPROX(m3, m1);
158  else
159  VERIFY_IS_APPROX(m3, -m2);
160  }
161 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
std::enable_if_t<(MatrixType::RowsAtCompileTime==1||MatrixType::ColsAtCompileTime==1), void > check_index(const MatrixType &m)
Definition: basicstuff.cpp:14
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
float real
Definition: datatypes.h:10
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
#define VERIFY_IS_NOT_APPROX(a, b)
Definition: integer_types.cpp:15
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b)
Definition: main.h:372
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
squared absolute sa ArrayBase::abs2 DOXCOMMA MatrixBase::cwiseAbs2 square(power 2)
const int Dynamic
Definition: Constants.h:25
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
double Zero
Definition: pseudosolid_node_update_elements.cc:35
list x
Definition: plotDoE.py:28
Definition: fft_test_shared.h:66

References b, calibrate::c, check_index(), cols, Eigen::Dynamic, i, m, m1, m2(), Eigen::numext::mini(), UniformPSDSelfTest::r, rows, Eigen::square(), v1(), VERIFY, VERIFY_IS_APPROX, VERIFY_IS_MUCH_SMALLER_THAN, VERIFY_IS_NOT_APPROX, VERIFY_IS_NOT_MUCH_SMALLER_THAN, VERIFY_RAISES_ASSERT, plotDoE::x, and oomph::PseudoSolidHelper::Zero.

Referenced by EIGEN_DECLARE_TEST().

◆ basicStuffComplex()

template<typename MatrixType >
void basicStuffComplex ( const MatrixType m)
164  {
165  typedef typename MatrixType::Scalar Scalar;
166  typedef typename NumTraits<Scalar>::Real RealScalar;
168 
169  Index rows = m.rows();
170  Index cols = m.cols();
171 
172  Scalar s1 = internal::random<Scalar>(), s2 = internal::random<Scalar>();
173 
176  numext::real_ref(s1) = numext::real(s2);
177  numext::imag_ref(s1) = numext::imag(s2);
179  // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
180 
181  RealMatrixType rm1 = RealMatrixType::Random(rows, cols), rm2 = RealMatrixType::Random(rows, cols);
182  MatrixType cm(rows, cols);
183  cm.real() = rm1;
184  cm.imag() = rm2;
185  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
186  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
187  rm1.setZero();
188  rm2.setZero();
189  rm1 = cm.real();
190  rm2 = cm.imag();
191  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
192  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
193  cm.real().setZero();
194  VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
195  VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
196 }
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1923
EIGEN_DEVICE_FUNC internal::add_const_on_value_type_t< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar)> imag_ref(const Scalar &x)
Definition: MathFunctions.h:1072
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
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References cols, imag(), Eigen::numext::imag_ref(), Eigen::internal::isApprox(), m, Eigen::numext::real_ref(), rows, VERIFY, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ casting_all()

void casting_all ( )
248  {
264 }
Definition: basicstuff.cpp:217
static void run()
Definition: basicstuff.cpp:218
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References casting_test_runner< SrcScalar, EnableIf >::run(), and run().

Referenced by EIGEN_DECLARE_TEST().

◆ check_index() [1/2]

template<typename MatrixType >
std::enable_if_t<!(MatrixType::RowsAtCompileTime == 1 || MatrixType::ColsAtCompileTime == 1), void> check_index ( const MatrixType )
22  {}

◆ check_index() [2/2]

template<typename MatrixType >
std::enable_if_t<(MatrixType::RowsAtCompileTime == 1 || MatrixType::ColsAtCompileTime == 1), void> check_index ( const MatrixType m)
15  {
17  VERIFY_RAISES_ASSERT((m + m)[0]);
18 }

References m, and VERIFY_RAISES_ASSERT.

Referenced by basicStuff().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( basicstuff  )
325  {
326  for (int i = 0; i < g_repeat; i++) {
328  CALL_SUBTEST_2(basicStuff(Matrix4d()));
330  MatrixXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
332  MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
334  MatrixXcd(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
337  internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
339 
341  MatrixXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
343  MatrixXcd(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
344  }
345 
346  CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
347  CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
348  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
349  CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
350  CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
351  CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
352 }
void basicStuffComplex(const MatrixType &m)
Definition: basicstuff.cpp:164
void casting_all()
Definition: basicstuff.cpp:248
void basicStuff(const MatrixType &m)
Definition: basicstuff.cpp:25
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References basicStuff(), basicStuffComplex(), CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, casting_all(), EIGEN_TEST_MAX_SIZE, Eigen::g_repeat, and i.

◆ fixedSizeMatrixConstruction()

template<typename Scalar >
void fixedSizeMatrixConstruction ( )
267  {
268  Scalar raw[4];
269  for (int k = 0; k < 4; ++k) raw[k] = internal::random<Scalar>();
270 
271  {
272  Matrix<Scalar, 4, 1> m(raw);
273  Array<Scalar, 4, 1> a(raw);
274  for (int k = 0; k < 4; ++k) VERIFY(m(k) == raw[k]);
275  for (int k = 0; k < 4; ++k) VERIFY(a(k) == raw[k]);
276  VERIFY_IS_EQUAL(m, (Matrix<Scalar, 4, 1>(raw[0], raw[1], raw[2], raw[3])));
277  VERIFY((a == (Array<Scalar, 4, 1>(raw[0], raw[1], raw[2], raw[3]))).all());
278  }
279  {
280  Matrix<Scalar, 3, 1> m(raw);
281  Array<Scalar, 3, 1> a(raw);
282  for (int k = 0; k < 3; ++k) VERIFY(m(k) == raw[k]);
283  for (int k = 0; k < 3; ++k) VERIFY(a(k) == raw[k]);
284  VERIFY_IS_EQUAL(m, (Matrix<Scalar, 3, 1>(raw[0], raw[1], raw[2])));
285  VERIFY((a == Array<Scalar, 3, 1>(raw[0], raw[1], raw[2])).all());
286  }
287  {
288  Matrix<Scalar, 2, 1> m(raw), m2((DenseIndex(raw[0])), (DenseIndex(raw[1])));
289  Array<Scalar, 2, 1> a(raw), a2((DenseIndex(raw[0])), (DenseIndex(raw[1])));
290  for (int k = 0; k < 2; ++k) VERIFY(m(k) == raw[k]);
291  for (int k = 0; k < 2; ++k) VERIFY(a(k) == raw[k]);
292  VERIFY_IS_EQUAL(m, (Matrix<Scalar, 2, 1>(raw[0], raw[1])));
293  VERIFY((a == Array<Scalar, 2, 1>(raw[0], raw[1])).all());
294  for (int k = 0; k < 2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
295  for (int k = 0; k < 2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
296  }
297  {
298  Matrix<Scalar, 1, 2> m(raw), m2((DenseIndex(raw[0])), (DenseIndex(raw[1]))), m3((int(raw[0])), (int(raw[1]))),
299  m4((float(raw[0])), (float(raw[1])));
300  Array<Scalar, 1, 2> a(raw), a2((DenseIndex(raw[0])), (DenseIndex(raw[1])));
301  for (int k = 0; k < 2; ++k) VERIFY(m(k) == raw[k]);
302  for (int k = 0; k < 2; ++k) VERIFY(a(k) == raw[k]);
303  VERIFY_IS_EQUAL(m, (Matrix<Scalar, 1, 2>(raw[0], raw[1])));
304  VERIFY((a == Array<Scalar, 1, 2>(raw[0], raw[1])).all());
305  for (int k = 0; k < 2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
306  for (int k = 0; k < 2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
307  for (int k = 0; k < 2; ++k) VERIFY(m3(k) == int(raw[k]));
308  for (int k = 0; k < 2; ++k) VERIFY((m4(k)) == Scalar(float(raw[k])));
309  }
310  {
311  Matrix<Scalar, 1, 1> m(raw), m1(raw[0]), m2((DenseIndex(raw[0]))), m3((int(raw[0])));
312  Array<Scalar, 1, 1> a(raw), a1(raw[0]), a2((DenseIndex(raw[0])));
313  VERIFY(m(0) == raw[0]);
314  VERIFY(a(0) == raw[0]);
315  VERIFY(m1(0) == raw[0]);
316  VERIFY(a1(0) == raw[0]);
317  VERIFY(m2(0) == DenseIndex(raw[0]));
318  VERIFY(a2(0) == DenseIndex(raw[0]));
319  VERIFY(m3(0) == int(raw[0]));
321  VERIFY((a == Array<Scalar, 1, 1>(raw[0])).all());
322  }
323 }
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:48
static constexpr Eigen::internal::all_t all
Definition: IndexedViewHelper.h:86
const Scalar * a
Definition: level2_cplx_impl.h:32
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75

References a, Eigen::placeholders::all, k, m, m1, m2(), VERIFY, and VERIFY_IS_EQUAL.