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.

727 lines
26 KiB

  1. namespace Eigen {
  2. /** \eigenManualPage QuickRefPage Quick reference guide
  3. \eigenAutoToc
  4. <hr>
  5. <a href="#" class="top">top</a>
  6. \section QuickRef_Headers Modules and Header files
  7. The Eigen library is divided in a Core module and several additional modules. Each module has a corresponding header file which has to be included in order to use the module. The \c %Dense and \c Eigen header files are provided to conveniently gain access to several modules at once.
  8. <table class="manual">
  9. <tr><th>Module</th><th>Header file</th><th>Contents</th></tr>
  10. <tr><td>\link Core_Module Core \endlink</td><td>\code#include <Eigen/Core>\endcode</td><td>Matrix and Array classes, basic linear algebra (including triangular and selfadjoint products), array manipulation</td></tr>
  11. <tr class="alt"><td>\link Geometry_Module Geometry \endlink</td><td>\code#include <Eigen/Geometry>\endcode</td><td>Transform, Translation, Scaling, Rotation2D and 3D rotations (Quaternion, AngleAxis)</td></tr>
  12. <tr><td>\link LU_Module LU \endlink</td><td>\code#include <Eigen/LU>\endcode</td><td>Inverse, determinant, LU decompositions with solver (FullPivLU, PartialPivLU)</td></tr>
  13. <tr><td>\link Cholesky_Module Cholesky \endlink</td><td>\code#include <Eigen/Cholesky>\endcode</td><td>LLT and LDLT Cholesky factorization with solver</td></tr>
  14. <tr class="alt"><td>\link Householder_Module Householder \endlink</td><td>\code#include <Eigen/Householder>\endcode</td><td>Householder transformations; this module is used by several linear algebra modules</td></tr>
  15. <tr><td>\link SVD_Module SVD \endlink</td><td>\code#include <Eigen/SVD>\endcode</td><td>SVD decomposition with least-squares solver (JacobiSVD)</td></tr>
  16. <tr class="alt"><td>\link QR_Module QR \endlink</td><td>\code#include <Eigen/QR>\endcode</td><td>QR decomposition with solver (HouseholderQR, ColPivHouseholderQR, FullPivHouseholderQR)</td></tr>
  17. <tr><td>\link Eigenvalues_Module Eigenvalues \endlink</td><td>\code#include <Eigen/Eigenvalues>\endcode</td><td>Eigenvalue, eigenvector decompositions (EigenSolver, SelfAdjointEigenSolver, ComplexEigenSolver)</td></tr>
  18. <tr class="alt"><td>\link Sparse_modules Sparse \endlink</td><td>\code#include <Eigen/Sparse>\endcode</td><td>%Sparse matrix storage and related basic linear algebra (SparseMatrix, DynamicSparseMatrix, SparseVector)</td></tr>
  19. <tr><td></td><td>\code#include <Eigen/Dense>\endcode</td><td>Includes Core, Geometry, LU, Cholesky, SVD, QR, and Eigenvalues header files</td></tr>
  20. <tr class="alt"><td></td><td>\code#include <Eigen/Eigen>\endcode</td><td>Includes %Dense and %Sparse header files (the whole Eigen library)</td></tr>
  21. </table>
  22. <a href="#" class="top">top</a>
  23. \section QuickRef_Types Array, matrix and vector types
  24. \b Recall: Eigen provides two kinds of dense objects: mathematical matrices and vectors which are both represented by the template class Matrix, and general 1D and 2D arrays represented by the template class Array:
  25. \code
  26. typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyMatrixType;
  27. typedef Array<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyArrayType;
  28. \endcode
  29. \li \c Scalar is the scalar type of the coefficients (e.g., \c float, \c double, \c bool, \c int, etc.).
  30. \li \c RowsAtCompileTime and \c ColsAtCompileTime are the number of rows and columns of the matrix as known at compile-time or \c Dynamic.
  31. \li \c Options can be \c ColMajor or \c RowMajor, default is \c ColMajor. (see class Matrix for more options)
  32. All combinations are allowed: you can have a matrix with a fixed number of rows and a dynamic number of columns, etc. The following are all valid:
  33. \code
  34. Matrix<double, 6, Dynamic> // Dynamic number of columns (heap allocation)
  35. Matrix<double, Dynamic, 2> // Dynamic number of rows (heap allocation)
  36. Matrix<double, Dynamic, Dynamic, RowMajor> // Fully dynamic, row major (heap allocation)
  37. Matrix<double, 13, 3> // Fully fixed (usually allocated on stack)
  38. \endcode
  39. In most cases, you can simply use one of the convenience typedefs for \ref matrixtypedefs "matrices" and \ref arraytypedefs "arrays". Some examples:
  40. <table class="example">
  41. <tr><th>Matrices</th><th>Arrays</th></tr>
  42. <tr><td>\code
  43. Matrix<float,Dynamic,Dynamic> <=> MatrixXf
  44. Matrix<double,Dynamic,1> <=> VectorXd
  45. Matrix<int,1,Dynamic> <=> RowVectorXi
  46. Matrix<float,3,3> <=> Matrix3f
  47. Matrix<float,4,1> <=> Vector4f
  48. \endcode</td><td>\code
  49. Array<float,Dynamic,Dynamic> <=> ArrayXXf
  50. Array<double,Dynamic,1> <=> ArrayXd
  51. Array<int,1,Dynamic> <=> RowArrayXi
  52. Array<float,3,3> <=> Array33f
  53. Array<float,4,1> <=> Array4f
  54. \endcode</td></tr>
  55. </table>
  56. Conversion between the matrix and array worlds:
  57. \code
  58. Array44f a1, a1;
  59. Matrix4f m1, m2;
  60. m1 = a1 * a2; // coeffwise product, implicit conversion from array to matrix.
  61. a1 = m1 * m2; // matrix product, implicit conversion from matrix to array.
  62. a2 = a1 + m1.array(); // mixing array and matrix is forbidden
  63. m2 = a1.matrix() + m1; // and explicit conversion is required.
  64. ArrayWrapper<Matrix4f> m1a(m1); // m1a is an alias for m1.array(), they share the same coefficients
  65. MatrixWrapper<Array44f> a1m(a1);
  66. \endcode
  67. In the rest of this document we will use the following symbols to emphasize the features which are specifics to a given kind of object:
  68. \li <a name="matrixonly"></a>\matrixworld linear algebra matrix and vector only
  69. \li <a name="arrayonly"></a>\arrayworld array objects only
  70. \subsection QuickRef_Basics Basic matrix manipulation
  71. <table class="manual">
  72. <tr><th></th><th>1D objects</th><th>2D objects</th><th>Notes</th></tr>
  73. <tr><td>Constructors</td>
  74. <td>\code
  75. Vector4d v4;
  76. Vector2f v1(x, y);
  77. Array3i v2(x, y, z);
  78. Vector4d v3(x, y, z, w);
  79. VectorXf v5; // empty object
  80. ArrayXf v6(size);
  81. \endcode</td><td>\code
  82. Matrix4f m1;
  83. MatrixXf m5; // empty object
  84. MatrixXf m6(nb_rows, nb_columns);
  85. \endcode</td><td class="note">
  86. By default, the coefficients \n are left uninitialized</td></tr>
  87. <tr class="alt"><td>Comma initializer</td>
  88. <td>\code
  89. Vector3f v1; v1 << x, y, z;
  90. ArrayXf v2(4); v2 << 1, 2, 3, 4;
  91. \endcode</td><td>\code
  92. Matrix3f m1; m1 << 1, 2, 3,
  93. 4, 5, 6,
  94. 7, 8, 9;
  95. \endcode</td><td></td></tr>
  96. <tr><td>Comma initializer (bis)</td>
  97. <td colspan="2">
  98. \include Tutorial_commainit_02.cpp
  99. </td>
  100. <td>
  101. output:
  102. \verbinclude Tutorial_commainit_02.out
  103. </td>
  104. </tr>
  105. <tr class="alt"><td>Runtime info</td>
  106. <td>\code
  107. vector.size();
  108. vector.innerStride();
  109. vector.data();
  110. \endcode</td><td>\code
  111. matrix.rows(); matrix.cols();
  112. matrix.innerSize(); matrix.outerSize();
  113. matrix.innerStride(); matrix.outerStride();
  114. matrix.data();
  115. \endcode</td><td class="note">Inner/Outer* are storage order dependent</td></tr>
  116. <tr><td>Compile-time info</td>
  117. <td colspan="2">\code
  118. ObjectType::Scalar ObjectType::RowsAtCompileTime
  119. ObjectType::RealScalar ObjectType::ColsAtCompileTime
  120. ObjectType::Index ObjectType::SizeAtCompileTime
  121. \endcode</td><td></td></tr>
  122. <tr class="alt"><td>Resizing</td>
  123. <td>\code
  124. vector.resize(size);
  125. vector.resizeLike(other_vector);
  126. vector.conservativeResize(size);
  127. \endcode</td><td>\code
  128. matrix.resize(nb_rows, nb_cols);
  129. matrix.resize(Eigen::NoChange, nb_cols);
  130. matrix.resize(nb_rows, Eigen::NoChange);
  131. matrix.resizeLike(other_matrix);
  132. matrix.conservativeResize(nb_rows, nb_cols);
  133. \endcode</td><td class="note">no-op if the new sizes match,<br/>otherwise data are lost<br/><br/>resizing with data preservation</td></tr>
  134. <tr><td>Coeff access with \n range checking</td>
  135. <td>\code
  136. vector(i) vector.x()
  137. vector[i] vector.y()
  138. vector.z()
  139. vector.w()
  140. \endcode</td><td>\code
  141. matrix(i,j)
  142. \endcode</td><td class="note">Range checking is disabled if \n NDEBUG or EIGEN_NO_DEBUG is defined</td></tr>
  143. <tr class="alt"><td>Coeff access without \n range checking</td>
  144. <td>\code
  145. vector.coeff(i)
  146. vector.coeffRef(i)
  147. \endcode</td><td>\code
  148. matrix.coeff(i,j)
  149. matrix.coeffRef(i,j)
  150. \endcode</td><td></td></tr>
  151. <tr><td>Assignment/copy</td>
  152. <td colspan="2">\code
  153. object = expression;
  154. object_of_float = expression_of_double.cast<float>();
  155. \endcode</td><td class="note">the destination is automatically resized (if possible)</td></tr>
  156. </table>
  157. \subsection QuickRef_PredefMat Predefined Matrices
  158. <table class="manual">
  159. <tr>
  160. <th>Fixed-size matrix or vector</th>
  161. <th>Dynamic-size matrix</th>
  162. <th>Dynamic-size vector</th>
  163. </tr>
  164. <tr style="border-bottom-style: none;">
  165. <td>
  166. \code
  167. typedef {Matrix3f|Array33f} FixedXD;
  168. FixedXD x;
  169. x = FixedXD::Zero();
  170. x = FixedXD::Ones();
  171. x = FixedXD::Constant(value);
  172. x = FixedXD::Random();
  173. x = FixedXD::LinSpaced(size, low, high);
  174. x.setZero();
  175. x.setOnes();
  176. x.setConstant(value);
  177. x.setRandom();
  178. x.setLinSpaced(size, low, high);
  179. \endcode
  180. </td>
  181. <td>
  182. \code
  183. typedef {MatrixXf|ArrayXXf} Dynamic2D;
  184. Dynamic2D x;
  185. x = Dynamic2D::Zero(rows, cols);
  186. x = Dynamic2D::Ones(rows, cols);
  187. x = Dynamic2D::Constant(rows, cols, value);
  188. x = Dynamic2D::Random(rows, cols);
  189. N/A
  190. x.setZero(rows, cols);
  191. x.setOnes(rows, cols);
  192. x.setConstant(rows, cols, value);
  193. x.setRandom(rows, cols);
  194. N/A
  195. \endcode
  196. </td>
  197. <td>
  198. \code
  199. typedef {VectorXf|ArrayXf} Dynamic1D;
  200. Dynamic1D x;
  201. x = Dynamic1D::Zero(size);
  202. x = Dynamic1D::Ones(size);
  203. x = Dynamic1D::Constant(size, value);
  204. x = Dynamic1D::Random(size);
  205. x = Dynamic1D::LinSpaced(size, low, high);
  206. x.setZero(size);
  207. x.setOnes(size);
  208. x.setConstant(size, value);
  209. x.setRandom(size);
  210. x.setLinSpaced(size, low, high);
  211. \endcode
  212. </td>
  213. </tr>
  214. <tr><td colspan="3">Identity and \link MatrixBase::Unit basis vectors \endlink \matrixworld</td></tr>
  215. <tr style="border-bottom-style: none;">
  216. <td>
  217. \code
  218. x = FixedXD::Identity();
  219. x.setIdentity();
  220. Vector3f::UnitX() // 1 0 0
  221. Vector3f::UnitY() // 0 1 0
  222. Vector3f::UnitZ() // 0 0 1
  223. \endcode
  224. </td>
  225. <td>
  226. \code
  227. x = Dynamic2D::Identity(rows, cols);
  228. x.setIdentity(rows, cols);
  229. N/A
  230. \endcode
  231. </td>
  232. <td>\code
  233. N/A
  234. VectorXf::Unit(size,i)
  235. VectorXf::Unit(4,1) == Vector4f(0,1,0,0)
  236. == Vector4f::UnitY()
  237. \endcode
  238. </td>
  239. </tr>
  240. </table>
  241. \subsection QuickRef_Map Mapping external arrays
  242. <table class="manual">
  243. <tr>
  244. <td>Contiguous \n memory</td>
  245. <td>\code
  246. float data[] = {1,2,3,4};
  247. Map<Vector3f> v1(data); // uses v1 as a Vector3f object
  248. Map<ArrayXf> v2(data,3); // uses v2 as a ArrayXf object
  249. Map<Array22f> m1(data); // uses m1 as a Array22f object
  250. Map<MatrixXf> m2(data,2,2); // uses m2 as a MatrixXf object
  251. \endcode</td>
  252. </tr>
  253. <tr>
  254. <td>Typical usage \n of strides</td>
  255. <td>\code
  256. float data[] = {1,2,3,4,5,6,7,8,9};
  257. Map<VectorXf,0,InnerStride<2> > v1(data,3); // = [1,3,5]
  258. Map<VectorXf,0,InnerStride<> > v2(data,3,InnerStride<>(3)); // = [1,4,7]
  259. Map<MatrixXf,0,OuterStride<3> > m2(data,2,3); // both lines |1,4,7|
  260. Map<MatrixXf,0,OuterStride<> > m1(data,2,3,OuterStride<>(3)); // are equal to: |2,5,8|
  261. \endcode</td>
  262. </tr>
  263. </table>
  264. <a href="#" class="top">top</a>
  265. \section QuickRef_ArithmeticOperators Arithmetic Operators
  266. <table class="manual">
  267. <tr><td>
  268. add \n subtract</td><td>\code
  269. mat3 = mat1 + mat2; mat3 += mat1;
  270. mat3 = mat1 - mat2; mat3 -= mat1;\endcode
  271. </td></tr>
  272. <tr class="alt"><td>
  273. scalar product</td><td>\code
  274. mat3 = mat1 * s1; mat3 *= s1; mat3 = s1 * mat1;
  275. mat3 = mat1 / s1; mat3 /= s1;\endcode
  276. </td></tr>
  277. <tr><td>
  278. matrix/vector \n products \matrixworld</td><td>\code
  279. col2 = mat1 * col1;
  280. row2 = row1 * mat1; row1 *= mat1;
  281. mat3 = mat1 * mat2; mat3 *= mat1; \endcode
  282. </td></tr>
  283. <tr class="alt"><td>
  284. transposition \n adjoint \matrixworld</td><td>\code
  285. mat1 = mat2.transpose(); mat1.transposeInPlace();
  286. mat1 = mat2.adjoint(); mat1.adjointInPlace();
  287. \endcode
  288. </td></tr>
  289. <tr><td>
  290. \link MatrixBase::dot() dot \endlink product \n inner product \matrixworld</td><td>\code
  291. scalar = vec1.dot(vec2);
  292. scalar = col1.adjoint() * col2;
  293. scalar = (col1.adjoint() * col2).value();\endcode
  294. </td></tr>
  295. <tr class="alt"><td>
  296. outer product \matrixworld</td><td>\code
  297. mat = col1 * col2.transpose();\endcode
  298. </td></tr>
  299. <tr><td>
  300. \link MatrixBase::norm() norm \endlink \n \link MatrixBase::normalized() normalization \endlink \matrixworld</td><td>\code
  301. scalar = vec1.norm(); scalar = vec1.squaredNorm()
  302. vec2 = vec1.normalized(); vec1.normalize(); // inplace \endcode
  303. </td></tr>
  304. <tr class="alt"><td>
  305. \link MatrixBase::cross() cross product \endlink \matrixworld</td><td>\code
  306. #include <Eigen/Geometry>
  307. vec3 = vec1.cross(vec2);\endcode</td></tr>
  308. </table>
  309. <a href="#" class="top">top</a>
  310. \section QuickRef_Coeffwise Coefficient-wise \& Array operators
  311. Coefficient-wise operators for matrices and vectors:
  312. <table class="manual">
  313. <tr><th>Matrix API \matrixworld</th><th>Via Array conversions</th></tr>
  314. <tr><td>\code
  315. mat1.cwiseMin(mat2)
  316. mat1.cwiseMax(mat2)
  317. mat1.cwiseAbs2()
  318. mat1.cwiseAbs()
  319. mat1.cwiseSqrt()
  320. mat1.cwiseProduct(mat2)
  321. mat1.cwiseQuotient(mat2)\endcode
  322. </td><td>\code
  323. mat1.array().min(mat2.array())
  324. mat1.array().max(mat2.array())
  325. mat1.array().abs2()
  326. mat1.array().abs()
  327. mat1.array().sqrt()
  328. mat1.array() * mat2.array()
  329. mat1.array() / mat2.array()
  330. \endcode</td></tr>
  331. </table>
  332. It is also very simple to apply any user defined function \c foo using DenseBase::unaryExpr together with std::ptr_fun:
  333. \code mat1.unaryExpr(std::ptr_fun(foo))\endcode
  334. Array operators:\arrayworld
  335. <table class="manual">
  336. <tr><td>Arithmetic operators</td><td>\code
  337. array1 * array2 array1 / array2 array1 *= array2 array1 /= array2
  338. array1 + scalar array1 - scalar array1 += scalar array1 -= scalar
  339. \endcode</td></tr>
  340. <tr><td>Comparisons</td><td>\code
  341. array1 < array2 array1 > array2 array1 < scalar array1 > scalar
  342. array1 <= array2 array1 >= array2 array1 <= scalar array1 >= scalar
  343. array1 == array2 array1 != array2 array1 == scalar array1 != scalar
  344. \endcode</td></tr>
  345. <tr><td>Trigo, power, and \n misc functions \n and the STL variants</td><td>\code
  346. array1.min(array2)
  347. array1.max(array2)
  348. array1.abs2()
  349. array1.abs() abs(array1)
  350. array1.sqrt() sqrt(array1)
  351. array1.log() log(array1)
  352. array1.exp() exp(array1)
  353. array1.pow(exponent) pow(array1,exponent)
  354. array1.square()
  355. array1.cube()
  356. array1.inverse()
  357. array1.sin() sin(array1)
  358. array1.cos() cos(array1)
  359. array1.tan() tan(array1)
  360. array1.asin() asin(array1)
  361. array1.acos() acos(array1)
  362. \endcode
  363. </td></tr>
  364. </table>
  365. <a href="#" class="top">top</a>
  366. \section QuickRef_Reductions Reductions
  367. Eigen provides several reduction methods such as:
  368. \link DenseBase::minCoeff() minCoeff() \endlink, \link DenseBase::maxCoeff() maxCoeff() \endlink,
  369. \link DenseBase::sum() sum() \endlink, \link DenseBase::prod() prod() \endlink,
  370. \link MatrixBase::trace() trace() \endlink \matrixworld,
  371. \link MatrixBase::norm() norm() \endlink \matrixworld, \link MatrixBase::squaredNorm() squaredNorm() \endlink \matrixworld,
  372. \link DenseBase::all() all() \endlink, and \link DenseBase::any() any() \endlink.
  373. All reduction operations can be done matrix-wise,
  374. \link DenseBase::colwise() column-wise \endlink or
  375. \link DenseBase::rowwise() row-wise \endlink. Usage example:
  376. <table class="manual">
  377. <tr><td rowspan="3" style="border-right-style:dashed;vertical-align:middle">\code
  378. 5 3 1
  379. mat = 2 7 8
  380. 9 4 6 \endcode
  381. </td> <td>\code mat.minCoeff(); \endcode</td><td>\code 1 \endcode</td></tr>
  382. <tr class="alt"><td>\code mat.colwise().minCoeff(); \endcode</td><td>\code 2 3 1 \endcode</td></tr>
  383. <tr style="vertical-align:middle"><td>\code mat.rowwise().minCoeff(); \endcode</td><td>\code
  384. 1
  385. 2
  386. 4
  387. \endcode</td></tr>
  388. </table>
  389. Special versions of \link DenseBase::minCoeff(IndexType*,IndexType*) const minCoeff \endlink and \link DenseBase::maxCoeff(IndexType*,IndexType*) const maxCoeff \endlink:
  390. \code
  391. int i, j;
  392. s = vector.minCoeff(&i); // s == vector[i]
  393. s = matrix.maxCoeff(&i, &j); // s == matrix(i,j)
  394. \endcode
  395. Typical use cases of all() and any():
  396. \code
  397. if((array1 > 0).all()) ... // if all coefficients of array1 are greater than 0 ...
  398. if((array1 < array2).any()) ... // if there exist a pair i,j such that array1(i,j) < array2(i,j) ...
  399. \endcode
  400. <a href="#" class="top">top</a>\section QuickRef_Blocks Sub-matrices
  401. Read-write access to a \link DenseBase::col(Index) column \endlink
  402. or a \link DenseBase::row(Index) row \endlink of a matrix (or array):
  403. \code
  404. mat1.row(i) = mat2.col(j);
  405. mat1.col(j1).swap(mat1.col(j2));
  406. \endcode
  407. Read-write access to sub-vectors:
  408. <table class="manual">
  409. <tr>
  410. <th>Default versions</th>
  411. <th>Optimized versions when the size \n is known at compile time</th></tr>
  412. <th></th>
  413. <tr><td>\code vec1.head(n)\endcode</td><td>\code vec1.head<n>()\endcode</td><td>the first \c n coeffs </td></tr>
  414. <tr><td>\code vec1.tail(n)\endcode</td><td>\code vec1.tail<n>()\endcode</td><td>the last \c n coeffs </td></tr>
  415. <tr><td>\code vec1.segment(pos,n)\endcode</td><td>\code vec1.segment<n>(pos)\endcode</td>
  416. <td>the \c n coeffs in the \n range [\c pos : \c pos + \c n - 1]</td></tr>
  417. <tr class="alt"><td colspan="3">
  418. Read-write access to sub-matrices:</td></tr>
  419. <tr>
  420. <td>\code mat1.block(i,j,rows,cols)\endcode
  421. \link DenseBase::block(Index,Index,Index,Index) (more) \endlink</td>
  422. <td>\code mat1.block<rows,cols>(i,j)\endcode
  423. \link DenseBase::block(Index,Index) (more) \endlink</td>
  424. <td>the \c rows x \c cols sub-matrix \n starting from position (\c i,\c j)</td></tr>
  425. <tr><td>\code
  426. mat1.topLeftCorner(rows,cols)
  427. mat1.topRightCorner(rows,cols)
  428. mat1.bottomLeftCorner(rows,cols)
  429. mat1.bottomRightCorner(rows,cols)\endcode
  430. <td>\code
  431. mat1.topLeftCorner<rows,cols>()
  432. mat1.topRightCorner<rows,cols>()
  433. mat1.bottomLeftCorner<rows,cols>()
  434. mat1.bottomRightCorner<rows,cols>()\endcode
  435. <td>the \c rows x \c cols sub-matrix \n taken in one of the four corners</td></tr>
  436. <tr><td>\code
  437. mat1.topRows(rows)
  438. mat1.bottomRows(rows)
  439. mat1.leftCols(cols)
  440. mat1.rightCols(cols)\endcode
  441. <td>\code
  442. mat1.topRows<rows>()
  443. mat1.bottomRows<rows>()
  444. mat1.leftCols<cols>()
  445. mat1.rightCols<cols>()\endcode
  446. <td>specialized versions of block() \n when the block fit two corners</td></tr>
  447. </table>
  448. <a href="#" class="top">top</a>\section QuickRef_Misc Miscellaneous operations
  449. \subsection QuickRef_Reverse Reverse
  450. Vectors, rows, and/or columns of a matrix can be reversed (see DenseBase::reverse(), DenseBase::reverseInPlace(), VectorwiseOp::reverse()).
  451. \code
  452. vec.reverse() mat.colwise().reverse() mat.rowwise().reverse()
  453. vec.reverseInPlace()
  454. \endcode
  455. \subsection QuickRef_Replicate Replicate
  456. Vectors, matrices, rows, and/or columns can be replicated in any direction (see DenseBase::replicate(), VectorwiseOp::replicate())
  457. \code
  458. vec.replicate(times) vec.replicate<Times>
  459. mat.replicate(vertical_times, horizontal_times) mat.replicate<VerticalTimes, HorizontalTimes>()
  460. mat.colwise().replicate(vertical_times, horizontal_times) mat.colwise().replicate<VerticalTimes, HorizontalTimes>()
  461. mat.rowwise().replicate(vertical_times, horizontal_times) mat.rowwise().replicate<VerticalTimes, HorizontalTimes>()
  462. \endcode
  463. <a href="#" class="top">top</a>\section QuickRef_DiagTriSymm Diagonal, Triangular, and Self-adjoint matrices
  464. (matrix world \matrixworld)
  465. \subsection QuickRef_Diagonal Diagonal matrices
  466. <table class="example">
  467. <tr><th>Operation</th><th>Code</th></tr>
  468. <tr><td>
  469. view a vector \link MatrixBase::asDiagonal() as a diagonal matrix \endlink \n </td><td>\code
  470. mat1 = vec1.asDiagonal();\endcode
  471. </td></tr>
  472. <tr><td>
  473. Declare a diagonal matrix</td><td>\code
  474. DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);
  475. diag1.diagonal() = vector;\endcode
  476. </td></tr>
  477. <tr><td>Access the \link MatrixBase::diagonal() diagonal \endlink and \link MatrixBase::diagonal(Index) super/sub diagonals \endlink of a matrix as a vector (read/write)</td>
  478. <td>\code
  479. vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonal
  480. vec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonal
  481. vec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonal
  482. vec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonal
  483. vec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal
  484. \endcode</td>
  485. </tr>
  486. <tr><td>Optimized products and inverse</td>
  487. <td>\code
  488. mat3 = scalar * diag1 * mat1;
  489. mat3 += scalar * mat1 * vec1.asDiagonal();
  490. mat3 = vec1.asDiagonal().inverse() * mat1
  491. mat3 = mat1 * diag1.inverse()
  492. \endcode</td>
  493. </tr>
  494. </table>
  495. \subsection QuickRef_TriangularView Triangular views
  496. TriangularView gives a view on a triangular part of a dense matrix and allows to perform optimized operations on it. The opposite triangular part is never referenced and can be used to store other information.
  497. \note The .triangularView() template member function requires the \c template keyword if it is used on an
  498. object of a type that depends on a template parameter; see \ref TopicTemplateKeyword for details.
  499. <table class="example">
  500. <tr><th>Operation</th><th>Code</th></tr>
  501. <tr><td>
  502. Reference to a triangular with optional \n
  503. unit or null diagonal (read/write):
  504. </td><td>\code
  505. m.triangularView<Xxx>()
  506. \endcode \n
  507. \c Xxx = ::Upper, ::Lower, ::StrictlyUpper, ::StrictlyLower, ::UnitUpper, ::UnitLower
  508. </td></tr>
  509. <tr><td>
  510. Writing to a specific triangular part:\n (only the referenced triangular part is evaluated)
  511. </td><td>\code
  512. m1.triangularView<Eigen::Lower>() = m2 + m3 \endcode
  513. </td></tr>
  514. <tr><td>
  515. Conversion to a dense matrix setting the opposite triangular part to zero:
  516. </td><td>\code
  517. m2 = m1.triangularView<Eigen::UnitUpper>()\endcode
  518. </td></tr>
  519. <tr><td>
  520. Products:
  521. </td><td>\code
  522. m3 += s1 * m1.adjoint().triangularView<Eigen::UnitUpper>() * m2
  523. m3 -= s1 * m2.conjugate() * m1.adjoint().triangularView<Eigen::Lower>() \endcode
  524. </td></tr>
  525. <tr><td>
  526. Solving linear equations:\n
  527. \f$ M_2 := L_1^{-1} M_2 \f$ \n
  528. \f$ M_3 := {L_1^*}^{-1} M_3 \f$ \n
  529. \f$ M_4 := M_4 U_1^{-1} \f$
  530. </td><td>\n \code
  531. L1.triangularView<Eigen::UnitLower>().solveInPlace(M2)
  532. L1.triangularView<Eigen::Lower>().adjoint().solveInPlace(M3)
  533. U1.triangularView<Eigen::Upper>().solveInPlace<OnTheRight>(M4)\endcode
  534. </td></tr>
  535. </table>
  536. \subsection QuickRef_SelfadjointMatrix Symmetric/selfadjoint views
  537. Just as for triangular matrix, you can reference any triangular part of a square matrix to see it as a selfadjoint
  538. matrix and perform special and optimized operations. Again the opposite triangular part is never referenced and can be
  539. used to store other information.
  540. \note The .selfadjointView() template member function requires the \c template keyword if it is used on an
  541. object of a type that depends on a template parameter; see \ref TopicTemplateKeyword for details.
  542. <table class="example">
  543. <tr><th>Operation</th><th>Code</th></tr>
  544. <tr><td>
  545. Conversion to a dense matrix:
  546. </td><td>\code
  547. m2 = m.selfadjointView<Eigen::Lower>();\endcode
  548. </td></tr>
  549. <tr><td>
  550. Product with another general matrix or vector:
  551. </td><td>\code
  552. m3 = s1 * m1.conjugate().selfadjointView<Eigen::Upper>() * m3;
  553. m3 -= s1 * m3.adjoint() * m1.selfadjointView<Eigen::Lower>();\endcode
  554. </td></tr>
  555. <tr><td>
  556. Rank 1 and rank K update: \n
  557. \f$ upper(M_1) \mathrel{{+}{=}} s_1 M_2 M_2^* \f$ \n
  558. \f$ lower(M_1) \mathbin{{-}{=}} M_2^* M_2 \f$
  559. </td><td>\n \code
  560. M1.selfadjointView<Eigen::Upper>().rankUpdate(M2,s1);
  561. M1.selfadjointView<Eigen::Lower>().rankUpdate(M2.adjoint(),-1); \endcode
  562. </td></tr>
  563. <tr><td>
  564. Rank 2 update: (\f$ M \mathrel{{+}{=}} s u v^* + s v u^* \f$)
  565. </td><td>\code
  566. M.selfadjointView<Eigen::Upper>().rankUpdate(u,v,s);
  567. \endcode
  568. </td></tr>
  569. <tr><td>
  570. Solving linear equations:\n(\f$ M_2 := M_1^{-1} M_2 \f$)
  571. </td><td>\code
  572. // via a standard Cholesky factorization
  573. m2 = m1.selfadjointView<Eigen::Upper>().llt().solve(m2);
  574. // via a Cholesky factorization with pivoting
  575. m2 = m1.selfadjointView<Eigen::Lower>().ldlt().solve(m2);
  576. \endcode
  577. </td></tr>
  578. </table>
  579. */
  580. /*
  581. <table class="tutorial_code">
  582. <tr><td>
  583. \link MatrixBase::asDiagonal() make a diagonal matrix \endlink \n from a vector </td><td>\code
  584. mat1 = vec1.asDiagonal();\endcode
  585. </td></tr>
  586. <tr><td>
  587. Declare a diagonal matrix</td><td>\code
  588. DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);
  589. diag1.diagonal() = vector;\endcode
  590. </td></tr>
  591. <tr><td>Access \link MatrixBase::diagonal() the diagonal and super/sub diagonals of a matrix \endlink as a vector (read/write)</td>
  592. <td>\code
  593. vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonal
  594. vec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonal
  595. vec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonal
  596. vec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonal
  597. vec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal
  598. \endcode</td>
  599. </tr>
  600. <tr><td>View on a triangular part of a matrix (read/write)</td>
  601. <td>\code
  602. mat2 = mat1.triangularView<Xxx>();
  603. // Xxx = Upper, Lower, StrictlyUpper, StrictlyLower, UnitUpper, UnitLower
  604. mat1.triangularView<Upper>() = mat2 + mat3; // only the upper part is evaluated and referenced
  605. \endcode</td></tr>
  606. <tr><td>View a triangular part as a symmetric/self-adjoint matrix (read/write)</td>
  607. <td>\code
  608. mat2 = mat1.selfadjointView<Xxx>(); // Xxx = Upper or Lower
  609. mat1.selfadjointView<Upper>() = mat2 + mat2.adjoint(); // evaluated and write to the upper triangular part only
  610. \endcode</td></tr>
  611. </table>
  612. Optimized products:
  613. \code
  614. mat3 += scalar * vec1.asDiagonal() * mat1
  615. mat3 += scalar * mat1 * vec1.asDiagonal()
  616. mat3.noalias() += scalar * mat1.triangularView<Xxx>() * mat2
  617. mat3.noalias() += scalar * mat2 * mat1.triangularView<Xxx>()
  618. mat3.noalias() += scalar * mat1.selfadjointView<Upper or Lower>() * mat2
  619. mat3.noalias() += scalar * mat2 * mat1.selfadjointView<Upper or Lower>()
  620. mat1.selfadjointView<Upper or Lower>().rankUpdate(mat2);
  621. mat1.selfadjointView<Upper or Lower>().rankUpdate(mat2.adjoint(), scalar);
  622. \endcode
  623. Inverse products: (all are optimized)
  624. \code
  625. mat3 = vec1.asDiagonal().inverse() * mat1
  626. mat3 = mat1 * diag1.inverse()
  627. mat1.triangularView<Xxx>().solveInPlace(mat2)
  628. mat1.triangularView<Xxx>().solveInPlace<OnTheRight>(mat2)
  629. mat2 = mat1.selfadjointView<Upper or Lower>().llt().solve(mat2)
  630. \endcode
  631. */
  632. }