blob: 8944c061ad4ec25d5251ccd263a4126117d1bc3c [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Benoit Jacobe078bb22010-06-26 14:00:00 -04003/** \page TutorialArrayClass Tutorial page 3 - The Array Class
Carlos Becker9d440052010-06-25 20:16:12 -04004 \ingroup Tutorial
5
Benoit Jacobe078bb22010-06-26 14:00:00 -04006\li \b Previous: \ref TutorialMatrixArithmetic
7\li \b Next: (not yet written)
8
Carlos Becker9d440052010-06-25 20:16:12 -04009This tutorial aims to provide an overview and explanations on how to use Eigen's \b Array class
10
11\b Table \b of \b contents
12 - \ref TutorialArrayClassWhatIs
13 - \ref TutorialArrayClassTypes
14 - \ref TutorialArrayClassAccess
15 - \ref TutorialArrayClassCoeffWiseExamples
16 - \ref TutorialArrayHowToUse
17 - \ref TutorialArrayClassCoeffWiseOperators
18
19\section TutorialArrayClassWhatIs What is the Array class?
20The \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.
21
22\subsection TutorialArrayClassTypes Array type and predefined types
23The \b Array class is actually a template that works in a similar way as the \b Matrix one:
24
25\code
26
27//declaring an Array instance
28Array<type,numRows,numCols> a;
29\endcode
30
31Eigen 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:
32
33FIXME: explain why these differences-
34
35<table class="tutorial_code" align="center">
36<tr><td align="center">\b Type</td><td align="center">\b Typedef</td></tr>
37<tr><td>
38\code Array<double,Dynamic,Dynamic> \endcode</td>
39<td>
40\code ArrayXXd \endcode</td></tr>
41<tr><td>
42\code Array<double,3,3> \endcode</td>
43<td>
44\code Array33d \endcode</td></tr>
45<tr><td>
46\code Array<float,Dynamic,Dynamic> \endcode</td>
47<td>
48\code ArrayXXf \endcode</td></tr>
49<tr><td>
50\code Array<float,3,3> \endcode</td>
51<td>
52\code Array33f \endcode</td></tr>
53</table>
54
55
56\subsection TutorialArrayClassAccess Accessing values inside \b Array
57Write 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:
58
59\code
60 ArrayXXf m(2,2);
61
62 //assign some values coefficient by coefficient
63 m(0,0) = 1.0; m(0,1) = 2.0;
64 m(1,0) = 3.0; m(1,1) = 4.0;
65
66 //print values to standard output
67 std::cout << m << std::endl;
68
69 // using the comma-initializer is also allowed
70 m << 1.0,2.0,
71 3.0,4.0;
72\endcode
73
74\subsection TutorialArrayClassCoeffWiseExamples Simple coefficient-wise operations
75As 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:
76
77\code
78 ArrayXXf m(4,4);
79 ArrayXXf n(4,4);
80 ArrayXXf result;
81
82 // after this operation is executed, result(i,j) = m(i,j) * n(i,j) for every position
83 result = m * n;
84\endcode
85
86
87
88Another example has to do with coefficient-wise addition:
89
90\code
91 ArrayXXf m(4,4);
92 ArrayXXf result;
93
94 // after this operation is executed, result(i,j) = m(i,j) + 4
95 result = m + 4;
96\endcode
97
98\section TutorialArrayHowToUse Using arrays and matrices
99It 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.
100
101The \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
102
103An example using this 'interoperability' is presented below:
104
105\code
106 MatrixXf m(4,4);
107 MatrixXf n(4,4);
108 MatrixXf x(4,4);
109 MatrixXf result;
110
111 //matrix multiplication (non coefficient-wise)
112 result = m * n;
113
114 //coefficient-wise multiplication
115 result = m.array() * n.array();
116
117 // --- More complex example ---
118 // This will perform coefficient-wise multiplication between m and n
119 // to later compute a matrix multiplication between that result and matrix x
120 result = (m.array() * n.array()).matrix() * x;
121
122\endcode
123
124\b NOTE: there is no need to call \p .matrix() to assign a \b Array type to a \b Matrix or vice-versa.
125
126\section TutorialArrayClassCoeffWiseOperators Array coefficient-wise operators
127<table class="noborder">
128<tr><td>
129<table class="tutorial_code" style="margin-right:10pt">
130<tr><td>Coefficient wise \link ArrayBase::operator*() product \arrayworld \endlink</td>
131<td>\code array3 = array1 * array2; \endcode
132</td></tr>
133<tr><td>
134Add a scalar to all coefficients</td><td>\code
135array3 = array1 + scalar;
136array3 += scalar;
137array3 -= scalar;
138\endcode
139</td></tr>
140<tr><td>
141Coefficient wise \link ArrayBase::operator/() division \endlink \arrayworld</td><td>\code
142array3 = array1 / array2; \endcode
143</td></tr>
144<tr><td>
145Coefficient wise \link ArrayBase::inverse() reciprocal \endlink \arrayworld</td><td>\code
146array3 = array1.inverse(); \endcode
147</td></tr>
148<tr><td>
149Coefficient wise comparisons \arrayworld \n
150(support all operators)</td><td>\code
151array3 = array1 < array2;
152array3 = array1 <= array2;
153array3 = array1 > array2;
154etc.
155\endcode
156</td></tr></table>
157</td>
158<td><table class="tutorial_code">
159<tr><td>
160\b Trigo \arrayworld: \n
161\link ArrayBase::sin sin \endlink, \link ArrayBase::cos cos \endlink</td><td>\code
162array3 = array1.sin();
163etc.
164\endcode
165</td></tr>
166<tr><td>
167\b Power \arrayworld: \n \link ArrayBase::pow() pow \endlink,
168\link ArrayBase::square square \endlink,
169\link ArrayBase::cube cube \endlink, \n
170\link ArrayBase::sqrt sqrt \endlink,
171\link ArrayBase::exp exp \endlink,
172\link ArrayBase::log log \endlink </td><td>\code
173array3 = array1.square();
174array3 = array1.pow(5);
175array3 = array1.log();
176etc.
177\endcode
178</td></tr>
179<tr><td>
180\link ArrayBase::min min \endlink, \link ArrayBase::max max \endlink, \n
181absolute value (\link ArrayBase::abs() abs \endlink, \link ArrayBase::abs2() abs2 \endlink \arrayworld)
182</td><td>\code
183array3 = array1.min(array2);
184array3 = array1.max(array2);
185array3 = array1.abs();
186array3 = array1.abs2();
187\endcode</td></tr>
188</table>
189</td></tr></table>
190
191
192**/
193}