From d9d69de38240d57ae211c9f10c657bc9dfaf2e7f Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Fri, 15 Aug 2008 14:28:06 +0000 Subject: updated tutorial --- doc/tutorial.cpp | 90 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 32 deletions(-) (limited to 'doc/tutorial.cpp') diff --git a/doc/tutorial.cpp b/doc/tutorial.cpp index b0627083d..941a80c1a 100644 --- a/doc/tutorial.cpp +++ b/doc/tutorial.cpp @@ -1,36 +1,62 @@ -#include +#include -USING_PART_OF_NAMESPACE_EIGEN +int main(int argc, char *argv[]) +{ + std::cout.precision(2); -using namespace std; + // demo static functions + Eigen::Matrix3f m3 = Eigen::Matrix3f::Random(); + Eigen::Matrix4f m4 = Eigen::Matrix4f::Identity(); -int main(int, char **) -{ - Matrix m; // 2x2 fixed-size matrix with uninitialized entries - m(0,0) = 1; - m(0,1) = 2; - m(1,0) = 3; - m(1,1) = 4; - - cout << "Here is a 2x2 matrix m:" << endl << m << endl; - cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl; - MatrixXd m2(4,4); // dynamic matrix with initial size 4x4 and uninitialized entries - // notice how we are mixing fixed-size and dynamic-size types. - - cout << "In the top-left block, we put the matrix m shown above." << endl; - m2.block<2,2>(0,0) = m; - cout << "In the bottom-left block, we put the matrix m*m, which is:" << endl << m*m << endl; - m2.block<2,2>(2,0) = m * m; - cout << "In the top-right block, we put the matrix m+m, which is:" << endl << m+m << endl; - m2.block<2,2>(0,2) = m + m; - cout << "In the bottom-right block, we put the matrix m-m, which is:" << endl << m-m << endl; - m2.block<2,2>(2,2) = m - m; - cout << "Now the 4x4 matrix m2 is:" << endl << m2 << endl; + std::cout << "*** Step 1 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl; + + // demo non-static set... functions + m4.setZero(); + m3.diagonal().setOnes(); - cout << "Row 0 of m2 is:" << endl << m2.row(0) << endl; - cout << "The third element in that row is " << m2.row(0)[2] << endl; - cout << "Column 1 of m2 is:" << endl << m2.col(1) << endl; - cout << "The transpose of m2 is:" << endl << m2.transpose() << endl; - cout << "The matrix m2 with row 0 and column 1 removed is:" << endl << m2.minor(0,1) << endl; - return 0; -} + std::cout << "*** Step 2 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl; + + // demo fixed-size block() expression as lvalue and as rvalue + m4.block<3,3>(0,1) = m3; + m3.row(2) = m4.block<1,3>(2,0); + + std::cout << "*** Step 3 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl; + + // demo dynamic-size block() + { + int rows = 3, cols = 3; + m4.block(0,1,3,3).setIdentity(); + std::cout << "*** Step 4 ***\nm4:\n" << m4 << std::endl; + } + + // demo vector blocks + m4.diagonal().block(1,2).setOnes(); + std::cout << "*** Step 5 ***\nm4.diagonal():\n" << m4.diagonal() << std::endl; + std::cout << "m4.diagonal().start(3)\n" << m4.diagonal().start(3) << std::endl; + + // demo coeff-wise operations + m4 = m4.cwise()*m4; + m3 = m3.cwise().cos(); + std::cout << "*** Step 6 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl; + + // sums of coefficients + std::cout << "*** Step 7 ***\n m4.sum(): " << m4.sum() << std::endl; + std::cout << "m4.col(2).sum(): " << m4.col(2).sum() << std::endl; + std::cout << "m4.colwise().sum():\n" << m4.colwise().sum() << std::endl; + std::cout << "m4.rowwise().sum():\n" << m4.rowwise().sum() << std::endl; + + // demo intelligent auto-evaluation + m4 = m4 * m4; // auto-evaluates so no aliasing problem (performance penalty is low) + Eigen::Matrix4f other = (m4 * m4).lazy(); // forces lazy evaluation + m4 = m4 + m4; // here Eigen goes for lazy evaluation, as with most expressions + m4 = -m4 + m4 + 5 * m4; // same here, Eigen chooses lazy evaluation for all that. + m4 = m4 * (m4 + m4); // here Eigen chooses to first evaluate m4 + m4 into a temporary. + // indeed, here it is an optimization to cache this intermediate result. + m3 = m3 * m4.block<3,3>(1,1); // here Eigen chooses NOT to evaluate transpose() into a temporary + // because accessing coefficients of that block expression is not more costly than accessing + // coefficients of a plain matrix. + m4 = m4 * m4.transpose(); // same here, lazy evaluation of the transpose. + m4 = m4 * m4.transpose().eval(); // forces immediate evaluation of the transpose + + std::cout << "*** Step 8 ***\nm3:\n" << m3 << "\nm4:\n" << m4 << std::endl; +} \ No newline at end of file -- cgit v1.2.3