blob: 4e3b4b08e9f85f8103771e06f52dfa6bdc79cf2f [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Benoit Jacob4d4a23c2010-06-30 10:11:55 -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
Carlos Becker82e2e8b2010-06-28 18:42:09 +01007\li \b Next: \ref TutorialBlockOperations
Benoit Jacobe078bb22010-06-26 14:00:00 -04008
Carlos Becker82e2e8b2010-06-28 18:42:09 +01009This tutorial aims to provide an overview and explanations on how to use
10Eigen's \link ArrayBase Array \endlink class
Carlos Becker9d440052010-06-25 20:16:12 -040011
12\b Table \b of \b contents
13 - \ref TutorialArrayClassWhatIs
14 - \ref TutorialArrayClassTypes
15 - \ref TutorialArrayClassAccess
Carlos Becker82e2e8b2010-06-28 18:42:09 +010016 - \ref TutorialArrayClassCoeffWiseOperations
Carlos Becker9d440052010-06-25 20:16:12 -040017 - \ref TutorialArrayHowToUse
18 - \ref TutorialArrayClassCoeffWiseOperators
19
20\section TutorialArrayClassWhatIs What is the Array class?
Carlos Becker82e2e8b2010-06-28 18:42:09 +010021The \link ArrayBase Array \endlink class provides general-purpose arrays, as opposed to the Matrix class which
22is intended for doing linear algebra. Furthermore, the \link ArrayBase Array \endlink class provides an easy way to
23perform coefficient-wise operations, which might not have a precise meaning in linear matrix algebra,
24such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
25
Carlos Becker9d440052010-06-25 20:16:12 -040026
27\subsection TutorialArrayClassTypes Array type and predefined types
Carlos Becker82e2e8b2010-06-28 18:42:09 +010028The \link ArrayBase Array \endlink class is actually a template that works in a similar way as the \b Matrix one:
Carlos Becker9d440052010-06-25 20:16:12 -040029
30\code
31
32//declaring an Array instance
33Array<type,numRows,numCols> a;
34\endcode
35
Carlos Becker82e2e8b2010-06-28 18:42:09 +010036Eigen provides a bunch of predefined types to make instantiation easier. These types follow the same
37conventions as the ones available for the \b Matrix ones but with some slight differences,
38as shown in the following table:
Carlos Becker9d440052010-06-25 20:16:12 -040039
40FIXME: explain why these differences-
41
42<table class="tutorial_code" align="center">
43<tr><td align="center">\b Type</td><td align="center">\b Typedef</td></tr>
44<tr><td>
45\code Array<double,Dynamic,Dynamic> \endcode</td>
46<td>
47\code ArrayXXd \endcode</td></tr>
48<tr><td>
49\code Array<double,3,3> \endcode</td>
50<td>
51\code Array33d \endcode</td></tr>
52<tr><td>
53\code Array<float,Dynamic,Dynamic> \endcode</td>
54<td>
55\code ArrayXXf \endcode</td></tr>
56<tr><td>
57\code Array<float,3,3> \endcode</td>
58<td>
59\code Array33f \endcode</td></tr>
60</table>
61
62
Carlos Becker82e2e8b2010-06-28 18:42:09 +010063\subsection TutorialArrayClassAccess Accessing values inside an Array
64Write and read-access to coefficients inside \link ArrayBase Array \endlink is done in the same way as with \b Matrix.
65Here some examples are presented, just for clarification:
Carlos Becker9d440052010-06-25 20:16:12 -040066
Carlos Becker82e2e8b2010-06-28 18:42:09 +010067\include Tutorial_ArrayClass_accessing.cpp
Carlos Becker9d440052010-06-25 20:16:12 -040068
Carlos Becker82e2e8b2010-06-28 18:42:09 +010069Output:
70\verbinclude Tutorial_ArrayClass_accessing.out
Carlos Becker9d440052010-06-25 20:16:12 -040071
Carlos Becker82e2e8b2010-06-28 18:42:09 +010072For more information about the comma initializer, refer to \ref TutorialAdvancedInitialization "this page".
Carlos Becker9d440052010-06-25 20:16:12 -040073
74
75
Carlos Becker9d440052010-06-25 20:16:12 -040076
Carlos Becker82e2e8b2010-06-28 18:42:09 +010077
78
79
80
81\section TutorialArrayClassCoeffWiseOperations Coefficient-wise operators
82
83
84
85\subsection TutorialArrayClassCoeffWiseAdd Coefficient-wise addition
86Adding two \link ArrayBase Array \endlink objects has the same result as adding to Matrices, performing coefficient-wise addition. This is valid as long as both arrays have the same dimensions:
87
88\include Tutorial_ArrayClass_addition.cpp
89
90Output:
91\verbinclude Tutorial_ArrayClass_addition.out
92
93The addition operator can also be used to add a scalar to each coefficient in an Array, providing a functionality that was not available for Matrix objects:
94
95\include Tutorial_ArrayClass_addition_scalar.cpp
96
97Output:
98\verbinclude Tutorial_ArrayClass_addition_scalar.out
99
100
101
102
103\subsection TutorialArrayClassCoeffWiseMult Coefficient-wise multiplication
104
105As said before, the \link ArrayBase Array \endlink class looks at operators from a coefficient-wise perspective.
106This makes an important difference with respect to \b Matrix algebraic operations, especially
107with the product operator. The following example performs coefficient-wise multiplication between two array instances:
108
109\include Tutorial_ArrayClass_mult.cpp
110
111Output:
112\verbinclude Tutorial_ArrayClass_mult.out
113
114
Carlos Becker9d440052010-06-25 20:16:12 -0400115
116\section TutorialArrayHowToUse Using arrays and matrices
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100117It is possible to treat the data inside a \b Matrix object as an \link ArrayBase Array \endlink
118and vice-versa. This allows the developer to perform a wide diversity of operators regardless
119of the actual type where the coefficients rely on.
Carlos Becker9d440052010-06-25 20:16:12 -0400120
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100121The \b Matrix class provides a \link MatrixBase::array() .array() \endlink method that
122'converts' it into an \link ArrayBase Array \endlink type, so that coefficient-wise operations
123can be applied easily. On the other side, the \link ArrayBase Array \endlink
124class provides a \link ArrayBase::matrix() .matrix() \endlink method. FIXME: note on overhead?
125Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
126can be used as \b rvalues or \b lvalues in expressions.
Carlos Becker9d440052010-06-25 20:16:12 -0400127
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100128<b>IMPORTANT NOTE:</b> Mixing matrices and arrays in an expression is forbidden with Eigen. However,
129it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
130\link ArrayBase::matrix() .matrix() \endlink. Conversely, assigning a Matrix to an
131\link ArrayBase Array \endlink or vice-versa is allowed.
Carlos Becker9d440052010-06-25 20:16:12 -0400132
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100133\subsection TutorialArrayInteropMatrix Matrix to array example
134The following example shows how to use Array operations with a Matrix object by employing
135the \link MatrixBase::array() .array() \endlink function:
Carlos Becker9d440052010-06-25 20:16:12 -0400136
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100137<table class="tutorial_code"><tr><td>
138\include Tutorial_ArrayClass_interop_matrix.cpp
139</td>
140<td>
141Output:
142\verbinclude Tutorial_ArrayClass_interop_matrix.out
143</td></tr></table>
144
145
146\subsection TutorialArrayInteropArray Array to matrix example
147The following example shows how to use Matrix operations with an \link ArrayBase Array \endlink
148object by employing the \link ArrayBase::matrix() .matrix() \endlink function:
149
150<table class="tutorial_code"><tr><td>
151\include Tutorial_ArrayClass_interop_array.cpp
152</td>
153<td>
154Output:
155\verbinclude Tutorial_ArrayClass_interop_array.out
156</td></tr></table>
157
158\subsection TutorialArrayInteropCombination Example with combinations of .array() and .matrix()
159Here there is a more advanced example:
160
161<table class="tutorial_code"><tr><td>
162\include Tutorial_ArrayClass_interop.cpp
163</td>
164<td>
165Output:
166\verbinclude Tutorial_ArrayClass_interop.out
167</td></tr></table>
168
169
170\b NOTE: there is no need to call \link ArrayBase::matrix() .matrix() \endlink to assign a
171\link ArrayBase Array \endlink type to a \b Matrix or vice-versa.
Carlos Becker9d440052010-06-25 20:16:12 -0400172
173\section TutorialArrayClassCoeffWiseOperators Array coefficient-wise operators
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100174
175FIXME: move to reference table? (I think it is already there)
176
Carlos Becker9d440052010-06-25 20:16:12 -0400177<table class="noborder">
178<tr><td>
179<table class="tutorial_code" style="margin-right:10pt">
180<tr><td>Coefficient wise \link ArrayBase::operator*() product \arrayworld \endlink</td>
181<td>\code array3 = array1 * array2; \endcode
182</td></tr>
183<tr><td>
184Add a scalar to all coefficients</td><td>\code
185array3 = array1 + scalar;
186array3 += scalar;
187array3 -= scalar;
188\endcode
189</td></tr>
190<tr><td>
191Coefficient wise \link ArrayBase::operator/() division \endlink \arrayworld</td><td>\code
192array3 = array1 / array2; \endcode
193</td></tr>
194<tr><td>
195Coefficient wise \link ArrayBase::inverse() reciprocal \endlink \arrayworld</td><td>\code
196array3 = array1.inverse(); \endcode
197</td></tr>
198<tr><td>
199Coefficient wise comparisons \arrayworld \n
200(support all operators)</td><td>\code
201array3 = array1 < array2;
202array3 = array1 <= array2;
203array3 = array1 > array2;
204etc.
205\endcode
206</td></tr></table>
207</td>
208<td><table class="tutorial_code">
209<tr><td>
210\b Trigo \arrayworld: \n
211\link ArrayBase::sin sin \endlink, \link ArrayBase::cos cos \endlink</td><td>\code
212array3 = array1.sin();
213etc.
214\endcode
215</td></tr>
216<tr><td>
217\b Power \arrayworld: \n \link ArrayBase::pow() pow \endlink,
218\link ArrayBase::square square \endlink,
219\link ArrayBase::cube cube \endlink, \n
220\link ArrayBase::sqrt sqrt \endlink,
221\link ArrayBase::exp exp \endlink,
222\link ArrayBase::log log \endlink </td><td>\code
223array3 = array1.square();
224array3 = array1.pow(5);
225array3 = array1.log();
226etc.
227\endcode
228</td></tr>
229<tr><td>
230\link ArrayBase::min min \endlink, \link ArrayBase::max max \endlink, \n
231absolute value (\link ArrayBase::abs() abs \endlink, \link ArrayBase::abs2() abs2 \endlink \arrayworld)
232</td><td>\code
233array3 = array1.min(array2);
234array3 = array1.max(array2);
235array3 = array1.abs();
236array3 = array1.abs2();
237\endcode</td></tr>
238</table>
239</td></tr></table>
240
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400241\li \b Next: \ref TutorialBlockOperations
Carlos Becker9d440052010-06-25 20:16:12 -0400242
243**/
244}