nullary.cpp File Reference
#include "main.h"

Functions

template<typename MatrixType >
bool equalsIdentity (const MatrixType &A)
 
template<typename VectorType >
void check_extremity_accuracy (const VectorType &v, const typename VectorType::Scalar &low, const typename VectorType::Scalar &high)
 
template<typename VectorType >
void testVectorType (const VectorType &base)
 
template<typename MatrixType >
void testMatrixType (const MatrixType &m)
 
template<int >
void bug79 ()
 
template<int >
void bug1630 ()
 
template<int >
void nullary_overflow ()
 
template<int >
void nullary_internal_logic ()
 
 EIGEN_DECLARE_TEST (nullary)
 

Function Documentation

◆ bug1630()

template<int >
void bug1630 ( )
238  {
239  Array4d x4 = Array4d::LinSpaced(0.0, 1.0);
240  Array3d x3(Array4d::LinSpaced(0.0, 1.0).head(3));
241  VERIFY_IS_APPROX(x4.head(3), x3);
242 }
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13

References VERIFY_IS_APPROX.

◆ bug79()

template<int >
void bug79 ( )
231  {
232  // Assignment of a RowVectorXd to a MatrixXd (regression test for bug #79).
233  VERIFY((MatrixXd(RowVectorXd::LinSpaced(3, 0, 1)) - RowVector3d(0, 0.5, 1)).norm() <
235 }
#define VERIFY(a)
Definition: main.h:362
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43

References oomph::SarahBL::epsilon, and VERIFY.

◆ check_extremity_accuracy()

template<typename VectorType >
void check_extremity_accuracy ( const VectorType v,
const typename VectorType::Scalar low,
const typename VectorType::Scalar high 
)
33  {
34  typedef typename VectorType::Scalar Scalar;
35  typedef typename VectorType::RealScalar RealScalar;
36 
39  Index size = v.size();
40 
41  if (size < 20) return;
42 
43  for (int i = 0; i < size; ++i) {
44  if (i < 5 || i > size - 6) {
45  Scalar ref =
46  (low * RealScalar(size - i - 1)) / RealScalar(size - 1) + (high * RealScalar(i)) / RealScalar(size - 1);
47  if (std::abs(ref) > 1) {
48  if (!internal::isApprox(v(i), ref, prec))
49  std::cout << v(i) << " != " << ref << " ; relative error: " << std::abs((v(i) - ref) / ref)
50  << " ; required precision: " << prec << " ; range: " << low << "," << high << " ; i: " << i
51  << "\n";
53  v(i),
54  (low * RealScalar(size - i - 1)) / RealScalar(size - 1) + (high * RealScalar(i)) / RealScalar(size - 1),
55  prec));
56  }
57  }
58  }
59 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
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
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
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References abs(), i, Eigen::internal::isApprox(), size, v, Eigen::value, and VERIFY.

