blob: a1d8d69859986c4d43b7d5462e29db6d4fe77f70 [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Benoit Jacob08c17c42010-07-01 20:29:13 -04003/** \page TutorialArrayClass Tutorial page 3 - The %Array class and coefficient-wise operations
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
Jitse Niesen140ad092010-07-12 22:45:57 +010010Eigen's Array class.
Carlos Becker9d440052010-06-25 20:16:12 -040011
12\b Table \b of \b contents
Benoit Jacob08c17c42010-07-01 20:29:13 -040013 - \ref TutorialArrayClassIntro
14 - \ref TutorialArrayClassTypes
15 - \ref TutorialArrayClassAccess
16 - \ref TutorialArrayClassAddSub
17 - \ref TutorialArrayClassMult
Jitse Niesen140ad092010-07-12 22:45:57 +010018 - \ref TutorialArrayClassCwiseOther
Benoit Jacob08c17c42010-07-01 20:29:13 -040019 - \ref TutorialArrayClassConvert
Carlos Becker9d440052010-06-25 20:16:12 -040020
Benoit Jacob08c17c42010-07-01 20:29:13 -040021\section TutorialArrayClassIntro What is the Array class?
22
23The Array class provides general-purpose arrays, as opposed to the Matrix class which
24is intended for linear algebra. Furthermore, the Array class provides an easy way to
25perform coefficient-wise operations, which might not have a linear algebraic meaning,
Carlos Becker82e2e8b2010-06-28 18:42:09 +010026such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
27
Carlos Becker9d440052010-06-25 20:16:12 -040028
Benoit Jacob08c17c42010-07-01 20:29:13 -040029\section TutorialArrayClassTypes Array types
30Array is a class template taking the same template parameters as Matrix.
Jitse Niesen140ad092010-07-12 22:45:57 +010031As with Matrix, the first three template parameters are mandatory:
Carlos Becker9d440052010-06-25 20:16:12 -040032\code
Benoit Jacob08c17c42010-07-01 20:29:13 -040033Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Carlos Becker9d440052010-06-25 20:16:12 -040034\endcode
Jitse Niesen140ad092010-07-12 22:45:57 +010035The last three template parameters are optional. Since this is exactly the same as for Matrix,
36we won't explain it again here and just refer to \ref TutorialMatrixClass.
Carlos Becker9d440052010-06-25 20:16:12 -040037
Benoit Jacob08c17c42010-07-01 20:29:13 -040038Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
39but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays.
Benoit Jacobbb8a25e2011-03-21 06:45:57 -040040We adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are
Jitse Niesen140ad092010-07-12 22:45:57 +010041the size and the scalar type, as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we
Benoit Jacob08c17c42010-07-01 20:29:13 -040042use typedefs of the form ArrayNNt. Some examples are shown in the following table:
Carlos Becker9d440052010-06-25 20:16:12 -040043
Gael Guennebaudf66fe262010-10-19 11:40:49 +020044<table class="manual">
Benoit Jacob08c17c42010-07-01 20:29:13 -040045 <tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020046 <th>Type </th>
47 <th>Typedef </th>
Benoit Jacob08c17c42010-07-01 20:29:13 -040048 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040049 <tr>
50 <td> \code Array<float,Dynamic,1> \endcode </td>
51 <td> \code ArrayXf \endcode </td>
52 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040053 <tr>
54 <td> \code Array<float,3,1> \endcode </td>
55 <td> \code Array3f \endcode </td>
56 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040057 <tr>
58 <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
59 <td> \code ArrayXXd \endcode </td>
60 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040061 <tr>
62 <td> \code Array<double,3,3> \endcode </td>
63 <td> \code Array33d \endcode </td>
64 </tr>
Carlos Becker9d440052010-06-25 20:16:12 -040065</table>
66
67
Benoit Jacob08c17c42010-07-01 20:29:13 -040068\section TutorialArrayClassAccess Accessing values inside an Array
Carlos Becker9d440052010-06-25 20:16:12 -040069
Jitse Niesen140ad092010-07-12 22:45:57 +010070The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices.
71Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them.
Carlos Becker9d440052010-06-25 20:16:12 -040072
Gael Guennebaudf66fe262010-10-19 11:40:49 +020073<table class="example">
74<tr><th>Example:</th><th>Output:</th></tr>
75<tr><td>
76\include Tutorial_ArrayClass_accessors.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010077</td>
78<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020079\verbinclude Tutorial_ArrayClass_accessors.out
Jitse Niesen140ad092010-07-12 22:45:57 +010080</td></tr></table>
81
82For more information about the comma initializer, see \ref TutorialAdvancedInitialization.
Carlos Becker9d440052010-06-25 20:16:12 -040083
84
Jitse Niesen140ad092010-07-12 22:45:57 +010085\section TutorialArrayClassAddSub Addition and subtraction
86
87Adding and subtracting two arrays is the same as for matrices.
88The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.
89
90Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array.
91This provides a functionality that is not directly available for Matrix objects.
92
Gael Guennebaudf66fe262010-10-19 11:40:49 +020093<table class="example">
94<tr><th>Example:</th><th>Output:</th></tr>
95<tr><td>
96\include Tutorial_ArrayClass_addition.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010097</td>
98<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020099\verbinclude Tutorial_ArrayClass_addition.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100100</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400101
Carlos Becker9d440052010-06-25 20:16:12 -0400102
Jitse Niesen140ad092010-07-12 22:45:57 +0100103\section TutorialArrayClassMult Array multiplication
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100104
Benoit Jacob08c17c42010-07-01 20:29:13 -0400105First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays
Jitse Niesen140ad092010-07-12 22:45:57 +0100106are fundamentally different from matrices, is when you multiply two together. Matrices interpret
Benoit Jacobbb8a25e2011-03-21 06:45:57 -0400107multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two
108arrays can be multiplied if and only if they have the same dimensions.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100109
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200110<table class="example">
111<tr><th>Example:</th><th>Output:</th></tr>
112<tr><td>
113\include Tutorial_ArrayClass_mult.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100114</td>
115<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200116\verbinclude Tutorial_ArrayClass_mult.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100117</td></tr></table>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100118
119
Jitse Niesen140ad092010-07-12 22:45:57 +0100120\section TutorialArrayClassCwiseOther Other coefficient-wise operations
121
Benoit Jacobbb8a25e2011-03-21 06:45:57 -0400122The Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication
123operators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute
Jitse Niesen140ad092010-07-12 22:45:57 +0100124value of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the
125coefficients. If you have two arrays of the same size, you can call \link ArrayBase::min() .min() \endlink to
126construct the array whose coefficients are the minimum of the corresponding coefficients of the two given
127arrays. These operations are illustrated in the following example.
128
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200129<table class="example">
130<tr><th>Example:</th><th>Output:</th></tr>
131<tr><td>
132\include Tutorial_ArrayClass_cwise_other.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100133</td>
134<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200135\verbinclude Tutorial_ArrayClass_cwise_other.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100136</td></tr></table>
137
138More coefficient-wise operations can be found in the \ref QuickRefPage.
139
Carlos Becker9d440052010-06-25 20:16:12 -0400140
Benoit Jacob08c17c42010-07-01 20:29:13 -0400141\section TutorialArrayClassConvert Converting between array and matrix expressions
Carlos Becker9d440052010-06-25 20:16:12 -0400142
Jitse Niesen140ad092010-07-12 22:45:57 +0100143When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot
144apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic
145operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise
146operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both
147Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives
148access to all operations regardless of the choice of declaring objects as arrays or as matrices.
Benoit Jacob08c17c42010-07-01 20:29:13 -0400149
Jitse Niesen140ad092010-07-12 22:45:57 +0100150\link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that
151'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations
Benoit Jacob08c17c42010-07-01 20:29:13 -0400152can be applied easily. Conversely, \link ArrayBase array expressions \endlink
153have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
154this doesn't have any runtime cost (provided that you let your compiler optimize).
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100155Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
Benoit Jacob5a52f282010-07-01 20:52:40 -0400156can be used as rvalues and as lvalues.
Carlos Becker9d440052010-06-25 20:16:12 -0400157
Tim Holy4a95bad2011-06-19 14:39:19 -0500158Mixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a matrix and
Jitse Niesen140ad092010-07-12 22:45:57 +0100159array directly; the operands of a \c + operator should either both be matrices or both be arrays. However,
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100160it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
Jitse Niesen140ad092010-07-12 22:45:57 +0100161\link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is
162allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix
163variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400164
Jitse Niesen140ad092010-07-12 22:45:57 +0100165The following example shows how to use array operations on a Matrix object by employing the
166\link MatrixBase::array() .array() \endlink method. For example, the statement
167<tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses
168* to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal
169because Eigen allows assigning array expressions to matrix variables).
Benoit Jacob08c17c42010-07-01 20:29:13 -0400170
Jitse Niesen140ad092010-07-12 22:45:57 +0100171As a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct()
172.cwiseProduct() \endlink method for matrices to compute the coefficient-wise product. This is also shown in
173the example program.
Carlos Becker9d440052010-06-25 20:16:12 -0400174
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200175<table class="example">
176<tr><th>Example:</th><th>Output:</th></tr>
177<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100178\include Tutorial_ArrayClass_interop_matrix.cpp
179</td>
180<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100181\verbinclude Tutorial_ArrayClass_interop_matrix.out
182</td></tr></table>
183
Jitse Niesen140ad092010-07-12 22:45:57 +0100184Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt>
185computes their matrix product.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100186
Jitse Niesen140ad092010-07-12 22:45:57 +0100187Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every
188coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the
189expression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices
190\c m and \c n and then the matrix product of the result with \c m.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100191
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200192<table class="example">
193<tr><th>Example:</th><th>Output:</th></tr>
194<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100195\include Tutorial_ArrayClass_interop.cpp
196</td>
197<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100198\verbinclude Tutorial_ArrayClass_interop.out
199</td></tr></table>
200
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400201\li \b Next: \ref TutorialBlockOperations
Carlos Becker9d440052010-06-25 20:16:12 -0400202
Benoit Jacob08c17c42010-07-01 20:29:13 -0400203*/
204
Carlos Becker9d440052010-06-25 20:16:12 -0400205}