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.

785 lines
28 KiB

  1. namespace StormEigen {
  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 class="alt"><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 ><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 class="alt"><td>\link SVD_Module SVD \endlink</td><td>\code#include <Eigen/SVD>\endcode</td><td>SVD decompositions with least-squares solver (JacobiSVD, BDCSVD)</td></tr>
  16. <tr ><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 class="alt"><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 ><td>\link Sparse_modules Sparse \endlink</td><td>\code#include <Eigen/Sparse>\endcode</td><td>%Sparse matrix storage and related basic linear algebra (SparseMatrix, SparseVector) \n (see \ref SparseQuickRefPage for details on sparse modules)</td></tr>
  19. <tr class="alt"><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 ><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(StormEigen::NoChange, nb_cols);
  130. matrix.resize(nb_rows, StormEigen::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. In addition to the aforementioned operators, Eigen supports numerous coefficient-wise operator and functions.
  312. Most of them unambiguously makes sense in array-world\arrayworld. The following operators are readily available for arrays,
  313. or available through .array() for vectors and matrices:
  314. <table class="manual">
  315. <tr><td>Arithmetic operators</td><td>\code
  316. array1 * array2 array1 / array2 array1 *= array2 array1 /= array2
  317. array1 + scalar array1 - scalar array1 += scalar array1 -= scalar
  318. \endcode</td></tr>
  319. <tr><td>Comparisons</td><td>\code
  320. array1 < array2 array1 > array2 array1 < scalar array1 > scalar
  321. array1 <= array2 array1 >= array2 array1 <= scalar array1 >= scalar
  322. array1 == array2 array1 != array2 array1 == scalar array1 != scalar
  323. array1.min(array2) array1.max(array2) array1.min(scalar) array1.max(scalar)
  324. \endcode</td></tr>
  325. <tr><td>Trigo, power, and \n misc functions \n and the STL-like variants</td><td>\code
  326. array1.abs2()
  327. array1.abs() abs(array1)
  328. array1.sqrt() sqrt(array1)
  329. array1.log() log(array1)
  330. array1.log10() log10(array1)
  331. array1.exp() exp(array1)
  332. array1.pow(array2) pow(array1,array2)
  333. array1.pow(scalar) pow(array1,scalar)
  334. pow(scalar,array2)
  335. array1.square()
  336. array1.cube()
  337. array1.inverse()
  338. array1.sin() sin(array1)
  339. array1.cos() cos(array1)
  340. array1.tan() tan(array1)
  341. array1.asin() asin(array1)
  342. array1.acos() acos(array1)
  343. array1.atan() atan(array1)
  344. array1.sinh() sinh(array1)
  345. array1.cosh() cosh(array1)
  346. array1.tanh() tanh(array1)
  347. array1.arg() arg(array1)
  348. array1.floor() floor(array1)
  349. array1.ceil() ceil(array1)
  350. array1.round() round(aray1)
  351. array1.isFinite() isfinite(array1)
  352. array1.isInf() isinf(array1)
  353. array1.isNaN() isnan(array1)
  354. \endcode
  355. </td></tr>
  356. </table>
  357. The following coefficient-wise operators are available for all kind of expressions (matrices, vectors, and arrays), and for both real or complex scalar types:
  358. <table class="manual">
  359. <tr><th>Eigen's API</th><th>STL-like APIs\arrayworld </th><th>Comments</th></tr>
  360. <tr><td>\code
  361. mat1.real()
  362. mat1.imag()
  363. mat1.conjugate()
  364. \endcode
  365. </td><td>\code
  366. real(array1)
  367. imag(array1)
  368. conj(array1)
  369. \endcode
  370. </td><td>
  371. \code
  372. // read-write, no-op for real expressions
  373. // read-only for real, read-write for complexes
  374. // no-op for real expressions
  375. \endcode
  376. </td></tr>
  377. </table>
  378. Some coefficient-wise operators are readily available for for matrices and vectors through the following cwise* methods:
  379. <table class="manual">
  380. <tr><th>Matrix API \matrixworld</th><th>Via Array conversions</th></tr>
  381. <tr><td>\code
  382. mat1.cwiseMin(mat2) mat1.cwiseMin(scalar)
  383. mat1.cwiseMax(mat2) mat1.cwiseMax(scalar)
  384. mat1.cwiseAbs2()
  385. mat1.cwiseAbs()
  386. mat1.cwiseSqrt()
  387. mat1.cwiseInverse()
  388. mat1.cwiseProduct(mat2)
  389. mat1.cwiseQuotient(mat2)
  390. mat1.cwiseEqual(mat2) mat1.cwiseEqual(scalar)
  391. mat1.cwiseNotEqual(mat2)
  392. \endcode
  393. </td><td>\code
  394. mat1.array().min(mat2.array()) mat1.array().min(scalar)
  395. mat1.array().max(mat2.array()) mat1.array().max(scalar)
  396. mat1.array().abs2()
  397. mat1.array().abs()
  398. mat1.array().sqrt()
  399. mat1.array().inverse()
  400. mat1.array() * mat2.array()
  401. mat1.array() / mat2.array()
  402. mat1.array() == mat2.array() mat1.array() == scalar
  403. mat1.array() != mat2.array()
  404. \endcode</td></tr>
  405. </table>
  406. The main difference between the two API is that the one based on cwise* methods returns an expression in the matrix world,
  407. while the second one (based on .array()) returns an array expression.
  408. Recall that .array() has no cost, it only changes the available API and interpretation of the data.
  409. It is also very simple to apply any user defined function \c foo using DenseBase::unaryExpr together with <a href="http://en.cppreference.com/w/cpp/utility/functional/ptr_fun">std::ptr_fun</a> (c++03), <a href="http://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> (c++11), or <a href="http://en.cppreference.com/w/cpp/language/lambda">lambdas</a> (c++11):
  410. \code
  411. mat1.unaryExpr(std::ptr_fun(foo));
  412. mat1.unaryExpr(std::ref(foo));
  413. mat1.unaryExpr([](double x) { return foo(x); });
  414. \endcode
  415. <a href="#" class="top">top</a>
  416. \section QuickRef_Reductions Reductions
  417. Eigen provides several reduction methods such as:
  418. \link DenseBase::minCoeff() minCoeff() \endlink, \link DenseBase::maxCoeff() maxCoeff() \endlink,
  419. \link DenseBase::sum() sum() \endlink, \link DenseBase::prod() prod() \endlink,
  420. \link MatrixBase::trace() trace() \endlink \matrixworld,
  421. \link MatrixBase::norm() norm() \endlink \matrixworld, \link MatrixBase::squaredNorm() squaredNorm() \endlink \matrixworld,
  422. \link DenseBase::all() all() \endlink, and \link DenseBase::any() any() \endlink.
  423. All reduction operations can be done matrix-wise,
  424. \link DenseBase::colwise() column-wise \endlink or
  425. \link DenseBase::rowwise() row-wise \endlink. Usage example:
  426. <table class="manual">
  427. <tr><td rowspan="3" style="border-right-style:dashed;vertical-align:middle">\code
  428. 5 3 1
  429. mat = 2 7 8
  430. 9 4 6 \endcode
  431. </td> <td>\code mat.minCoeff(); \endcode</td><td>\code 1 \endcode</td></tr>
  432. <tr class="alt"><td>\code mat.colwise().minCoeff(); \endcode</td><td>\code 2 3 1 \endcode</td></tr>
  433. <tr style="vertical-align:middle"><td>\code mat.rowwise().minCoeff(); \endcode</td><td>\code
  434. 1
  435. 2
  436. 4
  437. \endcode</td></tr>
  438. </table>
  439. Special versions of \link DenseBase::minCoeff(IndexType*,IndexType*) const minCoeff \endlink and \link DenseBase::maxCoeff(IndexType*,IndexType*) const maxCoeff \endlink:
  440. \code
  441. int i, j;
  442. s = vector.minCoeff(&i); // s == vector[i]
  443. s = matrix.maxCoeff(&i, &j); // s == matrix(i,j)
  444. \endcode
  445. Typical use cases of all() and any():
  446. \code
  447. if((array1 > 0).all()) ... // if all coefficients of array1 are greater than 0 ...
  448. if((array1 < array2).any()) ... // if there exist a pair i,j such that array1(i,j) < array2(i,j) ...
  449. \endcode
  450. <a href="#" class="top">top</a>\section QuickRef_Blocks Sub-matrices
  451. Read-write access to a \link DenseBase::col(Index) column \endlink
  452. or a \link DenseBase::row(Index) row \endlink of a matrix (or array):
  453. \code
  454. mat1.row(i) = mat2.col(j);
  455. mat1.col(j1).swap(mat1.col(j2));
  456. \endcode
  457. Read-write access to sub-vectors:
  458. <table class="manual">
  459. <tr>
  460. <th>Default versions</th>
  461. <th>Optimized versions when the size \n is known at compile time</th></tr>
  462. <th></th>
  463. <tr><td>\code vec1.head(n)\endcode</td><td>\code vec1.head<n>()\endcode</td><td>the first \c n coeffs </td></tr>
  464. <tr><td>\code vec1.tail(n)\endcode</td><td>\code vec1.tail<n>()\endcode</td><td>the last \c n coeffs </td></tr>
  465. <tr><td>\code vec1.segment(pos,n)\endcode</td><td>\code vec1.segment<n>(pos)\endcode</td>
  466. <td>the \c n coeffs in the \n range [\c pos : \c pos + \c n - 1]</td></tr>
  467. <tr class="alt"><td colspan="3">
  468. Read-write access to sub-matrices:</td></tr>
  469. <tr>
  470. <td>\code mat1.block(i,j,rows,cols)\endcode
  471. \link DenseBase::block(Index,Index,Index,Index) (more) \endlink</td>
  472. <td>\code mat1.block<rows,cols>(i,j)\endcode
  473. \link DenseBase::block(Index,Index) (more) \endlink</td>
  474. <td>the \c rows x \c cols sub-matrix \n starting from position (\c i,\c j)</td></tr>
  475. <tr><td>\code
  476. mat1.topLeftCorner(rows,cols)
  477. mat1.topRightCorner(rows,cols)
  478. mat1.bottomLeftCorner(rows,cols)
  479. mat1.bottomRightCorner(rows,cols)\endcode
  480. <td>\code
  481. mat1.topLeftCorner<rows,cols>()
  482. mat1.topRightCorner<rows,cols>()
  483. mat1.bottomLeftCorner<rows,cols>()
  484. mat1.bottomRightCorner<rows,cols>()\endcode
  485. <td>the \c rows x \c cols sub-matrix \n taken in one of the four corners</td></tr>
  486. <tr><td>\code
  487. mat1.topRows(rows)
  488. mat1.bottomRows(rows)
  489. mat1.leftCols(cols)
  490. mat1.rightCols(cols)\endcode
  491. <td>\code
  492. mat1.topRows<rows>()
  493. mat1.bottomRows<rows>()
  494. mat1.leftCols<cols>()
  495. mat1.rightCols<cols>()\endcode
  496. <td>specialized versions of block() \n when the block fit two corners</td></tr>
  497. </table>
  498. <a href="#" class="top">top</a>\section QuickRef_Misc Miscellaneous operations
  499. \subsection QuickRef_Reverse Reverse
  500. Vectors, rows, and/or columns of a matrix can be reversed (see DenseBase::reverse(), DenseBase::reverseInPlace(), VectorwiseOp::reverse()).
  501. \code
  502. vec.reverse() mat.colwise().reverse() mat.rowwise().reverse()
  503. vec.reverseInPlace()
  504. \endcode
  505. \subsection QuickRef_Replicate Replicate
  506. Vectors, matrices, rows, and/or columns can be replicated in any direction (see DenseBase::replicate(), VectorwiseOp::replicate())
  507. \code
  508. vec.replicate(times) vec.replicate<Times>
  509. mat.replicate(vertical_times, horizontal_times) mat.replicate<VerticalTimes, HorizontalTimes>()
  510. mat.colwise().replicate(vertical_times, horizontal_times) mat.colwise().replicate<VerticalTimes, HorizontalTimes>()
  511. mat.rowwise().replicate(vertical_times, horizontal_times) mat.rowwise().replicate<VerticalTimes, HorizontalTimes>()
  512. \endcode
  513. <a href="#" class="top">top</a>\section QuickRef_DiagTriSymm Diagonal, Triangular, and Self-adjoint matrices
  514. (matrix world \matrixworld)
  515. \subsection QuickRef_Diagonal Diagonal matrices
  516. <table class="example">
  517. <tr><th>Operation</th><th>Code</th></tr>
  518. <tr><td>
  519. view a vector \link MatrixBase::asDiagonal() as a diagonal matrix \endlink \n </td><td>\code
  520. mat1 = vec1.asDiagonal();\endcode
  521. </td></tr>
  522. <tr><td>
  523. Declare a diagonal matrix</td><td>\code
  524. DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);
  525. diag1.diagonal() = vector;\endcode
  526. </td></tr>
  527. <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>
  528. <td>\code
  529. vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonal
  530. vec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonal
  531. vec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonal
  532. vec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonal
  533. vec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal
  534. \endcode</td>
  535. </tr>
  536. <tr><td>Optimized products and inverse</td>
  537. <td>\code
  538. mat3 = scalar * diag1 * mat1;
  539. mat3 += scalar * mat1 * vec1.asDiagonal();
  540. mat3 = vec1.asDiagonal().inverse() * mat1
  541. mat3 = mat1 * diag1.inverse()
  542. \endcode</td>
  543. </tr>
  544. </table>
  545. \subsection QuickRef_TriangularView Triangular views
  546. 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.
  547. \note The .triangularView() template member function requires the \c template keyword if it is used on an
  548. object of a type that depends on a template parameter; see \ref TopicTemplateKeyword for details.
  549. <table class="example">
  550. <tr><th>Operation</th><th>Code</th></tr>
  551. <tr><td>
  552. Reference to a triangular with optional \n
  553. unit or null diagonal (read/write):
  554. </td><td>\code
  555. m.triangularView<Xxx>()
  556. \endcode \n
  557. \c Xxx = ::Upper, ::Lower, ::StrictlyUpper, ::StrictlyLower, ::UnitUpper, ::UnitLower
  558. </td></tr>
  559. <tr><td>
  560. Writing to a specific triangular part:\n (only the referenced triangular part is evaluated)
  561. </td><td>\code
  562. m1.triangularView<StormEigen::Lower>() = m2 + m3 \endcode
  563. </td></tr>
  564. <tr><td>
  565. Conversion to a dense matrix setting the opposite triangular part to zero:
  566. </td><td>\code
  567. m2 = m1.triangularView<StormEigen::UnitUpper>()\endcode
  568. </td></tr>
  569. <tr><td>
  570. Products:
  571. </td><td>\code
  572. m3 += s1 * m1.adjoint().triangularView<StormEigen::UnitUpper>() * m2
  573. m3 -= s1 * m2.conjugate() * m1.adjoint().triangularView<StormEigen::Lower>() \endcode
  574. </td></tr>
  575. <tr><td>
  576. Solving linear equations:\n
  577. \f$ M_2 := L_1^{-1} M_2 \f$ \n
  578. \f$ M_3 := {L_1^*}^{-1} M_3 \f$ \n
  579. \f$ M_4 := M_4 U_1^{-1} \f$
  580. </td><td>\n \code
  581. L1.triangularView<StormEigen::UnitLower>().solveInPlace(M2)
  582. L1.triangularView<StormEigen::Lower>().adjoint().solveInPlace(M3)
  583. U1.triangularView<StormEigen::Upper>().solveInPlace<OnTheRight>(M4)\endcode
  584. </td></tr>
  585. </table>
  586. \subsection QuickRef_SelfadjointMatrix Symmetric/selfadjoint views
  587. Just as for triangular matrix, you can reference any triangular part of a square matrix to see it as a selfadjoint
  588. matrix and perform special and optimized operations. Again the opposite triangular part is never referenced and can be
  589. used to store other information.
  590. \note The .selfadjointView() template member function requires the \c template keyword if it is used on an
  591. object of a type that depends on a template parameter; see \ref TopicTemplateKeyword for details.
  592. <table class="example">
  593. <tr><th>Operation</th><th>Code</th></tr>
  594. <tr><td>
  595. Conversion to a dense matrix:
  596. </td><td>\code
  597. m2 = m.selfadjointView<StormEigen::Lower>();\endcode
  598. </td></tr>
  599. <tr><td>
  600. Product with another general matrix or vector:
  601. </td><td>\code
  602. m3 = s1 * m1.conjugate().selfadjointView<StormEigen::Upper>() * m3;
  603. m3 -= s1 * m3.adjoint() * m1.selfadjointView<StormEigen::Lower>();\endcode
  604. </td></tr>
  605. <tr><td>
  606. Rank 1 and rank K update: \n
  607. \f$ upper(M_1) \mathrel{{+}{=}} s_1 M_2 M_2^* \f$ \n
  608. \f$ lower(M_1) \mathbin{{-}{=}} M_2^* M_2 \f$
  609. </td><td>\n \code
  610. M1.selfadjointView<StormEigen::Upper>().rankUpdate(M2,s1);
  611. M1.selfadjointView<StormEigen::Lower>().rankUpdate(M2.adjoint(),-1); \endcode
  612. </td></tr>
  613. <tr><td>
  614. Rank 2 update: (\f$ M \mathrel{{+}{=}} s u v^* + s v u^* \f$)
  615. </td><td>\code
  616. M.selfadjointView<StormEigen::Upper>().rankUpdate(u,v,s);
  617. \endcode
  618. </td></tr>
  619. <tr><td>
  620. Solving linear equations:\n(\f$ M_2 := M_1^{-1} M_2 \f$)
  621. </td><td>\code
  622. // via a standard Cholesky factorization
  623. m2 = m1.selfadjointView<StormEigen::Upper>().llt().solve(m2);
  624. // via a Cholesky factorization with pivoting
  625. m2 = m1.selfadjointView<StormEigen::Lower>().ldlt().solve(m2);
  626. \endcode
  627. </td></tr>
  628. </table>
  629. */
  630. /*
  631. <table class="tutorial_code">
  632. <tr><td>
  633. \link MatrixBase::asDiagonal() make a diagonal matrix \endlink \n from a vector </td><td>\code
  634. mat1 = vec1.asDiagonal();\endcode
  635. </td></tr>
  636. <tr><td>
  637. Declare a diagonal matrix</td><td>\code
  638. DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);
  639. diag1.diagonal() = vector;\endcode
  640. </td></tr>
  641. <tr><td>Access \link MatrixBase::diagonal() the diagonal and super/sub diagonals of a matrix \endlink as a vector (read/write)</td>
  642. <td>\code
  643. vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonal
  644. vec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonal
  645. vec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonal
  646. vec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonal
  647. vec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal
  648. \endcode</td>
  649. </tr>
  650. <tr><td>View on a triangular part of a matrix (read/write)</td>
  651. <td>\code
  652. mat2 = mat1.triangularView<Xxx>();
  653. // Xxx = Upper, Lower, StrictlyUpper, StrictlyLower, UnitUpper, UnitLower
  654. mat1.triangularView<Upper>() = mat2 + mat3; // only the upper part is evaluated and referenced
  655. \endcode</td></tr>
  656. <tr><td>View a triangular part as a symmetric/self-adjoint matrix (read/write)</td>
  657. <td>\code
  658. mat2 = mat1.selfadjointView<Xxx>(); // Xxx = Upper or Lower
  659. mat1.selfadjointView<Upper>() = mat2 + mat2.adjoint(); // evaluated and write to the upper triangular part only
  660. \endcode</td></tr>
  661. </table>
  662. Optimized products:
  663. \code
  664. mat3 += scalar * vec1.asDiagonal() * mat1
  665. mat3 += scalar * mat1 * vec1.asDiagonal()
  666. mat3.noalias() += scalar * mat1.triangularView<Xxx>() * mat2
  667. mat3.noalias() += scalar * mat2 * mat1.triangularView<Xxx>()
  668. mat3.noalias() += scalar * mat1.selfadjointView<Upper or Lower>() * mat2
  669. mat3.noalias() += scalar * mat2 * mat1.selfadjointView<Upper or Lower>()
  670. mat1.selfadjointView<Upper or Lower>().rankUpdate(mat2);
  671. mat1.selfadjointView<Upper or Lower>().rankUpdate(mat2.adjoint(), scalar);
  672. \endcode
  673. Inverse products: (all are optimized)
  674. \code
  675. mat3 = vec1.asDiagonal().inverse() * mat1
  676. mat3 = mat1 * diag1.inverse()
  677. mat1.triangularView<Xxx>().solveInPlace(mat2)
  678. mat1.triangularView<Xxx>().solveInPlace<OnTheRight>(mat2)
  679. mat2 = mat1.selfadjointView<Upper or Lower>().llt().solve(mat2)
  680. \endcode
  681. */
  682. }