Referenced by testVectorType().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( nullary  )
300  {
301  CALL_SUBTEST_1(testMatrixType(Matrix2d()));
302  CALL_SUBTEST_2(testMatrixType(MatrixXcf(internal::random<int>(1, 300), internal::random<int>(1, 300))));
303  CALL_SUBTEST_3(testMatrixType(MatrixXf(internal::random<int>(1, 300), internal::random<int>(1, 300))));
304 
305  for (int i = 0; i < g_repeat * 10; i++) {
306  CALL_SUBTEST_3(testVectorType(VectorXcd(internal::random<int>(1, 30000))));
307  CALL_SUBTEST_4(testVectorType(VectorXd(internal::random<int>(1, 30000))));
308  CALL_SUBTEST_5(testVectorType(Vector4d())); // regression test for bug 232
309  CALL_SUBTEST_6(testVectorType(Vector3d()));
310  CALL_SUBTEST_7(testVectorType(VectorXf(internal::random<int>(1, 30000))));
311  CALL_SUBTEST_8(testVectorType(Vector3f()));
312  CALL_SUBTEST_8(testVectorType(Vector4f()));
315 
316  CALL_SUBTEST_9(testVectorType(VectorXi(internal::random<int>(1, 10))));
317  CALL_SUBTEST_9(testVectorType(VectorXi(internal::random<int>(9, 300))));
319  }
320 
321  CALL_SUBTEST_6(bug79<0>());
322  CALL_SUBTEST_6(bug1630<0>());
323  CALL_SUBTEST_9(nullary_overflow<0>());
324  CALL_SUBTEST_10(nullary_internal_logic<0>());
325 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
static int g_repeat
Definition: main.h:191
void testVectorType(const VectorType &base)
Definition: nullary.cpp:62
void testMatrixType(const MatrixType &m)
Definition: nullary.cpp:205
#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
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58

References CALL_SUBTEST_1, CALL_SUBTEST_10, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, CALL_SUBTEST_9, Eigen::g_repeat, i, testMatrixType(), and testVectorType().

◆ equalsIdentity()

template<typename MatrixType >
bool equalsIdentity ( const MatrixType A)
14  {
15  bool offDiagOK = true;
16  for (Index i = 0; i < A.rows(); ++i) {
17  for (Index j = i + 1; j < A.cols(); ++j) {
18  offDiagOK = offDiagOK && numext::is_exactly_zero(A(i, j));
19  }
20  }
21  for (Index i = 0; i < A.rows(); ++i) {
22  for (Index j = 0; j < (std::min)(i, A.cols()); ++j) {
23  offDiagOK = offDiagOK && numext::is_exactly_zero(A(i, j));
24  }
25  }
26 
27  bool diagOK = (A.diagonal().array() == 1).all();
28  return offDiagOK && diagOK;
29 }
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
#define min(a, b)
Definition: datatypes.h:22
static constexpr Eigen::internal::all_t all
Definition: IndexedViewHelper.h:86
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool is_exactly_zero(const X &x)
Definition: Meta.h:592
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::placeholders::all, Eigen::PlainObjectBase< Derived >::cols(), i, Eigen::numext::is_exactly_zero(), j, min, and Eigen::PlainObjectBase< Derived >::rows().

Referenced by testMatrixType().

◆ nullary_internal_logic()

template<int >
void nullary_internal_logic ( )
257  {
258  // check some internal logic
259  VERIFY((internal::has_nullary_operator<internal::scalar_constant_op<double> >::value));
260  VERIFY((!internal::has_unary_operator<internal::scalar_constant_op<double> >::value));
261  VERIFY((!internal::has_binary_operator<internal::scalar_constant_op<double> >::value));
262  VERIFY((internal::functor_has_linear_access<internal::scalar_constant_op<double> >::ret));
263 
264  VERIFY((!internal::has_nullary_operator<internal::scalar_identity_op<double> >::value));
265  VERIFY((!internal::has_unary_operator<internal::scalar_identity_op<double> >::value));
266  VERIFY((internal::has_binary_operator<internal::scalar_identity_op<double> >::value));
267  VERIFY((!internal::functor_has_linear_access<internal::scalar_identity_op<double> >::ret));
268 
269  VERIFY((!internal::has_nullary_operator<internal::linspaced_op<float> >::value));
270  VERIFY((internal::has_unary_operator<internal::linspaced_op<float> >::value));
271  VERIFY((!internal::has_binary_operator<internal::linspaced_op<float> >::value));
272  VERIFY((internal::functor_has_linear_access<internal::linspaced_op<float> >::ret));
273 
274  // Regression unit test for a weird MSVC bug.
275  // Search "nullary_wrapper_workaround_msvc" in CoreEvaluators.h for the details.
276  // See also traits<Ref>::match.
277  {
278  MatrixXf A = MatrixXf::Random(3, 3);
279  Ref<const MatrixXf> R = 2.0 * A;
280  VERIFY_IS_APPROX(R, A + A);
281 
282  Ref<const MatrixXf> R1 = MatrixXf::Random(3, 3) + A;
283 
284  VectorXi V = VectorXi::Random(3);
285  Ref<const VectorXi> R2 = VectorXi::LinSpaced(3, 1, 3) + V;
286  VERIFY_IS_APPROX(R2, V + Vector3i(1, 2, 3));
287 
288  VERIFY((internal::has_nullary_operator<internal::scalar_constant_op<float> >::value));
289  VERIFY((!internal::has_unary_operator<internal::scalar_constant_op<float> >::value));
290  VERIFY((!internal::has_binary_operator<internal::scalar_constant_op<float> >::value));
291  VERIFY((internal::functor_has_linear_access<internal::scalar_constant_op<float> >::ret));
292 
293  VERIFY((!internal::has_nullary_operator<internal::linspaced_op<int> >::value));
294  VERIFY((internal::has_unary_operator<internal::linspaced_op<int> >::value));
295  VERIFY((!internal::has_binary_operator<internal::linspaced_op<int> >::value));
296  VERIFY((internal::functor_has_linear_access<internal::linspaced_op<int> >::ret));
297  }
298 }
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
@ R
Definition: StatisticsVector.h:21
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:264
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43

References R, ret, V, Eigen::value, VERIFY, and VERIFY_IS_APPROX.

◆ nullary_overflow()

template<int >
void nullary_overflow ( )
245  {
246  // Check possible overflow issue
247  int n = 60000;
248  ArrayXi a1(n), a2(n), a_ref(n);
249  a1.setLinSpaced(n, 0, n - 1);
250  a2.setEqualSpaced(n, 0, 1);
251  for (int i = 0; i < n; ++i) a_ref(i) = i;
252  VERIFY_IS_APPROX(a1, a_ref);
253  VERIFY_IS_APPROX(a2, a_ref);
254 }
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11

References i, n, and VERIFY_IS_APPROX.

◆ testMatrixType()

template<typename MatrixType >
void testMatrixType ( const MatrixType m)
205  {
206  using std::abs;
207  const Index rows = m.rows();
208  const Index cols = m.cols();
209  typedef typename MatrixType::Scalar Scalar;
210  typedef typename MatrixType::RealScalar RealScalar;
211 
212  Scalar s1;
213  do {
214  s1 = internal::random<Scalar>();
215  } while (abs(s1) < RealScalar(1e-5) && (!NumTraits<Scalar>::IsInteger));
216 
217  MatrixType A;
218  A.setIdentity(rows, cols);
220  VERIFY(equalsIdentity(MatrixType::Identity(rows, cols)));
221 
222  A = MatrixType::Constant(rows, cols, s1);
223  Index i = internal::random<Index>(0, rows - 1);
224  Index j = internal::random<Index>(0, cols - 1);
225  VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, s1)(i, j), s1);
226  VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, s1).coeff(i, j), s1);
227  VERIFY_IS_APPROX(A(i, j), s1);
228 }
Array< double, 1, 3 > e(1./3., 0.5, 2.)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
int * m
Definition: level2_cplx_impl.h:294
bool equalsIdentity(const MatrixType &A)
Definition: nullary.cpp:14

