blob: 5fc569a302a3bd1e5b524ada458993a25db72079 [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialMatrixArithmetic Matrix and vector arithmetic
Carlos Becker9d440052010-06-25 20:16:12 -04004
Gael Guennebaud091a49c2013-01-06 23:48:59 +01005This page aims to provide an overview and some details on how to perform arithmetic
Benoit Jacobe078bb22010-06-26 14:00:00 -04006between matrices, vectors and scalars with Eigen.
Carlos Becker9d440052010-06-25 20:16:12 -04007
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01008\eigenAutoToc
Carlos Becker9d440052010-06-25 20:16:12 -04009
Benoit Jacobe078bb22010-06-26 14:00:00 -040010\section TutorialArithmeticIntroduction Introduction
Carlos Becker9d440052010-06-25 20:16:12 -040011
Benoit Jacobe078bb22010-06-26 14:00:00 -040012Eigen offers matrix/vector arithmetic operations either through overloads of common C++ arithmetic operators such as +, -, *,
13or through special methods such as dot(), cross(), etc.
14For the Matrix class (matrices and vectors), operators are only overloaded to support
15linear-algebraic operations. For example, \c matrix1 \c * \c matrix2 means matrix-matrix product,
16and \c vector \c + \c scalar is just not allowed. If you want to perform all kinds of array operations,
Jitse Niesen2c03ca32010-07-09 11:46:07 +010017not linear algebra, see the \ref TutorialArrayClass "next page".
Benoit Jacobe078bb22010-06-26 14:00:00 -040018
Jitse Niesen30701642010-06-29 11:42:51 +010019\section TutorialArithmeticAddSub Addition and subtraction
Benoit Jacobe078bb22010-06-26 14:00:00 -040020
21The left hand side and right hand side must, of course, have the same numbers of rows and of columns. They must
22also have the same \c Scalar type, as Eigen doesn't do automatic type promotion. The operators at hand here are:
23\li binary operator + as in \c a+b
24\li binary operator - as in \c a-b
25\li unary operator - as in \c -a
26\li compound operator += as in \c a+=b
27\li compound operator -= as in \c a-=b
28
Gael Guennebaudf66fe262010-10-19 11:40:49 +020029<table class="example">
30<tr><th>Example:</th><th>Output:</th></tr>
31<tr><td>
32\include tut_arithmetic_add_sub.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +010033</td>
34<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020035\verbinclude tut_arithmetic_add_sub.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +010036</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040037
38\section TutorialArithmeticScalarMulDiv Scalar multiplication and division
39
40Multiplication and division by a scalar is very simple too. The operators at hand here are:
41\li binary operator * as in \c matrix*scalar
42\li binary operator * as in \c scalar*matrix
43\li binary operator / as in \c matrix/scalar
44\li compound operator *= as in \c matrix*=scalar
45\li compound operator /= as in \c matrix/=scalar
46
Gael Guennebaudf66fe262010-10-19 11:40:49 +020047<table class="example">
48<tr><th>Example:</th><th>Output:</th></tr>
49<tr><td>
50\include tut_arithmetic_scalar_mul_div.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +010051</td>
52<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020053\verbinclude tut_arithmetic_scalar_mul_div.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +010054</td></tr></table>
55
Benoit Jacobe078bb22010-06-26 14:00:00 -040056
57\section TutorialArithmeticMentionXprTemplates A note about expression templates
58
Jitse Niesen2c03ca32010-07-09 11:46:07 +010059This is an advanced topic that we explain on \ref TopicEigenExpressionTemplates "this page",
Benoit Jacobe078bb22010-06-26 14:00:00 -040060but it is useful to just mention it now. In Eigen, arithmetic operators such as \c operator+ don't
61perform any computation by themselves, they just return an "expression object" describing the computation to be
62performed. The actual computation happens later, when the whole expression is evaluated, typically in \c operator=.
63While this might sound heavy, any modern optimizing compiler is able to optimize away that abstraction and
64the result is perfectly optimized code. For example, when you do:
Carlos Becker9d440052010-06-25 20:16:12 -040065\code
Benoit Jacobe078bb22010-06-26 14:00:00 -040066VectorXf a(50), b(50), c(50), d(50);
67...
68a = 3*b + 4*c + 5*d;
Carlos Becker9d440052010-06-25 20:16:12 -040069\endcode
Jitse Niesen30701642010-06-29 11:42:51 +010070Eigen compiles it to just one for loop, so that the arrays are traversed only once. Simplifying (e.g. ignoring
Benoit Jacobe078bb22010-06-26 14:00:00 -040071SIMD optimizations), this loop looks like this:
Carlos Becker9d440052010-06-25 20:16:12 -040072\code
Benoit Jacobe078bb22010-06-26 14:00:00 -040073for(int i = 0; i < 50; ++i)
74 a[i] = 3*b[i] + 4*c[i] + 5*d[i];
Carlos Becker9d440052010-06-25 20:16:12 -040075\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -040076Thus, you should not be afraid of using relatively large arithmetic expressions with Eigen: it only gives Eigen
77more opportunities for optimization.
78
Gael Guennebaudde1220a2010-06-28 00:05:11 +020079\section TutorialArithmeticTranspose Transposition and conjugation
80
Jitse Niesen2c03ca32010-07-09 11:46:07 +010081The 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.
Gael Guennebaudde1220a2010-06-28 00:05:11 +020082
Gael Guennebaudf66fe262010-10-19 11:40:49 +020083<table class="example">
84<tr><th>Example:</th><th>Output:</th></tr>
85<tr><td>
86\include tut_arithmetic_transpose_conjugate.cpp
Gael Guennebaudde1220a2010-06-28 00:05:11 +020087</td>
88<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020089\verbinclude tut_arithmetic_transpose_conjugate.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +020090</td></tr></table>
91
Benoit Jacob07f24062010-11-24 08:23:17 -050092For real matrices, \c conjugate() is a no-operation, and so \c adjoint() is equivalent to \c transpose().
Gael Guennebaudde1220a2010-06-28 00:05:11 +020093
Jitse Niesen2c03ca32010-07-09 11:46:07 +010094As 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:
Gael Guennebaudf66fe262010-10-19 11:40:49 +020095<table class="example">
96<tr><th>Example:</th><th>Output:</th></tr>
97<tr><td>
98\include tut_arithmetic_transpose_aliasing.cpp
Gael Guennebaudde1220a2010-06-28 00:05:11 +020099</td>
100<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200101\verbinclude tut_arithmetic_transpose_aliasing.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200102</td></tr></table>
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100103This 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
105For \em in-place transposition, as for instance in <tt>a = a.transpose()</tt>, simply use the \link DenseBase::transposeInPlace() transposeInPlace()\endlink function:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200106<table class="example">
107<tr><th>Example:</th><th>Output:</th></tr>
108<tr><td>
109\include tut_arithmetic_transpose_inplace.cpp
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200110</td>
111<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200112\verbinclude tut_arithmetic_transpose_inplace.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200113</td></tr></table>
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100114There is also the \link MatrixBase::adjointInPlace() adjointInPlace()\endlink function for complex matrices.
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200115
Benoit Jacobe078bb22010-06-26 14:00:00 -0400116\section TutorialArithmeticMatrixMul Matrix-matrix and matrix-vector multiplication
117
118Matrix-matrix multiplication is again done with \c operator*. Since vectors are a special
119case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special
120case of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just
121two operators:
122\li binary operator * as in \c a*b
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100123\li compound operator *= as in \c a*=b (this multiplies on the right: \c a*=b is equivalent to <tt>a = a*b</tt>)
Benoit Jacobe078bb22010-06-26 14:00:00 -0400124
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200125<table class="example">
126<tr><th>Example:</th><th>Output:</th></tr>
127<tr><td>
128\include tut_arithmetic_matrix_mul.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100129</td>
130<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200131\verbinclude tut_arithmetic_matrix_mul.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100132</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400133
134Note: if you read the above paragraph on expression templates and are worried that doing \c m=m*m might cause
135aliasing issues, be reassured for now: Eigen treats matrix multiplication as a special case and takes care of
136introducing a temporary here, so it will compile \c m=m*m as:
Carlos Becker9d440052010-06-25 20:16:12 -0400137\code
Benoit Jacobe078bb22010-06-26 14:00:00 -0400138tmp = m*m;
139m = tmp;
Carlos Becker9d440052010-06-25 20:16:12 -0400140\endcode
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100141If 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.:
Gael Guennebaud75da2542010-06-28 00:42:57 +0200142\code
143c.noalias() += a * b;
144\endcode
Benoit Jacob07f24062010-11-24 08:23:17 -0500145For more details on this topic, see the page on \ref TopicAliasing "aliasing".
Carlos Becker9d440052010-06-25 20:16:12 -0400146
Gael Guennebaud75da2542010-06-28 00:42:57 +0200147\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
Benoit Jacobe078bb22010-06-26 14:00:00 -0400149\section TutorialArithmeticDotAndCross Dot product and cross product
Carlos Becker9d440052010-06-25 20:16:12 -0400150
Benoit Jacob07f24062010-11-24 08:23:17 -0500151For 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.
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200152<table class="example">
153<tr><th>Example:</th><th>Output:</th></tr>
154<tr><td>
155\include tut_arithmetic_dot_cross.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100156</td>
157<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200158\verbinclude tut_arithmetic_dot_cross.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100159</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400160
Benoit Jacobe078bb22010-06-26 14:00:00 -0400161Remember that cross product is only for vectors of size 3. Dot product is for vectors of any sizes.
162When using complex numbers, Eigen's dot product is conjugate-linear in the first variable and linear in the
163second variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400164
Benoit Jacobe078bb22010-06-26 14:00:00 -0400165\section TutorialArithmeticRedux Basic arithmetic reduction operations
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100166Eigen 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.
Carlos Becker9d440052010-06-25 20:16:12 -0400167
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200168<table class="example">
169<tr><th>Example:</th><th>Output:</th></tr>
170<tr><td>
171\include tut_arithmetic_redux_basic.cpp
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200172</td>
173<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200174\verbinclude tut_arithmetic_redux_basic.out
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200175</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400176
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100177The \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.
Carlos Becker9d440052010-06-25 20:16:12 -0400178
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200179There also exist variants of the \c minCoeff and \c maxCoeff functions returning the coordinates of the respective coefficient via the arguments:
Carlos Becker9d440052010-06-25 20:16:12 -0400180
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200181<table class="example">
182<tr><th>Example:</th><th>Output:</th></tr>
183<tr><td>
184\include tut_arithmetic_redux_minmax.cpp
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200185</td>
186<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200187\verbinclude tut_arithmetic_redux_minmax.out
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200188</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400189
Carlos Becker9d440052010-06-25 20:16:12 -0400190
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200191\section TutorialArithmeticValidity Validity of operations
Benoit Jacobe078bb22010-06-26 14:00:00 -0400192Eigen checks the validity of the operations that you perform. When possible,
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100193it checks them at compile time, producing compilation errors. These error messages can be long and ugly,
Benoit Jacobe078bb22010-06-26 14:00:00 -0400194but Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example:
195\code
196 Matrix3f m;
197 Vector4f v;
198 v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
199\endcode
Carlos Becker9d440052010-06-25 20:16:12 -0400200
Benoit Jacobe078bb22010-06-26 14:00:00 -0400201Of course, in many cases, for example when checking dynamic sizes, the check cannot be performed at compile time.
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100202Eigen 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.
Carlos Becker9d440052010-06-25 20:16:12 -0400203
Benoit Jacobe078bb22010-06-26 14:00:00 -0400204\code
205 MatrixXf m(3,3);
206 VectorXf v(4);
207 v = m * v; // Run-time assertion failure here: "invalid matrix product"
208\endcode
209
210For more details on this topic, see \ref TopicAssertions "this page".
211
Carlos Becker9d440052010-06-25 20:16:12 -0400212*/
213
214}