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.

472 lines
18 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 <vector>
  17. #include <typeinfo>
  18. #include <limits>
  19. #include <algorithm>
  20. #include <sstream>
  21. #include <complex>
  22. #include <deque>
  23. #include <queue>
  24. #define min(A,B) please_protect_your_min_with_parentheses
  25. #define max(A,B) please_protect_your_max_with_parentheses
  26. #define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes
  27. // B0 is defined in POSIX header termios.h
  28. #define B0 FORBIDDEN_IDENTIFIER
  29. // the following file is automatically generated by cmake
  30. #include "split_test_helper.h"
  31. #ifdef NDEBUG
  32. #undef NDEBUG
  33. #endif
  34. // bounds integer values for AltiVec
  35. #ifdef __ALTIVEC__
  36. #define EIGEN_MAKING_DOCS
  37. #endif
  38. #ifndef EIGEN_TEST_FUNC
  39. #error EIGEN_TEST_FUNC must be defined
  40. #endif
  41. #define DEFAULT_REPEAT 10
  42. #ifdef __ICC
  43. // disable warning #279: controlling expression is constant
  44. #pragma warning disable 279
  45. #endif
  46. namespace Eigen
  47. {
  48. static std::vector<std::string> g_test_stack;
  49. static int g_repeat;
  50. static unsigned int g_seed;
  51. static bool g_has_set_repeat, g_has_set_seed;
  52. }
  53. #define EI_PP_MAKE_STRING2(S) #S
  54. #define EI_PP_MAKE_STRING(S) EI_PP_MAKE_STRING2(S)
  55. #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "")
  56. #ifndef EIGEN_NO_ASSERTION_CHECKING
  57. namespace Eigen
  58. {
  59. static const bool should_raise_an_assert = false;
  60. // Used to avoid to raise two exceptions at a time in which
  61. // case the exception is not properly caught.
  62. // This may happen when a second exceptions is triggered in a destructor.
  63. static bool no_more_assert = false;
  64. static bool report_on_cerr_on_assert_failure = true;
  65. struct eigen_assert_exception
  66. {
  67. eigen_assert_exception(void) {}
  68. ~eigen_assert_exception() { Eigen::no_more_assert = false; }
  69. };
  70. }
  71. // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
  72. // one should have been, then the list of excecuted assertions is printed out.
  73. //
  74. // EIGEN_DEBUG_ASSERTS is not enabled by default as it
  75. // significantly increases the compilation time
  76. // and might even introduce side effects that would hide
  77. // some memory errors.
  78. #ifdef EIGEN_DEBUG_ASSERTS
  79. namespace Eigen
  80. {
  81. namespace internal
  82. {
  83. static bool push_assert = false;
  84. }
  85. static std::vector<std::string> eigen_assert_list;
  86. }
  87. #define eigen_assert(a) \
  88. if( (!(a)) && (!no_more_assert) ) \
  89. { \
  90. if(report_on_cerr_on_assert_failure) \
  91. std::cerr << #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \
  92. Eigen::no_more_assert = true; \
  93. throw Eigen::eigen_assert_exception(); \
  94. } \
  95. else if (Eigen::internal::push_assert) \
  96. { \
  97. eigen_assert_list.push_back(std::string(EI_PP_MAKE_STRING(__FILE__) " (" EI_PP_MAKE_STRING(__LINE__) ") : " #a) ); \
  98. }
  99. #define VERIFY_RAISES_ASSERT(a) \
  100. { \
  101. Eigen::no_more_assert = false; \
  102. Eigen::eigen_assert_list.clear(); \
  103. Eigen::internal::push_assert = true; \
  104. Eigen::report_on_cerr_on_assert_failure = false; \
  105. try { \
  106. a; \
  107. std::cerr << "One of the following asserts should have been triggered:\n"; \
  108. for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai) \
  109. std::cerr << " " << eigen_assert_list[ai] << "\n"; \
  110. VERIFY(Eigen::should_raise_an_assert && # a); \
  111. } catch (Eigen::eigen_assert_exception) { \
  112. Eigen::internal::push_assert = false; VERIFY(true); \
  113. } \
  114. Eigen::report_on_cerr_on_assert_failure = true; \
  115. Eigen::internal::push_assert = false; \
  116. }
  117. #else // EIGEN_DEBUG_ASSERTS
  118. // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3
  119. #define eigen_assert(a) \
  120. if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
  121. { \
  122. Eigen::no_more_assert = true; \
  123. if(report_on_cerr_on_assert_failure) \
  124. eigen_plain_assert(a); \
  125. else \
  126. throw Eigen::eigen_assert_exception(); \
  127. }
  128. #define VERIFY_RAISES_ASSERT(a) { \
  129. Eigen::no_more_assert = false; \
  130. Eigen::report_on_cerr_on_assert_failure = false; \
  131. try { \
  132. a; \
  133. VERIFY(Eigen::should_raise_an_assert && # a); \
  134. } \
  135. catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \
  136. Eigen::report_on_cerr_on_assert_failure = true; \
  137. }
  138. #endif // EIGEN_DEBUG_ASSERTS
  139. #define EIGEN_USE_CUSTOM_ASSERT
  140. #else // EIGEN_NO_ASSERTION_CHECKING
  141. #define VERIFY_RAISES_ASSERT(a) {}
  142. #endif // EIGEN_NO_ASSERTION_CHECKING
  143. #define EIGEN_INTERNAL_DEBUGGING
  144. #include <Eigen/QR> // required for createRandomPIMatrixOfRank
  145. static void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
  146. {
  147. if (!condition)
  148. {
  149. std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")" \
  150. << std::endl << " " << condition_as_string << std::endl << std::endl; \
  151. abort();
  152. }
  153. }
  154. #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a))
  155. #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b))
  156. #define VERIFY_IS_APPROX(a, b) VERIFY(test_isApprox(a, b))
  157. #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b))
  158. #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b))
  159. #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
  160. #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
  161. #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
  162. #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
  163. #define CALL_SUBTEST(FUNC) do { \
  164. g_test_stack.push_back(EI_PP_MAKE_STRING(FUNC)); \
  165. FUNC; \
  166. g_test_stack.pop_back(); \
  167. } while (0)
  168. namespace Eigen {
  169. template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); }
  170. template<> inline float test_precision<float>() { return 1e-3f; }
  171. template<> inline double test_precision<double>() { return 1e-6; }
  172. template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
  173. template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
  174. template<> inline long double test_precision<long double>() { return 1e-6; }
  175. inline bool test_isApprox(const int& a, const int& b)
  176. { return internal::isApprox(a, b, test_precision<int>()); }
  177. inline bool test_isMuchSmallerThan(const int& a, const int& b)
  178. { return internal::isMuchSmallerThan(a, b, test_precision<int>()); }
  179. inline bool test_isApproxOrLessThan(const int& a, const int& b)
  180. { return internal::isApproxOrLessThan(a, b, test_precision<int>()); }
  181. inline bool test_isApprox(const float& a, const float& b)
  182. { return internal::isApprox(a, b, test_precision<float>()); }
  183. inline bool test_isMuchSmallerThan(const float& a, const float& b)
  184. { return internal::isMuchSmallerThan(a, b, test_precision<float>()); }
  185. inline bool test_isApproxOrLessThan(const float& a, const float& b)
  186. { return internal::isApproxOrLessThan(a, b, test_precision<float>()); }
  187. inline bool test_isApprox(const double& a, const double& b)
  188. { return internal::isApprox(a, b, test_precision<double>()); }
  189. inline bool test_isMuchSmallerThan(const double& a, const double& b)
  190. { return internal::isMuchSmallerThan(a, b, test_precision<double>()); }
  191. inline bool test_isApproxOrLessThan(const double& a, const double& b)
  192. { return internal::isApproxOrLessThan(a, b, test_precision<double>()); }
  193. inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
  194. { return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
  195. inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
  196. { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
  197. inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
  198. { return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
  199. inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
  200. { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
  201. inline bool test_isApprox(const long double& a, const long double& b)
  202. {
  203. bool ret = internal::isApprox(a, b, test_precision<long double>());
  204. if (!ret) std::cerr
  205. << std::endl << " actual = " << a
  206. << std::endl << " expected = " << b << std::endl << std::endl;
  207. return ret;
  208. }
  209. inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
  210. { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
  211. inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
  212. { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
  213. template<typename Type1, typename Type2>
  214. inline bool test_isApprox(const Type1& a, const Type2& b)
  215. {
  216. return a.isApprox(b, test_precision<typename Type1::Scalar>());
  217. }
  218. // The idea behind this function is to compare the two scalars a and b where
  219. // the scalar ref is a hint about the expected order of magnitude of a and b.
  220. // Therefore, if for some reason a and b are very small compared to ref,
  221. // we won't issue a false negative.
  222. // This test could be: abs(a-b) <= eps * ref
  223. // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
  224. template<typename Scalar,typename ScalarRef>
  225. inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
  226. {
  227. return test_isApprox(a+ref, b+ref);
  228. }
  229. template<typename Derived1, typename Derived2>
  230. inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1,
  231. const MatrixBase<Derived2>& m2)
  232. {
  233. return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
  234. }
  235. template<typename Derived>
  236. inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m,
  237. const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s)
  238. {
  239. return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
  240. }
  241. template<typename Derived>
  242. inline bool test_isUnitary(const MatrixBase<Derived>& m)
  243. {
  244. return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
  245. }
  246. template<typename T, typename U>
  247. bool test_is_equal(const T& actual, const U& expected)
  248. {
  249. if (actual==expected)
  250. return true;
  251. // false:
  252. std::cerr
  253. << std::endl << " actual = " << actual
  254. << std::endl << " expected = " << expected << std::endl << std::endl;
  255. return false;
  256. }
  257. /** Creates a random Partial Isometry matrix of given rank.
  258. *
  259. * A partial isometry is a matrix all of whose singular values are either 0 or 1.
  260. * This is very useful to test rank-revealing algorithms.
  261. */
  262. template<typename MatrixType>
  263. void createRandomPIMatrixOfRank(typename MatrixType::Index desired_rank, typename MatrixType::Index rows, typename MatrixType::Index cols, MatrixType& m)
  264. {
  265. typedef typename internal::traits<MatrixType>::Index Index;
  266. typedef typename internal::traits<MatrixType>::Scalar Scalar;
  267. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
  268. typedef Matrix<Scalar, Dynamic, 1> VectorType;
  269. typedef Matrix<Scalar, Rows, Rows> MatrixAType;
  270. typedef Matrix<Scalar, Cols, Cols> MatrixBType;
  271. if(desired_rank == 0)
  272. {
  273. m.setZero(rows,cols);
  274. return;
  275. }
  276. if(desired_rank == 1)
  277. {
  278. // here we normalize the vectors to get a partial isometry
  279. m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose();
  280. return;
  281. }
  282. MatrixAType a = MatrixAType::Random(rows,rows);
  283. MatrixType d = MatrixType::Identity(rows,cols);
  284. MatrixBType b = MatrixBType::Random(cols,cols);
  285. // set the diagonal such that only desired_rank non-zero entries reamain
  286. const Index diag_size = (std::min)(d.rows(),d.cols());
  287. if(diag_size != desired_rank)
  288. d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
  289. HouseholderQR<MatrixAType> qra(a);
  290. HouseholderQR<MatrixBType> qrb(b);
  291. m = qra.householderQ() * d * qrb.householderQ();
  292. }
  293. template<typename PermutationVectorType>
  294. void randomPermutationVector(PermutationVectorType& v, typename PermutationVectorType::Index size)
  295. {
  296. typedef typename PermutationVectorType::Index Index;
  297. typedef typename PermutationVectorType::Scalar Scalar;
  298. v.resize(size);
  299. for(Index i = 0; i < size; ++i) v(i) = Scalar(i);
  300. if(size == 1) return;
  301. for(Index n = 0; n < 3 * size; ++n)
  302. {
  303. Index i = internal::random<Index>(0, size-1);
  304. Index j;
  305. do j = internal::random<Index>(0, size-1); while(j==i);
  306. std::swap(v(i), v(j));
  307. }
  308. }
  309. } // end namespace Eigen
  310. template<typename T> struct GetDifferentType;
  311. template<> struct GetDifferentType<float> { typedef double type; };
  312. template<> struct GetDifferentType<double> { typedef float type; };
  313. template<typename T> struct GetDifferentType<std::complex<T> >
  314. { typedef std::complex<typename GetDifferentType<T>::type> type; };
  315. template<typename T> std::string type_name() { return "other"; }
  316. template<> std::string type_name<float>() { return "float"; }
  317. template<> std::string type_name<double>() { return "double"; }
  318. template<> std::string type_name<int>() { return "int"; }
  319. template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
  320. template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
  321. template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
  322. // forward declaration of the main test function
  323. void EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
  324. using namespace Eigen;
  325. void set_repeat_from_string(const char *str)
  326. {
  327. errno = 0;
  328. g_repeat = int(strtoul(str, 0, 10));
  329. if(errno || g_repeat <= 0)
  330. {
  331. std::cout << "Invalid repeat value " << str << std::endl;
  332. exit(EXIT_FAILURE);
  333. }
  334. g_has_set_repeat = true;
  335. }
  336. void set_seed_from_string(const char *str)
  337. {
  338. errno = 0;
  339. g_seed = strtoul(str, 0, 10);
  340. if(errno || g_seed == 0)
  341. {
  342. std::cout << "Invalid seed value " << str << std::endl;
  343. exit(EXIT_FAILURE);
  344. }
  345. g_has_set_seed = true;
  346. }
  347. int main(int argc, char *argv[])
  348. {
  349. g_has_set_repeat = false;
  350. g_has_set_seed = false;
  351. bool need_help = false;
  352. for(int i = 1; i < argc; i++)
  353. {
  354. if(argv[i][0] == 'r')
  355. {
  356. if(g_has_set_repeat)
  357. {
  358. std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
  359. return 1;
  360. }
  361. set_repeat_from_string(argv[i]+1);
  362. }
  363. else if(argv[i][0] == 's')
  364. {
  365. if(g_has_set_seed)
  366. {
  367. std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
  368. return 1;
  369. }
  370. set_seed_from_string(argv[i]+1);
  371. }
  372. else
  373. {
  374. need_help = true;
  375. }
  376. }
  377. if(need_help)
  378. {
  379. std::cout << "This test application takes the following optional arguments:" << std::endl;
  380. std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
  381. std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl;
  382. std::cout << std::endl;
  383. std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
  384. std::cout << "will be used as default values for these parameters." << std::endl;
  385. return 1;
  386. }
  387. char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
  388. if(!g_has_set_repeat && env_EIGEN_REPEAT)
  389. set_repeat_from_string(env_EIGEN_REPEAT);
  390. char *env_EIGEN_SEED = getenv("EIGEN_SEED");
  391. if(!g_has_set_seed && env_EIGEN_SEED)
  392. set_seed_from_string(env_EIGEN_SEED);
  393. if(!g_has_set_seed) g_seed = (unsigned int) time(NULL);
  394. if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT;
  395. std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
  396. srand(g_seed);
  397. std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
  398. Eigen::g_test_stack.push_back(EI_PP_MAKE_STRING(EIGEN_TEST_FUNC));
  399. EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
  400. return 0;
  401. }