blob: 04ecfde37c383f0effa2687fa4578a54e42403d8 [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
3/** \page TutorialArrayClass Tutorial - The Array Class
4 \ingroup Tutorial
5
6This tutorial aims to provide an overview and explanations on how to use Eigen's \b Array class
7
8\b Table \b of \b contents
9 - \ref TutorialArrayClassWhatIs
10 - \ref TutorialArrayClassTypes
11 - \ref TutorialArrayClassAccess
12 - \ref TutorialArrayClassCoeffWiseExamples
13 - \ref TutorialArrayHowToUse
14 - \ref TutorialArrayClassCoeffWiseOperators
15
16\section TutorialArrayClassWhatIs What is the Array class?
17The \b Array class is provided by Eigen in order to perform coefficient-wise operations on matrices. As menioned in the previous section FIXME:link, only linear algebra operations are supported between matrices and vectors. The \b Array class provides a useful abstraction layer that allows the developer to perform a wide range of advanced operations on a matrix, such as coefficient-wise addition, division and multiplication.
18
19\subsection TutorialArrayClassTypes Array type and predefined types
20The \b Array class is actually a template that works in a similar way as the \b Matrix one:
21
22\code
23
24//declaring an Array instance
25Array<type,numRows,numCols> a;
26\endcode
27
28Eigen provides a bunch of predefined types to make instantiation easier. These types follow the same conventions as the ones available for the \b Matrix ones but with some slight differences, as shown in the following table:
29
30FIXME: explain why these differences-
31
32<table class="tutorial_code" align="center">
33<tr><td align="center">\b Type</td><td align="center">\b Typedef</td></tr>
34<tr><td>
35\code Array<double,Dynamic,Dynamic> \endcode</td>
36<td>
37\code ArrayXXd \endcode</td></tr>
38<tr><td>
39\code Array<double,3,3> \endcode</td>
40<td>
41\code Array33d \endcode</td></tr>
42<tr><td>
43\code Array<float,Dynamic,Dynamic> \endcode</td>
44<td>
45\code ArrayXXf \endcode</td></tr>
46<tr><td>
47\code Array<float,3,3> \endcode</td>
48<td>
49\code Array33f \endcode</td></tr>
50</table>
51
52
53\subsection TutorialArrayClassAccess Accessing values inside \b Array
54Write and read-access to coefficients inside \b Array is done in the same way as with \b Matrix. Here some examples are presented, just for clarification:
55
56\code
57 ArrayXXf m(2,2);
58
59 //assign some values coefficient by coefficient
60 m(0,0) = 1.0; m(0,1) = 2.0;
61 m(1,0) = 3.0; m(1,1) = 4.0;
62
63 //print values to standard output
64 std::cout << m << std::endl;
65
66 // using the comma-initializer is also allowed
67 m << 1.0,2.0,
68 3.0,4.0;
69\endcode
70
71\subsection TutorialArrayClassCoeffWiseExamples Simple coefficient-wise operations
72As said before, the \b Array class looks at operators from a coefficient-wise perspective. This makes an important difference with respect to \b Matrix algebraic operations, especially with the product operator. The following example performs coefficient-wise multiplication between two \b Array instances:
73
74\code
75 ArrayXXf m(4,4);
76 ArrayXXf n(4,4);
77 ArrayXXf result;
78
79 // after this operation is executed, result(i,j) = m(i,j) * n(i,j) for every position
80 result = m * n;
81\endcode
82
83
84
85Another example has to do with coefficient-wise addition:
86
87\code
88 ArrayXXf m(4,4);
89 ArrayXXf result;
90
91 // after this operation is executed, result(i,j) = m(i,j) + 4
92 result = m + 4;
93\endcode
94
95\section TutorialArrayHowToUse Using arrays and matrices
96It is possible to treat the data inside a \b Matrix object as an \b Array and vice-versa. This allows the developer to perform a wide diversity of operators regardless of the actual type where the coefficients rely on.
97
98The \b Matrix class provides a \p .array() method that 'converts' it into an \b Array type, so that coefficient-wise operations can be applied easily. On the other side, the \b Array class provides a \p .matrix() method. FIXME: note on overhead
99
100An example using this 'interoperability' is presented below:
101
102\code
103 MatrixXf m(4,4);
104 MatrixXf n(4,4);
105 MatrixXf x(4,4);
106 MatrixXf result;
107
108 //matrix multiplication (non coefficient-wise)
109 result = m * n;
110
111 //coefficient-wise multiplication
112 result = m.array() * n.array();
113
114 // --- More complex example ---
115 // This will perform coefficient-wise multiplication between m and n
116 // to later compute a matrix multiplication between that result and matrix x
117 result = (m.array() * n.array()).matrix() * x;
118
119\endcode
120
121\b NOTE: there is no need to call \p .matrix() to assign a \b Array type to a \b Matrix or vice-versa.
122
123\section TutorialArrayClassCoeffWiseOperators Array coefficient-wise operators
124<table class="noborder">
125<tr><td>
126<table class="tutorial_code" style="margin-right:10pt">
127<tr><td>Coefficient wise \link ArrayBase::operator*() product \arrayworld \endlink</td>
128<td>\code array3 = array1 * array2; \endcode
129</td></tr>
130<tr><td>
131Add a scalar to all coefficients</td><td>\code
132array3 = array1 + scalar;
133array3 += scalar;
134array3 -= scalar;
135\endcode
136</td></tr>
137<tr><td>
138Coefficient wise \link ArrayBase::operator/() division \endlink \arrayworld</td><td>\code
139array3 = array1 / array2; \endcode
140</td></tr>
141<tr><td>
142Coefficient wise \link ArrayBase::inverse() reciprocal \endlink \arrayworld</td><td>\code
143array3 = array1.inverse(); \endcode
144</td></tr>
145<tr><td>
146Coefficient wise comparisons \arrayworld \n
147(support all operators)</td><td>\code
148array3 = array1 < array2;
149array3 = array1 <= array2;
150array3 = array1 > array2;
151etc.
152\endcode
153</td></tr></table>
154</td>
155<td><table class="tutorial_code">
156<tr><td>
157\b Trigo \arrayworld: \n
158\link ArrayBase::sin sin \endlink, \link ArrayBase::cos cos \endlink</td><td>\code
159array3 = array1.sin();
160etc.
161\endcode
162</td></tr>
163<tr><td>
164\b Power \arrayworld: \n \link ArrayBase::pow() pow \endlink,
165\link ArrayBase::square square \endlink,
166\link ArrayBase::cube cube \endlink, \n
167\link ArrayBase::sqrt sqrt \endlink,
168\link ArrayBase::exp exp \endlink,
169\link ArrayBase::log log \endlink </td><td>\code
170array3 = array1.square();
171array3 = array1.pow(5);
172array3 = array1.log();
173etc.
174\endcode
175</td></tr>
176<tr><td>
177\link ArrayBase::min min \endlink, \link ArrayBase::max max \endlink, \n
178absolute value (\link ArrayBase::abs() abs \endlink, \link ArrayBase::abs2() abs2 \endlink \arrayworld)
179</td><td>\code
180array3 = array1.min(array2);
181array3 = array1.max(array2);
182array3 = array1.abs();
183array3 = array1.abs2();
184\endcode</td></tr>
185</table>
186</td></tr></table>
187
188
189**/
190}