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.

319 lines
12 KiB

  1. namespace Eigen {
  2. /** \page Eigen2ToEigen3 Porting from Eigen2 to Eigen3
  3. This page lists the most important API changes between Eigen2 and Eigen3,
  4. and gives tips to help porting your application from Eigen2 to Eigen3.
  5. \b Table \b of \b contents
  6. - \ref CompatibilitySupport
  7. - \ref Using
  8. - \ref ComplexDot
  9. - \ref VectorBlocks
  10. - \ref Corners
  11. - \ref CoefficientWiseOperations
  12. - \ref PartAndExtract
  13. - \ref TriangularSolveInPlace
  14. - \ref Decompositions
  15. - \ref LinearSolvers
  16. - \ref GeometryModule
  17. - \ref Transform
  18. - \ref LazyVsNoalias
  19. - \ref AlignMacros
  20. - \ref AlignedMap
  21. - \ref StdContainers
  22. - \ref eiPrefix
  23. \section CompatibilitySupport Eigen2 compatibility support
  24. In order to ease the switch from Eigen2 to Eigen3, Eigen3 features \ref Eigen2SupportModes "Eigen2 support modes".
  25. The quick way to enable this is to define the \c EIGEN2_SUPPORT preprocessor token \b before including any Eigen header (typically it should be set in your project options).
  26. A more powerful, \em staged migration path is also provided, which may be useful to migrate larger projects from Eigen2 to Eigen3. This is explained in the \ref Eigen2SupportModes "Eigen 2 support modes" page.
  27. \section Using The USING_PART_OF_NAMESPACE_EIGEN macro
  28. The USING_PART_OF_NAMESPACE_EIGEN macro has been removed. In Eigen 3, just do:
  29. \code
  30. using namespace Eigen;
  31. \endcode
  32. \section ComplexDot Dot products over complex numbers
  33. This is the single trickiest change between Eigen 2 and Eigen 3. It only affects code using \c std::complex numbers as scalar type.
  34. Eigen 2's dot product was linear in the first variable. Eigen 3's dot product is linear in the second variable. In other words, the Eigen 2 code \code x.dot(y) \endcode is equivalent to the Eigen 3 code \code y.dot(x) \endcode In yet other words, dot products are complex-conjugated in Eigen 3 compared to Eigen 2. The switch to the new convention was commanded by common usage, especially with the notation \f$ x^Ty \f$ for dot products of column-vectors.
  35. \section VectorBlocks Vector blocks
  36. <table class="manual">
  37. <tr><th>Eigen 2</th><th>Eigen 3</th></th>
  38. <tr><td>\code
  39. vector.start(length)
  40. vector.start<length>()
  41. vector.end(length)
  42. vector.end<length>()
  43. \endcode</td><td>\code
  44. vector.head(length)
  45. vector.head<length>()
  46. vector.tail(length)
  47. vector.tail<length>()
  48. \endcode</td></tr>
  49. </table>
  50. \section Corners Matrix Corners
  51. <table class="manual">
  52. <tr><th>Eigen 2</th><th>Eigen 3</th></th>
  53. <tr><td>\code
  54. matrix.corner(TopLeft,r,c)
  55. matrix.corner(TopRight,r,c)
  56. matrix.corner(BottomLeft,r,c)
  57. matrix.corner(BottomRight,r,c)
  58. matrix.corner<r,c>(TopLeft)
  59. matrix.corner<r,c>(TopRight)
  60. matrix.corner<r,c>(BottomLeft)
  61. matrix.corner<r,c>(BottomRight)
  62. \endcode</td><td>\code
  63. matrix.topLeftCorner(r,c)
  64. matrix.topRightCorner(r,c)
  65. matrix.bottomLeftCorner(r,c)
  66. matrix.bottomRightCorner(r,c)
  67. matrix.topLeftCorner<r,c>()
  68. matrix.topRightCorner<r,c>()
  69. matrix.bottomLeftCorner<r,c>()
  70. matrix.bottomRightCorner<r,c>()
  71. \endcode</td>
  72. </tr>
  73. </table>
  74. Notice that Eigen3 also provides these new convenience methods: topRows(), bottomRows(), leftCols(), rightCols(). See in class DenseBase.
  75. \section CoefficientWiseOperations Coefficient wise operations
  76. In Eigen2, coefficient wise operations which have no proper mathematical definition (as a coefficient wise product)
  77. were achieved using the .cwise() prefix, e.g.:
  78. \code a.cwise() * b \endcode
  79. In Eigen3 this .cwise() prefix has been superseded by a new kind of matrix type called
  80. Array for which all operations are performed coefficient wise. You can easily view a matrix as an array and vice versa using
  81. the MatrixBase::array() and ArrayBase::matrix() functions respectively. Here is an example:
  82. \code
  83. Vector4f a, b, c;
  84. c = a.array() * b.array();
  85. \endcode
  86. Note that the .array() function is not at all a synonym of the deprecated .cwise() prefix.
  87. While the .cwise() prefix changed the behavior of the following operator, the array() function performs
  88. a permanent conversion to the array world. Therefore, for binary operations such as the coefficient wise product,
  89. both sides must be converted to an \em array as in the above example. On the other hand, when you
  90. concatenate multiple coefficient wise operations you only have to do the conversion once, e.g.:
  91. \code
  92. Vector4f a, b, c;
  93. c = a.array().abs().pow(3) * b.array().abs().sin();
  94. \endcode
  95. With Eigen2 you would have written:
  96. \code
  97. c = (a.cwise().abs().cwise().pow(3)).cwise() * (b.cwise().abs().cwise().sin());
  98. \endcode
  99. \section PartAndExtract Triangular and self-adjoint matrices
  100. In Eigen 2 you had to play with the part, extract, and marked functions to deal with triangular and selfadjoint matrices. In Eigen 3, all these functions have been removed in favor of the concept of \em views:
  101. <table class="manual">
  102. <tr><th>Eigen 2</th><th>Eigen 3</th></tr>
  103. <tr><td>\code
  104. A.part<UpperTriangular>();
  105. A.part<StrictlyLowerTriangular>(); \endcode</td>
  106. <td>\code
  107. A.triangularView<Upper>()
  108. A.triangularView<StrictlyLower>()\endcode</td></tr>
  109. <tr><td>\code
  110. A.extract<UpperTriangular>();
  111. A.extract<StrictlyLowerTriangular>();\endcode</td>
  112. <td>\code
  113. A.triangularView<Upper>()
  114. A.triangularView<StrictlyLower>()\endcode</td></tr>
  115. <tr><td>\code
  116. A.marked<UpperTriangular>();
  117. A.marked<StrictlyLowerTriangular>();\endcode</td>
  118. <td>\code
  119. A.triangularView<Upper>()
  120. A.triangularView<StrictlyLower>()\endcode</td></tr>
  121. <tr><td colspan="2"></td></tr>
  122. <tr><td>\code
  123. A.part<SelfAdfjoint|UpperTriangular>();
  124. A.extract<SelfAdfjoint|LowerTriangular>();\endcode</td>
  125. <td>\code
  126. A.selfadjointView<Upper>()
  127. A.selfadjointView<Lower>()\endcode</td></tr>
  128. <tr><td colspan="2"></td></tr>
  129. <tr><td>\code
  130. UpperTriangular
  131. LowerTriangular
  132. UnitUpperTriangular
  133. UnitLowerTriangular
  134. StrictlyUpperTriangular
  135. StrictlyLowerTriangular
  136. \endcode</td><td>\code
  137. Upper
  138. Lower
  139. UnitUpper
  140. UnitLower
  141. StrictlyUpper
  142. StrictlyLower
  143. \endcode</td>
  144. </tr>
  145. </table>
  146. \sa class TriangularView, class SelfAdjointView
  147. \section TriangularSolveInPlace Triangular in-place solving
  148. <table class="manual">
  149. <tr><th>Eigen 2</th><th>Eigen 3</th></tr>
  150. <tr><td>\code A.triangularSolveInPlace<XxxTriangular>(Y);\endcode</td><td>\code A.triangularView<Xxx>().solveInPlace(Y);\endcode</td></tr>
  151. </table>
  152. \section Decompositions Matrix decompositions
  153. Some of Eigen 2's matrix decompositions have been renamed in Eigen 3, while some others have been removed and are replaced by other decompositions in Eigen 3.
  154. <table class="manual">
  155. <tr>
  156. <th>Eigen 2</th>
  157. <th>Eigen 3</th>
  158. <th>Notes</th>
  159. </tr>
  160. <tr>
  161. <td>LU</td>
  162. <td>FullPivLU</td>
  163. <td class="alt">See also the new PartialPivLU, it's much faster</td>
  164. </tr>
  165. <tr>
  166. <td>QR</td>
  167. <td>HouseholderQR</td>
  168. <td class="alt">See also the new ColPivHouseholderQR, it's more reliable</td>
  169. </tr>
  170. <tr>
  171. <td>SVD</td>
  172. <td>JacobiSVD</td>
  173. <td class="alt">We currently don't have a bidiagonalizing SVD; of course this is planned.</td>
  174. </tr>
  175. <tr>
  176. <td>EigenSolver and friends</td>
  177. <td>\code #include<Eigen/Eigenvalues> \endcode </td>
  178. <td class="alt">Moved to separate module</td>
  179. </tr>
  180. </table>
  181. \section LinearSolvers Linear solvers
  182. <table class="manual">
  183. <tr><th>Eigen 2</th><th>Eigen 3</th><th>Notes</th></tr>
  184. <tr><td>\code A.lu();\endcode</td>
  185. <td>\code A.fullPivLu();\endcode</td>
  186. <td class="alt">Now A.lu() returns a PartialPivLU</td></tr>
  187. <tr><td>\code A.lu().solve(B,&X);\endcode</td>
  188. <td>\code X = A.lu().solve(B);
  189. X = A.fullPivLu().solve(B);\endcode</td>
  190. <td class="alt">The returned by value is fully optimized</td></tr>
  191. <tr><td>\code A.llt().solve(B,&X);\endcode</td>
  192. <td>\code X = A.llt().solve(B);
  193. X = A.selfadjointView<Lower>.llt().solve(B);
  194. X = A.selfadjointView<Upper>.llt().solve(B);\endcode</td>
  195. <td class="alt">The returned by value is fully optimized and \n
  196. the selfadjointView API allows you to select the \n
  197. triangular part to work on (default is lower part)</td></tr>
  198. <tr><td>\code A.llt().solveInPlace(B);\endcode</td>
  199. <td>\code B = A.llt().solve(B);
  200. B = A.selfadjointView<Lower>.llt().solve(B);
  201. B = A.selfadjointView<Upper>.llt().solve(B);\endcode</td>
  202. <td class="alt">In place solving</td></tr>
  203. <tr><td>\code A.ldlt().solve(B,&X);\endcode</td>
  204. <td>\code X = A.ldlt().solve(B);
  205. X = A.selfadjointView<Lower>.ldlt().solve(B);
  206. X = A.selfadjointView<Upper>.ldlt().solve(B);\endcode</td>
  207. <td class="alt">The returned by value is fully optimized and \n
  208. the selfadjointView API allows you to select the \n
  209. triangular part to work on</td></tr>
  210. </table>
  211. \section GeometryModule Changes in the Geometry module
  212. The Geometry module is the one that changed the most. If you rely heavily on it, it's probably a good idea to use the \ref Eigen2SupportModes "Eigen 2 support modes" to perform your migration.
  213. \section Transform The Transform class
  214. In Eigen 2, the Transform class didn't really know whether it was a projective or affine transformation. In Eigen 3, it takes a new \a Mode template parameter, which indicates whether it's \a Projective or \a Affine transform. There is no default value.
  215. The Transform3f (etc) typedefs are no more. In Eigen 3, the Transform typedefs explicitly refer to the \a Projective and \a Affine modes:
  216. <table class="manual">
  217. <tr><th>Eigen 2</th><th>Eigen 3</th><th>Notes</th></tr>
  218. <tr>
  219. <td> Transform3f </td>
  220. <td> Affine3f or Projective3f </td>
  221. <td> Of course 3f is just an example here </td>
  222. </tr>
  223. </table>
  224. \section LazyVsNoalias Lazy evaluation and noalias
  225. In Eigen all operations are performed in a lazy fashion except the matrix products which are always evaluated into a temporary by default.
  226. In Eigen2, lazy evaluation could be enforced by tagging a product using the .lazy() function. However, in complex expressions it was not
  227. easy to determine where to put the lazy() function. In Eigen3, the lazy() feature has been superseded by the MatrixBase::noalias() function
  228. which can be used on the left hand side of an assignment when no aliasing can occur. Here is an example:
  229. \code
  230. MatrixXf a, b, c;
  231. ...
  232. c.noalias() += 2 * a.transpose() * b;
  233. \endcode
  234. However, the noalias mechanism does not cover all the features of the old .lazy(). Indeed, in some extremely rare cases,
  235. it might be useful to explicit request for a lay product, i.e., for a product which will be evaluated one coefficient at once, on request,
  236. just like any other expressions. To this end you can use the MatrixBase::lazyProduct() function, however we strongly discourage you to
  237. use it unless you are sure of what you are doing, i.e., you have rigourosly measured a speed improvement.
  238. \section AlignMacros Alignment-related macros
  239. The EIGEN_ALIGN_128 macro has been renamed to EIGEN_ALIGN16. Don't be surprised, it's just that we switched to counting in bytes ;-)
  240. The EIGEN_DONT_ALIGN option still exists in Eigen 3, but it has a new cousin: EIGEN_DONT_ALIGN_STATICALLY. It allows to get rid of all static alignment issues while keeping alignment of dynamic-size heap-allocated arrays, thus keeping vectorization for dynamic-size objects.
  241. \section AlignedMap Aligned Map objects
  242. A common issue with Eigen 2 was that when mapping an array with Map, there was no way to tell Eigen that your array was aligned. There was a ForceAligned option but it didn't mean that; it was just confusing and has been removed.
  243. New in Eigen3 is the #Aligned option. See the documentation of class Map. Use it like this:
  244. \code
  245. Map<Vector4f, Aligned> myMappedVector(some_aligned_array);
  246. \endcode
  247. There also are related convenience static methods, which actually are the preferred way as they take care of such things as constness:
  248. \code
  249. result = Vector4f::MapAligned(some_aligned_array);
  250. \endcode
  251. \section StdContainers STL Containers
  252. In Eigen2, #include<Eigen/StdVector> tweaked std::vector to automatically align elements. The problem was that that was quite invasive. In Eigen3, we only override standard behavior if you use Eigen::aligned_allocator<T> as your allocator type. So for example, if you use std::vector<Matrix4f>, you need to do the following change (note that aligned_allocator is under namespace Eigen):
  253. <table class="manual">
  254. <tr><th>Eigen 2</th><th>Eigen 3</th></tr>
  255. <tr>
  256. <td> \code std::vector<Matrix4f> \endcode </td>
  257. <td> \code std::vector<Matrix4f, aligned_allocator<Matrix4f> > \endcode </td>
  258. </tr>
  259. </table>
  260. \section eiPrefix Internal ei_ prefix
  261. In Eigen2, global internal functions and structures were prefixed by \c ei_. In Eigen3, they all have been moved into the more explicit \c internal namespace. So, e.g., \c ei_sqrt(x) now becomes \c internal::sqrt(x). Of course it is not recommended to rely on Eigen's internal features.
  262. */
  263. }