blob: e37c245cc8d949275399f6da0b8a21b5ba4e8337 [file] [log] [blame]
Benoit Jacob1af61c62007-09-09 09:41:15 +00001#include "../src/All"
Benoit Jacob61158b12007-09-07 08:18:21 +00002
3using namespace std;
4using namespace Eigen;
5
6int main(int, char **)
7{
Benoit Jacob55227b12007-09-26 14:06:14 +00008 Matrix<double,2,2> m, n; // 2x2 fixed-size matrix with uninitialized entries
Benoit Jacob61158b12007-09-07 08:18:21 +00009 m(0,0) = 1;
10 m(0,1) = 2;
11 m(1,0) = 3;
12 m(1,1) = 4;
Benoit Jacob55227b12007-09-26 14:06:14 +000013
14 n = m;
15 n = eval(n*n);
16 cout << n << endl;
17#if 0
Benoit Jacob61158b12007-09-07 08:18:21 +000018 cout << "Here is a 2x2 matrix m:" << endl << m << endl;
19 cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl;
Benoit Jacob55227b12007-09-26 14:06:14 +000020 Matrix<double,4,4> m2; // dynamic matrix with initial size 4x4 and uninitialized entries
Benoit Jacob61158b12007-09-07 08:18:21 +000021 // notice how we are mixing fixed-size and dynamic-size types.
22
23 cout << "In the top-left block, we put the matrix m shown above." << endl;
Benoit Jacob1dab53d2007-09-09 08:17:08 +000024 m2.block(0,1,0,1) = m;
Benoit Jacob55227b12007-09-26 14:06:14 +000025 cout << "m2 is now " << endl << m2 << endl;
26 cout << "m2.block(0,1,0,1) has " << m2.block(0,1,0,1).rows() << " rows" << endl;
Benoit Jacob61158b12007-09-07 08:18:21 +000027 cout << "In the bottom-left block, we put the matrix m*m, which is:" << endl << m*m << endl;
Benoit Jacob1dab53d2007-09-09 08:17:08 +000028 m2.block(2,3,0,1) = m * m;
Benoit Jacob61158b12007-09-07 08:18:21 +000029 cout << "In the top-right block, we put the matrix m+m, which is:" << endl << m+m << endl;
Benoit Jacob1dab53d2007-09-09 08:17:08 +000030 m2.block(0,1,2,3) = m + m;
Benoit Jacob61158b12007-09-07 08:18:21 +000031 cout << "In the bottom-right block, we put the matrix m-m, which is:" << endl << m-m << endl;
Benoit Jacob1dab53d2007-09-09 08:17:08 +000032 m2.block(2,3,2,3) = m - m;
Benoit Jacob61158b12007-09-07 08:18:21 +000033 cout << "Now the 4x4 matrix m2 is:" << endl << m2 << endl;
34
Benoit Jacob61158b12007-09-07 08:18:21 +000035 cout << "The central 2x2 block of m2 is:" << endl << m2.block(1,2,1,2) << endl;
36 cout << "Row 0 of m2, written as a column vector, is:" << endl << m2.row(0) << endl;
37 cout << "Column 1 of m2 is:" << endl << m2.col(1) << endl;
38 cout << "The matrix m2 with row 0 and column 1 removed is:" << endl << m2.minor(0,1) << endl;
39
40 cout << endl << "Now let us study a tricky issue." << endl;
41 cout << "Recall that the matrix product m*m is:" << endl << m*m << endl;
42 cout << "We want to store that into m, i.e. do \"m = m * m;\"" << endl;
43 cout << "Here we must be very careful. For if we do \"m = m * m;\"," << endl
44 << "the matrix m becomes" << endl;
45 Matrix<double,2,2> m_save = m;
46 m = m * m; // the bogus operation
47 cout << m << "," << endl;
48 cout << "which is not what was wanted!" << endl
49 << "Explanation: because of the way expression templates work, the matrix m gets" << endl
50 << "overwritten _while_ the matrix product m * m is being computed." << endl
51 << "This is the counterpart of eliminating temporary objects!" << endl
52 << "Anyway, if you want to store m * m into m, you can do this:" << endl
53 << " m.alias() = m * m;" << endl;
54 m = m_save;
55 m.alias() = m * m;
56 cout << "And m is now:" << endl << m << endl << "as was expected." << endl;
Benoit Jacob55227b12007-09-26 14:06:14 +000057#endif
Benoit Jacob61158b12007-09-07 08:18:21 +000058 return 0;
59}