indexed_view.cpp
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) 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 
10 #ifdef EIGEN_TEST_PART_2
11 // Make sure we also check c++11 max implementation
12 #define EIGEN_MAX_CPP_VER 11
13 #endif
14 
15 #ifdef EIGEN_TEST_PART_3
16 // Make sure we also check c++98 max implementation
17 #define EIGEN_MAX_CPP_VER 03
18 
19 // We need to disable this warning when compiling with c++11 while limiting Eigen to c++98
20 // Ideally we would rather configure the compiler to build in c++98 mode but this needs
21 // to be done at the CMakeLists.txt level.
22 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
23  #pragma GCC diagnostic ignored "-Wdeprecated"
24 #endif
25 
26 #if defined(__GNUC__) && (__GNUC__ >=9)
27  #pragma GCC diagnostic ignored "-Wdeprecated-copy"
28 #endif
29 #if defined(__clang__) && (__clang_major__ >= 10)
30  #pragma clang diagnostic ignored "-Wdeprecated-copy"
31 #endif
32 
33 #endif
34 
35 #include <valarray>
36 #include <vector>
37 #include "main.h"
38 
39 #if EIGEN_HAS_CXX11
40 #include <array>
41 #endif
42 
43 typedef std::pair<Index,Index> IndexPair;
44 
45 int encode(Index i, Index j) {
46  return int(i*100 + j);
47 }
48 
50  return IndexPair(ij / 100, ij % 100);
51 }
52 
53 template<typename T>
54 bool match(const T& xpr, std::string ref, std::string str_xpr = "") {
55  EIGEN_UNUSED_VARIABLE(str_xpr);
56  std::stringstream str;
57  str << xpr;
58  if(!(str.str() == ref))
59  std::cout << str_xpr << "\n" << xpr << "\n\n";
60  return str.str() == ref;
61 }
62 
63 #define MATCH(X,R) match(X, R, #X)
64 
65 template<typename T1,typename T2>
67 is_same_eq(const T1& a, const T2& b)
68 {
69  return (a == b).all();
70 }
71 
72 template<typename T1,typename T2>
73 bool is_same_seq(const T1& a, const T2& b)
74 {
75  bool ok = a.first()==b.first() && a.size() == b.size() && Index(a.incrObject())==Index(b.incrObject());;
76  if(!ok)
77  {
78  std::cerr << "seqN(" << a.first() << ", " << a.size() << ", " << Index(a.incrObject()) << ") != ";
79  std::cerr << "seqN(" << b.first() << ", " << b.size() << ", " << Index(b.incrObject()) << ")\n";
80  }
81  return ok;
82 }
83 
84 template<typename T1,typename T2>
86 is_same_seq_type(const T1& a, const T2& b)
87 {
88  return is_same_seq(a,b);
89 }
90 
91 
92 
93 #define VERIFY_EQ_INT(A,B) VERIFY_IS_APPROX(int(A),int(B))
94 
95 // C++03 does not allow local or unnamed enums as index
96 enum DummyEnum { XX=0, YY=1 };
97 
99 {
100  Index n = 10;
101 
102  ArrayXd a = ArrayXd::LinSpaced(n,0,n-1);
103  Array<double,1,Dynamic> b = a.transpose();
104 
105  #if EIGEN_COMP_CXXVER>=14
106  ArrayXXi A = ArrayXXi::NullaryExpr(n,n, std::ref(encode));
107  #else
108  ArrayXXi A = ArrayXXi::NullaryExpr(n,n, std::ptr_fun(&encode));
109  #endif
110 
111  for(Index i=0; i<n; ++i)
112  for(Index j=0; j<n; ++j)
113  VERIFY( decode(A(i,j)) == IndexPair(i,j) );
114 
115  Array4i eii(4); eii << 3, 1, 6, 5;
116  std::valarray<int> vali(4); Map<ArrayXi>(&vali[0],4) = eii;
117  std::vector<int> veci(4); Map<ArrayXi>(veci.data(),4) = eii;
118 
119  VERIFY( MATCH( A(3, seq(9,3,-1)),
120  "309 308 307 306 305 304 303")
121  );
122 
123  VERIFY( MATCH( A(seqN(2,5), seq(9,3,-1)),
124  "209 208 207 206 205 204 203\n"
125  "309 308 307 306 305 304 303\n"
126  "409 408 407 406 405 404 403\n"
127  "509 508 507 506 505 504 503\n"
128  "609 608 607 606 605 604 603")
129  );
130 
131  VERIFY( MATCH( A(seqN(2,5), 5),
132  "205\n"
133  "305\n"
134  "405\n"
135  "505\n"
136  "605")
137  );
138 
139  VERIFY( MATCH( A(seqN(last,5,-1), seq(2,last)),
140  "902 903 904 905 906 907 908 909\n"
141  "802 803 804 805 806 807 808 809\n"
142  "702 703 704 705 706 707 708 709\n"
143  "602 603 604 605 606 607 608 609\n"
144  "502 503 504 505 506 507 508 509")
145  );
146 
147  VERIFY( MATCH( A(eii, veci),
148  "303 301 306 305\n"
149  "103 101 106 105\n"
150  "603 601 606 605\n"
151  "503 501 506 505")
152  );
153 
154  VERIFY( MATCH( A(eii, all),
155  "300 301 302 303 304 305 306 307 308 309\n"
156  "100 101 102 103 104 105 106 107 108 109\n"
157  "600 601 602 603 604 605 606 607 608 609\n"
158  "500 501 502 503 504 505 506 507 508 509")
159  );
160 
161  // take row number 3, and repeat it 5 times
162  VERIFY( MATCH( A(seqN(3,5,0), all),
163  "300 301 302 303 304 305 306 307 308 309\n"
164  "300 301 302 303 304 305 306 307 308 309\n"
165  "300 301 302 303 304 305 306 307 308 309\n"
166  "300 301 302 303 304 305 306 307 308 309\n"
167  "300 301 302 303 304 305 306 307 308 309")
168  );
169 
170  VERIFY( MATCH( a(seqN(3,3),0), "3\n4\n5" ) );
171  VERIFY( MATCH( a(seq(3,5)), "3\n4\n5" ) );
172  VERIFY( MATCH( a(seqN(3,3,1)), "3\n4\n5" ) );
173  VERIFY( MATCH( a(seqN(5,3,-1)), "5\n4\n3" ) );
174 
175  VERIFY( MATCH( b(0,seqN(3,3)), "3 4 5" ) );
176  VERIFY( MATCH( b(seq(3,5)), "3 4 5" ) );
177  VERIFY( MATCH( b(seqN(3,3,1)), "3 4 5" ) );
178  VERIFY( MATCH( b(seqN(5,3,-1)), "5 4 3" ) );
179 
180  VERIFY( MATCH( b(all), "0 1 2 3 4 5 6 7 8 9" ) );
181  VERIFY( MATCH( b(eii), "3 1 6 5" ) );
182 
183  Array44i B;
184  B.setRandom();
185  VERIFY( (A(seqN(2,5), 5)).ColsAtCompileTime == 1);
186  VERIFY( (A(seqN(2,5), 5)).RowsAtCompileTime == Dynamic);
187  VERIFY_EQ_INT( (A(seqN(2,5), 5)).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
188  VERIFY_EQ_INT( (A(seqN(2,5), 5)).OuterStrideAtCompileTime , A.col(5).OuterStrideAtCompileTime);
189 
190  VERIFY_EQ_INT( (A(5,seqN(2,5))).InnerStrideAtCompileTime , A.row(5).InnerStrideAtCompileTime);
191  VERIFY_EQ_INT( (A(5,seqN(2,5))).OuterStrideAtCompileTime , A.row(5).OuterStrideAtCompileTime);
192  VERIFY_EQ_INT( (B(1,seqN(1,2))).InnerStrideAtCompileTime , B.row(1).InnerStrideAtCompileTime);
193  VERIFY_EQ_INT( (B(1,seqN(1,2))).OuterStrideAtCompileTime , B.row(1).OuterStrideAtCompileTime);
194 
195  VERIFY_EQ_INT( (A(seqN(2,5), seq(1,3))).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
196  VERIFY_EQ_INT( (A(seqN(2,5), seq(1,3))).OuterStrideAtCompileTime , A.OuterStrideAtCompileTime);
197  VERIFY_EQ_INT( (B(seqN(1,2), seq(1,3))).InnerStrideAtCompileTime , B.InnerStrideAtCompileTime);
198  VERIFY_EQ_INT( (B(seqN(1,2), seq(1,3))).OuterStrideAtCompileTime , B.OuterStrideAtCompileTime);
199  VERIFY_EQ_INT( (A(seqN(2,5,2), seq(1,3,2))).InnerStrideAtCompileTime , Dynamic);
200  VERIFY_EQ_INT( (A(seqN(2,5,2), seq(1,3,2))).OuterStrideAtCompileTime , Dynamic);
201  VERIFY_EQ_INT( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
202  VERIFY_EQ_INT( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , Dynamic);
203  VERIFY_EQ_INT( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
204  VERIFY_EQ_INT( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , 3*4);
205 
206  VERIFY_EQ_INT( (A(seqN(2,fix<5>), seqN(1,fix<3>))).RowsAtCompileTime, 5);
207  VERIFY_EQ_INT( (A(seqN(2,fix<5>), seqN(1,fix<3>))).ColsAtCompileTime, 3);
208  VERIFY_EQ_INT( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).RowsAtCompileTime, 5);
209  VERIFY_EQ_INT( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).ColsAtCompileTime, 3);
210  VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).RowsAtCompileTime, Dynamic);
211  VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).ColsAtCompileTime, Dynamic);
212  VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).rows(), 5);
213  VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).cols(), 3);
214 
215  VERIFY( is_same_seq_type( seqN(2,5,fix<-1>), seqN(2,5,fix<-1>(-1)) ) );
216  VERIFY( is_same_seq_type( seqN(2,5), seqN(2,5,fix<1>(1)) ) );
217  VERIFY( is_same_seq_type( seqN(2,5,3), seqN(2,5,fix<DynamicIndex>(3)) ) );
218  VERIFY( is_same_seq_type( seq(2,7,fix<3>), seqN(2,2,fix<3>) ) );
219  VERIFY( is_same_seq_type( seqN(2,fix<Dynamic>(5),3), seqN(2,5,fix<DynamicIndex>(3)) ) );
220  VERIFY( is_same_seq_type( seqN(2,fix<5>(5),fix<-2>), seqN(2,fix<5>,fix<-2>()) ) );
221 
222  VERIFY( is_same_seq_type( seq(2,fix<5>), seqN(2,4) ) );
223 #if EIGEN_HAS_CXX11
224  VERIFY( is_same_seq_type( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
225  VERIFY( is_same_seq( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
226  VERIFY( is_same_seq( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
227  seq(fix<1>,fix<5>,fix<2>()) ) );
228  VERIFY( is_same_seq_type( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
229  VERIFY( is_same_seq_type( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
230  seq(fix<1>,fix<5>,fix<2>()) ) );
231 
232  VERIFY( is_same_seq_type( seqN(2,std::integral_constant<int,5>()), seqN(2,fix<5>) ) );
233  VERIFY( is_same_seq_type( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>()), seq(fix<1>,fix<5>) ) );
234 #else
235  // sorry, no compile-time size recovery in c++98/03
236  VERIFY( is_same_seq( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
237 #endif
238 
239  VERIFY( (A(seqN(2,fix<5>), 5)).RowsAtCompileTime == 5);
240  VERIFY( (A(4, all)).ColsAtCompileTime == Dynamic);
241  VERIFY( (A(4, all)).RowsAtCompileTime == 1);
242  VERIFY( (B(1, all)).ColsAtCompileTime == 4);
243  VERIFY( (B(1, all)).RowsAtCompileTime == 1);
244  VERIFY( (B(all,1)).ColsAtCompileTime == 1);
245  VERIFY( (B(all,1)).RowsAtCompileTime == 4);
246 
247  VERIFY(int( (A(all, eii)).ColsAtCompileTime) == int(eii.SizeAtCompileTime));
248  VERIFY_EQ_INT( (A(eii, eii)).Flags&DirectAccessBit, (unsigned int)(0));
249  VERIFY_EQ_INT( (A(eii, eii)).InnerStrideAtCompileTime, 0);
250  VERIFY_EQ_INT( (A(eii, eii)).OuterStrideAtCompileTime, 0);
251 
252  VERIFY_IS_APPROX( A(seq(n-1,2,-2), seqN(n-1-6,3,-1)), A(seq(last,2,fix<-2>), seqN(last-6,3,fix<-1>)) );
253 
254  VERIFY_IS_APPROX( A(seq(n-1,2,-2), seqN(n-1-6,4)), A(seq(last,2,-2), seqN(last-6,4)) );
255  VERIFY_IS_APPROX( A(seq(n-1-6,n-1-2), seqN(n-1-6,4)), A(seq(last-6,last-2), seqN(6+last-6-6,4)) );
256  VERIFY_IS_APPROX( A(seq((n-1)/2,(n)/2+3), seqN(2,4)), A(seq(last/2,(last+1)/2+3), seqN(last+2-last,4)) );
257  VERIFY_IS_APPROX( A(seq(n-2,2,-2), seqN(n-8,4)), A(seq(lastp1-2,2,-2), seqN(lastp1-8,4)) );
258 
259  // Check all combinations of seq:
260  VERIFY_IS_APPROX( A(seq(1,n-1-2,2), seq(1,n-1-2,2)), A(seq(1,last-2,2), seq(1,last-2,fix<2>)) );
261  VERIFY_IS_APPROX( A(seq(n-1-5,n-1-2,2), seq(n-1-5,n-1-2,2)), A(seq(last-5,last-2,2), seq(last-5,last-2,fix<2>)) );
262  VERIFY_IS_APPROX( A(seq(n-1-5,7,2), seq(n-1-5,7,2)), A(seq(last-5,7,2), seq(last-5,7,fix<2>)) );
263  VERIFY_IS_APPROX( A(seq(1,n-1-2), seq(n-1-5,7)), A(seq(1,last-2), seq(last-5,7)) );
264  VERIFY_IS_APPROX( A(seq(n-1-5,n-1-2), seq(n-1-5,n-1-2)), A(seq(last-5,last-2), seq(last-5,last-2)) );
265 
266  VERIFY_IS_APPROX( A.col(A.cols()-1), A(all,last) );
267  VERIFY_IS_APPROX( A(A.rows()-2, A.cols()/2), A(last-1, lastp1/2) );
268  VERIFY_IS_APPROX( a(a.size()-2), a(last-1) );
269  VERIFY_IS_APPROX( a(a.size()/2), a((last+1)/2) );
270 
271  // Check fall-back to Block
272  {
273  VERIFY( is_same_eq(A.col(0), A(all,0)) );
274  VERIFY( is_same_eq(A.row(0), A(0,all)) );
275  VERIFY( is_same_eq(A.block(0,0,2,2), A(seqN(0,2),seq(0,1))) );
276  VERIFY( is_same_eq(A.middleRows(2,4), A(seqN(2,4),all)) );
277  VERIFY( is_same_eq(A.middleCols(2,4), A(all,seqN(2,4))) );
278 
279  VERIFY( is_same_eq(A.col(A.cols()-1), A(all,last)) );
280 
281  const ArrayXXi& cA(A);
282  VERIFY( is_same_eq(cA.col(0), cA(all,0)) );
283  VERIFY( is_same_eq(cA.row(0), cA(0,all)) );
284  VERIFY( is_same_eq(cA.block(0,0,2,2), cA(seqN(0,2),seq(0,1))) );
285  VERIFY( is_same_eq(cA.middleRows(2,4), cA(seqN(2,4),all)) );
286  VERIFY( is_same_eq(cA.middleCols(2,4), cA(all,seqN(2,4))) );
287 
288  VERIFY( is_same_eq(a.head(4), a(seq(0,3))) );
289  VERIFY( is_same_eq(a.tail(4), a(seqN(last-3,4))) );
290  VERIFY( is_same_eq(a.tail(4), a(seq(lastp1-4,last))) );
291  VERIFY( is_same_eq(a.segment<4>(3), a(seqN(3,fix<4>))) );
292  }
293 
294  ArrayXXi A1=A, A2 = ArrayXXi::Random(4,4);
295  ArrayXi range25(4); range25 << 3,2,4,5;
296  A1(seqN(3,4),seq(2,5)) = A2;
297  VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
298  A1 = A;
299  A2.setOnes();
300  A1(seq(6,3,-1),range25) = A2;
301  VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
302 
303  // check reverse
304  {
305  VERIFY( is_same_seq_type( seq(3,7).reverse(), seqN(7,5,fix<-1>) ) );
306  VERIFY( is_same_seq_type( seq(7,3,fix<-2>).reverse(), seqN(3,3,fix<2>) ) );
307  VERIFY_IS_APPROX( a(seqN(2,last/2).reverse()), a(seqN(2+(last/2-1)*1,last/2,fix<-1>)) );
308  VERIFY_IS_APPROX( a(seqN(last/2,fix<4>).reverse()),a(seqN(last/2,fix<4>)).reverse() );
309  VERIFY_IS_APPROX( A(seq(last-5,last-1,2).reverse(), seqN(last-3,3,fix<-2>).reverse()),
310  A(seq(last-5,last-1,2), seqN(last-3,3,fix<-2>)).reverse() );
311  }
312 
313 #if EIGEN_HAS_CXX11
314  // check lastN
315  VERIFY_IS_APPROX( a(lastN(3)), a.tail(3) );
316  VERIFY( MATCH( a(lastN(3)), "7\n8\n9" ) );
317  VERIFY_IS_APPROX( a(lastN(fix<3>())), a.tail<3>() );
318  VERIFY( MATCH( a(lastN(3,2)), "5\n7\n9" ) );
319  VERIFY( MATCH( a(lastN(3,fix<2>())), "5\n7\n9" ) );
320  VERIFY( a(lastN(fix<3>())).SizeAtCompileTime == 3 );
321 
322  VERIFY( (A(all, std::array<int,4>{{1,3,2,4}})).ColsAtCompileTime == 4);
323 
324  VERIFY_IS_APPROX( (A(std::array<int,3>{{1,3,5}}, std::array<int,4>{{9,6,3,0}})), A(seqN(1,3,2), seqN(9,4,-3)) );
325 
326 #if EIGEN_HAS_STATIC_ARRAY_TEMPLATE
327  VERIFY_IS_APPROX( A({3, 1, 6, 5}, all), A(std::array<int,4>{{3, 1, 6, 5}}, all) );
328  VERIFY_IS_APPROX( A(all,{3, 1, 6, 5}), A(all,std::array<int,4>{{3, 1, 6, 5}}) );
329  VERIFY_IS_APPROX( A({1,3,5},{3, 1, 6, 5}), A(std::array<int,3>{{1,3,5}},std::array<int,4>{{3, 1, 6, 5}}) );
330 
331  VERIFY_IS_EQUAL( A({1,3,5},{3, 1, 6, 5}).RowsAtCompileTime, 3 );
332  VERIFY_IS_EQUAL( A({1,3,5},{3, 1, 6, 5}).ColsAtCompileTime, 4 );
333 
334  VERIFY_IS_APPROX( a({3, 1, 6, 5}), a(std::array<int,4>{{3, 1, 6, 5}}) );
335  VERIFY_IS_EQUAL( a({1,3,5}).SizeAtCompileTime, 3 );
336 
337  VERIFY_IS_APPROX( b({3, 1, 6, 5}), b(std::array<int,4>{{3, 1, 6, 5}}) );
338  VERIFY_IS_EQUAL( b({1,3,5}).SizeAtCompileTime, 3 );
339 #endif
340 
341 #endif
342 
343  // check mat(i,j) with weird types for i and j
344  {
345  VERIFY_IS_APPROX( A(B.RowsAtCompileTime-1, 1), A(3,1) );
346  VERIFY_IS_APPROX( A(B.RowsAtCompileTime, 1), A(4,1) );
347  VERIFY_IS_APPROX( A(B.RowsAtCompileTime-1, B.ColsAtCompileTime-1), A(3,3) );
348  VERIFY_IS_APPROX( A(B.RowsAtCompileTime, B.ColsAtCompileTime), A(4,4) );
349  const Index I_ = 3, J_ = 4;
350  VERIFY_IS_APPROX( A(I_,J_), A(3,4) );
351  }
352 
353  // check extended block API
354  {
355  VERIFY( is_same_eq( A.block<3,4>(1,1), A.block(1,1,fix<3>,fix<4>)) );
356  VERIFY( is_same_eq( A.block<3,4>(1,1,3,4), A.block(1,1,fix<3>(),fix<4>(4))) );
357  VERIFY( is_same_eq( A.block<3,Dynamic>(1,1,3,4), A.block(1,1,fix<3>,4)) );
358  VERIFY( is_same_eq( A.block<Dynamic,4>(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<4>)) );
359  VERIFY( is_same_eq( A.block(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<Dynamic>(4))) );
360 
361  VERIFY( is_same_eq( A.topLeftCorner<3,4>(), A.topLeftCorner(fix<3>,fix<4>)) );
362  VERIFY( is_same_eq( A.bottomLeftCorner<3,4>(), A.bottomLeftCorner(fix<3>,fix<4>)) );
363  VERIFY( is_same_eq( A.bottomRightCorner<3,4>(), A.bottomRightCorner(fix<3>,fix<4>)) );
364  VERIFY( is_same_eq( A.topRightCorner<3,4>(), A.topRightCorner(fix<3>,fix<4>)) );
365 
366  VERIFY( is_same_eq( A.leftCols<3>(), A.leftCols(fix<3>)) );
367  VERIFY( is_same_eq( A.rightCols<3>(), A.rightCols(fix<3>)) );
368  VERIFY( is_same_eq( A.middleCols<3>(1), A.middleCols(1,fix<3>)) );
369 
370  VERIFY( is_same_eq( A.topRows<3>(), A.topRows(fix<3>)) );
371  VERIFY( is_same_eq( A.bottomRows<3>(), A.bottomRows(fix<3>)) );
372  VERIFY( is_same_eq( A.middleRows<3>(1), A.middleRows(1,fix<3>)) );
373 
374  VERIFY( is_same_eq( a.segment<3>(1), a.segment(1,fix<3>)) );
375  VERIFY( is_same_eq( a.head<3>(), a.head(fix<3>)) );
376  VERIFY( is_same_eq( a.tail<3>(), a.tail(fix<3>)) );
377 
378  const ArrayXXi& cA(A);
379  VERIFY( is_same_eq( cA.block<Dynamic,4>(1,1,3,4), cA.block(1,1,fix<Dynamic>(3),fix<4>)) );
380 
381  VERIFY( is_same_eq( cA.topLeftCorner<3,4>(), cA.topLeftCorner(fix<3>,fix<4>)) );
382  VERIFY( is_same_eq( cA.bottomLeftCorner<3,4>(), cA.bottomLeftCorner(fix<3>,fix<4>)) );
383  VERIFY( is_same_eq( cA.bottomRightCorner<3,4>(), cA.bottomRightCorner(fix<3>,fix<4>)) );
384  VERIFY( is_same_eq( cA.topRightCorner<3,4>(), cA.topRightCorner(fix<3>,fix<4>)) );
385 
386  VERIFY( is_same_eq( cA.leftCols<3>(), cA.leftCols(fix<3>)) );
387  VERIFY( is_same_eq( cA.rightCols<3>(), cA.rightCols(fix<3>)) );
388  VERIFY( is_same_eq( cA.middleCols<3>(1), cA.middleCols(1,fix<3>)) );
389 
390  VERIFY( is_same_eq( cA.topRows<3>(), cA.topRows(fix<3>)) );
391  VERIFY( is_same_eq( cA.bottomRows<3>(), cA.bottomRows(fix<3>)) );
392  VERIFY( is_same_eq( cA.middleRows<3>(1), cA.middleRows(1,fix<3>)) );
393  }
394 
395  // Check compilation of enums as index type:
396  a(XX) = 1;
397  A(XX,YY) = 1;
398  // Anonymous enums only work with C++11
399 #if EIGEN_HAS_CXX11
400  enum { X=0, Y=1 };
401  a(X) = 1;
402  A(X,Y) = 1;
403  A(XX,Y) = 1;
404  A(X,YY) = 1;
405 #endif
406 
407  // Check compilation of varying integer types as index types:
408  Index i = n/2;
409  short i_short(i);
410  std::size_t i_sizet(i);
411  VERIFY_IS_EQUAL( a(i), a.coeff(i_short) );
412  VERIFY_IS_EQUAL( a(i), a.coeff(i_sizet) );
413 
414  VERIFY_IS_EQUAL( A(i,i), A.coeff(i_short, i_short) );
415  VERIFY_IS_EQUAL( A(i,i), A.coeff(i_short, i) );
416  VERIFY_IS_EQUAL( A(i,i), A.coeff(i, i_short) );
417  VERIFY_IS_EQUAL( A(i,i), A.coeff(i, i_sizet) );
418  VERIFY_IS_EQUAL( A(i,i), A.coeff(i_sizet, i) );
419  VERIFY_IS_EQUAL( A(i,i), A.coeff(i_sizet, i_short) );
420  VERIFY_IS_EQUAL( A(i,i), A.coeff(5, i_sizet) );
421 
422  // Regression test for Max{Rows,Cols}AtCompileTime
423  {
424  Matrix3i A3 = Matrix3i::Random();
425  ArrayXi ind(5); ind << 1,1,1,1,1;
426  VERIFY_IS_EQUAL( A3(ind,ind).eval(), MatrixXi::Constant(5,5,A3(1,1)) );
427  }
428 
429  // Regression for bug 1736
430  {
431  VERIFY_IS_APPROX(A(all, eii).col(0).eval(), A.col(eii(0)));
432  A(all, eii).col(0) = A.col(eii(0));
433  }
434 
435  // bug 1815: IndexedView should allow linear access
436  {
437  VERIFY( MATCH( b(eii)(0), "3" ) );
438  VERIFY( MATCH( a(eii)(0), "3" ) );
439  VERIFY( MATCH( A(1,eii)(0), "103"));
440  VERIFY( MATCH( A(eii,1)(0), "301"));
441  VERIFY( MATCH( A(1,all)(1), "101"));
442  VERIFY( MATCH( A(all,1)(1), "101"));
443  }
444 
445 #if EIGEN_HAS_CXX11
446  //Bug IndexView with a single static row should be RowMajor:
447  {
448  // A(1, seq(0,2,1)).cwiseAbs().colwise().replicate(2).eval();
449  STATIC_CHECK(( (internal::evaluator<decltype( A(1,seq(0,2,1)) )>::Flags & RowMajorBit) == RowMajorBit ));
450  }
451 #endif
452 
453 }
454 
455 EIGEN_DECLARE_TEST(indexed_view)
456 {
457 // for(int i = 0; i < g_repeat; i++) {
461 // }
462 
463  // static checks of some internals:
473 }
ind
std::vector< int > ind
Definition: Slicing_stdvector_cxx11.cpp:1
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
encode
int encode(Index i, Index j)
Definition: indexed_view.cpp:45
col
m col(1)
is_same_eq
internal::enable_if< internal::is_same< T1, T2 >::value, bool >::type is_same_eq(const T1 &a, const T2 &b)
Definition: indexed_view.cpp:67
A3
static const double A3[]
Definition: expn.h:8
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
b
Scalar * b
Definition: benchVecAdd.cpp:17
Eigen::seqN
ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, typename internal::cleanup_index_type< SizeType >::type, typename internal::cleanup_seq_incr< IncrType >::type > seqN(FirstType first, SizeType size, IncrType incr)
Definition: ArithmeticSequence.h:162
gtsam::Y
GaussianFactorGraphValuePair Y
Definition: HybridGaussianProductFactor.cpp:29
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
check_indexed_view
void check_indexed_view()
Definition: indexed_view.cpp:98
X
#define X
Definition: icosphere.cpp:20
Eigen::last
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
Definition: IndexedViewHelper.h:38
XX
@ XX
Definition: indexed_view.cpp:96
Eigen::IndexPair
Definition: TensorMeta.h:247
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::DirectAccessBit
const unsigned int DirectAccessBit
Definition: Constants.h:155
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
n
int n
Definition: BiCGSTAB_simple.cpp:1
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(indexed_view)
Definition: indexed_view.cpp:455
EIGEN_UNUSED_VARIABLE
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1076
match
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Definition: indexed_view.cpp:54
YY
@ YY
Definition: indexed_view.cpp:96
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
is_same_seq
bool is_same_seq(const T1 &a, const T2 &b)
Definition: indexed_view.cpp:73
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
A2
static const double A2[]
Definition: expn.h:7
decode
IndexPair decode(Index ij)
Definition: indexed_view.cpp:49
T2
static const Pose3 T2(Rot3::Rodrigues(0.3, 0.2, 0.1), P2)
Eigen::Triplet< double >
str::str
str(const char *c, const SzType &n)
Definition: pytypes.h:1563
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
str
Definition: pytypes.h:1558
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
VERIFY_EQ_INT
#define VERIFY_EQ_INT(A, B)
Definition: indexed_view.cpp:93
DummyEnum
DummyEnum
Definition: indexed_view.cpp:96
is_same_seq_type
internal::enable_if< internal::is_same< T1, T2 >::value, bool >::type is_same_seq_type(const T1 &a, const T2 &b)
Definition: indexed_view.cpp:86
A1
static const double A1[]
Definition: expn.h:6
ref
Reference counting helper.
Definition: object.h:67
Eigen::lastp1
static const symbolic::AddExpr< symbolic::SymbolExpr< internal::symbolic_last_tag >, symbolic::ValueExpr< Eigen::internal::FixedInt< 1 > > > lastp1(last+fix< 1 >())
reverse
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:16
IndexPair
std::pair< Index, Index > IndexPair
Definition: indexed_view.cpp:43
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
T1
static const Similarity3 T1(R, Point3(3.5, -8.2, 4.2), 1)
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
Eigen::seq
internal::enable_if<!(symbolic::is_symbolic< FirstType >::value||symbolic::is_symbolic< LastType >::value), ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, Index > >::type seq(FirstType f, LastType l)
Definition: ArithmeticSequence.h:234
test_callbacks.value
value
Definition: test_callbacks.py:160
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
STATIC_CHECK
#define STATIC_CHECK(COND)
Definition: main.h:397
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
MATCH
#define MATCH(X, R)
Definition: indexed_view.cpp:63
test_eigen.ref
ref
Definition: test_eigen.py:9


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:17