TutorialReductionsVisitorsBroadcasting.dox 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. namespace Eigen {
  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 Eigen::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. Both functions also return the value of the minimum or maximum coefficient.
  79. \section TutorialReductionsVisitorsBroadcastingPartialReductions Partial reductions
  80. Partial reductions are reductions that can operate column- or row-wise on a Matrix or
  81. Array, applying the reduction operation on each column or row and
  82. returning a column or row vector with the corresponding values. Partial reductions are applied
  83. with \link DenseBase::colwise() colwise() \endlink or \link DenseBase::rowwise() rowwise() \endlink.
  84. A simple example is obtaining the maximum of the elements
  85. in each column in a given matrix, storing the result in a row vector:
  86. <table class="example">
  87. <tr><th>Example:</th><th>Output:</th></tr>
  88. <tr><td>
  89. \include Tutorial_ReductionsVisitorsBroadcasting_colwise.cpp
  90. </td>
  91. <td>
  92. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_colwise.out
  93. </td></tr></table>
  94. The same operation can be performed row-wise:
  95. <table class="example">
  96. <tr><th>Example:</th><th>Output:</th></tr>
  97. <tr><td>
  98. \include Tutorial_ReductionsVisitorsBroadcasting_rowwise.cpp
  99. </td>
  100. <td>
  101. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_rowwise.out
  102. </td></tr></table>
  103. <b>Note that column-wise operations return a row vector, while row-wise operations return a column vector.</b>
  104. \subsection TutorialReductionsVisitorsBroadcastingPartialReductionsCombined Combining partial reductions with other operations
  105. It is also possible to use the result of a partial reduction to do further processing.
  106. Here is another example that finds the column whose sum of elements is the maximum
  107. within a matrix. With column-wise partial reductions this can be coded as:
  108. <table class="example">
  109. <tr><th>Example:</th><th>Output:</th></tr>
  110. <tr><td>
  111. \include Tutorial_ReductionsVisitorsBroadcasting_maxnorm.cpp
  112. </td>
  113. <td>
  114. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_maxnorm.out
  115. </td></tr></table>
  116. The previous example applies the \link DenseBase::sum() sum() \endlink reduction on each column
  117. though the \link DenseBase::colwise() colwise() \endlink visitor, obtaining a new matrix whose
  118. size is 1x4.
  119. Therefore, if
  120. \f[
  121. \mbox{m} = \begin{bmatrix} 1 & 2 & 6 & 9 \\
  122. 3 & 1 & 7 & 2 \end{bmatrix}
  123. \f]
  124. then
  125. \f[
  126. \mbox{m.colwise().sum()} = \begin{bmatrix} 4 & 3 & 13 & 11 \end{bmatrix}
  127. \f]
  128. The \link DenseBase::maxCoeff() maxCoeff() \endlink reduction is finally applied
  129. to obtain the column index where the maximum sum is found,
  130. which is the column index 2 (third column) in this case.
  131. \section TutorialReductionsVisitorsBroadcastingBroadcasting Broadcasting
  132. The concept behind broadcasting is similar to partial reductions, with the difference that broadcasting
  133. constructs an expression where a vector (column or row) is interpreted as a matrix by replicating it in
  134. one direction.
  135. A simple example is to add a certain column vector to each column in a matrix.
  136. This can be accomplished with:
  137. <table class="example">
  138. <tr><th>Example:</th><th>Output:</th></tr>
  139. <tr><td>
  140. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.cpp
  141. </td>
  142. <td>
  143. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple.out
  144. </td></tr></table>
  145. We can interpret the instruction <tt>mat.colwise() += v</tt> in two equivalent ways. It adds the vector \c v
  146. to every column of the matrix. Alternatively, it can be interpreted as repeating the vector \c v four times to
  147. form a four-by-two matrix which is then added to \c mat:
  148. \f[
  149. \begin{bmatrix} 1 & 2 & 6 & 9 \\ 3 & 1 & 7 & 2 \end{bmatrix}
  150. + \begin{bmatrix} 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \end{bmatrix}
  151. = \begin{bmatrix} 1 & 2 & 6 & 9 \\ 4 & 2 & 8 & 3 \end{bmatrix}.
  152. \f]
  153. The operators <tt>-=</tt>, <tt>+</tt> and <tt>-</tt> can also be used column-wise and row-wise. On arrays, we
  154. can also use the operators <tt>*=</tt>, <tt>/=</tt>, <tt>*</tt> and <tt>/</tt> to perform coefficient-wise
  155. multiplication and division column-wise or row-wise. These operators are not available on matrices because it
  156. 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
  157. \c v(1), and so on, then use <tt>mat = mat * v.asDiagonal()</tt>.
  158. It is important to point out that the vector to be added column-wise or row-wise must be of type Vector,
  159. and cannot be a Matrix. If this is not met then you will get compile-time error. This also means that
  160. broadcasting operations can only be applied with an object of type Vector, when operating with Matrix.
  161. The same applies for the Array class, where the equivalent for VectorXf is ArrayXf. As always, you should
  162. not mix arrays and matrices in the same expression.
  163. To perform the same operation row-wise we can do:
  164. <table class="example">
  165. <tr><th>Example:</th><th>Output:</th></tr>
  166. <tr><td>
  167. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.cpp
  168. </td>
  169. <td>
  170. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_simple_rowwise.out
  171. </td></tr></table>
  172. \subsection TutorialReductionsVisitorsBroadcastingBroadcastingCombined Combining broadcasting with other operations
  173. Broadcasting can also be combined with other operations, such as Matrix or Array operations,
  174. reductions and partial reductions.
  175. Now that broadcasting, reductions and partial reductions have been introduced, we can dive into a more advanced example that finds
  176. 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,
  177. computing the squared Euclidean distance with the partial reduction named \link MatrixBase::squaredNorm() squaredNorm() \endlink:
  178. <table class="example">
  179. <tr><th>Example:</th><th>Output:</th></tr>
  180. <tr><td>
  181. \include Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.cpp
  182. </td>
  183. <td>
  184. \verbinclude Tutorial_ReductionsVisitorsBroadcasting_broadcast_1nn.out
  185. </td></tr></table>
  186. The line that does the job is
  187. \code
  188. (m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
  189. \endcode
  190. We will go step by step to understand what is happening:
  191. - <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
  192. is a new matrix whose size is the same as matrix <tt>m</tt>: \f[
  193. \mbox{m.colwise() - v} =
  194. \begin{bmatrix}
  195. -1 & 21 & 4 & 7 \\
  196. 0 & 8 & 4 & -1
  197. \end{bmatrix}
  198. \f]
  199. - <tt>(m.colwise() - v).colwise().squaredNorm()</tt> is a partial reduction, computing the squared norm column-wise. The result of
  200. 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[
  201. \mbox{(m.colwise() - v).colwise().squaredNorm()} =
  202. \begin{bmatrix}
  203. 1 & 505 & 32 & 50
  204. \end{bmatrix}
  205. \f]
  206. - 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
  207. distance.
  208. */
  209. }