symbolic_index.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifdef EIGEN_TEST_PART_2
  10. #define EIGEN_MAX_CPP_VER 03
  11. // see indexed_view.cpp
  12. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  13. #pragma GCC diagnostic ignored "-Wdeprecated"
  14. #endif
  15. #endif
  16. #include "main.h"
  17. template<typename T1,typename T2>
  18. bool is_same_symb(const T1& a, const T2& b, Index size)
  19. {
  20. return a.eval(last=size-1) == b.eval(last=size-1);
  21. }
  22. template<typename T>
  23. void check_is_symbolic(const T&) {
  24. STATIC_CHECK(( symbolic::is_symbolic<T>::value ))
  25. }
  26. template<typename T>
  27. void check_isnot_symbolic(const T&) {
  28. STATIC_CHECK(( !symbolic::is_symbolic<T>::value ))
  29. }
  30. #define VERIFY_EQ_INT(A,B) VERIFY_IS_APPROX(int(A),int(B))
  31. void check_symbolic_index()
  32. {
  33. check_is_symbolic(last);
  34. check_is_symbolic(lastp1);
  35. check_is_symbolic(last+1);
  36. check_is_symbolic(last-lastp1);
  37. check_is_symbolic(2*last-lastp1/2);
  38. check_isnot_symbolic(fix<3>());
  39. Index size=100;
  40. // First, let's check FixedInt arithmetic:
  41. VERIFY( is_same_type( (fix<5>()-fix<3>())*fix<9>()/(-fix<3>()), fix<-(5-3)*9/3>() ) );
  42. VERIFY( is_same_type( (fix<5>()-fix<3>())*fix<9>()/fix<2>(), fix<(5-3)*9/2>() ) );
  43. VERIFY( is_same_type( fix<9>()/fix<2>(), fix<9/2>() ) );
  44. VERIFY( is_same_type( fix<9>()%fix<2>(), fix<9%2>() ) );
  45. VERIFY( is_same_type( fix<9>()&fix<2>(), fix<9&2>() ) );
  46. VERIFY( is_same_type( fix<9>()|fix<2>(), fix<9|2>() ) );
  47. VERIFY( is_same_type( fix<9>()/2, int(9/2) ) );
  48. VERIFY( is_same_symb( lastp1-1, last, size) );
  49. VERIFY( is_same_symb( lastp1-fix<1>, last, size) );
  50. VERIFY_IS_EQUAL( ( (last*5-2)/3 ).eval(last=size-1), ((size-1)*5-2)/3 );
  51. VERIFY_IS_EQUAL( ( (last*fix<5>-fix<2>)/fix<3> ).eval(last=size-1), ((size-1)*5-2)/3 );
  52. VERIFY_IS_EQUAL( ( -last*lastp1 ).eval(last=size-1), -(size-1)*size );
  53. VERIFY_IS_EQUAL( ( lastp1-3*last ).eval(last=size-1), size- 3*(size-1) );
  54. VERIFY_IS_EQUAL( ( (lastp1-3*last)/lastp1 ).eval(last=size-1), (size- 3*(size-1))/size );
  55. #if EIGEN_HAS_CXX14
  56. {
  57. struct x_tag {}; static const symbolic::SymbolExpr<x_tag> x;
  58. struct y_tag {}; static const symbolic::SymbolExpr<y_tag> y;
  59. struct z_tag {}; static const symbolic::SymbolExpr<z_tag> z;
  60. VERIFY_IS_APPROX( int(((x+3)/y+z).eval(x=6,y=3,z=-13)), (6+3)/3+(-13) );
  61. }
  62. #endif
  63. }
  64. EIGEN_DECLARE_TEST(symbolic_index)
  65. {
  66. CALL_SUBTEST_1( check_symbolic_index() );
  67. CALL_SUBTEST_2( check_symbolic_index() );
  68. }