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