aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/C03_TutorialArrayClass.dox
blob: 57ec64219ba6b5666544d97fcc049d2e062b8542 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
namespace Eigen {

/** \page TutorialArrayClass Tutorial page 3 - The %Array class and coefficient-wise operations
    \ingroup Tutorial

\li \b Previous: \ref TutorialMatrixArithmetic
\li \b Next: \ref TutorialBlockOperations

This tutorial aims to provide an overview and explanations on how to use
Eigen's \link ArrayBase Array \endlink class

\b Table \b of \b contents
  - \ref TutorialArrayClassIntro
  - \ref TutorialArrayClassTypes
  - \ref TutorialArrayClassAccess
  - \ref TutorialArrayClassAddSub
  - \ref TutorialArrayClassMult
  - \ref TutorialArrayClassConvert

\section TutorialArrayClassIntro What is the Array class?

The Array class provides general-purpose arrays, as opposed to the Matrix class which
is intended for linear algebra. Furthermore, the Array class provides an easy way to
perform coefficient-wise operations, which might not have a linear algebraic meaning,
such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.


\section TutorialArrayClassTypes Array types
Array is a class template taking the same template parameters as Matrix.
As with with, the first 3 template parameters are mandatory:
\code
Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
\endcode
And the last 3 template parameters are optional. Since this is exactly the same as for Matrix,
we won't reexplain it and just refer to \ref TutorialMatrixClass "this page".

Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays.
We adopt that convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are
as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we
use typedefs of the form ArrayNNt. Some examples are shown in the following table:

<table class="tutorial_code" align="center">

  <tr>
    <td align="center">\b Type </td>
    <td align="center">\b Typedef </td>
  </tr>

  <tr>
    <td> \code Array<float,Dynamic,1> \endcode </td>
    <td> \code ArrayXf \endcode </td>
  </tr>

  <tr>
    <td> \code Array<float,3,1> \endcode </td>
    <td> \code Array3f \endcode </td>
  </tr>

  <tr>
    <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
    <td> \code ArrayXXd \endcode </td>
  </tr>

  <tr>
    <td> \code Array<double,3,3> \endcode </td>
    <td> \code Array33d \endcode </td>
  </tr>

</table>


\section TutorialArrayClassAccess Accessing values inside an Array
Write and read access to coefficients of an array expression is done in the same way as with matrix expressions.
For example:

\include Tutorial_ArrayClass_accessors.cpp
Output:
\verbinclude Tutorial_ArrayClass_accessors.out

For more information about the comma initializer, refer to \ref TutorialAdvancedInitialization "this page".


\section TutorialArrayClassAddSub Addition and substraction


Adding and substracting two arrays has the same result as for Matrices.
This is valid as long as both arrays have the same sizes:

\include Tutorial_ArrayClass_addition.cpp
Output:
\verbinclude Tutorial_ArrayClass_addition.out

It is also allowed to add a scalar to each coefficient in an Array,
providing a functionality that was not available for Matrix objects:

\include Tutorial_ArrayClass_addition_scalar.cpp
Output:
\verbinclude Tutorial_ArrayClass_addition_scalar.out


\subsection TutorialArrayClassMult Array multiplication

First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays
are fundamentally different from matrices, is when you multiply two arrays together. While Matrices interprete
multiplication as matrix product, arrays interprete multiplication as coefficient-wise product. For example:

\include Tutorial_ArrayClass_mult.cpp
Output:
\verbinclude Tutorial_ArrayClass_mult.out



\section TutorialArrayClassConvert Converting between array and matrix expressions

It is possible to wrap a matrix expression as an array expression and conversely. This gives access to all operations
regardless of the choice of declaring objects as arrays or as matrices.

\link MatrixBase Matrix expressions \endlink have a \link MatrixBase::array() .array() \endlink method that
'converts' them into \link ArrayBase array expressions \endlink, so that coefficient-wise operations
can be applied easily. Conversely, \link ArrayBase array expressions \endlink
have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
this doesn't have any runtime cost (provided that you let your compiler optimize).

Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink 
can be used as \b rvalues and as \b lvalues.

Mixing matrices and arrays in an expression is forbidden with Eigen. However,
it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and 
\link ArrayBase::matrix() .matrix() \endlink.

On the other hand, assigning a matrix expression to an array expression is allowed.

\subsection TutorialArrayClassInteropMatrix Matrix to array example
The following example shows how to use array operations on a Matrix object by employing
the \link MatrixBase::array() .array() \endlink method:

<table class="tutorial_code"><tr><td>
\include Tutorial_ArrayClass_interop_matrix.cpp
</td>
<td>
Output:
\verbinclude Tutorial_ArrayClass_interop_matrix.out
</td></tr></table>


\subsection TutorialArrayClassInteropArray Array to matrix example
The following example shows how to use matrix operations with an Array
object by employing the \link ArrayBase::matrix() .matrix() \endlink method:

<table class="tutorial_code"><tr><td>
\include Tutorial_ArrayClass_interop_array.cpp
</td>
<td>
Output:
\verbinclude Tutorial_ArrayClass_interop_array.out
</td></tr></table>

\subsection TutorialArrayClassInteropCombination Example with combinations of .array() and .matrix()
Here is a more advanced example:

<table class="tutorial_code"><tr><td>
\include Tutorial_ArrayClass_interop.cpp
</td>
<td>
Output:
\verbinclude Tutorial_ArrayClass_interop.out
</td></tr></table>

\li \b Next: \ref TutorialBlockOperations

*/

}