block.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) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
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 #define EIGEN_NO_STATIC_ASSERT // otherwise we fail at compile time on unused paths
11 #include "main.h"
12 
13 template<typename MatrixType, typename Index, typename Scalar>
16  // check cwise-Functions:
17  VERIFY_IS_APPROX(m1.row(r1).cwiseMax(s1), m1.cwiseMax(s1).row(r1));
18  VERIFY_IS_APPROX(m1.col(c1).cwiseMin(s1), m1.cwiseMin(s1).col(c1));
19 
20  VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMin(s1), m1.cwiseMin(s1).block(r1,c1,r2-r1+1,c2-c1+1));
21  VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMax(s1), m1.cwiseMax(s1).block(r1,c1,r2-r1+1,c2-c1+1));
22 
23  return Scalar(0);
24 }
25 
26 template<typename MatrixType, typename Index, typename Scalar>
29  return Scalar(0);
30 }
31 
32 
33 template<typename MatrixType> void block(const MatrixType& m)
34 {
35  typedef typename MatrixType::Scalar Scalar;
36  typedef typename MatrixType::RealScalar RealScalar;
40  typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType;
41 
42  Index rows = m.rows();
43  Index cols = m.cols();
44 
45  MatrixType m1 = MatrixType::Random(rows, cols),
46  m1_copy = m1,
47  m2 = MatrixType::Random(rows, cols),
48  m3(rows, cols),
49  ones = MatrixType::Ones(rows, cols);
50  VectorType v1 = VectorType::Random(rows);
51 
52  Scalar s1 = internal::random<Scalar>();
53 
54  Index r1 = internal::random<Index>(0,rows-1);
55  Index r2 = internal::random<Index>(r1,rows-1);
56  Index c1 = internal::random<Index>(0,cols-1);
57  Index c2 = internal::random<Index>(c1,cols-1);
58 
59  block_real_only(m1, r1, r2, c1, c1, s1);
60 
61  //check row() and col()
62  VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1));
63  //check operator(), both constant and non-constant, on row() and col()
64  m1 = m1_copy;
65  m1.row(r1) += s1 * m1_copy.row(r2);
66  VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2));
67  // check nested block xpr on lhs
68  m1.row(r1).row(0) += s1 * m1_copy.row(r2);
69  VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2));
70  m1 = m1_copy;
71  m1.col(c1) += s1 * m1_copy.col(c2);
72  VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2));
73  m1.col(c1).col(0) += s1 * m1_copy.col(c2);
74  VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2));
75 
76 
77  //check block()
78  Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
79 
80  RowVectorType br1(m1.block(r1,0,1,cols));
81  VectorType bc1(m1.block(0,c1,rows,1));
82  VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1));
83  VERIFY_IS_EQUAL(m1.row(r1), br1);
84  VERIFY_IS_EQUAL(m1.col(c1), bc1);
85  //check operator(), both constant and non-constant, on block()
86  m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
87  m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
88 
89  enum {
90  BlockRows = 2,
91  BlockCols = 5
92  };
93  if (rows>=5 && cols>=8)
94  {
95  // test fixed block() as lvalue
96  m1.template block<BlockRows,BlockCols>(1,1) *= s1;
97  // test operator() on fixed block() both as constant and non-constant
98  m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
99  // check that fixed block() and block() agree
100  Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
101  VERIFY_IS_EQUAL(b, m1.block(3,3,BlockRows,BlockCols));
102 
103  // same tests with mixed fixed/dynamic size
104  m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols) *= s1;
105  m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols)(0,3) = m1.template block<2,5>(1,1)(1,2);
106  Matrix<Scalar,Dynamic,Dynamic> b2 = m1.template block<Dynamic,BlockCols>(3,3,2,5);
107  VERIFY_IS_EQUAL(b2, m1.block(3,3,BlockRows,BlockCols));
108  }
109 
110  if (rows>2)
111  {
112  // test sub vectors
113  VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
114  VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
115  VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
116  VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
117  Index i = rows-2;
118  VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
119  VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
120  VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
121  VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
122  i = internal::random<Index>(0,rows-2);
123  VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
124  }
125 
126  // stress some basic stuffs with block matrices
127  VERIFY(numext::real(ones.col(c1).sum()) == RealScalar(rows));
128  VERIFY(numext::real(ones.row(r1).sum()) == RealScalar(cols));
129 
130  VERIFY(numext::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
131  VERIFY(numext::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
132 
133  // check that linear acccessors works on blocks
134  m1 = m1_copy;
135  if((MatrixType::Flags&RowMajorBit)==0)
136  VERIFY_IS_EQUAL(m1.leftCols(c1).coeff(r1+c1*rows), m1(r1,c1));
137  else
138  VERIFY_IS_EQUAL(m1.topRows(r1).coeff(c1+r1*cols), m1(r1,c1));
139 
140 
141  // now test some block-inside-of-block.
142 
143  // expressions with direct access
144  VERIFY_IS_EQUAL( (m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , (m1.block(r2,c2,rows-r2,cols-c2)) );
145  VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
146  VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
147  VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
148  VERIFY_IS_EQUAL( (m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
149 
150  // expressions without direct access
151  VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , ((m1+m2).block(r2,c2,rows-r2,cols-c2)) );
152  VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
153  VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , ((m1+m2).col(c1).segment(r1,r2-r1+1)) );
154  VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
155  VERIFY_IS_APPROX( ((m1+m2).transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
156 
157  VERIFY_IS_APPROX( (m1*1).topRows(r1), m1.topRows(r1) );
158  VERIFY_IS_APPROX( (m1*1).leftCols(c1), m1.leftCols(c1) );
159  VERIFY_IS_APPROX( (m1*1).transpose().topRows(c1), m1.transpose().topRows(c1) );
160  VERIFY_IS_APPROX( (m1*1).transpose().leftCols(r1), m1.transpose().leftCols(r1) );
161  VERIFY_IS_APPROX( (m1*1).transpose().middleRows(c1,c2-c1+1), m1.transpose().middleRows(c1,c2-c1+1) );
162  VERIFY_IS_APPROX( (m1*1).transpose().middleCols(r1,r2-r1+1), m1.transpose().middleCols(r1,r2-r1+1) );
163 
164  // evaluation into plain matrices from expressions with direct access (stress MapBase)
165  DynamicMatrixType dm;
166  DynamicVectorType dv;
167  dm.setZero();
168  dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
169  VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
170  dm.setZero();
171  dv.setZero();
172  dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
173  dv = m1.row(r1).segment(c1,c2-c1+1);
174  VERIFY_IS_EQUAL(dv, dm);
175  dm.setZero();
176  dv.setZero();
177  dm = m1.col(c1).segment(r1,r2-r1+1);
178  dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
179  VERIFY_IS_EQUAL(dv, dm);
180  dm.setZero();
181  dv.setZero();
182  dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
183  dv = m1.row(r1).segment(c1,c2-c1+1);
184  VERIFY_IS_EQUAL(dv, dm);
185  dm.setZero();
186  dv.setZero();
187  dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
188  dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
189  VERIFY_IS_EQUAL(dv, dm);
190 
191  VERIFY_IS_EQUAL( (m1.template block<Dynamic,1>(1,0,0,1)), m1.block(1,0,0,1));
192  VERIFY_IS_EQUAL( (m1.template block<1,Dynamic>(0,1,1,0)), m1.block(0,1,1,0));
193  VERIFY_IS_EQUAL( ((m1*1).template block<Dynamic,1>(1,0,0,1)), m1.block(1,0,0,1));
194  VERIFY_IS_EQUAL( ((m1*1).template block<1,Dynamic>(0,1,1,0)), m1.block(0,1,1,0));
195 
196  if (rows>=2 && cols>=2)
197  {
198  VERIFY_RAISES_ASSERT( m1 += m1.col(0) );
199  VERIFY_RAISES_ASSERT( m1 -= m1.col(0) );
200  VERIFY_RAISES_ASSERT( m1.array() *= m1.col(0).array() );
201  VERIFY_RAISES_ASSERT( m1.array() /= m1.col(0).array() );
202  }
203 }
204 
205 
206 template<typename MatrixType>
208 {
209  Index rows = m.rows();
210  Index cols = m.cols();
211  Index size = m.size();
212  Index innerStride = m.innerStride();
213  Index outerStride = m.outerStride();
214  Index rowStride = m.rowStride();
215  Index colStride = m.colStride();
216  const typename MatrixType::Scalar* data = m.data();
217 
218  for(int j=0;j<cols;++j)
219  for(int i=0;i<rows;++i)
220  VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
221 
222  if(!MatrixType::IsVectorAtCompileTime)
223  {
224  for(int j=0;j<cols;++j)
225  for(int i=0;i<rows;++i)
226  VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
227  ? i*outerStride + j*innerStride
228  : j*outerStride + i*innerStride]);
229  }
230 
231  if(MatrixType::IsVectorAtCompileTime)
232  {
233  VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
234  for (int i=0;i<size;++i)
235  VERIFY(m.coeff(i) == data[i*innerStride]);
236  }
237 }
238 
239 template<typename MatrixType>
241 {
242  Index rows = m.rows();
243  Index cols = m.cols();
244 
245  Index r1 = internal::random<Index>(0,rows-1);
246  Index r2 = internal::random<Index>(r1,rows-1);
247  Index c1 = internal::random<Index>(0,cols-1);
248  Index c2 = internal::random<Index>(c1,cols-1);
249 
250  MatrixType m1 = MatrixType::Random(rows, cols);
251  compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
252  compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
253  compare_using_data_and_stride(m1.row(r1));
254  compare_using_data_and_stride(m1.col(c1));
255  compare_using_data_and_stride(m1.row(r1).transpose());
256  compare_using_data_and_stride(m1.col(c1).transpose());
257 }
258 
260 {
261  for(int i = 0; i < g_repeat; i++) {
262  CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
263  CALL_SUBTEST_2( block(Matrix4d()) );
264  CALL_SUBTEST_3( block(MatrixXcf(3, 3)) );
265  CALL_SUBTEST_4( block(MatrixXi(8, 12)) );
266  CALL_SUBTEST_5( block(MatrixXcd(20, 20)) );
267  CALL_SUBTEST_6( block(MatrixXf(20, 20)) );
268 
269  CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
270 
271 #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
272  CALL_SUBTEST_6( data_and_stride(MatrixXf(internal::random(5,50), internal::random(5,50))) );
274 #endif
275  }
276 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
static const Key c2
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:285
void test_block()
Definition: block.cpp:259
EIGEN_DEVICE_FUNC ColsBlockXpr middleCols(Index startCol, Index numCols)
This is the const version of middleCols(Index,Index).
Definition: BlockMethods.h:715
float real
Definition: datatypes.h:10
Scalar * b
Definition: benchVecAdd.cpp:17
Vector v1
MatrixType m2(n_dims)
EIGEN_DEVICE_FUNC SegmentReturnType segment(Index start, Index n)
This is the const version of segment(Index,Index).
Definition: BlockMethods.h:888
MatrixXf MatrixType
EIGEN_DEVICE_FUNC RowsBlockXpr topRows(Index n)
This is the const version of topRows(Index).
Definition: BlockMethods.h:432
const unsigned int RowMajorBit
Definition: Constants.h:61
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
EIGEN_DEVICE_FUNC ColsBlockXpr leftCols(Index n)
This is the const version of leftCols(Index).
Definition: BlockMethods.h:602
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:331
Matrix3d m1
Definition: IOFormat.cpp:2
void data_and_stride(const MatrixType &m)
Definition: block.cpp:240
m row(1)
EIGEN_DEVICE_FUNC RowsBlockXpr middleRows(Index startRow, Index n)
This is the const version of middleRows(Index,Index).
Definition: BlockMethods.h:545
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
Vector2 b2(4,-5)
int data[]
static const double r2
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
v tail< 2 >().setZero()
Vector2 b1(2,-1)
static const double r1
#define VERIFY(a)
Definition: main.h:325
void compare_using_data_and_stride(const MatrixType &m)
Definition: block.cpp:207
const mpreal random(unsigned int seed=0)
Definition: mpreal.h:2614
m col(1)
The matrix class, also used for vectors and row-vectors.
void block(const MatrixType &m)
Definition: block.cpp:33
v head< 2 >().setZero()
std::ptrdiff_t j
static const Key c1
Eigen::internal::enable_if<!NumTraits< typename MatrixType::Scalar >::IsComplex, typename MatrixType::Scalar >::type block_real_only(const MatrixType &m1, Index r1, Index r2, Index c1, Index c2, const Scalar &s1)
Definition: block.cpp:15


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:43