xref: /aosp_15_r20/external/eigen/test/symbolic_index.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2017 Gael Guennebaud <[email protected]>
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 
10 #ifdef EIGEN_TEST_PART_2
11 #define EIGEN_MAX_CPP_VER 03
12 
13 // see indexed_view.cpp
14 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
15   #pragma GCC diagnostic ignored "-Wdeprecated"
16 #endif
17 
18 #endif
19 
20 #include "main.h"
21 
22 template<typename T1,typename T2>
is_same_symb(const T1 & a,const T2 & b,Index size)23 bool is_same_symb(const T1& a, const T2& b, Index size)
24 {
25   return a.eval(last=size-1) == b.eval(last=size-1);
26 }
27 
28 template<typename T>
check_is_symbolic(const T &)29 void check_is_symbolic(const T&) {
30   STATIC_CHECK(( symbolic::is_symbolic<T>::value ))
31 }
32 
33 template<typename T>
check_isnot_symbolic(const T &)34 void check_isnot_symbolic(const T&) {
35   STATIC_CHECK(( !symbolic::is_symbolic<T>::value ))
36 }
37 
38 #define VERIFY_EQ_INT(A,B) VERIFY_IS_APPROX(int(A),int(B))
39 
check_symbolic_index()40 void check_symbolic_index()
41 {
42   check_is_symbolic(last);
43   check_is_symbolic(lastp1);
44   check_is_symbolic(last+1);
45   check_is_symbolic(last-lastp1);
46   check_is_symbolic(2*last-lastp1/2);
47   check_isnot_symbolic(fix<3>());
48 
49   Index size=100;
50 
51   // First, let's check FixedInt arithmetic:
52   VERIFY( is_same_type( (fix<5>()-fix<3>())*fix<9>()/(-fix<3>()), fix<-(5-3)*9/3>() ) );
53   VERIFY( is_same_type( (fix<5>()-fix<3>())*fix<9>()/fix<2>(), fix<(5-3)*9/2>() ) );
54   VERIFY( is_same_type( fix<9>()/fix<2>(), fix<9/2>() ) );
55   VERIFY( is_same_type( fix<9>()%fix<2>(), fix<9%2>() ) );
56   VERIFY( is_same_type( fix<9>()&fix<2>(), fix<9&2>() ) );
57   VERIFY( is_same_type( fix<9>()|fix<2>(), fix<9|2>() ) );
58   VERIFY( is_same_type( fix<9>()/2, int(9/2) ) );
59 
60   VERIFY( is_same_symb( lastp1-1, last, size) );
61   VERIFY( is_same_symb( lastp1-fix<1>, last, size) );
62 
63   VERIFY_IS_EQUAL( ( (last*5-2)/3 ).eval(last=size-1), ((size-1)*5-2)/3 );
64   VERIFY_IS_EQUAL( ( (last*fix<5>-fix<2>)/fix<3> ).eval(last=size-1), ((size-1)*5-2)/3 );
65   VERIFY_IS_EQUAL( ( -last*lastp1  ).eval(last=size-1), -(size-1)*size );
66   VERIFY_IS_EQUAL( ( lastp1-3*last  ).eval(last=size-1), size- 3*(size-1) );
67   VERIFY_IS_EQUAL( ( (lastp1-3*last)/lastp1  ).eval(last=size-1), (size- 3*(size-1))/size );
68 
69 #if EIGEN_HAS_CXX14
70   {
71     struct x_tag {};  static const symbolic::SymbolExpr<x_tag> x;
72     struct y_tag {};  static const symbolic::SymbolExpr<y_tag> y;
73     struct z_tag {};  static const symbolic::SymbolExpr<z_tag> z;
74 
75     VERIFY_IS_APPROX( int(((x+3)/y+z).eval(x=6,y=3,z=-13)), (6+3)/3+(-13) );
76   }
77 #endif
78 }
79 
EIGEN_DECLARE_TEST(symbolic_index)80 EIGEN_DECLARE_TEST(symbolic_index)
81 {
82   CALL_SUBTEST_1( check_symbolic_index() );
83   CALL_SUBTEST_2( check_symbolic_index() );
84 }
85