aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/CustomizingEigen_NullaryExpr.dox
blob: d70f81065d49d043e2efd3ec8bb50311c1a64ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace Eigen {

/** \page TopicCustomizing_NullaryExpr Matrix manipulation via nullary-expressions


The main purpose of the class CwiseNullaryOp is to define \em procedural matrices such as constant or random matrices as returned by the Ones(), Zero(), Constant(), Identity() and Random() methods.
Nevertheless, with some imagination it is possible to accomplish very sophisticated matrix manipulation with minimal efforts such that \ref TopicNewExpressionType "implementing new expression" is rarely needed.

\section NullaryExpr_Circulant Example 1: circulant matrix

To explore these possibilities let us start with the  \em circulant example of the \ref TopicNewExpressionType "implementing new expression" topic.
Let us recall that a circulant matrix is a matrix where each column is the same as the
column to the left, except that it is cyclically shifted downwards.
For example, here is a 4-by-4 circulant matrix:
\f[ \begin{bmatrix}
    1 & 8 & 4 & 2 \\
    2 & 1 & 8 & 4 \\
    4 & 2 & 1 & 8 \\
    8 & 4 & 2 & 1
\end{bmatrix} \f]
A circulant matrix is uniquely determined by its first column. We wish
to write a function \c makeCirculant which, given the first column,
returns an expression representing the circulant matrix.

For this exercise, the return type of \c makeCirculant will be a CwiseNullaryOp that we need to instantiate with:
1 - a proper \c circulant_functor storing the input vector and implementing the adequate coefficient accessor \c operator(i,j)
2 - a template instantiation of class Matrix conveying compile-time information such as the scalar type, sizes, and preferred storage layout.

Calling \c ArgType the type of the input vector, we can construct the equivalent squared Matrix type as follows:

\snippet make_circulant2.cpp square

This little helper structure will help us to implement our \c makeCirculant function as follows:

\snippet make_circulant2.cpp makeCirculant

As usual, our function takes as argument a \c MatrixBase (see this \ref TopicFunctionTakingEigenTypes "page" for more details).
Then, the CwiseNullaryOp object is constructed through the DenseBase::NullaryExpr static method with the adequate runtime sizes.

Then, we need to implement our \c circulant_functor, which is a straightforward exercise:

\snippet make_circulant2.cpp circulant_func

We are now all set to try our new feature:

\snippet make_circulant2.cpp main


If all the fragments are combined, the following output is produced,
showing that the program works as expected:

\include make_circulant2.out

This implementation of \c makeCirculant is much simpler than \ref TopicNewExpressionType "defining a new expression" from scratch.

*/

}