main.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. #include <cstdlib>
  11. #include <cerrno>
  12. #include <ctime>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <vector>
  18. #include <typeinfo>
  19. #include <functional>
  20. // The following includes of STL headers have to be done _before_ the
  21. // definition of macros min() and max(). The reason is that many STL
  22. // implementations will not work properly as the min and max symbols collide
  23. // with the STL functions std:min() and std::max(). The STL headers may check
  24. // for the macro definition of min/max and issue a warning or undefine the
  25. // macros.
  26. //
  27. // Still, Windows defines min() and max() in windef.h as part of the regular
  28. // Windows system interfaces and many other Windows APIs depend on these
  29. // macros being available. To prevent the macro expansion of min/max and to
  30. // make Eigen compatible with the Windows environment all function calls of
  31. // std::min() and std::max() have to be written with parenthesis around the
  32. // function name.
  33. //
  34. // All STL headers used by Eigen should be included here. Because main.h is
  35. // included before any Eigen header and because the STL headers are guarded
  36. // against multiple inclusions, no STL header will see our own min/max macro
  37. // definitions.
  38. #include <limits>
  39. #include <algorithm>
  40. // Disable ICC's std::complex operator specializations so we can use our own.
  41. #define _OVERRIDE_COMPLEX_SPECIALIZATION_ 1
  42. #include <complex>
  43. #include <deque>
  44. #include <queue>
  45. #include <cassert>
  46. #include <list>
  47. #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
  48. #include <random>
  49. #include <chrono>
  50. #ifdef EIGEN_USE_THREADS
  51. #include <future>
  52. #endif
  53. #endif
  54. // Same for cuda_fp16.h
  55. #if defined(__CUDACC__) && !defined(EIGEN_NO_CUDA)
  56. // Means the compiler is either nvcc or clang with CUDA enabled
  57. #define EIGEN_CUDACC __CUDACC__
  58. #endif
  59. #if defined(EIGEN_CUDACC)
  60. #include <cuda.h>
  61. #define EIGEN_CUDA_SDK_VER (CUDA_VERSION * 10)
  62. #else
  63. #define EIGEN_CUDA_SDK_VER 0
  64. #endif
  65. #if EIGEN_CUDA_SDK_VER >= 70500
  66. #include <cuda_fp16.h>
  67. #endif
  68. // To test that all calls from Eigen code to std::min() and std::max() are
  69. // protected by parenthesis against macro expansion, the min()/max() macros
  70. // are defined here and any not-parenthesized min/max call will cause a
  71. // compiler error.
  72. #if !defined(__HIPCC__) && !defined(EIGEN_USE_SYCL)
  73. //
  74. // HIP header files include the following files
  75. // <thread>
  76. // <regex>
  77. // <unordered_map>
  78. // which seem to contain not-parenthesized calls to "max"/"min", triggering the following check and causing the compile to fail
  79. //
  80. // Including those header files before the following macro definition for "min" / "max", only partially resolves the issue
  81. // This is because other HIP header files also define "isnan" / "isinf" / "isfinite" functions, which are needed in other
  82. // headers.
  83. //
  84. // So instead choosing to simply disable this check for HIP
  85. //
  86. #define min(A,B) please_protect_your_min_with_parentheses
  87. #define max(A,B) please_protect_your_max_with_parentheses
  88. #define isnan(X) please_protect_your_isnan_with_parentheses
  89. #define isinf(X) please_protect_your_isinf_with_parentheses
  90. #define isfinite(X) please_protect_your_isfinite_with_parentheses
  91. #endif
  92. // test possible conflicts
  93. struct real {};
  94. struct imag {};
  95. #ifdef M_PI
  96. #undef M_PI
  97. #endif
  98. #define M_PI please_use_EIGEN_PI_instead_of_M_PI
  99. #define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes
  100. // B0 is defined in POSIX header termios.h
  101. #define B0 FORBIDDEN_IDENTIFIER
  102. // `I` may be defined by complex.h:
  103. #define I FORBIDDEN_IDENTIFIER
  104. // Unit tests calling Eigen's blas library must preserve the default blocking size
  105. // to avoid troubles.
  106. #ifndef EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
  107. #define EIGEN_DEBUG_SMALL_PRODUCT_BLOCKS
  108. #endif
  109. // shuts down ICC's remark #593: variable "XXX" was set but never used
  110. #define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
  111. #ifdef TEST_ENABLE_TEMPORARY_TRACKING
  112. static long int nb_temporaries;
  113. static long int nb_temporaries_on_assert = -1;
  114. inline void on_temporary_creation(long int size) {
  115. // here's a great place to set a breakpoint when debugging failures in this test!
  116. if(size!=0) nb_temporaries++;
  117. if(nb_temporaries_on_assert>0) assert(nb_temporaries<nb_temporaries_on_assert);
  118. }
  119. #define EIGEN_DENSE_STORAGE_CTOR_PLUGIN { on_temporary_creation(size); }
  120. #define VERIFY_EVALUATION_COUNT(XPR,N) {\
  121. nb_temporaries = 0; \
  122. XPR; \
  123. if(nb_temporaries!=(N)) { std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; }\
  124. VERIFY( (#XPR) && nb_temporaries==(N) ); \
  125. }
  126. #endif
  127. #include "split_test_helper.h"
  128. #ifdef NDEBUG
  129. #undef NDEBUG
  130. #endif
  131. // On windows CE, NDEBUG is automatically defined <assert.h> if NDEBUG is not defined.
  132. #ifndef DEBUG
  133. #define DEBUG
  134. #endif
  135. // bounds integer values for AltiVec
  136. #if defined(__ALTIVEC__) || defined(__VSX__)
  137. #define EIGEN_MAKING_DOCS
  138. #endif
  139. #define DEFAULT_REPEAT 10
  140. namespace Eigen
  141. {
  142. static std::vector<std::string> g_test_stack;
  143. // level == 0 <=> abort if test fail
  144. // level >= 1 <=> warning message to std::cerr if test fail
  145. static int g_test_level = 0;
  146. static int g_repeat = 1;
  147. static unsigned int g_seed = 0;
  148. static bool g_has_set_repeat = false, g_has_set_seed = false;
  149. class EigenTest
  150. {
  151. public:
  152. EigenTest() : m_func(0) {}
  153. EigenTest(const char* a_name, void (*func)(void))
  154. : m_name(a_name), m_func(func)
  155. {
  156. get_registered_tests().push_back(this);
  157. }
  158. const std::string& name() const { return m_name; }
  159. void operator()() const { m_func(); }
  160. static const std::vector<EigenTest*>& all() { return get_registered_tests(); }
  161. protected:
  162. static std::vector<EigenTest*>& get_registered_tests()
  163. {
  164. static std::vector<EigenTest*>* ms_registered_tests = new std::vector<EigenTest*>();
  165. return *ms_registered_tests;
  166. }
  167. std::string m_name;
  168. void (*m_func)(void);
  169. };
  170. // Declare and register a test, e.g.:
  171. // EIGEN_DECLARE_TEST(mytest) { ... }
  172. // will create a function:
  173. // void test_mytest() { ... }
  174. // that will be automatically called.
  175. #define EIGEN_DECLARE_TEST(X) \
  176. void EIGEN_CAT(test_,X) (); \
  177. static EigenTest EIGEN_CAT(test_handler_,X) (EIGEN_MAKESTRING(X), & EIGEN_CAT(test_,X)); \
  178. void EIGEN_CAT(test_,X) ()
  179. }
  180. #define TRACK std::cerr << __FILE__ << " " << __LINE__ << std::endl
  181. // #define TRACK while()
  182. #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "")
  183. #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__) && !defined(__SYCL_DEVICE_ONLY__)
  184. #define EIGEN_EXCEPTIONS
  185. #endif
  186. #ifndef EIGEN_NO_ASSERTION_CHECKING
  187. namespace Eigen
  188. {
  189. static const bool should_raise_an_assert = false;
  190. // Used to avoid to raise two exceptions at a time in which
  191. // case the exception is not properly caught.
  192. // This may happen when a second exceptions is triggered in a destructor.
  193. static bool no_more_assert = false;
  194. static bool report_on_cerr_on_assert_failure = true;
  195. struct eigen_assert_exception
  196. {
  197. eigen_assert_exception(void) {}
  198. ~eigen_assert_exception() { Eigen::no_more_assert = false; }
  199. };
  200. struct eigen_static_assert_exception
  201. {
  202. eigen_static_assert_exception(void) {}
  203. ~eigen_static_assert_exception() { Eigen::no_more_assert = false; }
  204. };
  205. }
  206. // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
  207. // one should have been, then the list of executed assertions is printed out.
  208. //
  209. // EIGEN_DEBUG_ASSERTS is not enabled by default as it
  210. // significantly increases the compilation time
  211. // and might even introduce side effects that would hide
  212. // some memory errors.
  213. #ifdef EIGEN_DEBUG_ASSERTS
  214. namespace Eigen
  215. {
  216. namespace internal
  217. {
  218. static bool push_assert = false;
  219. }
  220. static std::vector<std::string> eigen_assert_list;
  221. }
  222. #define eigen_assert(a) \
  223. if( (!(a)) && (!no_more_assert) ) \
  224. { \
  225. if(report_on_cerr_on_assert_failure) \
  226. std::cerr << #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \
  227. Eigen::no_more_assert = true; \
  228. EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
  229. } \
  230. else if (Eigen::internal::push_assert) \
  231. { \
  232. eigen_assert_list.push_back(std::string(EIGEN_MAKESTRING(__FILE__) " (" EIGEN_MAKESTRING(__LINE__) ") : " #a) ); \
  233. }
  234. #ifdef EIGEN_EXCEPTIONS
  235. #define VERIFY_RAISES_ASSERT(a) \
  236. { \
  237. Eigen::no_more_assert = false; \
  238. Eigen::eigen_assert_list.clear(); \
  239. Eigen::internal::push_assert = true; \
  240. Eigen::report_on_cerr_on_assert_failure = false; \
  241. try { \
  242. a; \
  243. std::cerr << "One of the following asserts should have been triggered:\n"; \
  244. for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai) \
  245. std::cerr << " " << eigen_assert_list[ai] << "\n"; \
  246. VERIFY(Eigen::should_raise_an_assert && # a); \
  247. } catch (Eigen::eigen_assert_exception) { \
  248. Eigen::internal::push_assert = false; VERIFY(true); \
  249. } \
  250. Eigen::report_on_cerr_on_assert_failure = true; \
  251. Eigen::internal::push_assert = false; \
  252. }
  253. #endif //EIGEN_EXCEPTIONS
  254. #elif !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY) // EIGEN_DEBUG_ASSERTS
  255. // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3
  256. #define eigen_assert(a) \
  257. if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
  258. { \
  259. Eigen::no_more_assert = true; \
  260. if(report_on_cerr_on_assert_failure) \
  261. eigen_plain_assert(a); \
  262. else \
  263. EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
  264. }
  265. #ifdef EIGEN_EXCEPTIONS
  266. #define VERIFY_RAISES_ASSERT(a) { \
  267. Eigen::no_more_assert = false; \
  268. Eigen::report_on_cerr_on_assert_failure = false; \
  269. try { \
  270. a; \
  271. VERIFY(Eigen::should_raise_an_assert && # a); \
  272. } \
  273. catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \
  274. Eigen::report_on_cerr_on_assert_failure = true; \
  275. }
  276. #endif // EIGEN_EXCEPTIONS
  277. #endif // EIGEN_DEBUG_ASSERTS
  278. #if defined(TEST_CHECK_STATIC_ASSERTIONS) && defined(EIGEN_EXCEPTIONS)
  279. #define EIGEN_STATIC_ASSERT(a,MSG) \
  280. if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
  281. { \
  282. Eigen::no_more_assert = true; \
  283. if(report_on_cerr_on_assert_failure) \
  284. eigen_plain_assert((a) && #MSG); \
  285. else \
  286. EIGEN_THROW_X(Eigen::eigen_static_assert_exception()); \
  287. }
  288. #define VERIFY_RAISES_STATIC_ASSERT(a) { \
  289. Eigen::no_more_assert = false; \
  290. Eigen::report_on_cerr_on_assert_failure = false; \
  291. try { \
  292. a; \
  293. VERIFY(Eigen::should_raise_an_assert && # a); \
  294. } \
  295. catch (Eigen::eigen_static_assert_exception&) { VERIFY(true); } \
  296. Eigen::report_on_cerr_on_assert_failure = true; \
  297. }
  298. #endif // TEST_CHECK_STATIC_ASSERTIONS
  299. #ifndef VERIFY_RAISES_ASSERT
  300. #define VERIFY_RAISES_ASSERT(a) \
  301. std::cout << "Can't VERIFY_RAISES_ASSERT( " #a " ) with exceptions disabled\n";
  302. #endif
  303. #ifndef VERIFY_RAISES_STATIC_ASSERT
  304. #define VERIFY_RAISES_STATIC_ASSERT(a) \
  305. std::cout << "Can't VERIFY_RAISES_STATIC_ASSERT( " #a " ) with exceptions disabled\n";
  306. #endif
  307. #if !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY)
  308. #define EIGEN_USE_CUSTOM_ASSERT
  309. #endif
  310. #else // EIGEN_NO_ASSERTION_CHECKING
  311. #define VERIFY_RAISES_ASSERT(a) {}
  312. #define VERIFY_RAISES_STATIC_ASSERT(a) {}
  313. #endif // EIGEN_NO_ASSERTION_CHECKING
  314. #define EIGEN_INTERNAL_DEBUGGING
  315. #include <Eigen/QR> // required for createRandomPIMatrixOfRank
  316. inline void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
  317. {
  318. if (!condition)
  319. {
  320. if(Eigen::g_test_level>0)
  321. std::cerr << "WARNING: ";
  322. std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")"
  323. << std::endl << " " << condition_as_string << std::endl;
  324. std::cerr << "Stack:\n";
  325. const int test_stack_size = static_cast<int>(Eigen::g_test_stack.size());
  326. for(int i=test_stack_size-1; i>=0; --i)
  327. std::cerr << " - " << Eigen::g_test_stack[i] << "\n";
  328. std::cerr << "\n";
  329. if(Eigen::g_test_level==0)
  330. abort();
  331. }
  332. }
  333. #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a))
  334. #define VERIFY_GE(a, b) ::verify_impl(a >= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a >= b))
  335. #define VERIFY_LE(a, b) ::verify_impl(a <= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a <= b))
  336. #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b, true))
  337. #define VERIFY_IS_NOT_EQUAL(a, b) VERIFY(test_is_equal(a, b, false))
  338. #define VERIFY_IS_APPROX(a, b) VERIFY(verifyIsApprox(a, b))
  339. #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b))
  340. #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b))
  341. #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
  342. #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
  343. #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
  344. #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
  345. #define STATIC_CHECK(COND) EIGEN_STATIC_ASSERT( (COND) , EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT )
  346. #define CALL_SUBTEST(FUNC) do { \
  347. g_test_stack.push_back(EIGEN_MAKESTRING(FUNC)); \
  348. FUNC; \
  349. g_test_stack.pop_back(); \
  350. } while (0)
  351. namespace Eigen {
  352. template<typename T1,typename T2>
  353. typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
  354. is_same_type(const T1&, const T2&)
  355. {
  356. return true;
  357. }
  358. template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); }
  359. template<> inline float test_precision<float>() { return 1e-3f; }
  360. template<> inline double test_precision<double>() { return 1e-6; }
  361. template<> inline long double test_precision<long double>() { return 1e-6l; }
  362. template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
  363. template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
  364. template<> inline long double test_precision<std::complex<long double> >() { return test_precision<long double>(); }
  365. #define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE) \
  366. inline bool test_isApprox(TYPE a, TYPE b) \
  367. { return internal::isApprox(a, b, test_precision<TYPE>()); } \
  368. inline bool test_isMuchSmallerThan(TYPE a, TYPE b) \
  369. { return internal::isMuchSmallerThan(a, b, test_precision<TYPE>()); } \
  370. inline bool test_isApproxOrLessThan(TYPE a, TYPE b) \
  371. { return internal::isApproxOrLessThan(a, b, test_precision<TYPE>()); }
  372. EIGEN_TEST_SCALAR_TEST_OVERLOAD(short)
  373. EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned short)
  374. EIGEN_TEST_SCALAR_TEST_OVERLOAD(int)
  375. EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned int)
  376. EIGEN_TEST_SCALAR_TEST_OVERLOAD(long)
  377. EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long)
  378. #if EIGEN_HAS_CXX11
  379. EIGEN_TEST_SCALAR_TEST_OVERLOAD(long long)
  380. EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long long)
  381. #endif
  382. EIGEN_TEST_SCALAR_TEST_OVERLOAD(float)
  383. EIGEN_TEST_SCALAR_TEST_OVERLOAD(double)
  384. EIGEN_TEST_SCALAR_TEST_OVERLOAD(half)
  385. EIGEN_TEST_SCALAR_TEST_OVERLOAD(bfloat16)
  386. #undef EIGEN_TEST_SCALAR_TEST_OVERLOAD
  387. #ifndef EIGEN_TEST_NO_COMPLEX
  388. inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
  389. { return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
  390. inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
  391. { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
  392. inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
  393. { return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
  394. inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
  395. { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
  396. #ifndef EIGEN_TEST_NO_LONGDOUBLE
  397. inline bool test_isApprox(const std::complex<long double>& a, const std::complex<long double>& b)
  398. { return internal::isApprox(a, b, test_precision<std::complex<long double> >()); }
  399. inline bool test_isMuchSmallerThan(const std::complex<long double>& a, const std::complex<long double>& b)
  400. { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<long double> >()); }
  401. #endif
  402. #endif
  403. #ifndef EIGEN_TEST_NO_LONGDOUBLE
  404. inline bool test_isApprox(const long double& a, const long double& b)
  405. {
  406. bool ret = internal::isApprox(a, b, test_precision<long double>());
  407. if (!ret) std::cerr
  408. << std::endl << " actual = " << a
  409. << std::endl << " expected = " << b << std::endl << std::endl;
  410. return ret;
  411. }
  412. inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
  413. { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
  414. inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
  415. { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
  416. #endif // EIGEN_TEST_NO_LONGDOUBLE
  417. // test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox.
  418. template<typename T1,typename T2>
  419. typename NumTraits<typename T1::RealScalar>::NonInteger test_relative_error(const EigenBase<T1> &a, const EigenBase<T2> &b)
  420. {
  421. using std::sqrt;
  422. typedef typename NumTraits<typename T1::RealScalar>::NonInteger RealScalar;
  423. typename internal::nested_eval<T1,2>::type ea(a.derived());
  424. typename internal::nested_eval<T2,2>::type eb(b.derived());
  425. return sqrt(RealScalar((ea-eb).cwiseAbs2().sum()) / RealScalar((std::min)(eb.cwiseAbs2().sum(),ea.cwiseAbs2().sum())));
  426. }
  427. template<typename T1,typename T2>
  428. typename T1::RealScalar test_relative_error(const T1 &a, const T2 &b, const typename T1::Coefficients* = 0)
  429. {
  430. return test_relative_error(a.coeffs(), b.coeffs());
  431. }
  432. template<typename T1,typename T2>
  433. typename T1::Scalar test_relative_error(const T1 &a, const T2 &b, const typename T1::MatrixType* = 0)
  434. {
  435. return test_relative_error(a.matrix(), b.matrix());
  436. }
  437. template<typename S, int D>
  438. S test_relative_error(const Translation<S,D> &a, const Translation<S,D> &b)
  439. {
  440. return test_relative_error(a.vector(), b.vector());
  441. }
  442. template <typename S, int D, int O>
  443. S test_relative_error(const ParametrizedLine<S,D,O> &a, const ParametrizedLine<S,D,O> &b)
  444. {
  445. return (std::max)(test_relative_error(a.origin(), b.origin()), test_relative_error(a.origin(), b.origin()));
  446. }
  447. template <typename S, int D>
  448. S test_relative_error(const AlignedBox<S,D> &a, const AlignedBox<S,D> &b)
  449. {
  450. return (std::max)(test_relative_error((a.min)(), (b.min)()), test_relative_error((a.max)(), (b.max)()));
  451. }
  452. template<typename Derived> class SparseMatrixBase;
  453. template<typename T1,typename T2>
  454. typename T1::RealScalar test_relative_error(const MatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
  455. {
  456. return test_relative_error(a,b.toDense());
  457. }
  458. template<typename Derived> class SparseMatrixBase;
  459. template<typename T1,typename T2>
  460. typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const MatrixBase<T2> &b)
  461. {
  462. return test_relative_error(a.toDense(),b);
  463. }
  464. template<typename Derived> class SparseMatrixBase;
  465. template<typename T1,typename T2>
  466. typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
  467. {
  468. return test_relative_error(a.toDense(),b.toDense());
  469. }
  470. template<typename T1,typename T2>
  471. typename NumTraits<typename NumTraits<T1>::Real>::NonInteger test_relative_error(const T1 &a, const T2 &b, typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T1>::Real>::value, T1>::type* = 0)
  472. {
  473. typedef typename NumTraits<typename NumTraits<T1>::Real>::NonInteger RealScalar;
  474. return numext::sqrt(RealScalar(numext::abs2(a-b))/(numext::mini)(RealScalar(numext::abs2(a)),RealScalar(numext::abs2(b))));
  475. }
  476. template<typename T>
  477. T test_relative_error(const Rotation2D<T> &a, const Rotation2D<T> &b)
  478. {
  479. return test_relative_error(a.angle(), b.angle());
  480. }
  481. template<typename T>
  482. T test_relative_error(const AngleAxis<T> &a, const AngleAxis<T> &b)
  483. {
  484. return (std::max)(test_relative_error(a.angle(), b.angle()), test_relative_error(a.axis(), b.axis()));
  485. }
  486. template<typename Type1, typename Type2>
  487. inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
  488. {
  489. return a.isApprox(b, test_precision<typename Type1::Scalar>());
  490. }
  491. // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions
  492. template<typename T>
  493. typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, const typename T::Scalar* = 0)
  494. {
  495. return test_precision<typename NumTraits<typename T::Scalar>::Real>();
  496. }
  497. template<typename T>
  498. typename NumTraits<T>::Real get_test_precision(const T&,typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
  499. {
  500. return test_precision<typename NumTraits<T>::Real>();
  501. }
  502. // verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails.
  503. template<typename Type1, typename Type2>
  504. inline bool verifyIsApprox(const Type1& a, const Type2& b)
  505. {
  506. bool ret = test_isApprox(a,b);
  507. if(!ret)
  508. {
  509. std::cerr << "Difference too large wrt tolerance " << get_test_precision(a) << ", relative error is: " << test_relative_error(a,b) << std::endl;
  510. }
  511. return ret;
  512. }
  513. // The idea behind this function is to compare the two scalars a and b where
  514. // the scalar ref is a hint about the expected order of magnitude of a and b.
  515. // WARNING: the scalar a and b must be positive
  516. // Therefore, if for some reason a and b are very small compared to ref,
  517. // we won't issue a false negative.
  518. // This test could be: abs(a-b) <= eps * ref
  519. // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
  520. template<typename Scalar,typename ScalarRef>
  521. inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
  522. {
  523. return test_isApprox(a+ref, b+ref);
  524. }
  525. template<typename Derived1, typename Derived2>
  526. inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1,
  527. const MatrixBase<Derived2>& m2)
  528. {
  529. return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
  530. }
  531. template<typename Derived>
  532. inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m,
  533. const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s)
  534. {
  535. return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
  536. }
  537. template<typename Derived>
  538. inline bool test_isUnitary(const MatrixBase<Derived>& m)
  539. {
  540. return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
  541. }
  542. // Forward declaration to avoid ICC warning
  543. template<typename T, typename U>
  544. bool test_is_equal(const T& actual, const U& expected, bool expect_equal=true);
  545. template<typename T, typename U>
  546. bool test_is_equal(const T& actual, const U& expected, bool expect_equal)
  547. {
  548. if ((actual==expected) == expect_equal)
  549. return true;
  550. // false:
  551. std::cerr
  552. << "\n actual = " << actual
  553. << "\n expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
  554. return false;
  555. }
  556. /** Creates a random Partial Isometry matrix of given rank.
  557. *
  558. * A partial isometry is a matrix all of whose singular values are either 0 or 1.
  559. * This is very useful to test rank-revealing algorithms.
  560. */
  561. // Forward declaration to avoid ICC warning
  562. template<typename MatrixType>
  563. void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m);
  564. template<typename MatrixType>
  565. void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
  566. {
  567. typedef typename internal::traits<MatrixType>::Scalar Scalar;
  568. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
  569. typedef Matrix<Scalar, Dynamic, 1> VectorType;
  570. typedef Matrix<Scalar, Rows, Rows> MatrixAType;
  571. typedef Matrix<Scalar, Cols, Cols> MatrixBType;
  572. if(desired_rank == 0)
  573. {
  574. m.setZero(rows,cols);
  575. return;
  576. }
  577. if(desired_rank == 1)
  578. {
  579. // here we normalize the vectors to get a partial isometry
  580. m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose();
  581. return;
  582. }
  583. MatrixAType a = MatrixAType::Random(rows,rows);
  584. MatrixType d = MatrixType::Identity(rows,cols);
  585. MatrixBType b = MatrixBType::Random(cols,cols);
  586. // set the diagonal such that only desired_rank non-zero entries reamain
  587. const Index diag_size = (std::min)(d.rows(),d.cols());
  588. if(diag_size != desired_rank)
  589. d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
  590. HouseholderQR<MatrixAType> qra(a);
  591. HouseholderQR<MatrixBType> qrb(b);
  592. m = qra.householderQ() * d * qrb.householderQ();
  593. }
  594. // Forward declaration to avoid ICC warning
  595. template<typename PermutationVectorType>
  596. void randomPermutationVector(PermutationVectorType& v, Index size);
  597. template<typename PermutationVectorType>
  598. void randomPermutationVector(PermutationVectorType& v, Index size)
  599. {
  600. typedef typename PermutationVectorType::Scalar Scalar;
  601. v.resize(size);
  602. for(Index i = 0; i < size; ++i) v(i) = Scalar(i);
  603. if(size == 1) return;
  604. for(Index n = 0; n < 3 * size; ++n)
  605. {
  606. Index i = internal::random<Index>(0, size-1);
  607. Index j;
  608. do j = internal::random<Index>(0, size-1); while(j==i);
  609. std::swap(v(i), v(j));
  610. }
  611. }
  612. template<typename T> bool isNotNaN(const T& x)
  613. {
  614. return x==x;
  615. }
  616. template<typename T> bool isPlusInf(const T& x)
  617. {
  618. return x > NumTraits<T>::highest();
  619. }
  620. template<typename T> bool isMinusInf(const T& x)
  621. {
  622. return x < NumTraits<T>::lowest();
  623. }
  624. } // end namespace Eigen
  625. template<typename T> struct GetDifferentType;
  626. template<> struct GetDifferentType<float> { typedef double type; };
  627. template<> struct GetDifferentType<double> { typedef float type; };
  628. template<typename T> struct GetDifferentType<std::complex<T> >
  629. { typedef std::complex<typename GetDifferentType<T>::type> type; };
  630. // Forward declaration to avoid ICC warning
  631. template<typename T> std::string type_name();
  632. template<typename T> std::string type_name() { return "other"; }
  633. template<> std::string type_name<float>() { return "float"; }
  634. template<> std::string type_name<double>() { return "double"; }
  635. template<> std::string type_name<long double>() { return "long double"; }
  636. template<> std::string type_name<int>() { return "int"; }
  637. template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
  638. template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
  639. template<> std::string type_name<std::complex<long double> >() { return "complex<long double>"; }
  640. template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
  641. using namespace Eigen;
  642. inline void set_repeat_from_string(const char *str)
  643. {
  644. errno = 0;
  645. g_repeat = int(strtoul(str, 0, 10));
  646. if(errno || g_repeat <= 0)
  647. {
  648. std::cout << "Invalid repeat value " << str << std::endl;
  649. exit(EXIT_FAILURE);
  650. }
  651. g_has_set_repeat = true;
  652. }
  653. inline void set_seed_from_string(const char *str)
  654. {
  655. errno = 0;
  656. g_seed = int(strtoul(str, 0, 10));
  657. if(errno || g_seed == 0)
  658. {
  659. std::cout << "Invalid seed value " << str << std::endl;
  660. exit(EXIT_FAILURE);
  661. }
  662. g_has_set_seed = true;
  663. }
  664. int main(int argc, char *argv[])
  665. {
  666. g_has_set_repeat = false;
  667. g_has_set_seed = false;
  668. bool need_help = false;
  669. for(int i = 1; i < argc; i++)
  670. {
  671. if(argv[i][0] == 'r')
  672. {
  673. if(g_has_set_repeat)
  674. {
  675. std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
  676. return 1;
  677. }
  678. set_repeat_from_string(argv[i]+1);
  679. }
  680. else if(argv[i][0] == 's')
  681. {
  682. if(g_has_set_seed)
  683. {
  684. std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
  685. return 1;
  686. }
  687. set_seed_from_string(argv[i]+1);
  688. }
  689. else
  690. {
  691. need_help = true;
  692. }
  693. }
  694. if(need_help)
  695. {
  696. std::cout << "This test application takes the following optional arguments:" << std::endl;
  697. std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
  698. std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl;
  699. std::cout << std::endl;
  700. std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
  701. std::cout << "will be used as default values for these parameters." << std::endl;
  702. return 1;
  703. }
  704. char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
  705. if(!g_has_set_repeat && env_EIGEN_REPEAT)
  706. set_repeat_from_string(env_EIGEN_REPEAT);
  707. char *env_EIGEN_SEED = getenv("EIGEN_SEED");
  708. if(!g_has_set_seed && env_EIGEN_SEED)
  709. set_seed_from_string(env_EIGEN_SEED);
  710. if(!g_has_set_seed) g_seed = (unsigned int) time(NULL);
  711. if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT;
  712. std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
  713. std::stringstream ss;
  714. ss << "Seed: " << g_seed;
  715. g_test_stack.push_back(ss.str());
  716. srand(g_seed);
  717. std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
  718. VERIFY(EigenTest::all().size()>0);
  719. for(std::size_t i=0; i<EigenTest::all().size(); ++i)
  720. {
  721. const EigenTest& current_test = *EigenTest::all()[i];
  722. Eigen::g_test_stack.push_back(current_test.name());
  723. current_test();
  724. Eigen::g_test_stack.pop_back();
  725. }
  726. return 0;
  727. }
  728. // These warning are disabled here such that they are still ON when parsing Eigen's header files.
  729. #if defined __INTEL_COMPILER
  730. // remark #383: value copied to temporary, reference to temporary used
  731. // -> this warning is raised even for legal usage as: g_test_stack.push_back("foo"); where g_test_stack is a std::vector<std::string>
  732. // remark #1418: external function definition with no prior declaration
  733. // -> this warning is raised for all our test functions. Declaring them static would fix the issue.
  734. // warning #279: controlling expression is constant
  735. // remark #1572: floating-point equality and inequality comparisons are unreliable
  736. #pragma warning disable 279 383 1418 1572
  737. #endif
  738. #ifdef _MSC_VER
  739. // 4503 - decorated name length exceeded, name was truncated
  740. #pragma warning( disable : 4503)
  741. #endif