Eigen-unsupported  5.0.1-dev
 
Loading...
Searching...
No Matches
MatrixFunctions
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Jitse Niesen <jitse@maths.leeds.ac.uk>
5// Copyright (C) 2012 Chen-Pang He <jdh8@ms63.hinet.net>
6//
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/.
10
11#ifndef EIGEN_MATRIX_FUNCTIONS_MODULE_H
12#define EIGEN_MATRIX_FUNCTIONS_MODULE_H
13
14#include <cfloat>
15#include <list>
16
17#include "../../Eigen/Core"
18#include "../../Eigen/LU"
19#include "../../Eigen/Eigenvalues"
20
21/**
22 * \defgroup MatrixFunctions_Module Matrix functions module
23 * \brief This module aims to provide various methods for the computation of
24 * matrix functions.
25 *
26 * To use this module, add
27 * \code
28 * #include <unsupported/Eigen/MatrixFunctions>
29 * \endcode
30 * at the start of your source file.
31 *
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
42 *
43 * These methods are the main entry points to this module.
44 *
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
48 * series
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]
53 *
54 */
55
56#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
57
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
65
66#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
67
68/**
69\page matrixbaseextra_page
70\ingroup MatrixFunctions_Module
71
72\section matrixbaseextra MatrixBase methods defined in the MatrixFunctions module
73
74The remainder of the page documents the following MatrixBase methods
75which are defined in the MatrixFunctions module.
76
77
78
79\subsection matrixbase_cos MatrixBase::cos()
80
81Compute the matrix cosine.
82
83\code
84const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
85\endcode
86
87\param[in] M a square matrix.
88\returns expression representing \f$ \cos(M) \f$.
89
90This function computes the matrix cosine. Use ArrayBase::cos() for computing the entry-wise cosine.
91
92The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cos().
93
94\sa \ref matrixbase_sin "sin()" for an example.
95
96
97
98\subsection matrixbase_cosh MatrixBase::cosh()
99
100Compute the matrix hyberbolic cosine.
101
102\code
103const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
104\endcode
105
106\param[in] M a square matrix.
107\returns expression representing \f$ \cosh(M) \f$
108
109This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cosh().
110
111\sa \ref matrixbase_sinh "sinh()" for an example.
112
113
114
115\subsection matrixbase_exp MatrixBase::exp()
116
117Compute the matrix exponential.
118
119\code
120const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
121\endcode
122
123\param[in] M matrix whose exponential is to be computed.
124\returns expression representing the matrix exponential of \p M.
125
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$.
132
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.
135
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
138norm of the matrix.
139
140The matrix exponential is computed using the scaling-and-squaring
141method combined with Pad&eacute; 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&eacute; approximant is chosen such
145that the approximation error is less than the round-off
146error. However, errors may accumulate during the squaring phase.
147
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&ndash;1193,
1512005.
152
153Example: The following program checks that
154\f[ \exp \left[ \begin{array}{ccc}
155 0 & \frac14\pi & 0 \\
156 -\frac14\pi & 0 & 0 \\
157 0 & 0 & 0
158 \end{array} \right] = \left[ \begin{array}{ccc}
159 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
160 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
161 0 & 0 & 1
162 \end{array} \right]. \f]
163This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
164the z-axis.
165
166\include MatrixExponential.cpp
167Output: \verbinclude MatrixExponential.out
168
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>` .
171
172
173\subsection matrixbase_log MatrixBase::log()
174
175Compute the matrix logarithm.
176
177\code
178const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
179\endcode
180
181\param[in] M invertible matrix whose logarithm is to be computed.
182\returns expression representing the matrix logarithm root of \p M.
183
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$.
189
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.
192
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.
197
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().
204
205Details of the algorithm can be found in Section 11.6.2 of:
206Nicholas J. Higham,
207<em>Functions of Matrices: Theory and Computation</em>,
208SIAM 2008. ISBN 978-0-898716-46-7.
209
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 \\
214 0 & 0 & 1
215 \end{array} \right] = \left[ \begin{array}{ccc}
216 0 & \frac14\pi & 0 \\
217 -\frac14\pi & 0 & 0 \\
218 0 & 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()".
223
224\include MatrixLogarithm.cpp
225Output: \verbinclude MatrixLogarithm.out
226
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>`.
229
230\sa MatrixBase::exp(), MatrixBase::matrixFunction(),
231 class MatrixLogarithmAtomic, MatrixBase::sqrt().
232
233
234\subsection matrixbase_pow MatrixBase::pow()
235
236Compute the matrix raised to arbitrary real power.
237
238\code
239const MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(RealScalar p) const
240\endcode
241
242\param[in] M base of the matrix power, should be a square matrix.
243\param[in] p exponent of the matrix power.
244
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.
249
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.
254
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&eacute;
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.
261
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
264rotations, i.e.
265
266\f[ T = \left[ \begin{array}{cc}
267 T_1 & T_2 \\
268 0 & 0
269 \end{array} \right] \f]
270
271where \f$ T_1 \f$ is invertible. Then \f$ T^p \f$ is given by
272
273\f[ T^p = \left[ \begin{array}{cc}
274 T_1^p & T_1^{-1} T_1^p T_2 \\
275 0 & 0
276 \end{array}. \right] \f]
277
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>
282#include <iostream>
283
284int main()
285{
286 Eigen::Matrix4d A;
287 A << 0, 0, 2, 3,
288 0, 0, 4, 5,
289 0, 0, 6, 7,
290 0, 0, 8, 9;
291 std::cout << A.pow(0.37) << std::endl;
292
293 // The 1 makes eigenvalue 0 non-semisimple.
294 A.coeffRef(0, 1) = 1;
295
296 // This fails if EIGEN_NO_DEBUG is undefined.
297 std::cout << A.pow(0.37) << std::endl;
298
299 return 0;
300}
301\endcode
302
303Details of the algorithm can be found in: Nicholas J. Higham and
304Lijing Lin, "A Schur-Pad&eacute; algorithm for fractional powers of a
305matrix," <em>SIAM J. %Matrix Anal. Applic.</em>,
306<b>32(3)</b>:1056&ndash;1078, 2011.
307
308Example: The following program checks that
309\f[ \left[ \begin{array}{ccc}
310 \cos1 & -\sin1 & 0 \\
311 \sin1 & \cos1 & 0 \\
312 0 & 0 & 1
313 \end{array} \right]^{\frac14\pi} = \left[ \begin{array}{ccc}
314 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
315 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
316 0 & 0 & 1
317 \end{array} \right]. \f]
318This corresponds to \f$ \frac14\pi \f$ rotations of 1 radian around
319the z-axis.
320
321\include MatrixPower.cpp
322Output: \verbinclude MatrixPower.out
323
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.
328
329Example:
330\include MatrixPower_optimal.cpp
331Output: \verbinclude MatrixPower_optimal.out
332
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> .
336
337\sa MatrixBase::exp(), MatrixBase::log(), class MatrixPower.
338
339
340\subsection matrixbase_matrixfunction MatrixBase::matrixFunction()
341
342Compute a matrix function.
343
344\code
345const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename
346internal::traits<Derived>::Scalar>::type f) const \endcode
347
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
350derivative of f at x.
351\returns expression representing \p f applied to \p M.
352
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
355\code
356ComplexScalar f(ComplexScalar, int)
357\endcode
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.
362
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&ndash;485, 2003.
367
368The actual work is done by the MatrixFunction class.
369
370Example: The following program checks that
371\f[ \exp \left[ \begin{array}{ccc}
372 0 & \frac14\pi & 0 \\
373 -\frac14\pi & 0 & 0 \\
374 0 & 0 & 0
375 \end{array} \right] = \left[ \begin{array}{ccc}
376 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
377 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
378 0 & 0 & 1
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()".
383
384\include MatrixFunction.cpp
385Output: \verbinclude MatrixFunction.out
386
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:
390\code
391A.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
392\endcode
393
394
395
396\subsection matrixbase_sin MatrixBase::sin()
397
398Compute the matrix sine.
399
400\code
401const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
402\endcode
403
404\param[in] M a square matrix.
405\returns expression representing \f$ \sin(M) \f$.
406
407This function computes the matrix sine. Use ArrayBase::sin() for computing the entry-wise sine.
408
409The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
410
411Example: \include MatrixSine.cpp
412Output: \verbinclude MatrixSine.out
413
414
415
416\subsection matrixbase_sinh MatrixBase::sinh()
417
418Compute the matrix hyperbolic sine.
419
420\code
421MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
422\endcode
423
424\param[in] M a square matrix.
425\returns expression representing \f$ \sinh(M) \f$
426
427This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
428
429Example: \include MatrixSinh.cpp
430Output: \verbinclude MatrixSinh.out
431
432
433\subsection matrixbase_sqrt MatrixBase::sqrt()
434
435Compute the matrix square root.
436
437\code
438const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
439\endcode
440
441\param[in] M invertible matrix whose square root is to be computed.
442\returns expression representing the matrix square root of \p M.
443
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
448latter.
449
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.
455
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
462indicates).
463
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&ndash;430, 1987.
467
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.
471
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
476cut.
477
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&Aring;ke Bj&ouml;rck and Sven Hammarling, "A Schur method for the
482square root of a matrix", <em>Linear Algebra Appl.</em>,
48352/53:127&ndash;140, 1983.
484
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]
495
496\include MatrixSquareRoot.cpp
497Output: \verbinclude MatrixSquareRoot.out
498
499\sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
500 SelfAdjointEigenSolver::operatorSqrt().
501
502*/
503
504#endif // EIGEN_MATRIX_FUNCTIONS_MODULE_H