Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 3 | /** \page TutorialArrayClass Tutorial page 3 - The %Array class |
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 |
| 13 | - \ref TutorialArrayClassWhatIs |
| 14 | - \ref TutorialArrayClassTypes |
| 15 | - \ref TutorialArrayClassAccess |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 16 | - \ref TutorialArrayClassCoeffWiseOperations |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 17 | - \ref TutorialArrayHowToUse |
| 18 | - \ref TutorialArrayClassCoeffWiseOperators |
| 19 | |
| 20 | \section TutorialArrayClassWhatIs What is the Array class? |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 21 | The \link ArrayBase Array \endlink class provides general-purpose arrays, as opposed to the Matrix class which |
| 22 | is intended for doing linear algebra. Furthermore, the \link ArrayBase Array \endlink class provides an easy way to |
| 23 | perform coefficient-wise operations, which might not have a precise meaning in linear matrix algebra, |
| 24 | such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise. |
| 25 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 26 | |
| 27 | \subsection TutorialArrayClassTypes Array type and predefined types |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 28 | The \link ArrayBase Array \endlink class is actually a template that works in a similar way as the \b Matrix one: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 29 | |
| 30 | \code |
| 31 | |
| 32 | //declaring an Array instance |
| 33 | Array<type,numRows,numCols> a; |
| 34 | \endcode |
| 35 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 36 | Eigen provides a bunch of predefined types to make instantiation easier. These types follow the same |
| 37 | conventions as the ones available for the \b Matrix ones but with some slight differences, |
| 38 | as shown in the following table: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 39 | |
| 40 | FIXME: explain why these differences- |
| 41 | |
| 42 | <table class="tutorial_code" align="center"> |
| 43 | <tr><td align="center">\b Type</td><td align="center">\b Typedef</td></tr> |
| 44 | <tr><td> |
| 45 | \code Array<double,Dynamic,Dynamic> \endcode</td> |
| 46 | <td> |
| 47 | \code ArrayXXd \endcode</td></tr> |
| 48 | <tr><td> |
| 49 | \code Array<double,3,3> \endcode</td> |
| 50 | <td> |
| 51 | \code Array33d \endcode</td></tr> |
| 52 | <tr><td> |
| 53 | \code Array<float,Dynamic,Dynamic> \endcode</td> |
| 54 | <td> |
| 55 | \code ArrayXXf \endcode</td></tr> |
| 56 | <tr><td> |
| 57 | \code Array<float,3,3> \endcode</td> |
| 58 | <td> |
| 59 | \code Array33f \endcode</td></tr> |
| 60 | </table> |
| 61 | |
| 62 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 63 | \subsection TutorialArrayClassAccess Accessing values inside an Array |
| 64 | Write and read-access to coefficients inside \link ArrayBase Array \endlink is done in the same way as with \b Matrix. |
| 65 | Here some examples are presented, just for clarification: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 66 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 67 | \include Tutorial_ArrayClass_accessing.cpp |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 68 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 69 | Output: |
| 70 | \verbinclude Tutorial_ArrayClass_accessing.out |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 71 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | |
| 75 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 76 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | \section TutorialArrayClassCoeffWiseOperations Coefficient-wise operators |
| 82 | |
| 83 | |
| 84 | |
| 85 | \subsection TutorialArrayClassCoeffWiseAdd Coefficient-wise addition |
| 86 | Adding two \link ArrayBase Array \endlink objects has the same result as adding to Matrices, performing coefficient-wise addition. This is valid as long as both arrays have the same dimensions: |
| 87 | |
| 88 | \include Tutorial_ArrayClass_addition.cpp |
| 89 | |
| 90 | Output: |
| 91 | \verbinclude Tutorial_ArrayClass_addition.out |
| 92 | |
| 93 | The addition operator can also be used to add a scalar to each coefficient in an Array, providing a functionality that was not available for Matrix objects: |
| 94 | |
| 95 | \include Tutorial_ArrayClass_addition_scalar.cpp |
| 96 | |
| 97 | Output: |
| 98 | \verbinclude Tutorial_ArrayClass_addition_scalar.out |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | \subsection TutorialArrayClassCoeffWiseMult Coefficient-wise multiplication |
| 104 | |
| 105 | As said before, the \link ArrayBase Array \endlink class looks at operators from a coefficient-wise perspective. |
| 106 | This makes an important difference with respect to \b Matrix algebraic operations, especially |
| 107 | with the product operator. The following example performs coefficient-wise multiplication between two array instances: |
| 108 | |
| 109 | \include Tutorial_ArrayClass_mult.cpp |
| 110 | |
| 111 | Output: |
| 112 | \verbinclude Tutorial_ArrayClass_mult.out |
| 113 | |
| 114 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 115 | |
| 116 | \section TutorialArrayHowToUse Using arrays and matrices |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 117 | It is possible to treat the data inside a \b Matrix object as an \link ArrayBase Array \endlink |
| 118 | and vice-versa. This allows the developer to perform a wide diversity of operators regardless |
| 119 | of the actual type where the coefficients rely on. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 120 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 121 | The \b Matrix class provides a \link MatrixBase::array() .array() \endlink method that |
| 122 | 'converts' it into an \link ArrayBase Array \endlink type, so that coefficient-wise operations |
| 123 | can be applied easily. On the other side, the \link ArrayBase Array \endlink |
| 124 | class provides a \link ArrayBase::matrix() .matrix() \endlink method. FIXME: note on overhead? |
| 125 | Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink |
| 126 | can be used as \b rvalues or \b lvalues in expressions. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 127 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 128 | <b>IMPORTANT NOTE:</b> Mixing matrices and arrays in an expression is forbidden with Eigen. However, |
| 129 | it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and |
| 130 | \link ArrayBase::matrix() .matrix() \endlink. Conversely, assigning a Matrix to an |
| 131 | \link ArrayBase Array \endlink or vice-versa is allowed. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 132 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 133 | \subsection TutorialArrayInteropMatrix Matrix to array example |
| 134 | The following example shows how to use Array operations with a Matrix object by employing |
| 135 | the \link MatrixBase::array() .array() \endlink function: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 136 | |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 137 | <table class="tutorial_code"><tr><td> |
| 138 | \include Tutorial_ArrayClass_interop_matrix.cpp |
| 139 | </td> |
| 140 | <td> |
| 141 | Output: |
| 142 | \verbinclude Tutorial_ArrayClass_interop_matrix.out |
| 143 | </td></tr></table> |
| 144 | |
| 145 | |
| 146 | \subsection TutorialArrayInteropArray Array to matrix example |
| 147 | The following example shows how to use Matrix operations with an \link ArrayBase Array \endlink |
| 148 | object by employing the \link ArrayBase::matrix() .matrix() \endlink function: |
| 149 | |
| 150 | <table class="tutorial_code"><tr><td> |
| 151 | \include Tutorial_ArrayClass_interop_array.cpp |
| 152 | </td> |
| 153 | <td> |
| 154 | Output: |
| 155 | \verbinclude Tutorial_ArrayClass_interop_array.out |
| 156 | </td></tr></table> |
| 157 | |
| 158 | \subsection TutorialArrayInteropCombination Example with combinations of .array() and .matrix() |
| 159 | Here there is a more advanced example: |
| 160 | |
| 161 | <table class="tutorial_code"><tr><td> |
| 162 | \include Tutorial_ArrayClass_interop.cpp |
| 163 | </td> |
| 164 | <td> |
| 165 | Output: |
| 166 | \verbinclude Tutorial_ArrayClass_interop.out |
| 167 | </td></tr></table> |
| 168 | |
| 169 | |
| 170 | \b NOTE: there is no need to call \link ArrayBase::matrix() .matrix() \endlink to assign a |
| 171 | \link ArrayBase Array \endlink type to a \b Matrix or vice-versa. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 172 | |
| 173 | \section TutorialArrayClassCoeffWiseOperators Array coefficient-wise operators |
Carlos Becker | 82e2e8b | 2010-06-28 18:42:09 +0100 | [diff] [blame] | 174 | |
| 175 | FIXME: move to reference table? (I think it is already there) |
| 176 | |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 177 | <table class="noborder"> |
| 178 | <tr><td> |
| 179 | <table class="tutorial_code" style="margin-right:10pt"> |
| 180 | <tr><td>Coefficient wise \link ArrayBase::operator*() product \arrayworld \endlink</td> |
| 181 | <td>\code array3 = array1 * array2; \endcode |
| 182 | </td></tr> |
| 183 | <tr><td> |
| 184 | Add a scalar to all coefficients</td><td>\code |
| 185 | array3 = array1 + scalar; |
| 186 | array3 += scalar; |
| 187 | array3 -= scalar; |
| 188 | \endcode |
| 189 | </td></tr> |
| 190 | <tr><td> |
| 191 | Coefficient wise \link ArrayBase::operator/() division \endlink \arrayworld</td><td>\code |
| 192 | array3 = array1 / array2; \endcode |
| 193 | </td></tr> |
| 194 | <tr><td> |
| 195 | Coefficient wise \link ArrayBase::inverse() reciprocal \endlink \arrayworld</td><td>\code |
| 196 | array3 = array1.inverse(); \endcode |
| 197 | </td></tr> |
| 198 | <tr><td> |
| 199 | Coefficient wise comparisons \arrayworld \n |
| 200 | (support all operators)</td><td>\code |
| 201 | array3 = array1 < array2; |
| 202 | array3 = array1 <= array2; |
| 203 | array3 = array1 > array2; |
| 204 | etc. |
| 205 | \endcode |
| 206 | </td></tr></table> |
| 207 | </td> |
| 208 | <td><table class="tutorial_code"> |
| 209 | <tr><td> |
| 210 | \b Trigo \arrayworld: \n |
| 211 | \link ArrayBase::sin sin \endlink, \link ArrayBase::cos cos \endlink</td><td>\code |
| 212 | array3 = array1.sin(); |
| 213 | etc. |
| 214 | \endcode |
| 215 | </td></tr> |
| 216 | <tr><td> |
| 217 | \b Power \arrayworld: \n \link ArrayBase::pow() pow \endlink, |
| 218 | \link ArrayBase::square square \endlink, |
| 219 | \link ArrayBase::cube cube \endlink, \n |
| 220 | \link ArrayBase::sqrt sqrt \endlink, |
| 221 | \link ArrayBase::exp exp \endlink, |
| 222 | \link ArrayBase::log log \endlink </td><td>\code |
| 223 | array3 = array1.square(); |
| 224 | array3 = array1.pow(5); |
| 225 | array3 = array1.log(); |
| 226 | etc. |
| 227 | \endcode |
| 228 | </td></tr> |
| 229 | <tr><td> |
| 230 | \link ArrayBase::min min \endlink, \link ArrayBase::max max \endlink, \n |
| 231 | absolute value (\link ArrayBase::abs() abs \endlink, \link ArrayBase::abs2() abs2 \endlink \arrayworld) |
| 232 | </td><td>\code |
| 233 | array3 = array1.min(array2); |
| 234 | array3 = array1.max(array2); |
| 235 | array3 = array1.abs(); |
| 236 | array3 = array1.abs2(); |
| 237 | \endcode</td></tr> |
| 238 | </table> |
| 239 | </td></tr></table> |
| 240 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 241 | \li \b Next: \ref TutorialBlockOperations |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 242 | |
| 243 | **/ |
| 244 | } |