aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2010-06-27 19:37:16 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2010-06-27 19:37:16 +0200
commitb5659dc9cf6f895c41c6119c06df1ba7c7f71367 (patch)
tree33b6de7207b1567797b3de0777eedc5888a31161 /doc
parent189d4b51c2280281bf93502db959899c2c097957 (diff)
show a more fancy example for the getting started tut
Diffstat (limited to 'doc')
-rw-r--r--doc/C00_QuickStartGuide.dox19
-rw-r--r--doc/examples/QuickStart_example2_dynamic.cpp16
-rw-r--r--doc/examples/QuickStart_example2_fixed.cpp16
3 files changed, 21 insertions, 30 deletions
diff --git a/doc/C00_QuickStartGuide.dox b/doc/C00_QuickStartGuide.dox
index 3c2ccc39a..e700689aa 100644
--- a/doc/C00_QuickStartGuide.dox
+++ b/doc/C00_QuickStartGuide.dox
@@ -61,31 +61,22 @@ The output is as follows:
\section GettingStartedExplanation2 Explanation of the second example
-The second example starts by declaring a 3-by-3 matrix \c m. The two nested \c for loops initializes the matrix to be:
+The second example starts by declaring a 3-by-3 matrix \c m which is initialized with random values in the range [-1:1]. The next line applies a linear mapping such that the values fit in the range [10:110]. The function call MatrixXd::Constant(3,3,1.2) returns \c 3x3 matrix expression having all coefficient equal to 1.2. The rest is standard arithmetics.
-\f[
-m =
-\begin{bmatrix}
- 0 & 0.01 & 0.02 \\
- 1 & 1.01 & 1.02 \\
- 2 & 2.01 & 2.02
-\end{bmatrix}.
-\f]
-
-The one but last line of the \c main function introduces a new type: \c VectorXd. This represent a (column) vector of arbitrary size. The function VectorXd::Ones() returns a vector of which every entry is one. So, the vector \c v is initialized to be
+The next line of the \c main function introduces a new type: \c VectorXd. This represents a (column) vector of arbitrary size. Here, the vector \c v is created to contains \c 3 coefficients which are left unitialized. The one but last line sets all coefficients of the vector \c v to be as follow:
\f[
v =
\begin{bmatrix}
1 \\
- 1 \\
- 1
+ 2 \\
+ 3
\end{bmatrix}.
\f]
The final line of the program multiplies the matrix \c m with the vector \c v and outputs the result.
-Now look back at the second example program. We presented two versions of it. In the version in the left column, the matrix is of type \c MatrixXd which represents matrices of arbitrary size. The version in the right column is similar, except that the matrix is of type \c Matrix3d, which represents matrices of a fixed size (here 3-by-3). Because the type already encodes the size of the matrix, it is not necessary to specify the size in the constructor; compare <tt>MatrixXd m(3,3)</tt> with <tt>Matrix3d m</tt>. Similarly, we have \c VectorXd on the left (arbitrary size) versus \c Vector3d on the right (fixed size).
+Now look back at the second example program. We presented two versions of it. In the version in the left column, the matrix is of type \c MatrixXd which represents matrices of arbitrary size. The version in the right column is similar, except that the matrix is of type \c Matrix3d, which represents matrices of a fixed size (here 3-by-3). Because the type already encodes the size of the matrix, it is not necessary to specify the size in the constructor; compare <tt>MatrixXd m(3,3)</tt> with <tt>Matrix3d m</tt>. Similarly, we have \c VectorXd on the left (arbitrary size) versus \c Vector3d on the right (fixed size). Note that here the coefficients of vector \c v are directly set in the constructor, though the same syntax of the left example could be used too.
The use of fixed-size matrices and vectors has two advantages. The compiler emits better (faster) code because it knows the size of the matrices and vectors. Specifying the size in the type also allows for more rigorous checking at compile-time. For instance, the compiler will complain if you try to multiply a \c Matrix4d (a 4-by-4 matrix) with a \c Vector3d (a vector of size 3). However, the use of many types increases compilation time and the size of the executable. The size of the matrix may also not be known at compile-time. A rule of thumb is to use fixed-size matrices for size 4-by-4 and smaller.
diff --git a/doc/examples/QuickStart_example2_dynamic.cpp b/doc/examples/QuickStart_example2_dynamic.cpp
index ccaba9c82..672ac82e9 100644
--- a/doc/examples/QuickStart_example2_dynamic.cpp
+++ b/doc/examples/QuickStart_example2_dynamic.cpp
@@ -1,15 +1,15 @@
#include <iostream>
#include <Eigen/Dense>
-using Eigen::MatrixXd;
-using Eigen::VectorXd;
+using namespace Eigen;
+using namespace std;
int main()
{
- MatrixXd m(3,3);
- for (int row = 0; row < 3; ++row)
- for (int column = 0; column < 3; ++column)
- m(row, column) = row + 0.01 * column;
- VectorXd v = VectorXd::Ones(3);
- std::cout << m * v << std::endl;
+ MatrixXf m = MatrixXf::Random(3,3);
+ m = (m + MatrixXf::Constant(3,3,1.2)) * 50;
+ cout << "m =" << endl << m << endl;
+ VectorXf v(3);
+ v << 1, 2, 3;
+ cout << "m * v =" << endl << m * v << endl;
}
diff --git a/doc/examples/QuickStart_example2_fixed.cpp b/doc/examples/QuickStart_example2_fixed.cpp
index c30f47734..edf3268cd 100644
--- a/doc/examples/QuickStart_example2_fixed.cpp
+++ b/doc/examples/QuickStart_example2_fixed.cpp
@@ -1,15 +1,15 @@
#include <iostream>
#include <Eigen/Dense>
-using Eigen::Matrix3d;
-using Eigen::Vector3d;
+using namespace Eigen;
+using namespace std;
int main()
{
- Matrix3d m;
- for (int row = 0; row < 3; ++row)
- for (int column = 0; column < 3; ++column)
- m(row, column) = row + 0.01 * column;
- Vector3d v = Vector3d::Ones();
- std::cout << m * v << std::endl;
+ Matrix3f m = Matrix3f::Random();
+ m = (m + Matrix3f::Constant(1.2)) * 50;
+ cout << "m =" << endl << m << endl;
+ Vector3f v(1,2,3);
+
+ cout << "m * v =" << endl << m * v << endl;
}