tutorial.cpp File Reference
#include <Eigen/Array>

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)
3  {
4  std::cout.precision(2);
5 
6  // demo static functions
7  Eigen::Matrix3f m3 = Eigen::Matrix3f::Random();
8  Eigen::Matrix4f m4 = Eigen::Matrix4f::Identity();
9 
10  std::cout << "*** Step 1 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
11 
12  // demo non-static set... functions
13  m4.setZero();
14  m3.diagonal().setOnes();
15 
16  std::cout << "*** Step 2 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
17 
18  // demo fixed-size block() expression as lvalue and as rvalue
19  m4.block<3, 3>(0, 1) = m3;
20  m3.row(2) = m4.block<1, 3>(2, 0);
21 
22  std::cout << "*** Step 3 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
23 
24  // demo dynamic-size block()
25  {
26  int rows = 3, cols = 3;
27  m4.block(0, 1, 3, 3).setIdentity();
28  std::cout << "*** Step 4 ***\nm4:\n" << m4 << std::endl;
29  }
30 
31  // demo vector blocks
32  m4.diagonal().block(1, 2).setOnes();
33  std::cout << "*** Step 5 ***\nm4.diagonal():\n" << m4.diagonal() << std::endl;
34  std::cout << "m4.diagonal().start(3)\n" << m4.diagonal().start(3) << std::endl;
35 
36  // demo coeff-wise operations
37  m4 = m4.cwise() * m4;
38  m3 = m3.cwise().cos();
39  std::cout << "*** Step 6 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
40 
41  // sums of coefficients
42  std::cout << "*** Step 7 ***\n m4.sum(): " << m4.sum() << std::endl;
43  std::cout << "m4.col(2).sum(): " << m4.col(2).sum() << std::endl;
44  std::cout << "m4.colwise().sum():\n" << m4.colwise().sum() << std::endl;
45  std::cout << "m4.rowwise().sum():\n" << m4.rowwise().sum() << std::endl;
46 
47  // demo intelligent auto-evaluation
48  m4 = m4 * m4; // auto-evaluates so no aliasing problem (performance penalty is low)
49  Eigen::Matrix4f other = (m4 * m4).lazy(); // forces lazy evaluation
50  m4 = m4 + m4; // here Eigen goes for lazy evaluation, as with most expressions
51  m4 = -m4 + m4 + 5 * m4; // same here, Eigen chooses lazy evaluation for all that.
52  m4 = m4 * (m4 + m4); // here Eigen chooses to first evaluate m4 + m4 into a temporary.
53  // indeed, here it is an optimization to cache this intermediate result.
54  m3 = m3 * m4.block<3, 3>(1, 1); // here Eigen chooses NOT to evaluate block() into a temporary
55  // because accessing coefficients of that block expression is not more costly than
56  // accessing coefficients of a plain matrix.
57  m4 = m4 * m4.transpose(); // same here, lazy evaluation of the transpose.
58  m4 = m4 * m4.transpose().eval(); // forces immediate evaluation of the transpose
59 
60  std::cout << "*** Step 8 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
61 }
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1

References cols, and rows.