blob: d076c804896ea2c37cb2c6333c674ed035d22cfe [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Benoit Jacobe078bb22010-06-26 14:00:00 -04003/** \page TutorialMatrixArithmetic Tutorial page 2 - %Matrix and vector arithmetic
Carlos Becker9d440052010-06-25 20:16:12 -04004 \ingroup Tutorial
5
Benoit Jacobe078bb22010-06-26 14:00:00 -04006\li \b Previous: \ref TutorialMatrixClass
7\li \b Next: \ref TutorialArrayClass
8
9This tutorial aims to provide an overview and some details on how to perform arithmetic
10between matrices, vectors and scalars with Eigen.
Carlos Becker9d440052010-06-25 20:16:12 -040011
12\b Table \b of \b contents
Benoit Jacobe078bb22010-06-26 14:00:00 -040013 - \ref TutorialArithmeticIntroduction
Benoit Jacobe078bb22010-06-26 14:00:00 -040014 - \ref TutorialArithmeticAddSub
15 - \ref TutorialArithmeticScalarMulDiv
16 - \ref TutorialArithmeticMentionXprTemplates
Gael Guennebaudde1220a2010-06-28 00:05:11 +020017 - \ref TutorialArithmeticTranspose
Benoit Jacobe078bb22010-06-26 14:00:00 -040018 - \ref TutorialArithmeticMatrixMul
19 - \ref TutorialArithmeticDotAndCross
20 - \ref TutorialArithmeticRedux
21 - \ref TutorialArithmeticValidity
Carlos Becker9d440052010-06-25 20:16:12 -040022
Benoit Jacobe078bb22010-06-26 14:00:00 -040023\section TutorialArithmeticIntroduction Introduction
Carlos Becker9d440052010-06-25 20:16:12 -040024
Benoit Jacobe078bb22010-06-26 14:00:00 -040025Eigen offers matrix/vector arithmetic operations either through overloads of common C++ arithmetic operators such as +, -, *,
26or through special methods such as dot(), cross(), etc.
27For the Matrix class (matrices and vectors), operators are only overloaded to support
28linear-algebraic operations. For example, \c matrix1 \c * \c matrix2 means matrix-matrix product,
29and \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 +010030not linear algebra, see the \ref TutorialArrayClass "next page".
Benoit Jacobe078bb22010-06-26 14:00:00 -040031
Jitse Niesen30701642010-06-29 11:42:51 +010032\section TutorialArithmeticAddSub Addition and subtraction
Benoit Jacobe078bb22010-06-26 14:00:00 -040033
34The left hand side and right hand side must, of course, have the same numbers of rows and of columns. They must
35also have the same \c Scalar type, as Eigen doesn't do automatic type promotion. The operators at hand here are:
36\li binary operator + as in \c a+b
37\li binary operator - as in \c a-b
38\li unary operator - as in \c -a
39\li compound operator += as in \c a+=b
40\li compound operator -= as in \c a-=b
41
Jitse Niesen2c03ca32010-07-09 11:46:07 +010042<table class="tutorial_code"><tr><td>
Benoit Jacobe078bb22010-06-26 14:00:00 -040043Example: \include tut_arithmetic_add_sub.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +010044</td>
45<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010046Output: \verbinclude tut_arithmetic_add_sub.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +010047</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040048
49\section TutorialArithmeticScalarMulDiv Scalar multiplication and division
50
51Multiplication and division by a scalar is very simple too. The operators at hand here are:
52\li binary operator * as in \c matrix*scalar
53\li binary operator * as in \c scalar*matrix
54\li binary operator / as in \c matrix/scalar
55\li compound operator *= as in \c matrix*=scalar
56\li compound operator /= as in \c matrix/=scalar
57
Jitse Niesen2c03ca32010-07-09 11:46:07 +010058<table class="tutorial_code"><tr><td>
Benoit Jacobe078bb22010-06-26 14:00:00 -040059Example: \include tut_arithmetic_scalar_mul_div.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +010060</td>
61<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010062Output: \verbinclude tut_arithmetic_scalar_mul_div.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +010063</td></tr></table>
64
Benoit Jacobe078bb22010-06-26 14:00:00 -040065
66\section TutorialArithmeticMentionXprTemplates A note about expression templates
67
Jitse Niesen2c03ca32010-07-09 11:46:07 +010068This is an advanced topic that we explain on \ref TopicEigenExpressionTemplates "this page",
Benoit Jacobe078bb22010-06-26 14:00:00 -040069but it is useful to just mention it now. In Eigen, arithmetic operators such as \c operator+ don't
70perform any computation by themselves, they just return an "expression object" describing the computation to be
71performed. The actual computation happens later, when the whole expression is evaluated, typically in \c operator=.
72While this might sound heavy, any modern optimizing compiler is able to optimize away that abstraction and
73the result is perfectly optimized code. For example, when you do:
Carlos Becker9d440052010-06-25 20:16:12 -040074\code
Benoit Jacobe078bb22010-06-26 14:00:00 -040075VectorXf a(50), b(50), c(50), d(50);
76...
77a = 3*b + 4*c + 5*d;
Carlos Becker9d440052010-06-25 20:16:12 -040078\endcode
Jitse Niesen30701642010-06-29 11:42:51 +010079Eigen 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 -040080SIMD optimizations), this loop looks like this:
Carlos Becker9d440052010-06-25 20:16:12 -040081\code
Benoit Jacobe078bb22010-06-26 14:00:00 -040082for(int i = 0; i < 50; ++i)
83 a[i] = 3*b[i] + 4*c[i] + 5*d[i];
Carlos Becker9d440052010-06-25 20:16:12 -040084\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -040085Thus, you should not be afraid of using relatively large arithmetic expressions with Eigen: it only gives Eigen
86more opportunities for optimization.
87
Gael Guennebaudde1220a2010-06-28 00:05:11 +020088\section TutorialArithmeticTranspose Transposition and conjugation
89
Jitse Niesen2c03ca32010-07-09 11:46:07 +010090The 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 +020091
92<table class="tutorial_code"><tr><td>
93Example: \include tut_arithmetic_transpose_conjugate.cpp
94</td>
95<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010096Output: \verbinclude tut_arithmetic_transpose_conjugate.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +020097</td></tr></table>
98
99For real matrices, \c conjugate() is a no-operation, and so \c adjoint() is 100% equivalent to \c transpose().
100
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100101As 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 Guennebaudde1220a2010-06-28 00:05:11 +0200102<table class="tutorial_code"><tr><td>
103Example: \include tut_arithmetic_transpose_aliasing.cpp
104</td>
105<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100106Output: \verbinclude tut_arithmetic_transpose_aliasing.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200107</td></tr></table>
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100108This 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.
109
110For \em in-place transposition, as for instance in <tt>a = a.transpose()</tt>, simply use the \link DenseBase::transposeInPlace() transposeInPlace()\endlink function:
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200111<table class="tutorial_code"><tr><td>
112Example: \include tut_arithmetic_transpose_inplace.cpp
113</td>
114<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100115Output: \verbinclude tut_arithmetic_transpose_inplace.out
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200116</td></tr></table>
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100117There is also the \link MatrixBase::adjointInPlace() adjointInPlace()\endlink function for complex matrices.
Gael Guennebaudde1220a2010-06-28 00:05:11 +0200118
Benoit Jacobe078bb22010-06-26 14:00:00 -0400119\section TutorialArithmeticMatrixMul Matrix-matrix and matrix-vector multiplication
120
121Matrix-matrix multiplication is again done with \c operator*. Since vectors are a special
122case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special
123case of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just
124two operators:
125\li binary operator * as in \c a*b
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100126\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 -0400127
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100128<table class="tutorial_code"><tr><td>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400129Example: \include tut_arithmetic_matrix_mul.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100130</td>
131<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100132Output: \verbinclude tut_arithmetic_matrix_mul.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100133</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400134
135Note: if you read the above paragraph on expression templates and are worried that doing \c m=m*m might cause
136aliasing issues, be reassured for now: Eigen treats matrix multiplication as a special case and takes care of
137introducing a temporary here, so it will compile \c m=m*m as:
Carlos Becker9d440052010-06-25 20:16:12 -0400138\code
Benoit Jacobe078bb22010-06-26 14:00:00 -0400139tmp = m*m;
140m = tmp;
Carlos Becker9d440052010-06-25 20:16:12 -0400141\endcode
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100142If 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 +0200143\code
144c.noalias() += a * b;
145\endcode
Benoit Jacobe078bb22010-06-26 14:00:00 -0400146For more details on this topic, see \ref TopicEigenExpressionTemplates "this page".
Carlos Becker9d440052010-06-25 20:16:12 -0400147
Gael Guennebaud75da2542010-06-28 00:42:57 +0200148\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.
149
Benoit Jacobe078bb22010-06-26 14:00:00 -0400150\section TutorialArithmeticDotAndCross Dot product and cross product
Carlos Becker9d440052010-06-25 20:16:12 -0400151
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100152The above-discussed \c operator* cannot be used to compute dot and cross products directly. For that, you need the \link MatrixBase::dot() dot()\endlink and \link MatrixBase::cross() cross()\endlink methods.
153<table class="tutorial_code"><tr><td>
Benoit Jacobe078bb22010-06-26 14:00:00 -0400154Example: \include tut_arithmetic_dot_cross.cpp
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100155</td>
156<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100157Output: \verbinclude tut_arithmetic_dot_cross.out
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100158</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400159
Benoit Jacobe078bb22010-06-26 14:00:00 -0400160Remember that cross product is only for vectors of size 3. Dot product is for vectors of any sizes.
161When using complex numbers, Eigen's dot product is conjugate-linear in the first variable and linear in the
162second variable.
Carlos Becker9d440052010-06-25 20:16:12 -0400163
Benoit Jacobe078bb22010-06-26 14:00:00 -0400164\section TutorialArithmeticRedux Basic arithmetic reduction operations
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100165Eigen 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 -0400166
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200167<table class="tutorial_code"><tr><td>
168Example: \include tut_arithmetic_redux_basic.cpp
169</td>
170<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100171Output: \verbinclude tut_arithmetic_redux_basic.out
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200172</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400173
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100174The \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 -0400175
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200176There 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 -0400177
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200178<table class="tutorial_code"><tr><td>
179Example: \include tut_arithmetic_redux_minmax.cpp
180</td>
181<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100182Output: \verbinclude tut_arithmetic_redux_minmax.out
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200183</td></tr></table>
Carlos Becker9d440052010-06-25 20:16:12 -0400184
Carlos Becker9d440052010-06-25 20:16:12 -0400185
Gael Guennebauddbefd7a2010-06-28 13:30:10 +0200186\section TutorialArithmeticValidity Validity of operations
Benoit Jacobe078bb22010-06-26 14:00:00 -0400187Eigen checks the validity of the operations that you perform. When possible,
Jitse Niesen2c03ca32010-07-09 11:46:07 +0100188it checks them at compile time, producing compilation errors. These error messages can be long and ugly,
Benoit Jacobe078bb22010-06-26 14:00:00 -0400189but Eigen writes the important message in UPPERCASE_LETTERS_SO_IT_STANDS_OUT. For example:
190\code
191 Matrix3f m;
192 Vector4f v;
193 v = m*v; // Compile-time error: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
194\endcode
Carlos Becker9d440052010-06-25 20:16:12 -0400195
Benoit Jacobe078bb22010-06-26 14:00:00 -0400196Of 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 +0100197Eigen 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 -0400198
Benoit Jacobe078bb22010-06-26 14:00:00 -0400199\code
200 MatrixXf m(3,3);
201 VectorXf v(4);
202 v = m * v; // Run-time assertion failure here: "invalid matrix product"
203\endcode
204
205For more details on this topic, see \ref TopicAssertions "this page".
206
207\li \b Next: \ref TutorialArrayClass
Carlos Becker9d440052010-06-25 20:16:12 -0400208
209*/
210
211}