1*bf2c3715SXin Linamespace Eigen { 2*bf2c3715SXin Li 3*bf2c3715SXin Li/** \eigenManualPage TutorialArrayClass The Array class and coefficient-wise operations 4*bf2c3715SXin Li 5*bf2c3715SXin LiThis page aims to provide an overview and explanations on how to use 6*bf2c3715SXin LiEigen's Array class. 7*bf2c3715SXin Li 8*bf2c3715SXin Li\eigenAutoToc 9*bf2c3715SXin Li 10*bf2c3715SXin Li\section TutorialArrayClassIntro What is the Array class? 11*bf2c3715SXin Li 12*bf2c3715SXin LiThe Array class provides general-purpose arrays, as opposed to the Matrix class which 13*bf2c3715SXin Liis intended for linear algebra. Furthermore, the Array class provides an easy way to 14*bf2c3715SXin Liperform coefficient-wise operations, which might not have a linear algebraic meaning, 15*bf2c3715SXin Lisuch as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise. 16*bf2c3715SXin Li 17*bf2c3715SXin Li 18*bf2c3715SXin Li\section TutorialArrayClassTypes Array types 19*bf2c3715SXin LiArray is a class template taking the same template parameters as Matrix. 20*bf2c3715SXin LiAs with Matrix, the first three template parameters are mandatory: 21*bf2c3715SXin Li\code 22*bf2c3715SXin LiArray<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> 23*bf2c3715SXin Li\endcode 24*bf2c3715SXin LiThe last three template parameters are optional. Since this is exactly the same as for Matrix, 25*bf2c3715SXin Liwe won't explain it again here and just refer to \ref TutorialMatrixClass. 26*bf2c3715SXin Li 27*bf2c3715SXin LiEigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs 28*bf2c3715SXin Libut with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays. 29*bf2c3715SXin LiWe adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are 30*bf2c3715SXin Lithe size and the scalar type, as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we 31*bf2c3715SXin Liuse typedefs of the form ArrayNNt. Some examples are shown in the following table: 32*bf2c3715SXin Li 33*bf2c3715SXin Li<table class="manual"> 34*bf2c3715SXin Li <tr> 35*bf2c3715SXin Li <th>Type </th> 36*bf2c3715SXin Li <th>Typedef </th> 37*bf2c3715SXin Li </tr> 38*bf2c3715SXin Li <tr> 39*bf2c3715SXin Li <td> \code Array<float,Dynamic,1> \endcode </td> 40*bf2c3715SXin Li <td> \code ArrayXf \endcode </td> 41*bf2c3715SXin Li </tr> 42*bf2c3715SXin Li <tr> 43*bf2c3715SXin Li <td> \code Array<float,3,1> \endcode </td> 44*bf2c3715SXin Li <td> \code Array3f \endcode </td> 45*bf2c3715SXin Li </tr> 46*bf2c3715SXin Li <tr> 47*bf2c3715SXin Li <td> \code Array<double,Dynamic,Dynamic> \endcode </td> 48*bf2c3715SXin Li <td> \code ArrayXXd \endcode </td> 49*bf2c3715SXin Li </tr> 50*bf2c3715SXin Li <tr> 51*bf2c3715SXin Li <td> \code Array<double,3,3> \endcode </td> 52*bf2c3715SXin Li <td> \code Array33d \endcode </td> 53*bf2c3715SXin Li </tr> 54*bf2c3715SXin Li</table> 55*bf2c3715SXin Li 56*bf2c3715SXin Li 57*bf2c3715SXin Li\section TutorialArrayClassAccess Accessing values inside an Array 58*bf2c3715SXin Li 59*bf2c3715SXin LiThe parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices. 60*bf2c3715SXin LiFurthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them. 61*bf2c3715SXin Li 62*bf2c3715SXin Li<table class="example"> 63*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 64*bf2c3715SXin Li<tr><td> 65*bf2c3715SXin Li\include Tutorial_ArrayClass_accessors.cpp 66*bf2c3715SXin Li</td> 67*bf2c3715SXin Li<td> 68*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_accessors.out 69*bf2c3715SXin Li</td></tr></table> 70*bf2c3715SXin Li 71*bf2c3715SXin LiFor more information about the comma initializer, see \ref TutorialAdvancedInitialization. 72*bf2c3715SXin Li 73*bf2c3715SXin Li 74*bf2c3715SXin Li\section TutorialArrayClassAddSub Addition and subtraction 75*bf2c3715SXin Li 76*bf2c3715SXin LiAdding and subtracting two arrays is the same as for matrices. 77*bf2c3715SXin LiThe operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise. 78*bf2c3715SXin Li 79*bf2c3715SXin LiArrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array. 80*bf2c3715SXin LiThis provides a functionality that is not directly available for Matrix objects. 81*bf2c3715SXin Li 82*bf2c3715SXin Li<table class="example"> 83*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 84*bf2c3715SXin Li<tr><td> 85*bf2c3715SXin Li\include Tutorial_ArrayClass_addition.cpp 86*bf2c3715SXin Li</td> 87*bf2c3715SXin Li<td> 88*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_addition.out 89*bf2c3715SXin Li</td></tr></table> 90*bf2c3715SXin Li 91*bf2c3715SXin Li 92*bf2c3715SXin Li\section TutorialArrayClassMult Array multiplication 93*bf2c3715SXin Li 94*bf2c3715SXin LiFirst of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays 95*bf2c3715SXin Liare fundamentally different from matrices, is when you multiply two together. Matrices interpret 96*bf2c3715SXin Limultiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two 97*bf2c3715SXin Liarrays can be multiplied if and only if they have the same dimensions. 98*bf2c3715SXin Li 99*bf2c3715SXin Li<table class="example"> 100*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 101*bf2c3715SXin Li<tr><td> 102*bf2c3715SXin Li\include Tutorial_ArrayClass_mult.cpp 103*bf2c3715SXin Li</td> 104*bf2c3715SXin Li<td> 105*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_mult.out 106*bf2c3715SXin Li</td></tr></table> 107*bf2c3715SXin Li 108*bf2c3715SXin Li 109*bf2c3715SXin Li\section TutorialArrayClassCwiseOther Other coefficient-wise operations 110*bf2c3715SXin Li 111*bf2c3715SXin LiThe Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication 112*bf2c3715SXin Lioperators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute 113*bf2c3715SXin Livalue of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the 114*bf2c3715SXin Licoefficients. If you have two arrays of the same size, you can call \link ArrayBase::min(const Eigen::ArrayBase<OtherDerived>&) const .min(.) \endlink to 115*bf2c3715SXin Liconstruct the array whose coefficients are the minimum of the corresponding coefficients of the two given 116*bf2c3715SXin Liarrays. These operations are illustrated in the following example. 117*bf2c3715SXin Li 118*bf2c3715SXin Li<table class="example"> 119*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 120*bf2c3715SXin Li<tr><td> 121*bf2c3715SXin Li\include Tutorial_ArrayClass_cwise_other.cpp 122*bf2c3715SXin Li</td> 123*bf2c3715SXin Li<td> 124*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_cwise_other.out 125*bf2c3715SXin Li</td></tr></table> 126*bf2c3715SXin Li 127*bf2c3715SXin LiMore coefficient-wise operations can be found in the \ref QuickRefPage. 128*bf2c3715SXin Li 129*bf2c3715SXin Li 130*bf2c3715SXin Li\section TutorialArrayClassConvert Converting between array and matrix expressions 131*bf2c3715SXin Li 132*bf2c3715SXin LiWhen should you use objects of the Matrix class and when should you use objects of the Array class? You cannot 133*bf2c3715SXin Liapply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic 134*bf2c3715SXin Lioperations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise 135*bf2c3715SXin Lioperations, then you should use arrays. However, sometimes it is not that simple, but you need to use both 136*bf2c3715SXin LiMatrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives 137*bf2c3715SXin Liaccess to all operations regardless of the choice of declaring objects as arrays or as matrices. 138*bf2c3715SXin Li 139*bf2c3715SXin Li\link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that 140*bf2c3715SXin Li'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations 141*bf2c3715SXin Lican be applied easily. Conversely, \link ArrayBase array expressions \endlink 142*bf2c3715SXin Lihave a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions, 143*bf2c3715SXin Lithis doesn't have any runtime cost (provided that you let your compiler optimize). 144*bf2c3715SXin LiBoth \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink 145*bf2c3715SXin Lican be used as rvalues and as lvalues. 146*bf2c3715SXin Li 147*bf2c3715SXin LiMixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a matrix and 148*bf2c3715SXin Liarray directly; the operands of a \c + operator should either both be matrices or both be arrays. However, 149*bf2c3715SXin Liit is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and 150*bf2c3715SXin Li\link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is 151*bf2c3715SXin Liallowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix 152*bf2c3715SXin Livariable. 153*bf2c3715SXin Li 154*bf2c3715SXin LiThe following example shows how to use array operations on a Matrix object by employing the 155*bf2c3715SXin Li\link MatrixBase::array() .array() \endlink method. For example, the statement 156*bf2c3715SXin Li<tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses 157*bf2c3715SXin Li* to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal 158*bf2c3715SXin Libecause Eigen allows assigning array expressions to matrix variables). 159*bf2c3715SXin Li 160*bf2c3715SXin LiAs a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct const 161*bf2c3715SXin Li.cwiseProduct(.) \endlink method for matrices to compute the coefficient-wise product. This is also shown in 162*bf2c3715SXin Lithe example program. 163*bf2c3715SXin Li 164*bf2c3715SXin Li<table class="example"> 165*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 166*bf2c3715SXin Li<tr><td> 167*bf2c3715SXin Li\include Tutorial_ArrayClass_interop_matrix.cpp 168*bf2c3715SXin Li</td> 169*bf2c3715SXin Li<td> 170*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_interop_matrix.out 171*bf2c3715SXin Li</td></tr></table> 172*bf2c3715SXin Li 173*bf2c3715SXin LiSimilarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt> 174*bf2c3715SXin Licomputes their matrix product. 175*bf2c3715SXin Li 176*bf2c3715SXin LiHere is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every 177*bf2c3715SXin Licoefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the 178*bf2c3715SXin Liexpression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices 179*bf2c3715SXin Li\c m and \c n and then the matrix product of the result with \c m. 180*bf2c3715SXin Li 181*bf2c3715SXin Li<table class="example"> 182*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 183*bf2c3715SXin Li<tr><td> 184*bf2c3715SXin Li\include Tutorial_ArrayClass_interop.cpp 185*bf2c3715SXin Li</td> 186*bf2c3715SXin Li<td> 187*bf2c3715SXin Li\verbinclude Tutorial_ArrayClass_interop.out 188*bf2c3715SXin Li</td></tr></table> 189*bf2c3715SXin Li 190*bf2c3715SXin Li*/ 191*bf2c3715SXin Li 192*bf2c3715SXin Li} 193