You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
9.6 KiB

  1. namespace Eigen {
  2. /** \eigenManualPage TutorialMatrixArithmetic Matrix and vector arithmetic
  3. This page aims to provide an overview and some details on how to perform arithmetic
  4. between matrices, vectors and scalars with Eigen.
  5. \eigenAutoToc
  6. \section TutorialArithmeticIntroduction Introduction
  7. Eigen offers matrix/vector arithmetic operations either through overloads of common C++ arithmetic operators such as +, -, *,
  8. or through special methods such as dot(), cross(), etc.
  9. For the Matrix class (matrices and vectors), operators are only overloaded to support
  10. linear-algebraic operations. For example, \c matrix1 \c * \c matrix2 means matrix-matrix product,
  11. and \c vector \c + \c scalar is just not allowed. If you want to perform all kinds of array operations,
  12. not linear algebra, see the \ref TutorialArrayClass "next page".
  13. \section TutorialArithmeticAddSub Addition and subtraction
  14. The left hand side and right hand side must, of course, have the same numbers of rows and of columns. They must
  15. also have the same \c Scalar type, as Eigen doesn't do automatic type promotion. The operators at hand here are:
  16. \li binary operator + as in \c a+b
  17. \li binary operator - as in \c a-b
  18. \li unary operator - as in \c -a
  19. \li compound operator += as in \c a+=b
  20. \li compound operator -= as in \c a-=b
  21. <table class="example">
  22. <tr><th>Example:</th><th>Output:</th></tr>
  23. <tr><td>
  24. \include tut_arithmetic_add_sub.cpp
  25. </td>
  26. <td>
  27. \verbinclude tut_arithmetic_add_sub.out
  28. </td></tr></table>
  29. \section TutorialArithmeticScalarMulDiv Scalar multiplication and division
  30. Multiplication and division by a scalar is very simple too. The operators at hand here are:
  31. \li binary operator * as in \c matrix*scalar
  32. \li binary operator * as in \c scalar*matrix
  33. \li binary operator / as in \c matrix/scalar
  34. \li compound operator *= as in \c matrix*=scalar
  35. \li compound operator /= as in \c matrix/=scalar
  36. <table class="example">
  37. <tr><th>Example:</th><th>Output:</th></tr>
  38. <tr><td>
  39. \include tut_arithmetic_scalar_mul_div.cpp
  40. </td>
  41. <td>
  42. \verbinclude tut_arithmetic_scalar_mul_div.out
  43. </td></tr></table>
  44. \section TutorialArithmeticMentionXprTemplates A note about expression templates
  45. This is an advanced topic that we explain on \ref TopicEigenExpressionTemplates "this page",
  46. but it is useful to just mention it now. In Eigen, arithmetic operators such as \c operator+ don't
  47. perform any computation by themselves, they just return an "expression object" describing the computation to be
  48. performed. The actual computation happens later, when the whole expression is evaluated, typically in \c operator=.
  49. While this might sound heavy, any modern optimizing compiler is able to optimize away that abstraction and
  50. the result is perfectly optimized code. For example, when you do:
  51. \code
  52. VectorXf a(50), b(50), c(50), d(50);
  53. ...
  54. a = 3*b + 4*c + 5*d;
  55. \endcode
  56. Eigen compiles it to just one for loop, so that the arrays are traversed only once. Simplifying (e.g. ignoring
  57. SIMD optimizations), this loop looks like this:
  58. \code
  59. for(int i = 0; i < 50; ++i)
  60. a[i] = 3*b[i] + 4*c[i] + 5*d[i];
  61. \endcode
  62. Thus, you should not be afraid of using relatively large arithmetic expressions with Eigen: it only gives Eigen
  63. more opportunities for optimization.
  64. \section TutorialArithmeticTranspose Transposition and conjugation
  65. The transpose \f$ a^T \f$, conjugate \f$ \bar{a} \f$, and adjoint (i.e., conjugate transpose) \f$ a^* \f$ of a matrix or vector \f$ a \f$ are obtained by the member functions \link DenseBase::transpose() transpose()\endlink, \link MatrixBase::conjugate() conjugate()\endlink, and \link MatrixBase::adjoint() adjoint()\endlink, respectively.
  66. <table class="example">
  67. <tr><th>Example:</th><th>Output:</th></tr>
  68. <tr><td>
  69. \include tut_arithmetic_transpose_conjugate.cpp
  70. </td>
  71. <td>
  72. \verbinclude tut_arithmetic_transpose_conjugate.out
  73. </td></tr></table>
  74. For real matrices, \c conjugate() is a no-operation, and so \c adjoint() is equivalent to \c transpose().
  75. As for basic arithmetic operators, \c transpose() and \c adjoint() simply return a proxy object without doing the actual transposition. If you do <tt>b = a.transpose()</tt>, then the transpose is evaluated at the same time as the result is written into \c b. However, there is a complication here. If you do <tt>a = a.transpose()</tt>, then Eigen starts writing the result into \c a before the evaluation of the transpose is finished. Therefore, the instruction <tt>a = a.transpose()</tt> does not replace \c a with its transpose, as one would expect:
  76. <table class="example">
  77. <tr><th>Example:</th><th>Output:</th></tr>
  78. <tr><td>
  79. \include tut_arithmetic_transpose_aliasing.cpp
  80. </td>
  81. <td>
  82. \verbinclude tut_arithmetic_transpose_aliasing.out
  83. </td></tr></table>
  84. This is the so-called \ref TopicAliasing "aliasing issue". In "debug mode", i.e., when \ref TopicAssertions "assertions" have not been disabled, such common pitfalls are automatically detected.
  85. For \em in-place transposition, as for instance in <tt>a = a.transpose()</tt>, simply use the \link DenseBase::transposeInPlace() transposeInPlace()\endlink function:
  86. <table class="example">
  87. <tr><th>Example:</th><th>Output:</th></tr>
  88. <tr><td>
  89. \include tut_arithmetic_transpose_inplace.cpp
  90. </td>
  91. <td>
  92. \verbinclude tut_arithmetic_transpose_inplace.out
  93. </td></tr></table>
  94. There is also the \link MatrixBase::adjointInPlace() adjointInPlace()\endlink function for complex matrices.
  95. \section TutorialArithmeticMatrixMul Matrix-matrix and matrix-vector multiplication
  96. Matrix-matrix multiplication is again done with \c operator*. Since vectors are a special
  97. case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special
  98. case of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just
  99. two operators:
  100. \li binary operator * as in \c a*b
  101. \li compound operator *= as in \c a*=b (this multiplies on the right: \c a*=b is equivalent to <tt>a = a*b</tt>)
  102. <table class="example">
  103. <tr><th>Example:</th><th>Output:</th></tr>
  104. <tr><td>
  105. \include tut_arithmetic_matrix_mul.cpp
  106. </td>
  107. <td>
  108. \verbinclude tut_arithmetic_matrix_mul.out
  109. </td></tr></table>
  110. Note: if you read the above paragraph on expression templates and are worried that doing \c m=m*m might cause
  111. aliasing issues, be reassured for now: Eigen treats matrix multiplication as a special case and takes care of
  112. introducing a temporary here, so it will compile \c m=m*m as:
  113. \code
  114. tmp = m*m;
  115. m = tmp;
  116. \endcode
  117. If you know your matrix product can be safely evaluated into the destination matrix without aliasing issue, then you can use the \link MatrixBase::noalias() noalias()\endlink function to avoid the temporary, e.g.:
  118. \code
  119. c.noalias() += a * b;
  120. \endcode
  121. For more details on this topic, see the page on \ref TopicAliasing "aliasing".
  122. \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.
  123. \section TutorialArithmeticDotAndCross Dot product and cross product
  124. For dot product and cross product, you need the \link MatrixBase::dot() dot()\endlink and \link MatrixBase::cross() cross()\endlink methods. Of course, the dot product can also be obtained as a 1x1 matrix as u.adjoint()*v.
  125. <table class="example">
  126. <tr><th>Example:</th><th>Output:</th></tr>
  127. <tr><td>
  128. \include tut_arithmetic_dot_cross.cpp
  129. </td>
  130. <td>
  131. \verbinclude tut_arithmetic_dot_cross.out
  132. </td></tr></table>
  133. Remember that cross product is only for vectors of size 3. Dot product is for vectors of any sizes.
  134. When using complex numbers, Eigen's dot product is conjugate-linear in the first variable and linear in the
  135. second variable.
  136. \section TutorialArithmeticRedux Basic arithmetic reduction operations
  137. Eigen also provides some reduction operations to reduce a given matrix or vector to a single value such as the sum (computed by \link DenseBase::sum() sum()\endlink), product (\link DenseBase::prod() prod()\endlink), or the maximum (\link DenseBase::maxCoeff() maxCoeff()\endlink) and minimum (\link DenseBase::minCoeff() minCoeff()\endlink) of all its coefficients.
  138. <table class="example">
  139. <tr><th>Example:</th><th>Output:</th></tr>
  140. <tr><td>
  141. \include tut_arithmetic_redux_basic.cpp
  142. </td>
  143. <td>
  144. \verbinclude tut_arithmetic_redux_basic.out
  145. </td></tr></table>
  146. The \em trace of a matrix, as returned by the function \link MatrixBase::trace() trace()\endlink, is the sum of the diagonal coefficients and can also be computed as efficiently using <tt>a.diagonal().sum()</tt>, as we will see later on.
  147. There also exist variants of the \c minCoeff and \c maxCoeff functions returning the coordinates of the respective coefficient via the arguments:
  148. <table class="example">
  149. <tr><th>Example:</th><th>Output:</th></tr>
  150. <tr><td>
  151. \include tut_arithmetic_redux_minmax.cpp
  152. </td>
  153. <td>
  154. \verbinclude tut_arithmetic_redux_minmax.out
  155. </td></tr></table>
  156. \section TutorialArithmeticValidity Validity of operations
  157. Eigen checks the validity of the operations that you perform. When possible,
  158. it checks them at compile time, producing compilation errors. These error messages can be long and ugly,
  159. but Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example:
  160. \code
  161. Matrix3f m;
  162. Vector4f v;
  163. v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
  164. \endcode
  165. Of course, in many cases, for example when checking dynamic sizes, the check cannot be performed at compile time.
  166. Eigen then uses runtime assertions. This means that the program will abort with an error message when executing an illegal operation if it is run in "debug mode", and it will probably crash if assertions are turned off.
  167. \code
  168. MatrixXf m(3,3);
  169. VectorXf v(4);
  170. v = m * v; // Run-time assertion failure here: "invalid matrix product"
  171. \endcode
  172. For more details on this topic, see \ref TopicAssertions "this page".
  173. */
  174. }