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 |
| 10 | Eigen's \link ArrayBase Array \endlink 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 |
| 18 | - \ref TutorialArrayClassConvert |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 19 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 20 | \section TutorialArrayClassIntro What is the Array class? |
| 21 | |
| 22 | The Array class provides general-purpose arrays, as opposed to the Matrix class which |
| 23 | is intended for linear algebra. Furthermore, the Array class provides an easy way to |
| 24 | perform coefficient-wise operations, which might not have a linear algebraic meaning, |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 25 | such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise. |
| 26 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 27 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 28 | \section TutorialArrayClassTypes Array types |
| 29 | Array is a class template taking the same template parameters as Matrix. |
| 30 | As with with, the first 3 template parameters are mandatory: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 31 | \code |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 32 | Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 33 | \endcode |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 34 | And the last 3 template parameters are optional. Since this is exactly the same as for Matrix, |
| 35 | we won't reexplain it and just refer to \ref TutorialMatrixClass "this page". |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 36 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 37 | Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs |
| 38 | but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays. |
| 39 | We adopt that convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are |
| 40 | as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we |
| 41 | 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] | 42 | |
| 43 | <table class="tutorial_code" align="center"> |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 44 | |
| 45 | <tr> |
| 46 | <td align="center">\b Type </td> |
| 47 | <td align="center">\b Typedef </td> |
| 48 | </tr> |
| 49 | |
| 50 | <tr> |
| 51 | <td> \code Array<float,Dynamic,1> \endcode </td> |
| 52 | <td> \code ArrayXf \endcode </td> |
| 53 | </tr> |
| 54 | |
| 55 | <tr> |
| 56 | <td> \code Array<float,3,1> \endcode </td> |
| 57 | <td> \code Array3f \endcode </td> |
| 58 | </tr> |
| 59 | |
| 60 | <tr> |
| 61 | <td> \code Array<double,Dynamic,Dynamic> \endcode </td> |
| 62 | <td> \code ArrayXXd \endcode </td> |
| 63 | </tr> |
| 64 | |
| 65 | <tr> |
| 66 | <td> \code Array<double,3,3> \endcode </td> |
| 67 | <td> \code Array33d \endcode </td> |
| 68 | </tr> |
| 69 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 70 | </table> |
| 71 | |
| 72 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 73 | \section TutorialArrayClassAccess Accessing values inside an Array |
| 74 | Write and read access to coefficients of an array expression is done in the same way as with matrix expressions. |
| 75 | For example: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 76 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 77 | \include Tutorial_ArrayClass_accessors.cpp |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 78 | Output: |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 79 | \verbinclude Tutorial_ArrayClass_accessors.out |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 80 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 81 | For more information about the comma initializer, refer to \ref TutorialAdvancedInitialization "this page". |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 82 | |
| 83 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 84 | \section TutorialArrayClassAddSub Addition and substraction |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 85 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 86 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 87 | Adding and substracting two arrays has the same result as for Matrices. |
| 88 | This is valid as long as both arrays have the same sizes: |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 89 | |
| 90 | \include Tutorial_ArrayClass_addition.cpp |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 91 | Output: |
| 92 | \verbinclude Tutorial_ArrayClass_addition.out |
| 93 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 94 | It is also allowed to add a scalar to each coefficient in an Array, |
| 95 | providing a functionality that was not available for Matrix objects: |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 96 | |
| 97 | \include Tutorial_ArrayClass_addition_scalar.cpp |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 98 | Output: |
| 99 | \verbinclude Tutorial_ArrayClass_addition_scalar.out |
| 100 | |
| 101 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 102 | \subsection TutorialArrayClassMult Array multiplication |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 103 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 104 | First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays |
| 105 | are fundamentally different from matrices, is when you multiply two arrays together. While Matrices interprete |
| 106 | multiplication as matrix product, arrays interprete multiplication as coefficient-wise product. For example: |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 107 | |
| 108 | \include Tutorial_ArrayClass_mult.cpp |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 109 | Output: |
| 110 | \verbinclude Tutorial_ArrayClass_mult.out |
| 111 | |
| 112 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 113 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 114 | \section TutorialArrayClassConvert Converting between array and matrix expressions |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 115 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 116 | It is possible to wrap a matrix expression as an array expression and conversely. This gives access to all operations |
| 117 | regardless of the choice of declaring objects as arrays or as matrices. |
| 118 | |
| 119 | \link MatrixBase Matrix expressions \endlink have a \link MatrixBase::array() .array() \endlink method that |
| 120 | 'converts' them into \link ArrayBase array expressions \endlink, so that coefficient-wise operations |
| 121 | can be applied easily. Conversely, \link ArrayBase array expressions \endlink |
| 122 | have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions, |
| 123 | this doesn't have any runtime cost (provided that you let your compiler optimize). |
| 124 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 125 | Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 126 | can be used as \b rvalues and as \b lvalues. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 127 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 128 | Mixing matrices and arrays in an expression is forbidden with Eigen. However, |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 129 | it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 130 | \link ArrayBase::matrix() .matrix() \endlink. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 131 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 132 | On the other hand, assigning a matrix expression to an array expression is allowed. |
| 133 | |
| 134 | \subsection TutorialArrayClassInteropMatrix Matrix to array example |
| 135 | The following example shows how to use array operations on a Matrix object by employing |
| 136 | the \link MatrixBase::array() .array() \endlink method: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 137 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 138 | <table class="tutorial_code"><tr><td> |
| 139 | \include Tutorial_ArrayClass_interop_matrix.cpp |
| 140 | </td> |
| 141 | <td> |
| 142 | Output: |
| 143 | \verbinclude Tutorial_ArrayClass_interop_matrix.out |
| 144 | </td></tr></table> |
| 145 | |
| 146 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 147 | \subsection TutorialArrayClassInteropArray Array to matrix example |
| 148 | The following example shows how to use matrix operations with an Array |
| 149 | object by employing the \link ArrayBase::matrix() .matrix() \endlink method: |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 150 | |
| 151 | <table class="tutorial_code"><tr><td> |
| 152 | \include Tutorial_ArrayClass_interop_array.cpp |
| 153 | </td> |
| 154 | <td> |
| 155 | Output: |
| 156 | \verbinclude Tutorial_ArrayClass_interop_array.out |
| 157 | </td></tr></table> |
| 158 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 159 | \subsection TutorialArrayClassInteropCombination Example with combinations of .array() and .matrix() |
| 160 | Here is a more advanced example: |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 161 | |
| 162 | <table class="tutorial_code"><tr><td> |
| 163 | \include Tutorial_ArrayClass_interop.cpp |
| 164 | </td> |
| 165 | <td> |
| 166 | Output: |
| 167 | \verbinclude Tutorial_ArrayClass_interop.out |
| 168 | </td></tr></table> |
| 169 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 170 | \li \b Next: \ref TutorialBlockOperations |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 171 | |
Benoit Jacob | 08c17c4 | 2010-07-01 20:29:13 -0400 | [diff] [blame^] | 172 | */ |
| 173 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 174 | } |