1*bf2c3715SXin Linamespace Eigen { 2*bf2c3715SXin Li 3*bf2c3715SXin Li/** \eigenManualPage TutorialMatrixArithmetic Matrix and vector arithmetic 4*bf2c3715SXin Li 5*bf2c3715SXin LiThis page aims to provide an overview and some details on how to perform arithmetic 6*bf2c3715SXin Libetween matrices, vectors and scalars with Eigen. 7*bf2c3715SXin Li 8*bf2c3715SXin Li\eigenAutoToc 9*bf2c3715SXin Li 10*bf2c3715SXin Li\section TutorialArithmeticIntroduction Introduction 11*bf2c3715SXin Li 12*bf2c3715SXin LiEigen offers matrix/vector arithmetic operations either through overloads of common C++ arithmetic operators such as +, -, *, 13*bf2c3715SXin Lior through special methods such as dot(), cross(), etc. 14*bf2c3715SXin LiFor the Matrix class (matrices and vectors), operators are only overloaded to support 15*bf2c3715SXin Lilinear-algebraic operations. For example, \c matrix1 \c * \c matrix2 means matrix-matrix product, 16*bf2c3715SXin Liand \c vector \c + \c scalar is just not allowed. If you want to perform all kinds of array operations, 17*bf2c3715SXin Linot linear algebra, see the \ref TutorialArrayClass "next page". 18*bf2c3715SXin Li 19*bf2c3715SXin Li\section TutorialArithmeticAddSub Addition and subtraction 20*bf2c3715SXin Li 21*bf2c3715SXin LiThe left hand side and right hand side must, of course, have the same numbers of rows and of columns. They must 22*bf2c3715SXin Lialso have the same \c Scalar type, as Eigen doesn't do automatic type promotion. The operators at hand here are: 23*bf2c3715SXin Li\li binary operator + as in \c a+b 24*bf2c3715SXin Li\li binary operator - as in \c a-b 25*bf2c3715SXin Li\li unary operator - as in \c -a 26*bf2c3715SXin Li\li compound operator += as in \c a+=b 27*bf2c3715SXin Li\li compound operator -= as in \c a-=b 28*bf2c3715SXin Li 29*bf2c3715SXin Li<table class="example"> 30*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 31*bf2c3715SXin Li<tr><td> 32*bf2c3715SXin Li\include tut_arithmetic_add_sub.cpp 33*bf2c3715SXin Li</td> 34*bf2c3715SXin Li<td> 35*bf2c3715SXin Li\verbinclude tut_arithmetic_add_sub.out 36*bf2c3715SXin Li</td></tr></table> 37*bf2c3715SXin Li 38*bf2c3715SXin Li\section TutorialArithmeticScalarMulDiv Scalar multiplication and division 39*bf2c3715SXin Li 40*bf2c3715SXin LiMultiplication and division by a scalar is very simple too. The operators at hand here are: 41*bf2c3715SXin Li\li binary operator * as in \c matrix*scalar 42*bf2c3715SXin Li\li binary operator * as in \c scalar*matrix 43*bf2c3715SXin Li\li binary operator / as in \c matrix/scalar 44*bf2c3715SXin Li\li compound operator *= as in \c matrix*=scalar 45*bf2c3715SXin Li\li compound operator /= as in \c matrix/=scalar 46*bf2c3715SXin Li 47*bf2c3715SXin Li<table class="example"> 48*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 49*bf2c3715SXin Li<tr><td> 50*bf2c3715SXin Li\include tut_arithmetic_scalar_mul_div.cpp 51*bf2c3715SXin Li</td> 52*bf2c3715SXin Li<td> 53*bf2c3715SXin Li\verbinclude tut_arithmetic_scalar_mul_div.out 54*bf2c3715SXin Li</td></tr></table> 55*bf2c3715SXin Li 56*bf2c3715SXin Li 57*bf2c3715SXin Li\section TutorialArithmeticMentionXprTemplates A note about expression templates 58*bf2c3715SXin Li 59*bf2c3715SXin LiThis is an advanced topic that we explain on \ref TopicEigenExpressionTemplates "this page", 60*bf2c3715SXin Libut it is useful to just mention it now. In Eigen, arithmetic operators such as \c operator+ don't 61*bf2c3715SXin Liperform any computation by themselves, they just return an "expression object" describing the computation to be 62*bf2c3715SXin Liperformed. The actual computation happens later, when the whole expression is evaluated, typically in \c operator=. 63*bf2c3715SXin LiWhile this might sound heavy, any modern optimizing compiler is able to optimize away that abstraction and 64*bf2c3715SXin Lithe result is perfectly optimized code. For example, when you do: 65*bf2c3715SXin Li\code 66*bf2c3715SXin LiVectorXf a(50), b(50), c(50), d(50); 67*bf2c3715SXin Li... 68*bf2c3715SXin Lia = 3*b + 4*c + 5*d; 69*bf2c3715SXin Li\endcode 70*bf2c3715SXin LiEigen compiles it to just one for loop, so that the arrays are traversed only once. Simplifying (e.g. ignoring 71*bf2c3715SXin LiSIMD optimizations), this loop looks like this: 72*bf2c3715SXin Li\code 73*bf2c3715SXin Lifor(int i = 0; i < 50; ++i) 74*bf2c3715SXin Li a[i] = 3*b[i] + 4*c[i] + 5*d[i]; 75*bf2c3715SXin Li\endcode 76*bf2c3715SXin LiThus, you should not be afraid of using relatively large arithmetic expressions with Eigen: it only gives Eigen 77*bf2c3715SXin Limore opportunities for optimization. 78*bf2c3715SXin Li 79*bf2c3715SXin Li\section TutorialArithmeticTranspose Transposition and conjugation 80*bf2c3715SXin Li 81*bf2c3715SXin LiThe transpose \f$ a^T \f$, conjugate \f$ \bar{a} \f$, and adjoint (i.e., conjugate transpose) \f$ a^* \f$ of a matrix or vector \f$ a \f$ are obtained by the member functions \link DenseBase::transpose() transpose()\endlink, \link MatrixBase::conjugate() conjugate()\endlink, and \link MatrixBase::adjoint() adjoint()\endlink, respectively. 82*bf2c3715SXin Li 83*bf2c3715SXin Li<table class="example"> 84*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 85*bf2c3715SXin Li<tr><td> 86*bf2c3715SXin Li\include tut_arithmetic_transpose_conjugate.cpp 87*bf2c3715SXin Li</td> 88*bf2c3715SXin Li<td> 89*bf2c3715SXin Li\verbinclude tut_arithmetic_transpose_conjugate.out 90*bf2c3715SXin Li</td></tr></table> 91*bf2c3715SXin Li 92*bf2c3715SXin LiFor real matrices, \c conjugate() is a no-operation, and so \c adjoint() is equivalent to \c transpose(). 93*bf2c3715SXin Li 94*bf2c3715SXin LiAs for basic arithmetic operators, \c transpose() and \c adjoint() simply return a proxy object without doing the actual transposition. If you do <tt>b = a.transpose()</tt>, then the transpose is evaluated at the same time as the result is written into \c b. However, there is a complication here. If you do <tt>a = a.transpose()</tt>, then Eigen starts writing the result into \c a before the evaluation of the transpose is finished. Therefore, the instruction <tt>a = a.transpose()</tt> does not replace \c a with its transpose, as one would expect: 95*bf2c3715SXin Li<table class="example"> 96*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 97*bf2c3715SXin Li<tr><td> 98*bf2c3715SXin Li\include tut_arithmetic_transpose_aliasing.cpp 99*bf2c3715SXin Li</td> 100*bf2c3715SXin Li<td> 101*bf2c3715SXin Li\verbinclude tut_arithmetic_transpose_aliasing.out 102*bf2c3715SXin Li</td></tr></table> 103*bf2c3715SXin LiThis is the so-called \ref TopicAliasing "aliasing issue". In "debug mode", i.e., when \ref TopicAssertions "assertions" have not been disabled, such common pitfalls are automatically detected. 104*bf2c3715SXin Li 105*bf2c3715SXin LiFor \em in-place transposition, as for instance in <tt>a = a.transpose()</tt>, simply use the \link DenseBase::transposeInPlace() transposeInPlace()\endlink function: 106*bf2c3715SXin Li<table class="example"> 107*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 108*bf2c3715SXin Li<tr><td> 109*bf2c3715SXin Li\include tut_arithmetic_transpose_inplace.cpp 110*bf2c3715SXin Li</td> 111*bf2c3715SXin Li<td> 112*bf2c3715SXin Li\verbinclude tut_arithmetic_transpose_inplace.out 113*bf2c3715SXin Li</td></tr></table> 114*bf2c3715SXin LiThere is also the \link MatrixBase::adjointInPlace() adjointInPlace()\endlink function for complex matrices. 115*bf2c3715SXin Li 116*bf2c3715SXin Li\section TutorialArithmeticMatrixMul Matrix-matrix and matrix-vector multiplication 117*bf2c3715SXin Li 118*bf2c3715SXin LiMatrix-matrix multiplication is again done with \c operator*. Since vectors are a special 119*bf2c3715SXin Licase of matrices, they are implicitly handled there too, so matrix-vector product is really just a special 120*bf2c3715SXin Licase of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just 121*bf2c3715SXin Litwo operators: 122*bf2c3715SXin Li\li binary operator * as in \c a*b 123*bf2c3715SXin Li\li compound operator *= as in \c a*=b (this multiplies on the right: \c a*=b is equivalent to <tt>a = a*b</tt>) 124*bf2c3715SXin Li 125*bf2c3715SXin Li<table class="example"> 126*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 127*bf2c3715SXin Li<tr><td> 128*bf2c3715SXin Li\include tut_arithmetic_matrix_mul.cpp 129*bf2c3715SXin Li</td> 130*bf2c3715SXin Li<td> 131*bf2c3715SXin Li\verbinclude tut_arithmetic_matrix_mul.out 132*bf2c3715SXin Li</td></tr></table> 133*bf2c3715SXin Li 134*bf2c3715SXin LiNote: if you read the above paragraph on expression templates and are worried that doing \c m=m*m might cause 135*bf2c3715SXin Lialiasing issues, be reassured for now: Eigen treats matrix multiplication as a special case and takes care of 136*bf2c3715SXin Liintroducing a temporary here, so it will compile \c m=m*m as: 137*bf2c3715SXin Li\code 138*bf2c3715SXin Litmp = m*m; 139*bf2c3715SXin Lim = tmp; 140*bf2c3715SXin Li\endcode 141*bf2c3715SXin LiIf you know your matrix product can be safely evaluated into the destination matrix without aliasing issue, then you can use the \link MatrixBase::noalias() noalias()\endlink function to avoid the temporary, e.g.: 142*bf2c3715SXin Li\code 143*bf2c3715SXin Lic.noalias() += a * b; 144*bf2c3715SXin Li\endcode 145*bf2c3715SXin LiFor more details on this topic, see the page on \ref TopicAliasing "aliasing". 146*bf2c3715SXin Li 147*bf2c3715SXin Li\b Note: for BLAS users worried about performance, expressions such as <tt>c.noalias() -= 2 * a.adjoint() * b;</tt> are fully optimized and trigger a single gemm-like function call. 148*bf2c3715SXin Li 149*bf2c3715SXin Li\section TutorialArithmeticDotAndCross Dot product and cross product 150*bf2c3715SXin Li 151*bf2c3715SXin LiFor dot product and cross product, you need the \link MatrixBase::dot() dot()\endlink and \link MatrixBase::cross() cross()\endlink methods. Of course, the dot product can also be obtained as a 1x1 matrix as u.adjoint()*v. 152*bf2c3715SXin Li<table class="example"> 153*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 154*bf2c3715SXin Li<tr><td> 155*bf2c3715SXin Li\include tut_arithmetic_dot_cross.cpp 156*bf2c3715SXin Li</td> 157*bf2c3715SXin Li<td> 158*bf2c3715SXin Li\verbinclude tut_arithmetic_dot_cross.out 159*bf2c3715SXin Li</td></tr></table> 160*bf2c3715SXin Li 161*bf2c3715SXin LiRemember that cross product is only for vectors of size 3. Dot product is for vectors of any sizes. 162*bf2c3715SXin LiWhen using complex numbers, Eigen's dot product is conjugate-linear in the first variable and linear in the 163*bf2c3715SXin Lisecond variable. 164*bf2c3715SXin Li 165*bf2c3715SXin Li\section TutorialArithmeticRedux Basic arithmetic reduction operations 166*bf2c3715SXin LiEigen also provides some reduction operations to reduce a given matrix or vector to a single value such as the sum (computed by \link DenseBase::sum() sum()\endlink), product (\link DenseBase::prod() prod()\endlink), or the maximum (\link DenseBase::maxCoeff() maxCoeff()\endlink) and minimum (\link DenseBase::minCoeff() minCoeff()\endlink) of all its coefficients. 167*bf2c3715SXin Li 168*bf2c3715SXin Li<table class="example"> 169*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 170*bf2c3715SXin Li<tr><td> 171*bf2c3715SXin Li\include tut_arithmetic_redux_basic.cpp 172*bf2c3715SXin Li</td> 173*bf2c3715SXin Li<td> 174*bf2c3715SXin Li\verbinclude tut_arithmetic_redux_basic.out 175*bf2c3715SXin Li</td></tr></table> 176*bf2c3715SXin Li 177*bf2c3715SXin LiThe \em trace of a matrix, as returned by the function \link MatrixBase::trace() trace()\endlink, is the sum of the diagonal coefficients and can also be computed as efficiently using <tt>a.diagonal().sum()</tt>, as we will see later on. 178*bf2c3715SXin Li 179*bf2c3715SXin LiThere also exist variants of the \c minCoeff and \c maxCoeff functions returning the coordinates of the respective coefficient via the arguments: 180*bf2c3715SXin Li 181*bf2c3715SXin Li<table class="example"> 182*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 183*bf2c3715SXin Li<tr><td> 184*bf2c3715SXin Li\include tut_arithmetic_redux_minmax.cpp 185*bf2c3715SXin Li</td> 186*bf2c3715SXin Li<td> 187*bf2c3715SXin Li\verbinclude tut_arithmetic_redux_minmax.out 188*bf2c3715SXin Li</td></tr></table> 189*bf2c3715SXin Li 190*bf2c3715SXin Li 191*bf2c3715SXin Li\section TutorialArithmeticValidity Validity of operations 192*bf2c3715SXin LiEigen checks the validity of the operations that you perform. When possible, 193*bf2c3715SXin Liit checks them at compile time, producing compilation errors. These error messages can be long and ugly, 194*bf2c3715SXin Libut Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example: 195*bf2c3715SXin Li\code 196*bf2c3715SXin Li Matrix3f m; 197*bf2c3715SXin Li Vector4f v; 198*bf2c3715SXin Li v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES 199*bf2c3715SXin Li\endcode 200*bf2c3715SXin Li 201*bf2c3715SXin LiOf course, in many cases, for example when checking dynamic sizes, the check cannot be performed at compile time. 202*bf2c3715SXin LiEigen then uses runtime assertions. This means that the program will abort with an error message when executing an illegal operation if it is run in "debug mode", and it will probably crash if assertions are turned off. 203*bf2c3715SXin Li 204*bf2c3715SXin Li\code 205*bf2c3715SXin Li MatrixXf m(3,3); 206*bf2c3715SXin Li VectorXf v(4); 207*bf2c3715SXin Li v = m * v; // Run-time assertion failure here: "invalid matrix product" 208*bf2c3715SXin Li\endcode 209*bf2c3715SXin Li 210*bf2c3715SXin LiFor more details on this topic, see \ref TopicAssertions "this page". 211*bf2c3715SXin Li 212*bf2c3715SXin Li*/ 213*bf2c3715SXin Li 214*bf2c3715SXin Li} 215