From 951da96f146f3c6dc806a9e3d0fbbd96bbeb495b Mon Sep 17 00:00:00 2001 From: Carlos Becker Date: Thu, 8 Jul 2010 18:16:39 +0100 Subject: Added more redux types/examples in tutorial and fixed some display issues --- doc/C07_TutorialReductionsVisitorsBroadcasting.dox | 51 ++++++++++++++++++---- ...uctionsVisitorsBroadcasting_reductions_bool.cpp | 24 ++++++++++ ...uctionsVisitorsBroadcasting_reductions_norm.cpp | 28 ++++++++++++ ...ctionsVisitorsBroadcasting_reductions_norm.cpp~ | 28 ++++++++++++ 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp create mode 100644 doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp create mode 100644 doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp~ (limited to 'doc') diff --git a/doc/C07_TutorialReductionsVisitorsBroadcasting.dox b/doc/C07_TutorialReductionsVisitorsBroadcasting.dox index 17124cb3b..b2c5b62cb 100644 --- a/doc/C07_TutorialReductionsVisitorsBroadcasting.dox +++ b/doc/C07_TutorialReductionsVisitorsBroadcasting.dox @@ -11,6 +11,8 @@ This tutorial explains Eigen's reductions, visitors and broadcasting and how the \b Table \b of \b contents - \ref TutorialReductionsVisitorsBroadcastingReductions + - \ref TutorialReductionsVisitorsBroadcastingReductionsNorm + - \ref TutorialReductionsVisitorsBroadcastingReductionsBool - FIXME: .redux() - \ref TutorialReductionsVisitorsBroadcastingVisitors - \ref TutorialReductionsVisitorsBroadcastingPartialReductions @@ -21,7 +23,7 @@ This tutorial explains Eigen's reductions, visitors and broadcasting and how the \section TutorialReductionsVisitorsBroadcastingReductions Reductions In Eigen, a reduction is a function that is applied to a certain matrix or array, returning a single -value of type scalar. One of the most used reductions is \link MatrixBase::sum() .sum() \endlink, +value of type scalar. One of the most used reductions is \link DenseBase::sum() .sum() \endlink, which returns the addition of all the coefficients inside a given matrix or array.
@@ -33,6 +35,37 @@ Output: \include tut_arithmetic_redux_basic.out The \em trace of a matrix, as returned by the function \c trace(), is the sum of the diagonal coefficients and can also be computed as efficiently using a.diagonal().sum(), as we will see later on. + +\subsection TutorialReductionsVisitorsBroadcastingReductionsNorm Norm reductions +Eigen also provides reductions to obtain the norm or squared norm of a vector with \link DenseBase::norm() norm() \endlink and \link DenseBase::squaredNorm() squaredNorm() \endlink respectively. +These operations can also operate on objects such as Matrices or Arrays, as shown in the following example: + + +
+Example: \include Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp + +Output: +\verbinclude Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.out +
+ +\subsection TutorialReductionsVisitorsBroadcastingReductionsBool Boolean-like reductions + +Another interesting type of reductions are the ones that deal with \b true and \b false values: + - \link DenseBase::all() all() \endlink returns \b true if all of the coefficients in a given Matrix or \link ArrayBase Array \endlink are \b true . + - \link DenseBase::any() any() \endlink returns \b true if at least one of the coefficients in a given Matrix or \link ArrayBase Array \endlink are \b true . + - \link DenseBase::count() count() \endlink returns the number of \b true coefficients in a given Matrix or \link ArrayBase Array \endlink. + +Their behaviour can be seen in the following example: + + +
+Example: \include Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp + +Output: +\verbinclude Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.out +
+ + \section TutorialReductionsVisitorsBroadcastingVisitors Visitors Visitors are useful when the location of a coefficient inside a Matrix or \link ArrayBase Array \endlink wants to be obtained. The simplest example are the @@ -43,10 +76,10 @@ the location of the greatest or smallest coefficient in a Matrix or The arguments passed to a visitor are pointers to the variables where the row and column position are to be stored. These variables are of type -\link DenseBase::Index Index \endlink FIXME: link? ok?, as shown below: +\link DenseBase::Index Index \endlink (FIXME: link ok?), as shown below:
-\include Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp Output: @@ -66,7 +99,7 @@ A simple example is obtaining the sum of the elements in each column in a given matrix, storing the result in a row-vector:
-\include Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp Output: @@ -76,7 +109,7 @@ Output: The same operation can be performed row-wise:
-\include Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp Output: @@ -92,7 +125,7 @@ Here there is another example that aims to find the the column whose sum of elem within a matrix. With column-wise partial reductions this can be coded as:
-\include Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp Output: @@ -129,7 +162,7 @@ A simple example is to add a certain column-vector to each column in a matrix. This can be accomplished with:
-\include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp Output: @@ -144,7 +177,7 @@ The same applies for the \link ArrayBase Array \endlink class, where the equival Therefore, to perform the same operation row-wise we can do:
-\include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp Output: @@ -160,7 +193,7 @@ the nearest neighbour of a vector v within the columns of matrix m< computing the squared Euclidean distance with the partial reduction named \link DenseBase::squaredNorm() squaredNorm() \endlink:
-\include Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp +Example: \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp Output: diff --git a/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp new file mode 100644 index 000000000..10916877f --- /dev/null +++ b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; +using namespace Eigen; + +int main() +{ + MatrixXf m(2,2), n(2,2); + + m << 0,2, + 3,4; + + n << 1,2, + 3,4; + + cout << "m.all() = " << m.all() << endl; + cout << "m.any() = " << m.any() << endl; + cout << "m.count() = " << m.count() << endl; + cout << endl; + cout << "n.all() = " << n.all() << endl; + cout << "n.any() = " << n.any() << endl; + cout << "n.count() = " << n.count() << endl; +} diff --git a/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp new file mode 100644 index 000000000..f3364d7fc --- /dev/null +++ b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; +using namespace Eigen; + +int main() +{ + VectorXf v(2); + MatrixXf m(2,2), n(2,2); + + v << 5, + 10; + + m << 2,2, + 3,4; + + n << 1, 2, + 32,12; + + cout << "v.norm() = " << v.norm() << endl; + cout << "m.norm() = " << m.norm() << endl; + cout << "n.norm() = " << n.norm() << endl; + cout << endl; + cout << "v.squaredNorm() = " << v.squaredNorm() << endl; + cout << "m.squaredNorm() = " << m.squaredNorm() << endl; + cout << "n.squaredNorm() = " << n.squaredNorm() << endl; +} diff --git a/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp~ b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp~ new file mode 100644 index 000000000..03057f8d2 --- /dev/null +++ b/doc/examples/Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp~ @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; +using namespace Eigen; + +int main() +{ + VectorXf v(2); + MatrixXf m(2,2), n(2,2); + + v << 2, + 5; + + m << 0,2, + 3,4; + + n << 1,2, + 3,4; + + cout << "v.norm() = " << m.norm() << endl; + cout << "m.norm() = " << m.norm() << endl; + cout << "n.norm() = " << m.norm() << endl; + cout << endl; + cout << "v.squaredNorm() = " << v.squaredNorm() << endl; + cout << "m.squaredNorm() = " << m.squaredNorm() << endl; + cout << "n.squaredNorm() = " << n.squaredNorm() << endl; +} -- cgit v1.2.3