blob: 6432684aa55ddc9edd140a3cbb010f3ad98d634b [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialArrayClass The Array class and coefficient-wise operations
Carlos Becker9d440052010-06-25 20:16:12 -04004
Gael Guennebaud091a49c2013-01-06 23:48:59 +01005This page aims to provide an overview and explanations on how to use
Jitse Niesen140ad092010-07-12 22:45:57 +01006Eigen's Array class.
Carlos Becker9d440052010-06-25 20:16:12 -04007
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01008\eigenAutoToc
Gael Guennebaud2ea1e492012-12-28 18:58:07 +01009
Benoit Jacob08c17c42010-07-01 20:29:13 -040010\section TutorialArrayClassIntro What is the Array class?
11
12The Array class provides general-purpose arrays, as opposed to the Matrix class which
13is intended for linear algebra. Furthermore, the Array class provides an easy way to
14perform coefficient-wise operations, which might not have a linear algebraic meaning,
Carlos Becker82e2e8b2010-06-28 18:42:09 +010015such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
16
Carlos Becker9d440052010-06-25 20:16:12 -040017
Benoit Jacob08c17c42010-07-01 20:29:13 -040018\section TutorialArrayClassTypes Array types
19Array is a class template taking the same template parameters as Matrix.
Jitse Niesen140ad092010-07-12 22:45:57 +010020As with Matrix, the first three template parameters are mandatory:
Carlos Becker9d440052010-06-25 20:16:12 -040021\code
Benoit Jacob08c17c42010-07-01 20:29:13 -040022Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Carlos Becker9d440052010-06-25 20:16:12 -040023\endcode
Jitse Niesen140ad092010-07-12 22:45:57 +010024The last three template parameters are optional. Since this is exactly the same as for Matrix,
25we won't explain it again here and just refer to \ref TutorialMatrixClass.
Carlos Becker9d440052010-06-25 20:16:12 -040026
Benoit Jacob08c17c42010-07-01 20:29:13 -040027Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
28but 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 -040029We 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 +010030the 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 -040031use typedefs of the form ArrayNNt. Some examples are shown in the following table:
Carlos Becker9d440052010-06-25 20:16:12 -040032
Gael Guennebaudf66fe262010-10-19 11:40:49 +020033<table class="manual">
Benoit Jacob08c17c42010-07-01 20:29:13 -040034 <tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020035 <th>Type </th>
36 <th>Typedef </th>
Benoit Jacob08c17c42010-07-01 20:29:13 -040037 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040038 <tr>
39 <td> \code Array<float,Dynamic,1> \endcode </td>
40 <td> \code ArrayXf \endcode </td>
41 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040042 <tr>
43 <td> \code Array<float,3,1> \endcode </td>
44 <td> \code Array3f \endcode </td>
45 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040046 <tr>
47 <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
48 <td> \code ArrayXXd \endcode </td>
49 </tr>
Benoit Jacob08c17c42010-07-01 20:29:13 -040050 <tr>
51 <td> \code Array<double,3,3> \endcode </td>
52 <td> \code Array33d \endcode </td>
53 </tr>
Carlos Becker9d440052010-06-25 20:16:12 -040054</table>
55
56
Benoit Jacob08c17c42010-07-01 20:29:13 -040057\section TutorialArrayClassAccess Accessing values inside an Array
Carlos Becker9d440052010-06-25 20:16:12 -040058
Jitse Niesen140ad092010-07-12 22:45:57 +010059The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices.
60Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them.
Carlos Becker9d440052010-06-25 20:16:12 -040061
Gael Guennebaudf66fe262010-10-19 11:40:49 +020062<table class="example">
63<tr><th>Example:</th><th>Output:</th></tr>
64<tr><td>
65\include Tutorial_ArrayClass_accessors.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010066</td>
67<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020068\verbinclude Tutorial_ArrayClass_accessors.out
Jitse Niesen140ad092010-07-12 22:45:57 +010069</td></tr></table>
70
71For more information about the comma initializer, see \ref TutorialAdvancedInitialization.
Carlos Becker9d440052010-06-25 20:16:12 -040072
73
Jitse Niesen140ad092010-07-12 22:45:57 +010074\section TutorialArrayClassAddSub Addition and subtraction
75
76Adding and subtracting two arrays is the same as for matrices.
77The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.
78
79Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array.
80This provides a functionality that is not directly available for Matrix objects.
81
Gael Guennebaudf66fe262010-10-19 11:40:49 +020082<table class="example">
83<tr><th>Example:</th><th>Output:</th></tr>
84<tr><td>
85\include Tutorial_ArrayClass_addition.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +010086</td>
87<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020088\verbinclude Tutorial_ArrayClass_addition.out
Jitse Niesen140ad092010-07-12 22:45:57 +010089</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -040090
Carlos Becker9d440052010-06-25 20:16:12 -040091
Jitse Niesen140ad092010-07-12 22:45:57 +010092\section TutorialArrayClassMult Array multiplication
Carlos Becker82e2e8b2010-06-28 18:42:09 +010093
Benoit Jacob08c17c42010-07-01 20:29:13 -040094First 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 +010095are fundamentally different from matrices, is when you multiply two together. Matrices interpret
Benoit Jacobbb8a25e2011-03-21 06:45:57 -040096multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two
97arrays can be multiplied if and only if they have the same dimensions.
Carlos Becker82e2e8b2010-06-28 18:42:09 +010098
Gael Guennebaudf66fe262010-10-19 11:40:49 +020099<table class="example">
100<tr><th>Example:</th><th>Output:</th></tr>
101<tr><td>
102\include Tutorial_ArrayClass_mult.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100103</td>
104<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200105\verbinclude Tutorial_ArrayClass_mult.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100106</td></tr></table>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100107
108
Jitse Niesen140ad092010-07-12 22:45:57 +0100109\section TutorialArrayClassCwiseOther Other coefficient-wise operations
110
Benoit Jacobbb8a25e2011-03-21 06:45:57 -0400111The Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication
112operators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute
Jitse Niesen140ad092010-07-12 22:45:57 +0100113value of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the
Gael Guennebaudf41d96d2012-12-24 13:33:22 +0100114coefficients. 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 +0100115construct the array whose coefficients are the minimum of the corresponding coefficients of the two given
116arrays. These operations are illustrated in the following example.
117
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200118<table class="example">
119<tr><th>Example:</th><th>Output:</th></tr>
120<tr><td>
121\include Tutorial_ArrayClass_cwise_other.cpp
Jitse Niesen140ad092010-07-12 22:45:57 +0100122</td>
123<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200124\verbinclude Tutorial_ArrayClass_cwise_other.out
Jitse Niesen140ad092010-07-12 22:45:57 +0100125</td></tr></table>
126
127More coefficient-wise operations can be found in the \ref QuickRefPage.
128
Carlos Becker9d440052010-06-25 20:16:12 -0400129
Benoit Jacob08c17c42010-07-01 20:29:13 -0400130\section TutorialArrayClassConvert Converting between array and matrix expressions
Carlos Becker9d440052010-06-25 20:16:12 -0400131
Jitse Niesen140ad092010-07-12 22:45:57 +0100132When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot
133apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic
134operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise
135operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both
136Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives
137access to all operations regardless of the choice of declaring objects as arrays or as matrices.
Benoit Jacob08c17c42010-07-01 20:29:13 -0400138
Jitse Niesen140ad092010-07-12 22:45:57 +0100139\link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that
140'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations
Benoit Jacob08c17c42010-07-01 20:29:13 -0400141can be applied easily. Conversely, \link ArrayBase array expressions \endlink
142have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
143this doesn't have any runtime cost (provided that you let your compiler optimize).
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100144Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
Benoit Jacob5a52f282010-07-01 20:52:40 -0400145can be used as rvalues and as lvalues.
Carlos Becker9d440052010-06-25 20:16:12 -0400146
Tim Holy4a95bad2011-06-19 14:39:19 -0500147Mixing 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 +0100148array directly; the operands of a \c + operator should either both be matrices or both be arrays. However,
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100149it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
Jitse Niesen140ad092010-07-12 22:45:57 +0100150\link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is
151allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix
152variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400153
Jitse Niesen140ad092010-07-12 22:45:57 +0100154The following example shows how to use array operations on a Matrix object by employing the
155\link MatrixBase::array() .array() \endlink method. For example, the statement
156<tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses
157* to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal
158because Eigen allows assigning array expressions to matrix variables).
Benoit Jacob08c17c42010-07-01 20:29:13 -0400159
Gael Guennebaudf41d96d2012-12-24 13:33:22 +0100160As a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct() const
161.cwiseProduct(.) \endlink method for matrices to compute the coefficient-wise product. This is also shown in
Jitse Niesen140ad092010-07-12 22:45:57 +0100162the example program.
Carlos Becker9d440052010-06-25 20:16:12 -0400163
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200164<table class="example">
165<tr><th>Example:</th><th>Output:</th></tr>
166<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100167\include Tutorial_ArrayClass_interop_matrix.cpp
168</td>
169<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100170\verbinclude Tutorial_ArrayClass_interop_matrix.out
171</td></tr></table>
172
Jitse Niesen140ad092010-07-12 22:45:57 +0100173Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt>
174computes their matrix product.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100175
Jitse Niesen140ad092010-07-12 22:45:57 +0100176Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every
177coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the
178expression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices
179\c m and \c n and then the matrix product of the result with \c m.
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100180
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200181<table class="example">
182<tr><th>Example:</th><th>Output:</th></tr>
183<tr><td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100184\include Tutorial_ArrayClass_interop.cpp
185</td>
186<td>
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100187\verbinclude Tutorial_ArrayClass_interop.out
188</td></tr></table>
189
Benoit Jacob08c17c42010-07-01 20:29:13 -0400190*/
191
Carlos Becker9d440052010-06-25 20:16:12 -0400192}