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.

268 lines
12 KiB

  1. namespace StormEigen {
  2. /** \eigenManualPage TutorialReductionsVisitorsBroadcasting Reductions, visitors and broadcasting
  3. This page explains Eigen's reductions, visitors and broadcasting and how they are used with
  4. \link MatrixBase matrices \endlink and \link ArrayBase arrays \endlink.
  5. \eigenAutoToc
  6. \section TutorialReductionsVisitorsBroadcastingReductions Reductions
  7. In Eigen, a reduction is a function taking a matrix or array, and returning a single
  8. scalar value. One of the most used reductions is \link DenseBase::sum() .sum() \endlink,
  9. returning the sum of all the coefficients inside a given matrix or array.
  10. <table class="example">
  11. <tr><th>Example:</th><th>Output:</th></tr>
  12. <tr><td>
  13. \include tut_arithmetic_redux_basic.cpp
  14. </td>
  15. <td>
  16. \verbinclude tut_arithmetic_redux_basic.out
  17. </td></tr></table>
  18. The \em trace of a matrix, as returned by the function \c trace(), is the sum of the diagonal coefficients and can equivalently be computed <tt>a.diagonal().sum()</tt>.
  19. \subsection TutorialReductionsVisitorsBroadcastingReductionsNorm Norm computations
  20. The (Euclidean a.k.a. \f$\ell^2\f$) squared norm of a vector can be obtained \link MatrixBase::squaredNorm() squaredNorm() \endlink. It is equal to the dot product of the vector by itself, and equivalently to the sum of squared absolute values of its coefficients.
  21. Eigen also provides the \link MatrixBase::norm() norm() \endlink method, which returns the square root of \link MatrixBase::squaredNorm() squaredNorm() \endlink.
  22. These operations can also operate on matrices; in that case, a n-by-p matrix is seen as a vector of size (n*p), so for example the \link MatrixBase::norm() norm() \endlink method returns the "Frobenius" or "Hilbert-Schmidt" norm. We refrain from speaking of the \f$\ell^2\f$ norm of a matrix because that can mean different things.
  23. If you want other coefficient-wise \f$\ell^p\f$ norms, use the \link MatrixBase::lpNorm() lpNorm<p>() \endlink method. The template parameter \a p can take the special value \a Infinity if you want the \f$\ell^\infty\f$ norm, which is the maximum of the absolute values of the coefficients.
  24. The following example demonstrates these methods.
  25. <table class="example">
  26. <tr><th>Example:</th><th>Output:</th></tr>
  27. <tr><td>
  28. \include Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.cpp
  29. </td>
  30. <td>
  31. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_reductions_norm.out
  32. </td></tr></table>
  33. \b Operator \b norm: The 1-norm and \f$\infty\f$-norm <a href="https://en.wikipedia.org/wiki/Operator_norm">matrix operator norms</a> can easily be computed as follows:
  34. <table class="example">
  35. <tr><th>Example:</th><th>Output:</th></tr>
  36. <tr><td>
  37. \include Tutorial_ReductionsVisitorsBroadcasting_reductions_operatornorm.cpp
  38. </td>
  39. <td>
  40. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_reductions_operatornorm.out
  41. </td></tr></table>
  42. See below for more explanations on the syntax of these expressions.
  43. \subsection TutorialReductionsVisitorsBroadcastingReductionsBool Boolean reductions
  44. The following reductions operate on boolean values:
  45. - \link DenseBase::all() all() \endlink returns \b true if all of the coefficients in a given Matrix or Array evaluate to \b true .
  46. - \link DenseBase::any() any() \endlink returns \b true if at least one of the coefficients in a given Matrix or Array evaluates to \b true .
  47. - \link DenseBase::count() count() \endlink returns the number of coefficients in a given Matrix or Array that evaluate to \b true.
  48. These are typically used in conjunction with the coefficient-wise comparison and equality operators provided by Array. For instance, <tt>array > 0</tt> is an %Array of the same size as \c array , with \b true at those positions where the corresponding coefficient of \c array is positive. Thus, <tt>(array > 0).all()</tt> tests whether all coefficients of \c array are positive. This can be seen in the following example:
  49. <table class="example">
  50. <tr><th>Example:</th><th>Output:</th></tr>
  51. <tr><td>
  52. \include Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.cpp
  53. </td>
  54. <td>
  55. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_reductions_bool.out
  56. </td></tr></table>
  57. \subsection TutorialReductionsVisitorsBroadcastingReductionsUserdefined User defined reductions
  58. TODO
  59. In the meantime you can have a look at the DenseBase::redux() function.
  60. \section TutorialReductionsVisitorsBroadcastingVisitors Visitors
  61. Visitors are useful when one wants to obtain the location of a coefficient inside
  62. a Matrix or Array. The simplest examples are
  63. \link MatrixBase::maxCoeff() maxCoeff(&x,&y) \endlink and
  64. \link MatrixBase::minCoeff() minCoeff(&x,&y)\endlink, which can be used to find
  65. the location of the greatest or smallest coefficient in a Matrix or
  66. Array.
  67. The arguments passed to a visitor are pointers to the variables where the
  68. row and column position are to be stored. These variables should be of type
  69. \link DenseBase::Index Index \endlink, as shown below:
  70. <table class="example">
  71. <tr><th>Example:</th><th>Output:</th></tr>
  72. <tr><td>
  73. \include Tutorial_ReductionsVisitorsBroadcasting_visitors.cpp
  74. </td>
  75. <td>
  76. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_visitors.out
  77. </td></tr></table>
  78. Note that both functions also return the value of the minimum or maximum coefficient if needed,
  79. as if it was a typical reduction operation.
  80. \section TutorialReductionsVisitorsBroadcastingPartialReductions Partial reductions
  81. Partial reductions are reductions that can operate column- or row-wise on a Matrix or
  82. Array, applying the reduction operation on each column or row and
  83. returning a column or row-vector with the corresponding values. Partial reductions are applied
  84. with \link DenseBase::colwise() colwise() \endlink or \link DenseBase::rowwise() rowwise() \endlink.
  85. A simple example is obtaining the maximum of the elements
  86. in each column in a given matrix, storing the result in a row-vector:
  87. <table class="example">
  88. <tr><th>Example:</th><th>Output:</th></tr>
  89. <tr><td>
  90. \include Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp
  91. </td>
  92. <td>
  93. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_colwise.out
  94. </td></tr></table>
  95. The same operation can be performed row-wise:
  96. <table class="example">
  97. <tr><th>Example:</th><th>Output:</th></tr>
  98. <tr><td>
  99. \include Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp
  100. </td>
  101. <td>
  102. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_rowwise.out
  103. </td></tr></table>
  104. <b>Note that column-wise operations return a 'row-vector' while row-wise operations
  105. return a 'column-vector'</b>
  106. \subsection TutorialReductionsVisitorsBroadcastingPartialReductionsCombined Combining partial reductions with other operations
  107. It is also possible to use the result of a partial reduction to do further processing.
  108. Here is another example that finds the column whose sum of elements is the maximum
  109. within a matrix. With column-wise partial reductions this can be coded as:
  110. <table class="example">
  111. <tr><th>Example:</th><th>Output:</th></tr>
  112. <tr><td>
  113. \include Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp
  114. </td>
  115. <td>
  116. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_maxnorm.out
  117. </td></tr></table>
  118. The previous example applies the \link DenseBase::sum() sum() \endlink reduction on each column
  119. though the \link DenseBase::colwise() colwise() \endlink visitor, obtaining a new matrix whose
  120. size is 1x4.
  121. Therefore, if
  122. \f[
  123. \mbox{m} = \begin{bmatrix} 1 & 2 & 6 & 9 \\
  124. 3 & 1 & 7 & 2 \end{bmatrix}
  125. \f]
  126. then
  127. \f[
  128. \mbox{m.colwise().sum()} = \begin{bmatrix} 4 & 3 & 13 & 11 \end{bmatrix}
  129. \f]
  130. The \link DenseBase::maxCoeff() maxCoeff() \endlink reduction is finally applied
  131. to obtain the column index where the maximum sum is found,
  132. which is the column index 2 (third column) in this case.
  133. \section TutorialReductionsVisitorsBroadcastingBroadcasting Broadcasting
  134. The concept behind broadcasting is similar to partial reductions, with the difference that broadcasting
  135. constructs an expression where a vector (column or row) is interpreted as a matrix by replicating it in
  136. one direction.
  137. A simple example is to add a certain column-vector to each column in a matrix.
  138. This can be accomplished with:
  139. <table class="example">
  140. <tr><th>Example:</th><th>Output:</th></tr>
  141. <tr><td>
  142. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp
  143. </td>
  144. <td>
  145. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.out
  146. </td></tr></table>
  147. We can interpret the instruction <tt>mat.colwise() += v</tt> in two equivalent ways. It adds the vector \c v
  148. to every column of the matrix. Alternatively, it can be interpreted as repeating the vector \c v four times to
  149. form a four-by-two matrix which is then added to \c mat:
  150. \f[
  151. \begin{bmatrix} 1 & 2 & 6 & 9 \\ 3 & 1 & 7 & 2 \end{bmatrix}
  152. + \begin{bmatrix} 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \end{bmatrix}
  153. = \begin{bmatrix} 1 & 2 & 6 & 9 \\ 4 & 2 & 8 & 3 \end{bmatrix}.
  154. \f]
  155. The operators <tt>-=</tt>, <tt>+</tt> and <tt>-</tt> can also be used column-wise and row-wise. On arrays, we
  156. can also use the operators <tt>*=</tt>, <tt>/=</tt>, <tt>*</tt> and <tt>/</tt> to perform coefficient-wise
  157. multiplication and division column-wise or row-wise. These operators are not available on matrices because it
  158. is not clear what they would do. If you want multiply column 0 of a matrix \c mat with \c v(0), column 1 with
  159. \c v(1), and so on, then use <tt>mat = mat * v.asDiagonal()</tt>.
  160. It is important to point out that the vector to be added column-wise or row-wise must be of type Vector,
  161. and cannot be a Matrix. If this is not met then you will get compile-time error. This also means that
  162. broadcasting operations can only be applied with an object of type Vector, when operating with Matrix.
  163. The same applies for the Array class, where the equivalent for VectorXf is ArrayXf. As always, you should
  164. not mix arrays and matrices in the same expression.
  165. To perform the same operation row-wise we can do:
  166. <table class="example">
  167. <tr><th>Example:</th><th>Output:</th></tr>
  168. <tr><td>
  169. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp
  170. </td>
  171. <td>
  172. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.out
  173. </td></tr></table>
  174. \subsection TutorialReductionsVisitorsBroadcastingBroadcastingCombined Combining broadcasting with other operations
  175. Broadcasting can also be combined with other operations, such as Matrix or Array operations,
  176. reductions and partial reductions.
  177. Now that broadcasting, reductions and partial reductions have been introduced, we can dive into a more advanced example that finds
  178. the nearest neighbour of a vector <tt>v</tt> within the columns of matrix <tt>m</tt>. The Euclidean distance will be used in this example,
  179. computing the squared Euclidean distance with the partial reduction named \link MatrixBase::squaredNorm() squaredNorm() \endlink:
  180. <table class="example">
  181. <tr><th>Example:</th><th>Output:</th></tr>
  182. <tr><td>
  183. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp
  184. </td>
  185. <td>
  186. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.out
  187. </td></tr></table>
  188. The line that does the job is
  189. \code
  190. (m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
  191. \endcode
  192. We will go step by step to understand what is happening:
  193. - <tt>m.colwise() - v</tt> is a broadcasting operation, subtracting <tt>v</tt> from each column in <tt>m</tt>. The result of this operation
  194. is a new matrix whose size is the same as matrix <tt>m</tt>: \f[
  195. \mbox{m.colwise() - v} =
  196. \begin{bmatrix}
  197. -1 & 21 & 4 & 7 \\
  198. 0 & 8 & 4 & -1
  199. \end{bmatrix}
  200. \f]
  201. - <tt>(m.colwise() - v).colwise().squaredNorm()</tt> is a partial reduction, computing the squared norm column-wise. The result of
  202. this operation is a row-vector where each coefficient is the squared Euclidean distance between each column in <tt>m</tt> and <tt>v</tt>: \f[
  203. \mbox{(m.colwise() - v).colwise().squaredNorm()} =
  204. \begin{bmatrix}
  205. 1 & 505 & 32 & 50
  206. \end{bmatrix}
  207. \f]
  208. - Finally, <tt>minCoeff(&index)</tt> is used to obtain the index of the column in <tt>m</tt> that is closest to <tt>v</tt> in terms of Euclidean
  209. distance.
  210. */
  211. }