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.

96 lines
4.1 KiB

  1. namespace Eigen {
  2. /** \page TutorialMapClass Tutorial page 10 - Interfacing with C/C++ arrays and external libraries: the %Map class
  3. \ingroup Tutorial
  4. \li \b Previous: \ref TutorialSparse
  5. \li \b Next: \ref TODO
  6. This tutorial page explains how to work with "raw" C++ arrays. This can be useful in a variety of contexts, particularly when "importing" vectors and matrices from other libraries into Eigen.
  7. \b Table \b of \b contents
  8. - \ref TutorialMapIntroduction
  9. - \ref TutorialMapTypes
  10. - \ref TutorialMapUsing
  11. - \ref TutorialMapPlacementNew
  12. \section TutorialMapIntroduction Introduction
  13. Occasionally you may have a pre-defined array of numbers that you want to use within Eigen as a vector or matrix. While one option is to make a copy of the data, most commonly you probably want to re-use this memory as an Eigen type. Fortunately, this is very easy with the Map class.
  14. \section TutorialMapTypes Map types and declaring Map variables
  15. A Map object has a type defined by its Eigen equivalent:
  16. \code
  17. Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >
  18. \endcode
  19. Note that, in this default case, a Map requires just a single template parameter.
  20. To construct a Map variable, you need two other pieces of information: a pointer to the region of memory defining the array of coefficients, and the desired shape of the matrix or vector. For example, to define a matrix of \c float with sizes determined at compile time, you might do the following:
  21. \code
  22. Map<MatrixXf> mf(pf,rows,columns);
  23. \endcode
  24. where \c pf is a \c float \c * pointing to the array of memory. A fixed-size read-only vector of integers might be declared as
  25. \code
  26. Map<const Vector4i> mi(pi);
  27. \endcode
  28. where \c pi is an \c int \c *. In this case the size does not have to be passed to the constructor, because it is already specified by the Matrix/Array type.
  29. Note that Map does not have a default constructor; you \em must pass a pointer to intialize the object. However, you can work around this requirement (see \ref TutorialMapPlacementNew).
  30. Map is flexible enough to accomodate a variety of different data representations. There are two other (optional) template parameters:
  31. \code
  32. Map<typename MatrixType,
  33. int MapOptions,
  34. typename StrideType>
  35. \endcode
  36. \li \c MapOptions specifies whether the pointer is \c #Aligned, or \c #Unaligned. The default is \c #Unaligned.
  37. \li \c StrideType allows you to specify a custom layout for the memory array, using the Stride class. One example would be to specify that the data array is organized in row-major format:
  38. <table class="example">
  39. <tr><th>Example:</th><th>Output:</th></tr>
  40. <tr>
  41. <td>\include Tutorial_Map_rowmajor.cpp </td>
  42. <td>\verbinclude Tutorial_Map_rowmajor.out </td>
  43. </table>
  44. However, Stride is even more flexible than this; for details, see the documentation for the Map and Stride classes.
  45. \section TutorialMapUsing Using Map variables
  46. You can use a Map object just like any other Eigen type:
  47. <table class="example">
  48. <tr><th>Example:</th><th>Output:</th></tr>
  49. <tr>
  50. <td>\include Tutorial_Map_using.cpp </td>
  51. <td>\verbinclude Tutorial_Map_using.out </td>
  52. </table>
  53. However, when writing functions taking Eigen types, it is important to realize that a Map type is \em not identical to its Dense equivalent. See \ref TopicFunctionTakingEigenTypesMultiarguments for details.
  54. \section TutorialMapPlacementNew Changing the mapped array
  55. It is possible to change the array of a Map object after declaration, using the C++ "placement new" syntax:
  56. <table class="example">
  57. <tr><th>Example:</th><th>Output:</th></tr>
  58. <tr>
  59. <td>\include Map_placement_new.cpp </td>
  60. <td>\verbinclude Map_placement_new.out </td>
  61. </table>
  62. Despite appearances, this does not invoke the memory allocator, because the syntax specifies the location for storing the result.
  63. This syntax makes it possible to declare a Map object without first knowing the mapped array's location in memory:
  64. \code
  65. Map<Matrix3f> A(NULL); // don't try to use this matrix yet!
  66. VectorXf b(n_matrices);
  67. for (int i = 0; i < n_matrices; i++)
  68. {
  69. new (&A) Map<Matrix3f>(get_matrix_pointer(i));
  70. b(i) = A.trace();
  71. }
  72. \endcode
  73. \li \b Next: \ref TODO
  74. */
  75. }