blob: 92bcebe9d9a18d58cb93cf80e6a26431ccbc364e [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.
40We adopt that 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
44<table class="tutorial_code" align="center">
Benoit Jacob08c17c42010-07-01 20:29:13 -040045
46 <tr>
47 <td align="center">\b Type </td>
48 <td align="center">\b Typedef </td>
49 </tr>
50
51 <tr>
52 <td> \code Array<float,Dynamic,1> \endcode </td>
53 <td> \code ArrayXf \endcode </td>
54 </tr>
55
56 <tr>
57 <td> \code Array<float,3,1> \endcode </td>
58 <td> \code Array3f \endcode </td>
59 </tr>
60
61 <tr>
62 <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
63 <td> \code ArrayXXd \endcode </td>
64 </tr>
65
66 <tr>
67 <td> \code Array<double,3,3> \endcode </td>
68 <td> \code Array33d \endcode </td>
69 </tr>
70
Carlos Becker9d440052010-06-25 20:16:12 -040071</table>
72
73
Benoit Jacob08c17c42010-07-01 20:29:13 -040074\section TutorialArrayClassAccess Accessing values inside an Array
Carlos Becker9d440052010-06-25 20:16:12 -040075
Jitse Niesen140ad092010-07-12 22:45:57 +010076The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices.
77Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them.
Carlos Becker9d440052010-06-25 20:16:12 -040078
Jitse Niesen140ad092010-07-12 22:45:57 +010079<table class="tutorial_code"><tr><td>
80Example: \include Tutorial_ArrayClass_accessors.cpp
81</td>
82<td>
83Output: \verbinclude Tutorial_ArrayClass_accessors.out
84</td></tr></table>
85
86For more information about the comma initializer, see \ref TutorialAdvancedInitialization.
Carlos Becker9d440052010-06-25 20:16:12 -040087
88
Jitse Niesen140ad092010-07-12 22:45:57 +010089\section TutorialArrayClassAddSub Addition and subtraction
90
91Adding and subtracting two arrays is the same as for matrices.
92The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.
93
94Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array.
95This provides a functionality that is not directly available for Matrix objects.
96
97<table class="tutorial_code"><tr><td>
98Example: \include Tutorial_ArrayClass_addition.cpp
99</td>
100<td>
101Output: \verbinclude Tutorial_ArrayClass_addition.out
102</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400103
Carlos Becker9d440052010-06-25 20:16:12 -0400104
Jitse Niesen140ad092010-07-12 22:45:57 +0100105\section TutorialArrayClassMult Array multiplication
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100106
Benoit Jacob08c17c42010-07-01 20:29:13 -0400107First 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 +0100108are fundamentally different from matrices, is when you multiply two together. Matrices interpret
109multiplication as the matrix product and arrays interpret multiplication as the coefficient-wise product. Thus, two
110arrays can be multiplied if they have the same size.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100111
Jitse Niesen140ad092010-07-12 22:45:57 +0100112<table class="tutorial_code"><tr><td>
113Example: \include Tutorial_ArrayClass_mult.cpp
114</td>
115<td>
116Output: \verbinclude Tutorial_ArrayClass_mult.out
117</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
122The Array class defined other coefficient-wise operations besides the addition, subtraction and multiplication
123operators described about. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute
124value 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
129<table class="tutorial_code"><tr><td>
130Example: \include Tutorial_ArrayClass_cwise_other.cpp
131</td>
132<td>
133Output: \verbinclude Tutorial_ArrayClass_cwise_other.out
134</td></tr></table>
135
136More coefficient-wise operations can be found in the \ref QuickRefPage.
137
Carlos Becker9d440052010-06-25 20:16:12 -0400138
Benoit Jacob08c17c42010-07-01 20:29:13 -0400139\section TutorialArrayClassConvert Converting between array and matrix expressions
Carlos Becker9d440052010-06-25 20:16:12 -0400140
Jitse Niesen140ad092010-07-12 22:45:57 +0100141When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot
142apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic
143operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise
144operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both
145Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives
146access to all operations regardless of the choice of declaring objects as arrays or as matrices.
Benoit Jacob08c17c42010-07-01 20:29:13 -0400147
Jitse Niesen140ad092010-07-12 22:45:57 +0100148\link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that
149'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations
Benoit Jacob08c17c42010-07-01 20:29:13 -0400150can be applied easily. Conversely, \link ArrayBase array expressions \endlink
151have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
152this doesn't have any runtime cost (provided that you let your compiler optimize).
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100153Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
Benoit Jacob5a52f282010-07-01 20:52:40 -0400154can be used as rvalues and as lvalues.
Carlos Becker9d440052010-06-25 20:16:12 -0400155
Jitse Niesen140ad092010-07-12 22:45:57 +0100156Mixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a amtrix and
157array directly; the operands of a \c + operator should either both be matrices or both be arrays. However,
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100158it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
Jitse Niesen140ad092010-07-12 22:45:57 +0100159\link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is
160allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix
161variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400162
Jitse Niesen140ad092010-07-12 22:45:57 +0100163The following example shows how to use array operations on a Matrix object by employing the
164\link MatrixBase::array() .array() \endlink method. For example, the statement
165<tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses
166* to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal
167because Eigen allows assigning array expressions to matrix variables).
Benoit Jacob08c17c42010-07-01 20:29:13 -0400168
Jitse Niesen140ad092010-07-12 22:45:57 +0100169As a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct()
170.cwiseProduct() \endlink method for matrices to compute the coefficient-wise product. This is also shown in
171the example program.
Carlos Becker9d440052010-06-25 20:16:12 -0400172
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100173<table class="tutorial_code"><tr><td>
174\include Tutorial_ArrayClass_interop_matrix.cpp
175</td>
176<td>
177Output:
178\verbinclude Tutorial_ArrayClass_interop_matrix.out
179</td></tr></table>
180
Jitse Niesen140ad092010-07-12 22:45:57 +0100181Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt>
182computes their matrix product.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100183
Jitse Niesen140ad092010-07-12 22:45:57 +0100184Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every
185coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the
186expression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices
187\c m and \c n and then the matrix product of the result with \c m.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100188
189<table class="tutorial_code"><tr><td>
190\include Tutorial_ArrayClass_interop.cpp
191</td>
192<td>
193Output:
194\verbinclude Tutorial_ArrayClass_interop.out
195</td></tr></table>
196
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400197\li \b Next: \ref TutorialBlockOperations
Carlos Becker9d440052010-06-25 20:16:12 -0400198
Benoit Jacob08c17c42010-07-01 20:29:13 -0400199*/
200
Carlos Becker9d440052010-06-25 20:16:12 -0400201}