1// This file is part of Eigen, a lightweight C++ template library
4// Copyright (C) 2009 Jitse Niesen <jitse@maths.leeds.ac.uk>
5// Copyright (C) 2012 Chen-Pang He <jdh8@ms63.hinet.net>
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11#ifndef EIGEN_MATRIX_FUNCTIONS_MODULE_H
12#define EIGEN_MATRIX_FUNCTIONS_MODULE_H
17#include "../../Eigen/Core"
18#include "../../Eigen/LU"
19#include "../../Eigen/Eigenvalues"
22 * \defgroup MatrixFunctions_Module Matrix functions module
23 * \brief This module aims to provide various methods for the computation of
26 * To use this module, add
28 * #include <unsupported/Eigen/MatrixFunctions>
30 * at the start of your source file.
32 * This module defines the following MatrixBase methods.
33 * - \ref matrixbase_cos "MatrixBase::cos()", for computing the matrix cosine
34 * - \ref matrixbase_cosh "MatrixBase::cosh()", for computing the matrix hyperbolic cosine
35 * - \ref matrixbase_exp "MatrixBase::exp()", for computing the matrix exponential
36 * - \ref matrixbase_log "MatrixBase::log()", for computing the matrix logarithm
37 * - \ref matrixbase_pow "MatrixBase::pow()", for computing the matrix power
38 * - \ref matrixbase_matrixfunction "MatrixBase::matrixFunction()", for computing general matrix functions
39 * - \ref matrixbase_sin "MatrixBase::sin()", for computing the matrix sine
40 * - \ref matrixbase_sinh "MatrixBase::sinh()", for computing the matrix hyperbolic sine
41 * - \ref matrixbase_sqrt "MatrixBase::sqrt()", for computing the matrix square root
43 * These methods are the main entry points to this module.
45 * %Matrix functions are defined as follows. Suppose that \f$ f \f$
46 * is an entire function (that is, a function on the complex plane
47 * that is everywhere complex differentiable). Then its Taylor
49 * \f[ f(0) + f'(0) x + \frac{f''(0)}{2} x^2 + \frac{f'''(0)}{3!} x^3 + \cdots \f]
50 * converges to \f$ f(x) \f$. In this case, we can define the matrix
51 * function by the same series:
52 * \f[ f(M) = f(0) + f'(0) M + \frac{f''(0)}{2} M^2 + \frac{f'''(0)}{3!} M^3 + \cdots \f]
56#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
58// IWYU pragma: begin_exports
59#include "src/MatrixFunctions/MatrixExponential.h"
60#include "src/MatrixFunctions/MatrixFunction.h"
61#include "src/MatrixFunctions/MatrixSquareRoot.h"
62#include "src/MatrixFunctions/MatrixLogarithm.h"
63#include "src/MatrixFunctions/MatrixPower.h"
64// IWYU pragma: end_exports
66#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
69\page matrixbaseextra_page
70\ingroup MatrixFunctions_Module
72\section matrixbaseextra MatrixBase methods defined in the MatrixFunctions module
74The remainder of the page documents the following MatrixBase methods
75which are defined in the MatrixFunctions module.
79\subsection matrixbase_cos MatrixBase::cos()
81Compute the matrix cosine.
84const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
87\param[in] M a square matrix.
88\returns expression representing \f$ \cos(M) \f$.
90This function computes the matrix cosine. Use ArrayBase::cos() for computing the entry-wise cosine.
92The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cos().
94\sa \ref matrixbase_sin "sin()" for an example.
98\subsection matrixbase_cosh MatrixBase::cosh()
100Compute the matrix hyberbolic cosine.
103const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
106\param[in] M a square matrix.
107\returns expression representing \f$ \cosh(M) \f$
109This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cosh().
111\sa \ref matrixbase_sinh "sinh()" for an example.
115\subsection matrixbase_exp MatrixBase::exp()
117Compute the matrix exponential.
120const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
123\param[in] M matrix whose exponential is to be computed.
124\returns expression representing the matrix exponential of \p M.
126The matrix exponential of \f$ M \f$ is defined by
127\f[ \exp(M) = \sum_{k=0}^\infty \frac{M^k}{k!}. \f]
128The matrix exponential can be used to solve linear ordinary
129differential equations: the solution of \f$ y' = My \f$ with the
130initial condition \f$ y(0) = y_0 \f$ is given by
131\f$ y(t) = \exp(M) y_0 \f$.
133The matrix exponential is different from applying the exp function to all the entries in the matrix.
134Use ArrayBase::exp() if you want to do the latter.
136The cost of the computation is approximately \f$ 20 n^3 \f$ for
137matrices of size \f$ n \f$. The number 20 depends weakly on the
140The matrix exponential is computed using the scaling-and-squaring
141method combined with Padé approximation. The matrix is first
142rescaled, then the exponential of the reduced matrix is computed
143approximant, and then the rescaling is undone by repeated
144squaring. The degree of the Padé approximant is chosen such
145that the approximation error is less than the round-off
146error. However, errors may accumulate during the squaring phase.
148Details of the algorithm can be found in: Nicholas J. Higham, "The
149scaling and squaring method for the matrix exponential revisited,"
150<em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179–1193,
153Example: The following program checks that
154\f[ \exp \left[ \begin{array}{ccc}
155 0 & \frac14\pi & 0 \\
156 -\frac14\pi & 0 & 0 \\
158 \end{array} \right] = \left[ \begin{array}{ccc}
159 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
160 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
162 \end{array} \right]. \f]
163This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
166\include MatrixExponential.cpp
167Output: \verbinclude MatrixExponential.out
169\note \p M has to be a matrix of \c float, \c double, `long double`
170\c complex<float>, \c complex<double>, or `complex<long double>` .
173\subsection matrixbase_log MatrixBase::log()
175Compute the matrix logarithm.
178const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
181\param[in] M invertible matrix whose logarithm is to be computed.
182\returns expression representing the matrix logarithm root of \p M.
184The matrix logarithm of \f$ M \f$ is a matrix \f$ X \f$ such that
185\f$ \exp(X) = M \f$ where exp denotes the matrix exponential. As for
186the scalar logarithm, the equation \f$ \exp(X) = M \f$ may have
187multiple solutions; this function returns a matrix whose eigenvalues
188have imaginary part in the interval \f$ (-\pi,\pi] \f$.
190The matrix logarithm is different from applying the log function to all the entries in the matrix.
191Use ArrayBase::log() if you want to do the latter.
193In the real case, the matrix \f$ M \f$ should be invertible and
194it should have no eigenvalues which are real and negative (pairs of
195complex conjugate eigenvalues are allowed). In the complex case, it
196only needs to be invertible.
198This function computes the matrix logarithm using the Schur-Parlett
199algorithm as implemented by MatrixBase::matrixFunction(). The
200logarithm of an atomic block is computed by MatrixLogarithmAtomic,
201which uses direct computation for 1-by-1 and 2-by-2 blocks and an
202inverse scaling-and-squaring algorithm for bigger blocks, with the
203square roots computed by MatrixBase::sqrt().
205Details of the algorithm can be found in Section 11.6.2 of:
207<em>Functions of Matrices: Theory and Computation</em>,
208SIAM 2008. ISBN 978-0-898716-46-7.
210Example: The following program checks that
211\f[ \log \left[ \begin{array}{ccc}
212 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
213 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
215 \end{array} \right] = \left[ \begin{array}{ccc}
216 0 & \frac14\pi & 0 \\
217 -\frac14\pi & 0 & 0 \\
219 \end{array} \right]. \f]
220This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
221the z-axis. This is the inverse of the example used in the
222documentation of \ref matrixbase_exp "exp()".
224\include MatrixLogarithm.cpp
225Output: \verbinclude MatrixLogarithm.out
227\note \p M has to be a matrix of \c float, \c double, `long
228double`, \c complex<float>, \c complex<double>, or `complex<long double>`.
230\sa MatrixBase::exp(), MatrixBase::matrixFunction(),
231 class MatrixLogarithmAtomic, MatrixBase::sqrt().
234\subsection matrixbase_pow MatrixBase::pow()
236Compute the matrix raised to arbitrary real power.
239const MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(RealScalar p) const
242\param[in] M base of the matrix power, should be a square matrix.
243\param[in] p exponent of the matrix power.
245The matrix power \f$ M^p \f$ is defined as \f$ \exp(p \log(M)) \f$,
246where exp denotes the matrix exponential, and log denotes the matrix
247logarithm. This is different from raising all the entries in the matrix
248to the p-th power. Use ArrayBase::pow() if you want to do the latter.
250If \p p is complex, the scalar type of \p M should be the type of \p
251p . \f$ M^p \f$ simply evaluates into \f$ \exp(p \log(M)) \f$.
252Therefore, the matrix \f$ M \f$ should meet the conditions to be an
253argument of matrix logarithm.
255If \p p is real, it is casted into the real scalar type of \p M. Then
256this function computes the matrix power using the Schur-Padé
257algorithm as implemented by class MatrixPower. The exponent is split
258into integral part and fractional part, where the fractional part is
259in the interval \f$ (-1, 1) \f$. The main diagonal and the first
260super-diagonal is directly computed.
262If \p M is singular with a semisimple zero eigenvalue and \p p is
263positive, the Schur factor \f$ T \f$ is reordered with Givens
266\f[ T = \left[ \begin{array}{cc}
269 \end{array} \right] \f]
271where \f$ T_1 \f$ is invertible. Then \f$ T^p \f$ is given by
273\f[ T^p = \left[ \begin{array}{cc}
274 T_1^p & T_1^{-1} T_1^p T_2 \\
276 \end{array}. \right] \f]
278\warning Fractional power of a matrix with a non-semisimple zero
279eigenvalue is not well-defined. We introduce an assertion failure
280against inaccurate result, e.g. \code
281#include <unsupported/Eigen/MatrixFunctions>
291 std::cout << A.pow(0.37) << std::endl;
293 // The 1 makes eigenvalue 0 non-semisimple.
294 A.coeffRef(0, 1) = 1;
296 // This fails if EIGEN_NO_DEBUG is undefined.
297 std::cout << A.pow(0.37) << std::endl;
303Details of the algorithm can be found in: Nicholas J. Higham and
304Lijing Lin, "A Schur-Padé algorithm for fractional powers of a
305matrix," <em>SIAM J. %Matrix Anal. Applic.</em>,
306<b>32(3)</b>:1056–1078, 2011.
308Example: The following program checks that
309\f[ \left[ \begin{array}{ccc}
310 \cos1 & -\sin1 & 0 \\
313 \end{array} \right]^{\frac14\pi} = \left[ \begin{array}{ccc}
314 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
315 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
317 \end{array} \right]. \f]
318This corresponds to \f$ \frac14\pi \f$ rotations of 1 radian around
321\include MatrixPower.cpp
322Output: \verbinclude MatrixPower.out
324MatrixBase::pow() is user-friendly. However, there are some
325circumstances under which you should use class MatrixPower directly.
326MatrixPower can save the result of Schur decomposition, so it's
327better for computing various powers for the same matrix.
330\include MatrixPower_optimal.cpp
331Output: \verbinclude MatrixPower_optimal.out
333\note \p M has to be a matrix of \c float, \c double, `long
334double`, \c complex<float>, \c complex<double>, or
335\c complex<long double> .
337\sa MatrixBase::exp(), MatrixBase::log(), class MatrixPower.
340\subsection matrixbase_matrixfunction MatrixBase::matrixFunction()
342Compute a matrix function.
345const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename
346internal::traits<Derived>::Scalar>::type f) const \endcode
348\param[in] M argument of matrix function, should be a square matrix.
349\param[in] f an entire function; \c f(x,n) should compute the n-th
351\returns expression representing \p f applied to \p M.
353Suppose that \p M is a matrix whose entries have type \c Scalar.
354Then, the second argument, \p f, should be a function with prototype
356ComplexScalar f(ComplexScalar, int)
358where \c ComplexScalar = \c std::complex<Scalar> if \c Scalar is
359real (e.g., \c float or \c double) and \c ComplexScalar =
360\c Scalar if \c Scalar is complex. The return value of \c f(x,n)
361should be \f$ f^{(n)}(x) \f$, the n-th derivative of f at x.
363This routine uses the algorithm described in:
364Philip Davies and Nicholas J. Higham,
365"A Schur-Parlett algorithm for computing matrix functions",
366<em>SIAM J. %Matrix Anal. Applic.</em>, <b>25</b>:464–485, 2003.
368The actual work is done by the MatrixFunction class.
370Example: The following program checks that
371\f[ \exp \left[ \begin{array}{ccc}
372 0 & \frac14\pi & 0 \\
373 -\frac14\pi & 0 & 0 \\
375 \end{array} \right] = \left[ \begin{array}{ccc}
376 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
377 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
379 \end{array} \right]. \f]
380This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
381the z-axis. This is the same example as used in the documentation
382of \ref matrixbase_exp "exp()".
384\include MatrixFunction.cpp
385Output: \verbinclude MatrixFunction.out
387Note that the function \c expfn is defined for complex numbers
388\c x, even though the matrix \c A is over the reals. Instead of
389\c expfn, we could also have used StdStemFunctions::exp:
391A.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
396\subsection matrixbase_sin MatrixBase::sin()
398Compute the matrix sine.
401const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
404\param[in] M a square matrix.
405\returns expression representing \f$ \sin(M) \f$.
407This function computes the matrix sine. Use ArrayBase::sin() for computing the entry-wise sine.
409The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
411Example: \include MatrixSine.cpp
412Output: \verbinclude MatrixSine.out
416\subsection matrixbase_sinh MatrixBase::sinh()
418Compute the matrix hyperbolic sine.
421MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
424\param[in] M a square matrix.
425\returns expression representing \f$ \sinh(M) \f$
427This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
429Example: \include MatrixSinh.cpp
430Output: \verbinclude MatrixSinh.out
433\subsection matrixbase_sqrt MatrixBase::sqrt()
435Compute the matrix square root.
438const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
441\param[in] M invertible matrix whose square root is to be computed.
442\returns expression representing the matrix square root of \p M.
444The matrix square root of \f$ M \f$ is the matrix \f$ M^{1/2} \f$
445whose square is the original matrix; so if \f$ S = M^{1/2} \f$ then
446\f$ S^2 = M \f$. This is different from taking the square root of all
447the entries in the matrix; use ArrayBase::sqrt() if you want to do the
450In the <b>real case</b>, the matrix \f$ M \f$ should be invertible and
451it should have no eigenvalues which are real and negative (pairs of
452complex conjugate eigenvalues are allowed). In that case, the matrix
453has a square root which is also real, and this is the square root
454computed by this function.
456The matrix square root is computed by first reducing the matrix to
457quasi-triangular form with the real Schur decomposition. The square
458root of the quasi-triangular matrix can then be computed directly. The
459cost is approximately \f$ 25 n^3 \f$ real flops for the real Schur
460decomposition and \f$ 3\frac13 n^3 \f$ real flops for the remainder
461(though the computation time in practice is likely more than this
464Details of the algorithm can be found in: Nicholas J. Highan,
465"Computing real square roots of a real matrix", <em>Linear Algebra
466Appl.</em>, 88/89:405–430, 1987.
468If the matrix is <b>positive-definite symmetric</b>, then the square
469root is also positive-definite symmetric. In this case, it is best to
470use SelfAdjointEigenSolver::operatorSqrt() to compute it.
472In the <b>complex case</b>, the matrix \f$ M \f$ should be invertible;
473this is a restriction of the algorithm. The square root computed by
474this algorithm is the one whose eigenvalues have an argument in the
475interval \f$ (-\frac12\pi, \frac12\pi] \f$. This is the usual branch
478The computation is the same as in the real case, except that the
479complex Schur decomposition is used to reduce the matrix to a
480triangular matrix. The theoretical cost is the same. Details are in:
481Åke Björck and Sven Hammarling, "A Schur method for the
482square root of a matrix", <em>Linear Algebra Appl.</em>,
48352/53:127–140, 1983.
485Example: The following program checks that the square root of
486\f[ \left[ \begin{array}{cc}
487 \cos(\frac13\pi) & -\sin(\frac13\pi) \\
488 \sin(\frac13\pi) & \cos(\frac13\pi)
489 \end{array} \right], \f]
490corresponding to a rotation over 60 degrees, is a rotation over 30 degrees:
491\f[ \left[ \begin{array}{cc}
492 \cos(\frac16\pi) & -\sin(\frac16\pi) \\
493 \sin(\frac16\pi) & \cos(\frac16\pi)
494 \end{array} \right]. \f]
496\include MatrixSquareRoot.cpp
497Output: \verbinclude MatrixSquareRoot.out
499\sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
500 SelfAdjointEigenSolver::operatorSqrt().
504#endif // EIGEN_MATRIX_FUNCTIONS_MODULE_H