You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.5 KiB

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