References abs(), cols, e(), equalsIdentity(), i, j, m, rows, VERIFY, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ testVectorType()

template<typename VectorType >
void testVectorType ( const VectorType base)
62  {
63  typedef typename VectorType::Scalar Scalar;
64  typedef typename VectorType::RealScalar RealScalar;
65 
66  const Index size = base.size();
67 
68  Scalar high = internal::random<Scalar>(-500, 500);
69  Scalar low = (size == 1 ? high : internal::random<Scalar>(-500, 500));
70  if (numext::real(low) > numext::real(high)) std::swap(low, high);
71 
72  // check low==high
73  if (internal::random<float>(0.f, 1.f) < 0.05f) low = high;
74  // check abs(low) >> abs(high)
75  else if (size > 2 && std::numeric_limits<RealScalar>::max_exponent10 > 0 && internal::random<float>(0.f, 1.f) < 0.1f)
76  low = -internal::random<Scalar>(1, 2) *
77  RealScalar(std::pow(RealScalar(10), std::numeric_limits<RealScalar>::max_exponent10 / 2));
78 
79  const Scalar step = ((size == 1) ? 1 : (high - low) / RealScalar(size - 1));
80 
81  // check whether the result yields what we expect it to do
82  VectorType m(base), o(base);
83  m.setLinSpaced(size, low, high);
84  o.setEqualSpaced(size, low, step);
85 
87  VectorType n(size);
88  for (int i = 0; i < size; ++i) n(i) = low + RealScalar(i) * step;
90  VERIFY_IS_APPROX(n, o);
91 
93  }
94 
95  RealScalar range_length = numext::real(high - low);
96  if ((!NumTraits<Scalar>::IsInteger) || (range_length >= size && (Index(range_length) % (size - 1)) == 0) ||
97  (Index(range_length + 1) < size && (size % Index(range_length + 1)) == 0)) {
98  VectorType n(size);
99  if ((!NumTraits<Scalar>::IsInteger) || (range_length >= size))
100  for (int i = 0; i < size; ++i) n(i) = size == 1 ? low : (low + ((high - low) * Scalar(i)) / RealScalar(size - 1));
101  else
102  for (int i = 0; i < size; ++i)
103  n(i) = size == 1 ? low : low + Scalar((double(range_length + 1) * double(i)) / double(size));
104  VERIFY_IS_APPROX(m, n);
105 
106  // random access version
107  m = VectorType::LinSpaced(size, low, high);
108  VERIFY_IS_APPROX(m, n);
109  VERIFY(internal::isApprox(m(m.size() - 1), high));
110  VERIFY(size == 1 || internal::isApprox(m(0), low));
111  VERIFY_IS_EQUAL(m(m.size() - 1), high);
113  }
114 
115  VERIFY(numext::real(m(m.size() - 1)) <= numext::real(high));
116  VERIFY((m.array().real() <= numext::real(high)).all());
117  VERIFY((m.array().real() >= numext::real(low)).all());
118 
119  VERIFY(numext::real(m(m.size() - 1)) >= numext::real(low));
120  if (size >= 1) {
121  VERIFY(internal::isApprox(m(0), low));
122  VERIFY_IS_EQUAL(m(0), low);
123  }
124 
125  // check whether everything works with row and col major vectors
126  Matrix<Scalar, Dynamic, 1> row_vector(size);
127  Matrix<Scalar, 1, Dynamic> col_vector(size);
128  row_vector.setLinSpaced(size, low, high);
129  col_vector.setLinSpaced(size, low, high);
130  // when using the extended precision (e.g., FPU) the relative error might exceed 1 bit
131  // when computing the squared sum in isApprox, thus the 2x factor.
132  VERIFY(row_vector.isApprox(col_vector.transpose(), RealScalar(2) * NumTraits<Scalar>::epsilon()));
133 
134  Matrix<Scalar, Dynamic, 1> size_changer(size + 50);
135  size_changer.setLinSpaced(size, low, high);
136  VERIFY(size_changer.size() == size);
137 
138  typedef Matrix<Scalar, 1, 1> ScalarMatrix;
139  ScalarMatrix scalar;
140  scalar.setLinSpaced(1, low, high);
141  VERIFY_IS_APPROX(scalar, ScalarMatrix::Constant(high));
142  VERIFY_IS_APPROX(ScalarMatrix::LinSpaced(1, low, high), ScalarMatrix::Constant(high));
143 
144  // regression test for bug 526 (linear vectorized transversal)
145  if (size > 1 && (!NumTraits<Scalar>::IsInteger)) {
146  m.tail(size - 1).setLinSpaced(low, high);
147  VERIFY_IS_APPROX(m(size - 1), high);
148  }
149 
150  // regression test for bug 1383 (LinSpaced with empty size/range)
151  {
152  Index n0 = VectorType::SizeAtCompileTime == Dynamic ? 0 : VectorType::SizeAtCompileTime;
153  low = internal::random<Scalar>();
154  m = VectorType::LinSpaced(n0, low, low - RealScalar(1));
155  VERIFY(m.size() == n0);
156 
157  if (VectorType::SizeAtCompileTime == Dynamic) {
158  VERIFY_IS_EQUAL(VectorType::LinSpaced(n0, 0, Scalar(n0 - 1)).sum(), Scalar(0));
159  VERIFY_IS_EQUAL(VectorType::LinSpaced(n0, low, low - RealScalar(1)).sum(), Scalar(0));
160  }
161 
162  m.setLinSpaced(n0, 0, Scalar(n0 - 1));
163  VERIFY(m.size() == n0);
164  m.setLinSpaced(n0, low, low - RealScalar(1));
165  VERIFY(m.size() == n0);
166 
167  // empty range only:
168  VERIFY_IS_APPROX(VectorType::LinSpaced(size, low, low), VectorType::Constant(size, low));
169  m.setLinSpaced(size, low, low);
170  VERIFY_IS_APPROX(m, VectorType::Constant(size, low));
171 
173  VERIFY_IS_APPROX(VectorType::LinSpaced(size, low, low + Scalar(size - 1)),
174  VectorType::LinSpaced(size, low + Scalar(size - 1), low).reverse());
175 
176  if (VectorType::SizeAtCompileTime == Dynamic) {
177  // Check negative multiplicator path:
178  for (Index k = 1; k < 5; ++k)
179  VERIFY_IS_APPROX(VectorType::LinSpaced(size, low, low + Scalar((size - 1) * k)),
180  VectorType::LinSpaced(size, low + Scalar((size - 1) * k), low).reverse());
181  // Check negative divisor path:
182  for (Index k = 1; k < 5; ++k)
183  VERIFY_IS_APPROX(VectorType::LinSpaced(size * k, low, low + Scalar(size - 1)),
184  VectorType::LinSpaced(size * k, low + Scalar(size - 1), low).reverse());
185  }
186  }
187  }
188 
189  // test setUnit()
190  if (m.size() > 0) {
191  for (Index k = 0; k < 10; ++k) {
192  Index i = internal::random<Index>(0, m.size() - 1);
193  m.setUnit(i);
194  VERIFY_IS_APPROX(m, VectorType::Unit(m.size(), i));
195  }
196  if (VectorType::SizeAtCompileTime == Dynamic) {
197  Index i = internal::random<Index>(0, 2 * m.size() - 1);
198  m.setUnit(2 * m.size(), i);
199  VERIFY_IS_APPROX(m, VectorType::Unit(m.size(), i));
200  }
201  }
202 }
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:17
float real
Definition: datatypes.h:10
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117
char char char int int * k
Definition: level2_impl.h:374
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
const int Dynamic
Definition: Constants.h:25
void check_extremity_accuracy(const VectorType &v, const typename VectorType::Scalar &low, const typename VectorType::Scalar &high)
Definition: nullary.cpp:32
Definition: fft_test_shared.h:66

References CALL_SUBTEST, check_extremity_accuracy(), Eigen::Dynamic, i, Eigen::internal::isApprox(), k, m, n, Eigen::bfloat16_impl::pow(), reverse(), size, swap(), VERIFY, VERIFY_IS_APPROX, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().