namespace Eigen { /** \page TutorialMatrixArithmetic Tutorial - Matrix and vector arithmetic \ingroup Tutorial This tutorial aims to provide an overview and some details on how to perform arithmetic between matrices, vectors and scalars with Eigen. \b Table \b of \b contents - \ref TutorialMatrixArithmCommaInitializer - \ref TutorialMatrixArithmElementaryOperations - \ref TutorialMatrixArithmExamples - \ref TutorialMatrixArithmProduct - \ref TutorialMatrixArithmSimpleExample - \ref TutorialMatrixCombiningOperators - \ref TutorialMatrixOperatorValidity - \ref TutorialMatrixArithmReductionOperations \section TutorialMatrixArithmCommaInitializer Comma initializer Eigen offers a comma initializer syntax which allows to set all the coefficients of any dense objects (matrix, vector, array, block, etc.) to specific values:
\code Matrix3f m; m << 1, 2, 3, 4, 5, 6, 7, 8, 9; cout << m; \endcode | output: \code 1 2 3 4 5 6 7 8 9 \endcode |
\code int rows=5, cols=5; MatrixXf m(rows,cols); m << (Matrix3f() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished(), MatrixXf::Zero(3,cols-3), MatrixXf::Zero(rows-3,3), MatrixXf::Identity(rows-3,cols-3); cout << m; \endcode | output: \code 1 2 3 0 0 4 5 6 0 0 7 8 9 0 0 0 0 0 1 0 0 0 0 0 1 \endcode |
matrix/vector product \matrixworld | \code col2 = mat1 * col1; row2 = row1 * mat1; row1 *= mat1; mat3 = mat1 * mat2; mat3 *= mat1; \endcode |
add/subtract | \code mat3 = mat1 + mat2; mat3 += mat1; mat3 = mat1 - mat2; mat3 -= mat1;\endcode |
scalar product/division | \code mat3 = mat1 * s1; mat3 = s1 * mat1; mat3 *= s1; mat3 = mat1 / s1; mat3 /= s1;\endcode |
\b Reduction \b operation | \b Usage \b example |
Sum of all the coefficients in a matrix | \code MatrixXf m; float totalSum = m.sum();\endcode |
Maximum coefficient in a matrix | \code MatrixXf m; int row, col; // minimum value will be stored in minValue // and the row and column where it was found in row and col, // (these two parameters are optional) float minValue = m.minCoeff(&row,&col);\endcode |
Maximum coefficient in a matrix | \code MatrixXf m; int row, col; // maximum value will be stored in maxValue // and the row and column where it was found in row and col, // (these two parameters are optional) float maxValue = m.maxCoeff(&row,&col);\endcode |
Product between all coefficients in a matrix | \code MatrixXf m; float product = m.prod();\endcode |
Mean of coefficients in a matrix | \code MatrixXf m; float mean = m.mean();\endcode |
Matrix's trace | \code MatrixXf m; float trace = m.trace();\endcode |