You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

724 lines
27 KiB

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