Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 3 | /** \page TutorialMatrixArithmetic Tutorial page 2 - %Matrix and vector arithmetic |
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 TutorialMatrixClass |
| 7 | \li \b Next: \ref TutorialArrayClass |
| 8 | |
| 9 | This tutorial aims to provide an overview and some details on how to perform arithmetic |
| 10 | between matrices, vectors and scalars with Eigen. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 11 | |
| 12 | \b Table \b of \b contents |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 13 | - \ref TutorialArithmeticIntroduction |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 14 | - \ref TutorialArithmeticAddSub |
| 15 | - \ref TutorialArithmeticScalarMulDiv |
| 16 | - \ref TutorialArithmeticMentionXprTemplates |
Gael Guennebaud | de1220a | 2010-06-28 00:05:11 +0200 | [diff] [blame] | 17 | - \ref TutorialArithmeticTranspose |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 18 | - \ref TutorialArithmeticMatrixMul |
| 19 | - \ref TutorialArithmeticDotAndCross |
| 20 | - \ref TutorialArithmeticRedux |
| 21 | - \ref TutorialArithmeticValidity |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 22 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 23 | \section TutorialArithmeticIntroduction Introduction |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 24 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 25 | Eigen offers matrix/vector arithmetic operations either through overloads of common C++ arithmetic operators such as +, -, *, |
| 26 | or through special methods such as dot(), cross(), etc. |
| 27 | For the Matrix class (matrices and vectors), operators are only overloaded to support |
| 28 | linear-algebraic operations. For example, \c matrix1 \c * \c matrix2 means matrix-matrix product, |
| 29 | and \c vector \c + \c scalar is just not allowed. If you want to perform all kinds of array operations, |
| 30 | not linear algebra, see \ref TutorialArrayClass "next page". |
| 31 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 32 | \section TutorialArithmeticAddSub Addition and substraction |
| 33 | |
| 34 | The left hand side and right hand side must, of course, have the same numbers of rows and of columns. They must |
| 35 | also have the same \c Scalar type, as Eigen doesn't do automatic type promotion. The operators at hand here are: |
| 36 | \li binary operator + as in \c a+b |
| 37 | \li binary operator - as in \c a-b |
| 38 | \li unary operator - as in \c -a |
| 39 | \li compound operator += as in \c a+=b |
| 40 | \li compound operator -= as in \c a-=b |
| 41 | |
| 42 | Example: \include tut_arithmetic_add_sub.cpp |
| 43 | Output: \verbinclude tut_arithmetic_add_sub.out |
| 44 | |
| 45 | \section TutorialArithmeticScalarMulDiv Scalar multiplication and division |
| 46 | |
| 47 | Multiplication and division by a scalar is very simple too. The operators at hand here are: |
| 48 | \li binary operator * as in \c matrix*scalar |
| 49 | \li binary operator * as in \c scalar*matrix |
| 50 | \li binary operator / as in \c matrix/scalar |
| 51 | \li compound operator *= as in \c matrix*=scalar |
| 52 | \li compound operator /= as in \c matrix/=scalar |
| 53 | |
| 54 | Example: \include tut_arithmetic_scalar_mul_div.cpp |
| 55 | Output: \verbinclude tut_arithmetic_scalar_mul_div.out |
| 56 | |
| 57 | \section TutorialArithmeticMentionXprTemplates A note about expression templates |
| 58 | |
| 59 | This is an advanced topic that we explain in \ref TopicEigenExpressionTemplates "this page", |
| 60 | but it is useful to just mention it now. In Eigen, arithmetic operators such as \c operator+ don't |
| 61 | perform any computation by themselves, they just return an "expression object" describing the computation to be |
| 62 | performed. The actual computation happens later, when the whole expression is evaluated, typically in \c operator=. |
| 63 | While this might sound heavy, any modern optimizing compiler is able to optimize away that abstraction and |
| 64 | the result is perfectly optimized code. For example, when you do: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 65 | \code |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 66 | VectorXf a(50), b(50), c(50), d(50); |
| 67 | ... |
| 68 | a = 3*b + 4*c + 5*d; |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 69 | \endcode |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 70 | Eigen compiles it to just one for loop, so that the arrays are traversed only once. Simplyfying (e.g. ignoring |
| 71 | SIMD optimizations), this loop looks like this: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 72 | \code |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 73 | for(int i = 0; i < 50; ++i) |
| 74 | a[i] = 3*b[i] + 4*c[i] + 5*d[i]; |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 75 | \endcode |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 76 | Thus, you should not be afraid of using relatively large arithmetic expressions with Eigen: it only gives Eigen |
| 77 | more opportunities for optimization. |
| 78 | |
Gael Guennebaud | de1220a | 2010-06-28 00:05:11 +0200 | [diff] [blame] | 79 | \section TutorialArithmeticTranspose Transposition and conjugation |
| 80 | |
| 81 | The \c transpose \f$ a^T \f$, \c conjugate \f$ \bar{a} \f$, and the \c adjoint (i.e., conjugate transpose) of the matrix or vector \f$ a \f$, are simply obtained by the functions of the same names. |
| 82 | |
| 83 | <table class="tutorial_code"><tr><td> |
| 84 | Example: \include tut_arithmetic_transpose_conjugate.cpp |
| 85 | </td> |
| 86 | <td> |
| 87 | Output: \include tut_arithmetic_transpose_conjugate.out |
| 88 | </td></tr></table> |
| 89 | |
| 90 | For real matrices, \c conjugate() is a no-operation, and so \c adjoint() is 100% equivalent to \c transpose(). |
| 91 | |
| 92 | As for basic arithmetic operators, \c transpose and \c adjoint simply return a proxy object without doing the actual transposition. Therefore, <tt>a=a.transpose()</tt> leads to an unexpected result: |
| 93 | <table class="tutorial_code"><tr><td> |
| 94 | Example: \include tut_arithmetic_transpose_aliasing.cpp |
| 95 | </td> |
| 96 | <td> |
| 97 | Output: \include tut_arithmetic_transpose_aliasing.out |
| 98 | </td></tr></table> |
| 99 | In "debug mode", i.e., when assertions have not been disabled, such common pitfalls are automatically detected. For \em in-place transposition, simply use the transposeInPlace() function: |
| 100 | <table class="tutorial_code"><tr><td> |
| 101 | Example: \include tut_arithmetic_transpose_inplace.cpp |
| 102 | </td> |
| 103 | <td> |
| 104 | Output: \include tut_arithmetic_transpose_inplace.out |
| 105 | </td></tr></table> |
| 106 | There is also the adjointInPlace() function for complex matrix. |
| 107 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 108 | \section TutorialArithmeticMatrixMul Matrix-matrix and matrix-vector multiplication |
| 109 | |
| 110 | Matrix-matrix multiplication is again done with \c operator*. Since vectors are a special |
| 111 | case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special |
| 112 | case of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just |
| 113 | two operators: |
| 114 | \li binary operator * as in \c a*b |
| 115 | \li compound operator *= as in \c a*=b |
| 116 | |
| 117 | Example: \include tut_arithmetic_matrix_mul.cpp |
| 118 | Output: \verbinclude tut_arithmetic_matrix_mul.out |
| 119 | |
| 120 | Note: if you read the above paragraph on expression templates and are worried that doing \c m=m*m might cause |
| 121 | aliasing issues, be reassured for now: Eigen treats matrix multiplication as a special case and takes care of |
| 122 | introducing a temporary here, so it will compile \c m=m*m as: |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 123 | \code |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 124 | tmp = m*m; |
| 125 | m = tmp; |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 126 | \endcode |
Gael Guennebaud | 75da254 | 2010-06-28 00:42:57 +0200 | [diff] [blame^] | 127 | If you know your matrix product can be safely evluated into the destination matrix without aliasing issue, then you can use the \c nolias() function to avoid the temporary, e.g.: |
| 128 | \code |
| 129 | c.noalias() += a * b; |
| 130 | \endcode |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 131 | For more details on this topic, see \ref TopicEigenExpressionTemplates "this page". |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 132 | |
Gael Guennebaud | 75da254 | 2010-06-28 00:42:57 +0200 | [diff] [blame^] | 133 | \b Note: for BLAS users worried about performance, expressions such as <tt>c.noalias() -= 2 * a.adjoint() * b;</tt> are fully optimized and trigger a single gemm-like function call. |
| 134 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 135 | \section TutorialArithmeticDotAndCross Dot product and cross product |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 136 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 137 | The above-discussed \c operator* does not allow to compute dot and cross products. For that, you need the dot() and cross() methods. |
| 138 | Example: \include tut_arithmetic_dot_cross.cpp |
| 139 | Output: \verbinclude tut_arithmetic_dot_cross.out |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 140 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 141 | Remember that cross product is only for vectors of size 3. Dot product is for vectors of any sizes. |
| 142 | When using complex numbers, Eigen's dot product is conjugate-linear in the first variable and linear in the |
| 143 | second variable. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 144 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 145 | \section TutorialArithmeticRedux Basic arithmetic reduction operations |
| 146 | Eigen also provides some reduction operations to obtain values such as the sum or the maximum |
| 147 | or minimum of all the coefficients in a given matrix or vector. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 148 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 149 | TODO: convert this from table format to tutorial/examples format. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 150 | |
| 151 | <table class="tutorial_code" align="center"> |
| 152 | <tr><td align="center">\b Reduction \b operation</td><td align="center">\b Usage \b example</td></tr> |
| 153 | <tr><td> |
| 154 | Sum of all the coefficients in a matrix</td><td>\code |
| 155 | MatrixXf m; |
| 156 | float totalSum = m.sum();\endcode</td></tr> |
| 157 | <tr><td> |
| 158 | Maximum coefficient in a matrix</td><td>\code |
| 159 | MatrixXf m; |
| 160 | int row, col; |
| 161 | |
| 162 | // minimum value will be stored in minValue |
| 163 | // and the row and column where it was found in row and col, |
| 164 | // (these two parameters are optional) |
| 165 | float minValue = m.minCoeff(&row,&col);\endcode</td></tr> |
| 166 | <tr><td> |
| 167 | Maximum coefficient in a matrix</td><td>\code |
| 168 | MatrixXf m; |
| 169 | int row, col; |
| 170 | |
| 171 | // maximum value will be stored in maxValue |
| 172 | // and the row and column where it was found in row and col, |
| 173 | // (these two parameters are optional) |
| 174 | float maxValue = m.maxCoeff(&row,&col);\endcode</td></tr> |
| 175 | <tr><td> |
| 176 | Product between all coefficients in a matrix</td><td>\code |
| 177 | MatrixXf m; |
| 178 | |
| 179 | float product = m.prod();\endcode</td></tr> |
| 180 | <tr><td> |
| 181 | Mean of coefficients in a matrix</td><td>\code |
| 182 | MatrixXf m; |
| 183 | |
| 184 | float mean = m.mean();\endcode</td></tr> |
| 185 | <tr><td> |
| 186 | Matrix's trace</td><td>\code |
| 187 | MatrixXf m; |
| 188 | |
| 189 | float trace = m.trace();\endcode</td></tr> |
| 190 | </table> |
| 191 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 192 | \subsection TutorialArithmeticValidity Validity of operations |
| 193 | Eigen checks the validity of the operations that you perform. When possible, |
| 194 | it checks them at compile-time, producing compilation errors. These error messages can be long and ugly, |
| 195 | but Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example: |
| 196 | \code |
| 197 | Matrix3f m; |
| 198 | Vector4f v; |
| 199 | v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES |
| 200 | \endcode |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 201 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 202 | Of course, in many cases, for example when checking dynamic sizes, the check cannot be performed at compile time. |
| 203 | Eigen then uses runtime assertions. This means that executing an illegal operation will result in a crash at runtime, |
| 204 | with an error message. |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 205 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 206 | \code |
| 207 | MatrixXf m(3,3); |
| 208 | VectorXf v(4); |
| 209 | v = m * v; // Run-time assertion failure here: "invalid matrix product" |
| 210 | \endcode |
| 211 | |
| 212 | For more details on this topic, see \ref TopicAssertions "this page". |
| 213 | |
| 214 | \li \b Next: \ref TutorialArrayClass |
Carlos Becker | 9d44005 | 2010-06-25 20:16:12 -0400 | [diff] [blame] | 215 | |
| 216 | */ |
| 217 | |
| 218 | } |