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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:35