blob: 3fdb4e6b08a1702a195a4f538aa4ad9c5ce8782d [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
Gael Guennebaud2ea1e492012-12-28 18:58:07 +010012\tableofcontents
13
Benoit Jacob08c17c42010-07-01 20:29:13 -040014\section TutorialArrayClassIntro What is the Array class?
15
16The Array class provides general-purpose arrays, as opposed to the Matrix class which
17is intended for linear algebra. Furthermore, the Array class provides an easy way to
18perform coefficient-wise operations, which might not have a linear algebraic meaning,
Carlos Becker82e2e8b2010-06-28 18:42:09 +010019such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
20
Carlos Becker9d440052010-06-25 20:16:12 -040021
Benoit Jacob08c17c42010-07-01 20:29:13 -040022\section TutorialArrayClassTypes Array types
23Array is a class template taking the same template parameters as Matrix.
Jitse Niesen140ad092010-07-12 22:45:57 +010024As with Matrix, the first three template parameters are mandatory:
Carlos Becker9d440052010-06-25 20:16:12 -040025\code
Benoit Jacob08c17c42010-07-01 20:29:13 -040026Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Carlos Becker9d440052010-06-25 20:16:12 -040027\endcode
Jitse Niesen140ad092010-07-12 22:45:57 +010028The last three template parameters are optional. Since this is exactly the same as for Matrix,
29we won't explain it again here and just refer to \ref TutorialMatrixClass.
Carlos Becker9d440052010-06-25 20:16:12 -040030
Benoit Jacob08c17c42010-07-01 20:29:13 -040031Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
32but 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 -040033We 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 +010034the 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 -040035use typedefs of the form ArrayNNt. Some examples are shown in the following table:
Carlos Becker9d440052010-06-25 20:16:12 -040036
Gael Guennebaudf66fe262010-10-19 11:40:49 +020037<table class="manual">
Benoit Jacob08c17c42010-07-01 20:29:13 -040038 <tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020039 <th>Type </th>
40 <th>Typedef </th>
Benoit Jacob08c17c42010-07-01 20:29:13 -040041 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040042 <tr>
43 <td> \code Array<float,Dynamic,1> \endcode </td>
44 <td> \code ArrayXf \endcode </td>
45 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040046 <tr>
47 <td> \code Array<float,3,1> \endcode </td>
48 <td> \code Array3f \endcode </td>
49 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040050 <tr>
51 <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
52 <td> \code ArrayXXd \endcode </td>
53 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040054 <tr>
55 <td> \code Array<double,3,3> \endcode </td>
56 <td> \code Array33d \endcode </td>
57 </tr>
Carlos Becker9d440052010-06-25 20:16:12 -040058</table>
59
60
Benoit Jacob08c17c42010-07-01 20:29:13 -040061\section TutorialArrayClassAccess Accessing values inside an Array
Carlos Becker9d440052010-06-25 20:16:12 -040062
Jitse Niesen140ad092010-07-12 22:45:57 +010063The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices.
64Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them.
Carlos Becker9d440052010-06-25 20:16:12 -040065
Gael Guennebaudf66fe262010-10-19 11:40:49 +020066<table class="example">
67<tr><th>Example:</th><th>Output:</th></tr>
68<tr><td>
69\include Tutorial_ArrayClass_accessors.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010070</td>
71<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020072\verbinclude Tutorial_ArrayClass_accessors.out
Jitse Niesen140ad092010-07-12 22:45:57 +010073</td></tr></table>
74
75For more information about the comma initializer, see \ref TutorialAdvancedInitialization.
Carlos Becker9d440052010-06-25 20:16:12 -040076
77
Jitse Niesen140ad092010-07-12 22:45:57 +010078\section TutorialArrayClassAddSub Addition and subtraction
79
80Adding and subtracting two arrays is the same as for matrices.
81The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.
82
83Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array.
84This provides a functionality that is not directly available for Matrix objects.
85
Gael Guennebaudf66fe262010-10-19 11:40:49 +020086<table class="example">
87<tr><th>Example:</th><th>Output:</th></tr>
88<tr><td>
89\include Tutorial_ArrayClass_addition.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010090</td>
91<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020092\verbinclude Tutorial_ArrayClass_addition.out
Jitse Niesen140ad092010-07-12 22:45:57 +010093</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -040094
Carlos Becker9d440052010-06-25 20:16:12 -040095
Jitse Niesen140ad092010-07-12 22:45:57 +010096\section TutorialArrayClassMult Array multiplication
Carlos Becker82e2e8b2010-06-28 18:42:09 +010097
Benoit Jacob08c17c42010-07-01 20:29:13 -040098First 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 +010099are fundamentally different from matrices, is when you multiply two together. Matrices interpret
Benoit Jacobbb8a25e2011-03-21 06:45:57 -0400100multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two
101arrays can be multiplied if and only if they have the same dimensions.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100102
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200103<table class="example">
104<tr><th>Example:</th><th>Output:</th></tr>
105<tr><td>
106\include Tutorial_ArrayClass_mult.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100107</td>
108<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200109\verbinclude Tutorial_ArrayClass_mult.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100110</td></tr></table>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100111
112
Jitse Niesen140ad092010-07-12 22:45:57 +0100113\section TutorialArrayClassCwiseOther Other coefficient-wise operations
114
Benoit Jacobbb8a25e2011-03-21 06:45:57 -0400115The Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication
116operators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute
Jitse Niesen140ad092010-07-12 22:45:57 +0100117value of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the
Gael Guennebaudf41d96d2012-12-24 13:33:22 +0100118coefficients. If you have two arrays of the same size, you can call \link ArrayBase::min(const Eigen::ArrayBase<OtherDerived>&) const .min(.) \endlink to
Jitse Niesen140ad092010-07-12 22:45:57 +0100119construct the array whose coefficients are the minimum of the corresponding coefficients of the two given
120arrays. These operations are illustrated in the following example.
121
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200122<table class="example">
123<tr><th>Example:</th><th>Output:</th></tr>
124<tr><td>
125\include Tutorial_ArrayClass_cwise_other.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100126</td>
127<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200128\verbinclude Tutorial_ArrayClass_cwise_other.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100129</td></tr></table>
130
131More coefficient-wise operations can be found in the \ref QuickRefPage.
132
Carlos Becker9d440052010-06-25 20:16:12 -0400133
Benoit Jacob08c17c42010-07-01 20:29:13 -0400134\section TutorialArrayClassConvert Converting between array and matrix expressions
Carlos Becker9d440052010-06-25 20:16:12 -0400135
Jitse Niesen140ad092010-07-12 22:45:57 +0100136When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot
137apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic
138operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise
139operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both
140Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives
141access to all operations regardless of the choice of declaring objects as arrays or as matrices.
Benoit Jacob08c17c42010-07-01 20:29:13 -0400142
Jitse Niesen140ad092010-07-12 22:45:57 +0100143\link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that
144'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations
Benoit Jacob08c17c42010-07-01 20:29:13 -0400145can be applied easily. Conversely, \link ArrayBase array expressions \endlink
146have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
147this doesn't have any runtime cost (provided that you let your compiler optimize).
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100148Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
Benoit Jacob5a52f282010-07-01 20:52:40 -0400149can be used as rvalues and as lvalues.
Carlos Becker9d440052010-06-25 20:16:12 -0400150
Tim Holy4a95bad2011-06-19 14:39:19 -0500151Mixing 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 +0100152array directly; the operands of a \c + operator should either both be matrices or both be arrays. However,
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100153it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
Jitse Niesen140ad092010-07-12 22:45:57 +0100154\link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is
155allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix
156variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400157
Jitse Niesen140ad092010-07-12 22:45:57 +0100158The following example shows how to use array operations on a Matrix object by employing the
159\link MatrixBase::array() .array() \endlink method. For example, the statement
160<tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses
161* to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal
162because Eigen allows assigning array expressions to matrix variables).
Benoit Jacob08c17c42010-07-01 20:29:13 -0400163
Gael Guennebaudf41d96d2012-12-24 13:33:22 +0100164As a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct() const
165.cwiseProduct(.) \endlink method for matrices to compute the coefficient-wise product. This is also shown in
Jitse Niesen140ad092010-07-12 22:45:57 +0100166the example program.
Carlos Becker9d440052010-06-25 20:16:12 -0400167
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200168<table class="example">
169<tr><th>Example:</th><th>Output:</th></tr>
170<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100171\include Tutorial_ArrayClass_interop_matrix.cpp
172</td>
173<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100174\verbinclude Tutorial_ArrayClass_interop_matrix.out
175</td></tr></table>
176
Jitse Niesen140ad092010-07-12 22:45:57 +0100177Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt>
178computes their matrix product.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100179
Jitse Niesen140ad092010-07-12 22:45:57 +0100180Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every
181coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the
182expression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices
183\c m and \c n and then the matrix product of the result with \c m.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100184
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200185<table class="example">
186<tr><th>Example:</th><th>Output:</th></tr>
187<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100188\include Tutorial_ArrayClass_interop.cpp
189</td>
190<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100191\verbinclude Tutorial_ArrayClass_interop.out
192</td></tr></table>
193
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400194\li \b Next: \ref TutorialBlockOperations
Carlos Becker9d440052010-06-25 20:16:12 -0400195
Benoit Jacob08c17c42010-07-01 20:29:13 -0400196*/
197
Carlos Becker9d440052010-06-25 20:16:12 -0400198}