main.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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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 #include <cstdlib>
12 #include <cerrno>
13 #include <ctime>
14 #include <iostream>
15 #include <iomanip>
16 #include <fstream>
17 #include <string>
18 #include <sstream>
19 #include <vector>
20 #include <typeinfo>
21 #include <type_traits>
22 #include <functional>
23 #ifdef EIGEN_USE_SYCL
24 #include <CL/sycl.hpp>
25 #endif
26 
27 // The following includes of STL headers have to be done _before_ the
28 // definition of macros min() and max(). The reason is that many STL
29 // implementations will not work properly as the min and max symbols collide
30 // with the STL functions std::min() and std::max(). The STL headers may check
31 // for the macro definition of min/max and issue a warning or undefine the
32 // macros.
33 //
34 // Still, Windows defines min() and max() in windef.h as part of the regular
35 // Windows system interfaces and many other Windows APIs depend on these
36 // macros being available. To prevent the macro expansion of min/max and to
37 // make Eigen compatible with the Windows environment all function calls of
38 // std::min() and std::max() have to be written with parenthesis around the
39 // function name.
40 //
41 // All STL headers used by Eigen should be included here. Because main.h is
42 // included before any Eigen header and because the STL headers are guarded
43 // against multiple inclusions, no STL header will see our own min/max macro
44 // definitions.
45 #include <limits>
46 #include <algorithm>
47 // Disable ICC's std::complex operator specializations so we can use our own.
48 #define _OVERRIDE_COMPLEX_SPECIALIZATION_ 1
49 #include <complex>
50 #include <deque>
51 #include <queue>
52 #include <cassert>
53 #include <list>
54 #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
55 #include <random>
56 #include <chrono>
57 #endif
58 #if __cplusplus > 201703L
59 // libstdc++ 9's <memory> indirectly uses max() via <bit>.
60 // libstdc++ 10's <memory> indirectly uses max() via ranges headers.
61 #include <memory>
62 // libstdc++ 11's <thread> indirectly uses max() via semaphore headers.
63 #include <thread>
64 #endif
65 
66 // Configure GPU.
67 #if defined(EIGEN_USE_HIP)
68 #if defined(__HIPCC__) && !defined(EIGEN_NO_HIP)
69 #define EIGEN_HIPCC __HIPCC__
70 #include <hip/hip_runtime.h>
71 #include <hip/hip_runtime_api.h>
72 #endif
73 #elif defined(__CUDACC__) && !defined(EIGEN_NO_CUDA)
74 #define EIGEN_CUDACC __CUDACC__
75 #include <cuda.h>
76 #include <cuda_runtime.h>
77 #include <cuda_runtime_api.h>
78 #if CUDA_VERSION >= 7050
79 #include <cuda_fp16.h>
80 #endif
81 #endif
82 
83 #if defined(EIGEN_CUDACC) || defined(EIGEN_HIPCC)
84 #define EIGEN_TEST_NO_LONGDOUBLE
85 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
86 #endif
87 
88 // To test that all calls from Eigen code to std::min() and std::max() are
89 // protected by parenthesis against macro expansion, the min()/max() macros
90 // are defined here and any not-parenthesized min/max call will cause a
91 // compiler error.
92 #if !defined(__HIPCC__) && !defined(EIGEN_USE_SYCL) && !defined(EIGEN_POCKETFFT_DEFAULT)
93 //
94 // HIP header files include the following files
95 // <thread>
96 // <regex>
97 // <unordered_map>
98 // which seem to contain not-parenthesized calls to "max"/"min", triggering the following check and causing the compile
99 // to fail
100 //
101 // Including those header files before the following macro definition for "min" / "max", only partially resolves the
102 // issue This is because other HIP header files also define "isnan" / "isinf" / "isfinite" functions, which are needed
103 // in other headers.
104 //
105 // So instead choosing to simply disable this check for HIP
106 //
107 #define min(A, B) please_protect_your_min_with_parentheses
108 #define max(A, B) please_protect_your_max_with_parentheses
109 #define isnan(X) please_protect_your_isnan_with_parentheses
110 #define isinf(X) please_protect_your_isinf_with_parentheses
111 #define isfinite(X) please_protect_your_isfinite_with_parentheses
112 #endif
113 
114 // test possible conflicts
115 struct real {};
116 struct imag {};
117 
118 #ifdef M_PI
119 #undef M_PI
120 #endif
121 #define M_PI please_use_EIGEN_PI_instead_of_M_PI
122 
123 #define FORBIDDEN_IDENTIFIER \
124  (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes
125 // B0 is defined in POSIX header termios.h
126 #define B0 FORBIDDEN_IDENTIFIER
127 #define I FORBIDDEN_IDENTIFIER
128 
129 // _res is defined by resolv.h
130 #define _res FORBIDDEN_IDENTIFIER
131 
132 // Unit tests calling Eigen's blas library must preserve the default blocking size
133 // to avoid troubles.
134 #ifndef EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
135 #define EIGEN_DEBUG_SMALL_PRODUCT_BLOCKS
136 #endif
137 
138 // shuts down ICC's remark #593: variable "XXX" was set but never used
139 #define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
140 
141 #ifdef TEST_ENABLE_TEMPORARY_TRACKING
142 
143 static long int nb_temporaries;
144 static long int nb_temporaries_on_assert = -1;
145 
146 #ifdef TEST_IGNORE_STACK_ALLOCATED_TEMPORARY
147 inline void on_temporary_creation(long int size, int SizeAtCompileTime) {
148  // ignore stack-allocated temporaries
149  if (SizeAtCompileTime != -1) return;
150 #else
151 inline void on_temporary_creation(long int size, int) {
152 #endif
153  // here's a great place to set a breakpoint when debugging failures in this test!
154  if (size != 0) nb_temporaries++;
155  if (nb_temporaries_on_assert > 0) assert(nb_temporaries < nb_temporaries_on_assert);
156 }
157 
158 #define EIGEN_DENSE_STORAGE_CTOR_PLUGIN \
159  { on_temporary_creation(size, Size); }
160 
161 #define VERIFY_EVALUATION_COUNT(XPR, N) \
162  { \
163  nb_temporaries = 0; \
164  XPR; \
165  if (nb_temporaries != (N)) { \
166  std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; \
167  } \
168  VERIFY((#XPR) && nb_temporaries == (N)); \
169  }
170 
171 #endif
172 
173 #include "split_test_helper.h"
174 
175 #ifdef NDEBUG
176 #undef NDEBUG
177 #endif
178 
179 // On windows CE, NDEBUG is automatically defined <assert.h> if NDEBUG is not defined.
180 #ifndef DEBUG
181 #define DEBUG
182 #endif
183 
184 #define DEFAULT_REPEAT 10
185 
186 namespace Eigen {
187 static std::vector<std::string> g_test_stack;
188 // level == 0 <=> abort if test fail
189 // level >= 1 <=> warning message to std::cerr if test fail
190 static int g_test_level = 0;
191 static int g_repeat = 1;
192 static unsigned int g_seed = 0;
193 static bool g_has_set_repeat = false, g_has_set_seed = false;
194 
195 class EigenTest {
196  public:
197  EigenTest() : m_func(0) {}
198  EigenTest(const char* a_name, void (*func)(void)) : m_name(a_name), m_func(func) {
199  get_registered_tests().push_back(this);
200  }
201  const std::string& name() const { return m_name; }
202  void operator()() const { m_func(); }
203 
204  static const std::vector<EigenTest*>& all() { return get_registered_tests(); }
205 
206  protected:
207  static std::vector<EigenTest*>& get_registered_tests() {
208  static std::vector<EigenTest*>* ms_registered_tests = new std::vector<EigenTest*>();
209  return *ms_registered_tests;
210  }
212  void (*m_func)(void);
213 };
214 
215 // Declare and register a test, e.g.:
216 // EIGEN_DECLARE_TEST(mytest) { ... }
217 // will create a function:
218 // void test_mytest() { ... }
219 // that will be automatically called.
220 #define EIGEN_DECLARE_TEST(X) \
221  void EIGEN_CAT(test_, X)(); \
222  static EigenTest EIGEN_CAT(test_handler_, X)(EIGEN_MAKESTRING(X), &EIGEN_CAT(test_, X)); \
223  void EIGEN_CAT(test_, X)()
224 } // namespace Eigen
225 
226 #define TRACK std::cerr << __FILE__ << " " << __LINE__ << std::endl
227 
228 #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "")
229 
230 #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__) && \
231  !defined(__SYCL_DEVICE_ONLY__)
232 #define EIGEN_EXCEPTIONS
233 #endif
234 
235 #ifndef EIGEN_NO_ASSERTION_CHECKING
236 
237 namespace Eigen {
238 static const bool should_raise_an_assert = false;
239 
240 // Used to avoid to raise two exceptions at a time in which
241 // case the exception is not properly caught.
242 // This may happen when a second exceptions is triggered in a destructor.
243 static bool no_more_assert = false;
245 
249 };
250 
254 };
255 } // namespace Eigen
256 // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
257 // one should have been, then the list of executed assertions is printed out.
258 //
259 // EIGEN_DEBUG_ASSERTS is not enabled by default as it
260 // significantly increases the compilation time
261 // and might even introduce side effects that would hide
262 // some memory errors.
263 #ifdef EIGEN_DEBUG_ASSERTS
264 
265 namespace Eigen {
266 namespace internal {
267 static bool push_assert = false;
268 }
269 static std::vector<std::string> eigen_assert_list;
270 } // namespace Eigen
271 #define eigen_assert(a) \
272  if ((!(a)) && (!no_more_assert)) { \
273  if (report_on_cerr_on_assert_failure) std::cerr << #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \
274  Eigen::no_more_assert = true; \
275  EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
276  } else if (Eigen::internal::push_assert) { \
277  eigen_assert_list.push_back(std::string(EIGEN_MAKESTRING(__FILE__) " (" EIGEN_MAKESTRING(__LINE__) ") : " #a)); \
278  }
279 
280 #ifdef EIGEN_EXCEPTIONS
281 #define VERIFY_RAISES_ASSERT(a) \
282  { \
283  Eigen::no_more_assert = false; \
284  Eigen::eigen_assert_list.clear(); \
285  Eigen::internal::push_assert = true; \
286  Eigen::report_on_cerr_on_assert_failure = false; \
287  try { \
288  a; \
289  std::cerr << "One of the following asserts should have been triggered:\n"; \
290  for (uint ai = 0; ai < eigen_assert_list.size(); ++ai) std::cerr << " " << eigen_assert_list[ai] << "\n"; \
291  VERIFY(Eigen::should_raise_an_assert&& #a); \
292  } catch (Eigen::eigen_assert_exception) { \
293  Eigen::internal::push_assert = false; \
294  VERIFY(true); \
295  } \
296  Eigen::report_on_cerr_on_assert_failure = true; \
297  Eigen::internal::push_assert = false; \
298  }
299 #endif // EIGEN_EXCEPTIONS
300 
301 #elif !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(__SYCL_DEVICE_ONLY__) // EIGEN_DEBUG_ASSERTS
302 #define eigen_assert(a) \
303  if ((!(a)) && (!no_more_assert)) { \
304  Eigen::no_more_assert = true; \
305  if (report_on_cerr_on_assert_failure) { \
306  eigen_plain_assert(a); \
307  } else { \
308  EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
309  } \
310  }
311 
312 #ifdef EIGEN_EXCEPTIONS
313 #define VERIFY_RAISES_ASSERT(a) \
314  { \
315  Eigen::no_more_assert = false; \
316  Eigen::report_on_cerr_on_assert_failure = false; \
317  try { \
318  a; \
319  VERIFY(Eigen::should_raise_an_assert&& #a); \
320  } catch (Eigen::eigen_assert_exception&) { \
321  VERIFY(true); \
322  } \
323  Eigen::report_on_cerr_on_assert_failure = true; \
324  }
325 #endif // EIGEN_EXCEPTIONS
326 #endif // EIGEN_DEBUG_ASSERTS
327 
328 #ifndef VERIFY_RAISES_ASSERT
329 #define VERIFY_RAISES_ASSERT(a) std::cout << "Can't VERIFY_RAISES_ASSERT( " #a " ) with exceptions disabled\n";
330 #endif
331 
332 #if !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY)
333 #define EIGEN_USE_CUSTOM_ASSERT
334 #endif
335 
336 #else // EIGEN_NO_ASSERTION_CHECKING
337 
338 #define VERIFY_RAISES_ASSERT(a) \
339  {}
340 
341 #endif // EIGEN_NO_ASSERTION_CHECKING
342 
343 #if !defined(EIGEN_TESTING_CONSTEXPR) && !defined(EIGEN_TESTING_PLAINOBJECT_CTOR)
344 #define EIGEN_INTERNAL_DEBUGGING
345 #endif
346 #include <Eigen/QR> // required for createRandomPIMatrixOfRank and generateRandomMatrixSvs
347 
348 inline void verify_impl(bool condition, const char* testname, const char* file, int line,
349  const char* condition_as_string) {
350  if (!condition) {
351  if (Eigen::g_test_level > 0) std::cerr << "WARNING: ";
352  std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")" << std::endl
353  << " " << condition_as_string << std::endl;
354  std::cerr << "Stack:\n";
355  const int test_stack_size = static_cast<int>(Eigen::g_test_stack.size());
356  for (int i = test_stack_size - 1; i >= 0; --i) std::cerr << " - " << Eigen::g_test_stack[i] << "\n";
357  std::cerr << "\n";
358  if (Eigen::g_test_level == 0) abort();
359  }
360 }
361 
362 #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a))
363 
364 #define VERIFY_GE(a, b) ::verify_impl(a >= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a >= b))
365 #define VERIFY_LE(a, b) ::verify_impl(a <= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a <= b))
366 
367 #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b, true))
368 #define VERIFY_IS_NOT_EQUAL(a, b) VERIFY(test_is_equal(a, b, false))
369 #define VERIFY_IS_APPROX(a, b) VERIFY(verifyIsApprox(a, b))
370 #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b))
371 #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b))
372 #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
373 #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
374 #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
375 #define VERIFY_IS_CWISE_EQUAL(a, b) VERIFY(verifyIsCwiseApprox(a, b, true))
376 #define VERIFY_IS_CWISE_APPROX(a, b) VERIFY(verifyIsCwiseApprox(a, b, false))
377 
378 #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
379 
380 #define STATIC_CHECK(COND) EIGEN_STATIC_ASSERT((COND), EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT)
381 
382 #define CALL_SUBTEST(FUNC) \
383  do { \
384  g_test_stack.push_back(EIGEN_MAKESTRING(FUNC)); \
385  FUNC; \
386  g_test_stack.pop_back(); \
387  } while (0)
388 
389 // Forward declarations to avoid ICC warnings
390 #if EIGEN_COMP_ICC
391 
392 template <typename T>
394 
395 namespace Eigen {
396 
397 template <typename T, typename U>
398 bool test_is_equal(const T& actual, const U& expected, bool expect_equal = true);
399 
400 } // end namespace Eigen
401 
402 #endif // EIGEN_COMP_ICC
403 
404 namespace Eigen {
405 
406 template <typename T1, typename T2>
408  return true;
409 }
410 
411 template <typename T>
414 }
415 template <>
416 inline float test_precision<float>() {
417  return 1e-3f;
418 }
419 template <>
420 inline double test_precision<double>() {
421  return 1e-6;
422 }
423 template <>
424 inline long double test_precision<long double>() {
425  return 1e-6l;
426 }
427 template <>
428 inline float test_precision<std::complex<float> >() {
429  return test_precision<float>();
430 }
431 template <>
432 inline double test_precision<std::complex<double> >() {
433  return test_precision<double>();
434 }
435 template <>
436 inline long double test_precision<std::complex<long double> >() {
438 }
439 
440 #define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE) \
441  inline bool test_isApprox(TYPE a, TYPE b) { \
442  return numext::equal_strict(a, b) || ((numext::isnan)(a) && (numext::isnan)(b)) || \
443  (internal::isApprox(a, b, test_precision<TYPE>())); \
444  } \
445  inline bool test_isCwiseApprox(TYPE a, TYPE b, bool exact) { \
446  return numext::equal_strict(a, b) || ((numext::isnan)(a) && (numext::isnan)(b)) || \
447  (!exact && internal::isApprox(a, b, test_precision<TYPE>())); \
448  } \
449  inline bool test_isMuchSmallerThan(TYPE a, TYPE b) { \
450  return internal::isMuchSmallerThan(a, b, test_precision<TYPE>()); \
451  } \
452  inline bool test_isApproxOrLessThan(TYPE a, TYPE b) { \
453  return internal::isApproxOrLessThan(a, b, test_precision<TYPE>()); \
454  }
455 
457 EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned short)
461 EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long)
463 EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long long)
468 
469 #undef EIGEN_TEST_SCALAR_TEST_OVERLOAD
470 
471 #ifndef EIGEN_TEST_NO_COMPLEX
472 inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b) {
473  return internal::isApprox(a, b, test_precision<std::complex<float> >());
474 }
475 inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b) {
476  return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >());
477 }
478 
479 inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b) {
480  return internal::isApprox(a, b, test_precision<std::complex<double> >());
481 }
482 inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b) {
483  return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >());
484 }
485 
486 #ifndef EIGEN_TEST_NO_LONGDOUBLE
487 inline bool test_isApprox(const std::complex<long double>& a, const std::complex<long double>& b) {
488  return internal::isApprox(a, b, test_precision<std::complex<long double> >());
489 }
490 inline bool test_isMuchSmallerThan(const std::complex<long double>& a, const std::complex<long double>& b) {
491  return internal::isMuchSmallerThan(a, b, test_precision<std::complex<long double> >());
492 }
493 #endif
494 #endif
495 
496 #ifndef EIGEN_TEST_NO_LONGDOUBLE
497 inline bool test_isApprox(const long double& a, const long double& b) {
499  if (!ret)
500  std::cerr << std::endl << " actual = " << a << std::endl << " expected = " << b << std::endl << std::endl;
501  return ret;
502 }
503 
504 inline bool test_isMuchSmallerThan(const long double& a, const long double& b) {
506 }
507 inline bool test_isApproxOrLessThan(const long double& a, const long double& b) {
509 }
510 #endif // EIGEN_TEST_NO_LONGDOUBLE
511 
512 // test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox.
513 template <typename T1, typename T2>
515  const EigenBase<T2>& b) {
516  using std::sqrt;
518  typename internal::nested_eval<T1, 2>::type ea(a.derived());
519  typename internal::nested_eval<T2, 2>::type eb(b.derived());
520  return sqrt(RealScalar((ea.matrix() - eb.matrix()).cwiseAbs2().sum()) /
521  RealScalar((std::min)(eb.cwiseAbs2().sum(), ea.cwiseAbs2().sum())));
522 }
523 
524 template <typename T1, typename T2>
525 typename T1::RealScalar test_relative_error(const T1& a, const T2& b, const typename T1::Coefficients* = 0) {
526  return test_relative_error(a.coeffs(), b.coeffs());
527 }
528 
529 template <typename T1, typename T2>
530 typename T1::Scalar test_relative_error(const T1& a, const T2& b, const typename T1::MatrixType* = 0) {
531  return test_relative_error(a.matrix(), b.matrix());
532 }
533 
534 template <typename S, int D>
536  return test_relative_error(a.vector(), b.vector());
537 }
538 
539 template <typename S, int D, int O>
541  return (std::max)(test_relative_error(a.origin(), b.origin()), test_relative_error(a.origin(), b.origin()));
542 }
543 
544 template <typename S, int D>
546  return (std::max)(test_relative_error((a.min)(), (b.min)()), test_relative_error((a.max)(), (b.max)()));
547 }
548 
549 template <typename Derived>
550 class SparseMatrixBase;
551 template <typename T1, typename T2>
553  return test_relative_error(a, b.toDense());
554 }
555 
556 template <typename Derived>
557 class SparseMatrixBase;
558 template <typename T1, typename T2>
560  return test_relative_error(a.toDense(), b);
561 }
562 
563 template <typename Derived>
564 class SparseMatrixBase;
565 template <typename T1, typename T2>
567  return test_relative_error(a.toDense(), b.toDense());
568 }
569 
570 template <typename T1, typename T2>
572  const T1& a, const T2& b, std::enable_if_t<internal::is_arithmetic<typename NumTraits<T1>::Real>::value, T1>* = 0) {
573  typedef typename NumTraits<typename NumTraits<T1>::Real>::NonInteger RealScalar;
574  return numext::sqrt(RealScalar(numext::abs2(a - b)) /
576 }
577 
578 template <typename T>
580  return test_relative_error(a.angle(), b.angle());
581 }
582 
583 template <typename T>
585  return (std::max)(test_relative_error(a.angle(), b.angle()), test_relative_error(a.axis(), b.axis()));
586 }
587 
588 template <typename Type1, typename Type2>
589 inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
590 {
591  return a.isApprox(b, test_precision<typename Type1::Scalar>());
592 }
593 
594 // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or
595 // expressions
596 template <typename T>
597 typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, const typename T::Scalar* = 0) {
599 }
600 
601 template <typename T>
603  const T&, std::enable_if_t<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>* = 0) {
605 }
606 
607 // verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails.
608 template <typename Type1, typename Type2>
609 inline bool verifyIsApprox(const Type1& a, const Type2& b) {
610  bool ret = test_isApprox(a, b);
611  if (!ret) {
612  std::cerr << "Difference too large wrt tolerance " << get_test_precision(a)
613  << ", relative error is: " << test_relative_error(a, b) << std::endl;
614  }
615  return ret;
616 }
617 
618 // verifyIsCwiseApprox is a wrapper to test_isCwiseApprox that outputs the relative difference magnitude if the test
619 // fails.
620 template <typename Type1, typename Type2>
621 inline bool verifyIsCwiseApprox(const Type1& a, const Type2& b, bool exact) {
622  bool ret = test_isCwiseApprox(a, b, exact);
623  if (!ret) {
624  if (exact) {
625  std::cerr << "Values are not an exact match";
626  } else {
627  std::cerr << "Difference too large wrt tolerance " << get_test_precision(a);
628  }
629  std::cerr << ", relative error is: " << test_relative_error(a, b) << std::endl;
630  }
631  return ret;
632 }
633 
634 // The idea behind this function is to compare the two scalars a and b where
635 // the scalar ref is a hint about the expected order of magnitude of a and b.
636 // WARNING: the scalar a and b must be positive
637 // Therefore, if for some reason a and b are very small compared to ref,
638 // we won't issue a false negative.
639 // This test could be: abs(a-b) <= eps * ref
640 // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
641 template <typename Scalar, typename ScalarRef>
642 inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref) {
643  return test_isApprox(a + ref, b + ref);
644 }
645 
646 template <typename Derived1, typename Derived2>
648  return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
649 }
650 
651 template <typename Derived>
653  const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s) {
654  return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
655 }
656 
657 template <typename Derived>
658 inline bool test_isUnitary(const MatrixBase<Derived>& m) {
659  return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
660 }
661 
662 // Checks component-wise, works with infs and nans.
663 template <typename Derived1, typename Derived2>
665  if (m1.rows() != m2.rows()) {
666  return false;
667  }
668  if (m1.cols() != m2.cols()) {
669  return false;
670  }
671  for (Index r = 0; r < m1.rows(); ++r) {
672  for (Index c = 0; c < m1.cols(); ++c) {
673  if (m1(r, c) != m2(r, c) && !((numext::isnan)(m1(r, c)) && (numext::isnan)(m2(r, c))) &&
674  (exact || !test_isApprox(m1(r, c), m2(r, c)))) {
675  return false;
676  }
677  }
678  }
679  return true;
680 }
681 
682 template <typename Derived1, typename Derived2>
684  return test_isCwiseApprox(m1.toDense(), m2.toDense(), exact);
685 }
686 
687 template <typename T, typename U>
688 bool test_is_equal(const T& actual, const U& expected, bool expect_equal) {
689  if (numext::equal_strict(actual, expected) == expect_equal) return true;
690  // false:
691  std::cerr << "\n actual = " << actual << "\n expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
692  return false;
693 }
694 
702 template <typename T>
703 bool isNotNaN(const T& x) {
704  return x == x;
705 }
706 
714 template <typename T>
715 bool isPlusInf(const T& x) {
716  return x > NumTraits<T>::highest();
717 }
718 
726 template <typename T>
727 bool isMinusInf(const T& x) {
728  return x < NumTraits<T>::lowest();
729 }
730 
731 } // end namespace Eigen
732 
733 #include "random_matrix_helper.h"
734 
735 template <typename T>
737 
738 template <>
739 struct GetDifferentType<float> {
740  typedef double type;
741 };
742 template <>
744  typedef float type;
745 };
746 template <typename T>
747 struct GetDifferentType<std::complex<T> > {
749 };
750 
751 template <typename T>
753  return typeid(T).name();
754 }
755 template <>
757  return "float";
758 }
759 template <>
761  return "double";
762 }
763 template <>
765  return "long double";
766 }
767 template <>
768 std::string type_name<Eigen::half>(Eigen::half) {
769  return "half";
770 }
771 template <>
772 std::string type_name<Eigen::bfloat16>(Eigen::bfloat16) {
773  return "bfloat16";
774 }
775 template <>
777  return "int8_t";
778 }
779 template <>
781  return "int16_t";
782 }
783 template <>
785  return "int32_t";
786 }
787 template <>
789  return "int64_t";
790 }
791 template <>
793  return "uint8_t";
794 }
795 template <>
797  return "uint16_t";
798 }
799 template <>
801  return "uint32_t";
802 }
803 template <>
805  return "uint64_t";
806 }
807 template <>
808 std::string type_name<std::complex<float> >(std::complex<float>) {
809  return "complex<float>";
810 }
811 template <>
812 std::string type_name<std::complex<double> >(std::complex<double>) {
813  return "complex<double>";
814 }
815 template <>
816 std::string type_name<std::complex<long double> >(std::complex<long double>) {
817  return "complex<long double>";
818 }
819 template <>
820 std::string type_name<std::complex<int> >(std::complex<int>) {
821  return "complex<int>";
822 }
823 template <typename T>
825  return type_name(T());
826 }
827 
828 using namespace Eigen;
829 
835 inline void set_repeat_from_string(const char* str) {
836  errno = 0;
837  g_repeat = int(strtoul(str, 0, 10));
838  if (errno || g_repeat <= 0) {
839  std::cout << "Invalid repeat value " << str << std::endl;
840  exit(EXIT_FAILURE);
841  }
842  g_has_set_repeat = true;
843 }
844 
850 inline void set_seed_from_string(const char* str) {
851  errno = 0;
852  g_seed = int(strtoul(str, 0, 10));
853  if (errno || g_seed == 0) {
854  std::cout << "Invalid seed value " << str << std::endl;
855  exit(EXIT_FAILURE);
856  }
857  g_has_set_seed = true;
858 }
859 
860 int main(int argc, char* argv[]) {
861  g_has_set_repeat = false;
862  g_has_set_seed = false;
863  bool need_help = false;
864 
865  for (int i = 1; i < argc; i++) {
866  if (argv[i][0] == 'r') {
867  if (g_has_set_repeat) {
868  std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
869  return 1;
870  }
871  set_repeat_from_string(argv[i] + 1);
872  } else if (argv[i][0] == 's') {
873  if (g_has_set_seed) {
874  std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
875  return 1;
876  }
877  set_seed_from_string(argv[i] + 1);
878  } else {
879  need_help = true;
880  }
881  }
882 
883  if (need_help) {
884  std::cout << "This test application takes the following optional arguments:" << std::endl;
885  std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
886  std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl;
887  std::cout << std::endl;
888  std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
889  std::cout << "will be used as default values for these parameters." << std::endl;
890  return 1;
891  }
892 
893  char* env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
894  if (!g_has_set_repeat && env_EIGEN_REPEAT) set_repeat_from_string(env_EIGEN_REPEAT);
895  char* env_EIGEN_SEED = getenv("EIGEN_SEED");
896  if (!g_has_set_seed && env_EIGEN_SEED) set_seed_from_string(env_EIGEN_SEED);
897 
898  if (!g_has_set_seed) g_seed = (unsigned int)time(NULL);
900 
901  std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
902  std::stringstream ss;
903  ss << "Seed: " << g_seed;
904  g_test_stack.push_back(ss.str());
905  srand(g_seed);
906  std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
907 
908  VERIFY(EigenTest::all().size() > 0);
909 
910  for (std::size_t i = 0; i < EigenTest::all().size(); ++i) {
911  const EigenTest& current_test = *EigenTest::all()[i];
912  Eigen::g_test_stack.push_back(current_test.name());
913  current_test();
914  Eigen::g_test_stack.pop_back();
915  }
916 
917  return 0;
918 }
919 
920 // These warning are disabled here such that they are still ON when parsing Eigen's header files.
921 #if defined __INTEL_COMPILER
922 // remark #383: value copied to temporary, reference to temporary used
923 // -> this warning is raised even for legal usage as: g_test_stack.push_back("foo"); where g_test_stack is a
924 // std::vector<std::string>
925 // remark #1418: external function definition with no prior declaration
926 // -> this warning is raised for all our test functions. Declaring them static would fix the issue.
927 // warning #279: controlling expression is constant
928 // remark #1572: floating-point equality and inequality comparisons are unreliable
929 #pragma warning disable 279 383 1418 1572
930 #endif
931 
932 #ifdef _MSC_VER
933 // 4503 - decorated name length exceeded, name was truncated
934 #pragma warning(disable : 4503)
935 #endif
936 
937 #include "gpu_test_helper.h"
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
Matrix3d m1
Definition: IOFormat.cpp:2
#define assert(e,...)
Definition: Logger.h:744
MatrixType m2(n_dims)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 100 >, boost::multiprecision::et_on > Real
Definition: boostmultiprec.cpp:77
An axis aligned box.
Definition: AlignedBox.h:69
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition: AngleAxis.h:52
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
Definition: main.h:195
EigenTest(const char *a_name, void(*func)(void))
Definition: main.h:198
EigenTest()
Definition: main.h:197
void operator()() const
Definition: main.h:202
std::string m_name
Definition: main.h:211
const std::string & name() const
Definition: main.h:201
void(* m_func)(void)
Definition: main.h:212
static const std::vector< EigenTest * > & all()
Definition: main.h:204
static std::vector< EigenTest * > & get_registered_tests()
Definition: main.h:207
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
A parametrized line.
Definition: ParametrizedLine.h:33
Represents a rotation/orientation in a 2 dimensional space.
Definition: Rotation2D.h:44
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
Represents a translation transformation.
Definition: Translation.h:33
RealScalar s
Definition: level1_cplx_impl.h:130
return int(ret)+1
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
std::string type_name< float >(float)
Definition: main.h:756
std::string type_name< uint32_t >(uint32_t)
Definition: main.h:800
int main(int argc, char *argv[])
Definition: main.h:860
std::string type_name< int8_t >(int8_t)
Definition: main.h:776
#define max(A, B)
Definition: main.h:108
#define min(A, B)
Definition: main.h:107
#define DEFAULT_REPEAT
Definition: main.h:184
std::string type_name(T)
Definition: main.h:752
std::string type_name< int16_t >(int16_t)
Definition: main.h:780
std::string type_name< int64_t >(int64_t)
Definition: main.h:788
void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
Definition: main.h:348
#define VERIFY(a)
Definition: main.h:362
std::string type_name< double >(double)
Definition: main.h:760
std::string type_name< uint8_t >(uint8_t)
Definition: main.h:792
std::string type_name< int32_t >(int32_t)
Definition: main.h:784
void set_repeat_from_string(const char *str)
Definition: main.h:835
std::string type_name< uint64_t >(uint64_t)
Definition: main.h:804
void set_seed_from_string(const char *str)
Definition: main.h:850
std::string type_name< long double >(long double)
Definition: main.h:764
std::string type_name< uint16_t >(uint16_t)
Definition: main.h:796
#define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE)
Definition: main.h:440
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 bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
EIGEN_DEVICE_FUNC bool isApproxOrLessThan(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1930
std::int32_t int32_t
Definition: Meta.h:41
std::int8_t int8_t
Definition: Meta.h:37
std::uint8_t uint8_t
Definition: Meta.h:36
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool equal_strict(const X &x, const Y &y)
Definition: Meta.h:571
std::int16_t int16_t
Definition: Meta.h:39
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool() isnan(const Eigen::bfloat16 &h)
Definition: BFloat16.h:742
std::int64_t int64_t
Definition: Meta.h:43
std::uint16_t uint16_t
Definition: Meta.h:38
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float &x)
Definition: arch/SSE/MathFunctions.h:69
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920
std::uint32_t uint32_t
Definition: Meta.h:40
std::uint64_t uint64_t
Definition: Meta.h:42
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
NumTraits< T >::Real test_precision()
Definition: main.h:412
bool isNotNaN(const T &x)
Definition: main.h:703
static bool no_more_assert
Definition: main.h:243
squared absolute value
Definition: GlobalFunctions.h:87
bool test_isApproxOrLessThan(const long double &a, const long double &b)
Definition: main.h:507
bool verifyIsCwiseApprox(const Type1 &a, const Type2 &b, bool exact)
Definition: main.h:621
bool test_isMuchSmallerThan(const std::complex< float > &a, const std::complex< float > &b)
Definition: main.h:475
static int g_repeat
Definition: main.h:191
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
float test_precision< float >()
Definition: main.h:416
std::enable_if_t< internal::is_same< T1, T2 >::value, bool > is_same_type(const T1 &, const T2 &)
Definition: main.h:407
bool isMinusInf(const T &x)
Definition: main.h:727
long double test_precision< long double >()
Definition: main.h:424
bool verifyIsApprox(const Type1 &a, const Type2 &b)
Definition: main.h:609
bool test_isCwiseApprox(const DenseBase< Derived1 > &m1, const DenseBase< Derived2 > &m2, bool exact)
Definition: main.h:664
double test_precision< double >()
Definition: main.h:420
bool test_isApprox(const std::complex< float > &a, const std::complex< float > &b)
Definition: main.h:472
static int g_test_level
Definition: main.h:190
NumTraits< typename T::Scalar >::Real get_test_precision(const T &, const typename T::Scalar *=0)
Definition: main.h:597
bool test_isUnitary(const MatrixBase< Derived > &m)
Definition: main.h:658
static unsigned int g_seed
Definition: main.h:192
bool isPlusInf(const T &x)
Definition: main.h:715
bool test_is_equal(const T &actual, const U &expected, bool expect_equal)
Definition: main.h:688
static bool g_has_set_seed
Definition: main.h:193
bool test_isApproxWithRef(const Scalar &a, const Scalar &b, const ScalarRef &ref)
Definition: main.h:642
NumTraits< typename T1::RealScalar >::NonInteger test_relative_error(const EigenBase< T1 > &a, const EigenBase< T2 > &b)
Definition: main.h:514
static std::vector< std::string > g_test_stack
Definition: main.h:187
static bool g_has_set_repeat
Definition: main.h:193
static const bool should_raise_an_assert
Definition: main.h:238
static bool report_on_cerr_on_assert_failure
Definition: main.h:244
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
line
Definition: calibrate.py:103
type
Definition: compute_granudrum_aor.py:141
str
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
@ S
Definition: quadtree.h:62
list x
Definition: plotDoE.py:28
string name
Definition: plotDoE.py:33
static long int nb_temporaries
Definition: sparse_permutations.cpp:21
Definition: EigenBase.h:33
T Real
Definition: NumTraits.h:183
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: BFloat16.h:101
Definition: main.h:246
eigen_assert_exception(void)
Definition: main.h:247
~eigen_assert_exception()
Definition: main.h:248
eigen_static_assert_exception(void)
Definition: main.h:252
~eigen_static_assert_exception()
Definition: main.h:253
Definition: Half.h:139
Definition: Meta.h:145
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: ForwardDeclarations.h:21
float type
Definition: main.h:744
double type
Definition: main.h:740
std::complex< typename GetDifferentType< T >::type > type
Definition: main.h:748
Definition: main.h:736
Definition: datatypes.h:12
Definition: benchGeometry.cpp:21
Definition: main.h:116
Definition: main.h:115
void on_temporary_creation()
Definition: test/sparse_product.cpp:18