Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 3 | /** \page TutorialArrayClass Tutorial page 3 - The %Array class and coefficient-wise operations |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 4 | \ingroup Tutorial |
| 5 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 6 | \li \b Previous: \ref TutorialMatrixArithmetic |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 7 | \li \b Next: \ref TutorialBlockOperations |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 8 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 9 | This tutorial aims to provide an overview and explanations on how to use |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 10 | Eigen's Array class. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 11 | |
| 12 | \b Table \b of \b contents |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 13 | - \ref TutorialArrayClassIntro |
| 14 | - \ref TutorialArrayClassTypes |
| 15 | - \ref TutorialArrayClassAccess |
| 16 | - \ref TutorialArrayClassAddSub |
| 17 | - \ref TutorialArrayClassMult |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 18 | - \ref TutorialArrayClassCwiseOther |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 19 | - \ref TutorialArrayClassConvert |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 20 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 21 | \section TutorialArrayClassIntro What is the Array class? |
| 22 | |
| 23 | The Array class provides general-purpose arrays, as opposed to the Matrix class which |
| 24 | is intended for linear algebra. Furthermore, the Array class provides an easy way to |
| 25 | perform coefficient-wise operations, which might not have a linear algebraic meaning, |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 26 | such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise. |
| 27 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 28 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 29 | \section TutorialArrayClassTypes Array types |
| 30 | Array is a class template taking the same template parameters as Matrix. |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 31 | As with Matrix, the first three template parameters are mandatory: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 32 | \code |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 33 | Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 34 | \endcode |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 35 | The last three template parameters are optional. Since this is exactly the same as for Matrix, |
| 36 | we won't explain it again here and just refer to \ref TutorialMatrixClass. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 37 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 38 | Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs |
| 39 | but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays. |
Benoit Jacob | bb8a25e | 2011-03-21 06:45:57 -0400 | [diff] [blame] | 40 | We adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 41 | the size and the scalar type, as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 42 | use typedefs of the form ArrayNNt. Some examples are shown in the following table: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 43 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 44 | <table class="manual"> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 45 | <tr> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 46 | <th>Type </th> |
| 47 | <th>Typedef </th> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 48 | </tr> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 49 | <tr> |
| 50 | <td> \code Array<float,Dynamic,1> \endcode </td> |
| 51 | <td> \code ArrayXf \endcode </td> |
| 52 | </tr> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 53 | <tr> |
| 54 | <td> \code Array<float,3,1> \endcode </td> |
| 55 | <td> \code Array3f \endcode </td> |
| 56 | </tr> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 57 | <tr> |
| 58 | <td> \code Array<double,Dynamic,Dynamic> \endcode </td> |
| 59 | <td> \code ArrayXXd \endcode </td> |
| 60 | </tr> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 61 | <tr> |
| 62 | <td> \code Array<double,3,3> \endcode </td> |
| 63 | <td> \code Array33d \endcode </td> |
| 64 | </tr> |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 65 | </table> |
| 66 | |
| 67 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 68 | \section TutorialArrayClassAccess Accessing values inside an Array |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 69 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 70 | The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices. |
| 71 | Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 72 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 73 | <table class="example"> |
| 74 | <tr><th>Example:</th><th>Output:</th></tr> |
| 75 | <tr><td> |
| 76 | \include Tutorial_ArrayClass_accessors.cpp |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 77 | </td> |
| 78 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 79 | \verbinclude Tutorial_ArrayClass_accessors.out |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 80 | </td></tr></table> |
| 81 | |
| 82 | For more information about the comma initializer, see \ref TutorialAdvancedInitialization. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 83 | |
| 84 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 85 | \section TutorialArrayClassAddSub Addition and subtraction |
| 86 | |
| 87 | Adding and subtracting two arrays is the same as for matrices. |
| 88 | The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise. |
| 89 | |
| 90 | Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array. |
| 91 | This provides a functionality that is not directly available for Matrix objects. |
| 92 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 93 | <table class="example"> |
| 94 | <tr><th>Example:</th><th>Output:</th></tr> |
| 95 | <tr><td> |
| 96 | \include Tutorial_ArrayClass_addition.cpp |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 97 | </td> |
| 98 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 99 | \verbinclude Tutorial_ArrayClass_addition.out |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 100 | </td></tr></table> |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 101 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 102 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 103 | \section TutorialArrayClassMult Array multiplication |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 104 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 105 | First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 106 | are fundamentally different from matrices, is when you multiply two together. Matrices interpret |
Benoit Jacob | bb8a25e | 2011-03-21 06:45:57 -0400 | [diff] [blame] | 107 | multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two |
| 108 | arrays can be multiplied if and only if they have the same dimensions. |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 109 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 110 | <table class="example"> |
| 111 | <tr><th>Example:</th><th>Output:</th></tr> |
| 112 | <tr><td> |
| 113 | \include Tutorial_ArrayClass_mult.cpp |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 114 | </td> |
| 115 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 116 | \verbinclude Tutorial_ArrayClass_mult.out |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 117 | </td></tr></table> |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 118 | |
| 119 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 120 | \section TutorialArrayClassCwiseOther Other coefficient-wise operations |
| 121 | |
Benoit Jacob | bb8a25e | 2011-03-21 06:45:57 -0400 | [diff] [blame] | 122 | The Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication |
| 123 | operators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 124 | value of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the |
| 125 | coefficients. If you have two arrays of the same size, you can call \link ArrayBase::min() .min() \endlink to |
| 126 | construct the array whose coefficients are the minimum of the corresponding coefficients of the two given |
| 127 | arrays. These operations are illustrated in the following example. |
| 128 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 129 | <table class="example"> |
| 130 | <tr><th>Example:</th><th>Output:</th></tr> |
| 131 | <tr><td> |
| 132 | \include Tutorial_ArrayClass_cwise_other.cpp |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 133 | </td> |
| 134 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 135 | \verbinclude Tutorial_ArrayClass_cwise_other.out |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 136 | </td></tr></table> |
| 137 | |
| 138 | More coefficient-wise operations can be found in the \ref QuickRefPage. |
| 139 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 140 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 141 | \section TutorialArrayClassConvert Converting between array and matrix expressions |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 142 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 143 | When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot |
| 144 | apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic |
| 145 | operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise |
| 146 | operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both |
| 147 | Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives |
| 148 | access to all operations regardless of the choice of declaring objects as arrays or as matrices. |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 149 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 150 | \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 Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 152 | can be applied easily. Conversely, \link ArrayBase array expressions \endlink |
| 153 | have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions, |
| 154 | this doesn't have any runtime cost (provided that you let your compiler optimize). |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 155 | Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink |
Benoit Jacob | 5a52f28 | 2010-07-01 20:52:40 -0400 | [diff] [blame] | 156 | can be used as rvalues and as lvalues. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 157 | |
Tim Holy | 4a95bad | 2011-06-19 14:39:19 -0500 | [diff] [blame] | 158 | Mixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a matrix and |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 159 | array directly; the operands of a \c + operator should either both be matrices or both be arrays. However, |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 160 | it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 161 | \link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is |
| 162 | allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix |
| 163 | variable. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 164 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 165 | The 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 |
| 169 | because Eigen allows assigning array expressions to matrix variables). |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 170 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 171 | As 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 |
| 173 | the example program. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 174 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 175 | <table class="example"> |
| 176 | <tr><th>Example:</th><th>Output:</th></tr> |
| 177 | <tr><td> |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 178 | \include Tutorial_ArrayClass_interop_matrix.cpp |
| 179 | </td> |
| 180 | <td> |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 181 | \verbinclude Tutorial_ArrayClass_interop_matrix.out |
| 182 | </td></tr></table> |
| 183 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 184 | Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt> |
| 185 | computes their matrix product. |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 186 | |
Jitse Niesen | 140ad09 | 2010-07-12 22:45:57 +0100 | [diff] [blame] | 187 | Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every |
| 188 | coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the |
| 189 | expression <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 Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 191 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 192 | <table class="example"> |
| 193 | <tr><th>Example:</th><th>Output:</th></tr> |
| 194 | <tr><td> |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 195 | \include Tutorial_ArrayClass_interop.cpp |
| 196 | </td> |
| 197 | <td> |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 198 | \verbinclude Tutorial_ArrayClass_interop.out |
| 199 | </td></tr></table> |
| 200 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 201 | \li \b Next: \ref TutorialBlockOperations |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 202 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame] | 203 | */ |
| 204 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 205 | } |