tutorial.cpp
Go to the documentation of this file.
00001 #include <Eigen/Array>
00002 
00003 int main(int argc, char *argv[])
00004 {
00005   std::cout.precision(2);
00006 
00007   // demo static functions
00008   Eigen::Matrix3f m3 = Eigen::Matrix3f::Random();
00009   Eigen::Matrix4f m4 = Eigen::Matrix4f::Identity();
00010 
00011   std::cout << "*** Step 1 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
00012 
00013   // demo non-static set... functions
00014   m4.setZero();
00015   m3.diagonal().setOnes();
00016   
00017   std::cout << "*** Step 2 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
00018 
00019   // demo fixed-size block() expression as lvalue and as rvalue
00020   m4.block<3,3>(0,1) = m3;
00021   m3.row(2) = m4.block<1,3>(2,0);
00022 
00023   std::cout << "*** Step 3 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
00024 
00025   // demo dynamic-size block()
00026   {
00027     int rows = 3, cols = 3;
00028     m4.block(0,1,3,3).setIdentity();
00029     std::cout << "*** Step 4 ***\nm4:\n" << m4 << std::endl;
00030   }
00031 
00032   // demo vector blocks
00033   m4.diagonal().block(1,2).setOnes();
00034   std::cout << "*** Step 5 ***\nm4.diagonal():\n" << m4.diagonal() << std::endl;
00035   std::cout << "m4.diagonal().start(3)\n" << m4.diagonal().start(3) << std::endl;
00036 
00037   // demo coeff-wise operations
00038   m4 = m4.cwise()*m4;
00039   m3 = m3.cwise().cos();
00040   std::cout << "*** Step 6 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
00041 
00042   // sums of coefficients
00043   std::cout << "*** Step 7 ***\n m4.sum(): " << m4.sum() << std::endl;
00044   std::cout << "m4.col(2).sum(): " << m4.col(2).sum() << std::endl;
00045   std::cout << "m4.colwise().sum():\n" << m4.colwise().sum() << std::endl;
00046   std::cout << "m4.rowwise().sum():\n" << m4.rowwise().sum() << std::endl;
00047 
00048   // demo intelligent auto-evaluation
00049   m4 = m4 * m4; // auto-evaluates so no aliasing problem (performance penalty is low)
00050   Eigen::Matrix4f other = (m4 * m4).lazy(); // forces lazy evaluation
00051   m4 = m4 + m4; // here Eigen goes for lazy evaluation, as with most expressions
00052   m4 = -m4 + m4 + 5 * m4; // same here, Eigen chooses lazy evaluation for all that.
00053   m4 = m4 * (m4 + m4); // here Eigen chooses to first evaluate m4 + m4 into a temporary.
00054                        // indeed, here it is an optimization to cache this intermediate result.
00055   m3 = m3 * m4.block<3,3>(1,1); // here Eigen chooses NOT to evaluate block() into a temporary
00056     // because accessing coefficients of that block expression is not more costly than accessing
00057     // coefficients of a plain matrix.
00058   m4 = m4 * m4.transpose(); // same here, lazy evaluation of the transpose.
00059   m4 = m4 * m4.transpose().eval(); // forces immediate evaluation of the transpose
00060 
00061   std::cout << "*** Step 8 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl;
00062 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:27