blob: 60dc0a69b3a169fc429abaa0b652a39df5f7138e [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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
12#define EIGEN_MATRIX_FUNCTIONS
13
14#include <cfloat>
15#include <list>
Brian Silverman72890c22015-09-19 14:37:37 -040016
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 "src/MatrixFunctions/MatrixExponential.h"
57#include "src/MatrixFunctions/MatrixFunction.h"
58#include "src/MatrixFunctions/MatrixSquareRoot.h"
59#include "src/MatrixFunctions/MatrixLogarithm.h"
60#include "src/MatrixFunctions/MatrixPower.h"
61
62
63/**
64\page matrixbaseextra_page
65\ingroup MatrixFunctions_Module
66
67\section matrixbaseextra MatrixBase methods defined in the MatrixFunctions module
68
69The remainder of the page documents the following MatrixBase methods
70which are defined in the MatrixFunctions module.
71
72
73
74\subsection matrixbase_cos MatrixBase::cos()
75
76Compute the matrix cosine.
77
78\code
79const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
80\endcode
81
82\param[in] M a square matrix.
83\returns expression representing \f$ \cos(M) \f$.
84
Austin Schuh189376f2018-12-20 22:11:15 +110085This function computes the matrix cosine. Use ArrayBase::cos() for computing the entry-wise cosine.
86
87The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cos().
Brian Silverman72890c22015-09-19 14:37:37 -040088
89\sa \ref matrixbase_sin "sin()" for an example.
90
91
92
93\subsection matrixbase_cosh MatrixBase::cosh()
94
95Compute the matrix hyberbolic cosine.
96
97\code
98const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
99\endcode
100
101\param[in] M a square matrix.
102\returns expression representing \f$ \cosh(M) \f$
103
104This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cosh().
105
106\sa \ref matrixbase_sinh "sinh()" for an example.
107
108
109
110\subsection matrixbase_exp MatrixBase::exp()
111
112Compute the matrix exponential.
113
114\code
115const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
116\endcode
117
118\param[in] M matrix whose exponential is to be computed.
119\returns expression representing the matrix exponential of \p M.
120
121The matrix exponential of \f$ M \f$ is defined by
122\f[ \exp(M) = \sum_{k=0}^\infty \frac{M^k}{k!}. \f]
123The matrix exponential can be used to solve linear ordinary
124differential equations: the solution of \f$ y' = My \f$ with the
125initial condition \f$ y(0) = y_0 \f$ is given by
126\f$ y(t) = \exp(M) y_0 \f$.
127
Austin Schuh189376f2018-12-20 22:11:15 +1100128The matrix exponential is different from applying the exp function to all the entries in the matrix.
129Use ArrayBase::exp() if you want to do the latter.
130
Brian Silverman72890c22015-09-19 14:37:37 -0400131The cost of the computation is approximately \f$ 20 n^3 \f$ for
132matrices of size \f$ n \f$. The number 20 depends weakly on the
133norm of the matrix.
134
135The matrix exponential is computed using the scaling-and-squaring
136method combined with Pad&eacute; approximation. The matrix is first
137rescaled, then the exponential of the reduced matrix is computed
138approximant, and then the rescaling is undone by repeated
139squaring. The degree of the Pad&eacute; approximant is chosen such
140that the approximation error is less than the round-off
141error. However, errors may accumulate during the squaring phase.
142
143Details of the algorithm can be found in: Nicholas J. Higham, "The
144scaling and squaring method for the matrix exponential revisited,"
145<em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179&ndash;1193,
1462005.
147
148Example: The following program checks that
149\f[ \exp \left[ \begin{array}{ccc}
150 0 & \frac14\pi & 0 \\
151 -\frac14\pi & 0 & 0 \\
152 0 & 0 & 0
153 \end{array} \right] = \left[ \begin{array}{ccc}
154 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
155 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
156 0 & 0 & 1
157 \end{array} \right]. \f]
158This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
159the z-axis.
160
161\include MatrixExponential.cpp
162Output: \verbinclude MatrixExponential.out
163
Austin Schuh189376f2018-12-20 22:11:15 +1100164\note \p M has to be a matrix of \c float, \c double, `long double`
165\c complex<float>, \c complex<double>, or `complex<long double>` .
Brian Silverman72890c22015-09-19 14:37:37 -0400166
167
168\subsection matrixbase_log MatrixBase::log()
169
170Compute the matrix logarithm.
171
172\code
173const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
174\endcode
175
176\param[in] M invertible matrix whose logarithm is to be computed.
177\returns expression representing the matrix logarithm root of \p M.
178
179The matrix logarithm of \f$ M \f$ is a matrix \f$ X \f$ such that
180\f$ \exp(X) = M \f$ where exp denotes the matrix exponential. As for
181the scalar logarithm, the equation \f$ \exp(X) = M \f$ may have
182multiple solutions; this function returns a matrix whose eigenvalues
183have imaginary part in the interval \f$ (-\pi,\pi] \f$.
184
Austin Schuh189376f2018-12-20 22:11:15 +1100185The matrix logarithm is different from applying the log function to all the entries in the matrix.
186Use ArrayBase::log() if you want to do the latter.
187
Brian Silverman72890c22015-09-19 14:37:37 -0400188In the real case, the matrix \f$ M \f$ should be invertible and
189it should have no eigenvalues which are real and negative (pairs of
190complex conjugate eigenvalues are allowed). In the complex case, it
191only needs to be invertible.
192
193This function computes the matrix logarithm using the Schur-Parlett
194algorithm as implemented by MatrixBase::matrixFunction(). The
195logarithm of an atomic block is computed by MatrixLogarithmAtomic,
196which uses direct computation for 1-by-1 and 2-by-2 blocks and an
197inverse scaling-and-squaring algorithm for bigger blocks, with the
198square roots computed by MatrixBase::sqrt().
199
200Details of the algorithm can be found in Section 11.6.2 of:
201Nicholas J. Higham,
202<em>Functions of Matrices: Theory and Computation</em>,
203SIAM 2008. ISBN 978-0-898716-46-7.
204
205Example: The following program checks that
206\f[ \log \left[ \begin{array}{ccc}
207 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
208 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
209 0 & 0 & 1
210 \end{array} \right] = \left[ \begin{array}{ccc}
211 0 & \frac14\pi & 0 \\
212 -\frac14\pi & 0 & 0 \\
213 0 & 0 & 0
214 \end{array} \right]. \f]
215This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
216the z-axis. This is the inverse of the example used in the
217documentation of \ref matrixbase_exp "exp()".
218
219\include MatrixLogarithm.cpp
220Output: \verbinclude MatrixLogarithm.out
221
Austin Schuh189376f2018-12-20 22:11:15 +1100222\note \p M has to be a matrix of \c float, \c double, `long
223double`, \c complex<float>, \c complex<double>, or `complex<long double>`.
Brian Silverman72890c22015-09-19 14:37:37 -0400224
225\sa MatrixBase::exp(), MatrixBase::matrixFunction(),
226 class MatrixLogarithmAtomic, MatrixBase::sqrt().
227
228
229\subsection matrixbase_pow MatrixBase::pow()
230
231Compute the matrix raised to arbitrary real power.
232
233\code
234const MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(RealScalar p) const
235\endcode
236
237\param[in] M base of the matrix power, should be a square matrix.
Austin Schuh189376f2018-12-20 22:11:15 +1100238\param[in] p exponent of the matrix power.
Brian Silverman72890c22015-09-19 14:37:37 -0400239
240The matrix power \f$ M^p \f$ is defined as \f$ \exp(p \log(M)) \f$,
241where exp denotes the matrix exponential, and log denotes the matrix
Austin Schuh189376f2018-12-20 22:11:15 +1100242logarithm. This is different from raising all the entries in the matrix
243to the p-th power. Use ArrayBase::pow() if you want to do the latter.
Brian Silverman72890c22015-09-19 14:37:37 -0400244
Austin Schuh189376f2018-12-20 22:11:15 +1100245If \p p is complex, the scalar type of \p M should be the type of \p
246p . \f$ M^p \f$ simply evaluates into \f$ \exp(p \log(M)) \f$.
247Therefore, the matrix \f$ M \f$ should meet the conditions to be an
248argument of matrix logarithm.
Brian Silverman72890c22015-09-19 14:37:37 -0400249
Austin Schuh189376f2018-12-20 22:11:15 +1100250If \p p is real, it is casted into the real scalar type of \p M. Then
251this function computes the matrix power using the Schur-Pad&eacute;
Brian Silverman72890c22015-09-19 14:37:37 -0400252algorithm as implemented by class MatrixPower. The exponent is split
253into integral part and fractional part, where the fractional part is
254in the interval \f$ (-1, 1) \f$. The main diagonal and the first
255super-diagonal is directly computed.
256
Austin Schuh189376f2018-12-20 22:11:15 +1100257If \p M is singular with a semisimple zero eigenvalue and \p p is
258positive, the Schur factor \f$ T \f$ is reordered with Givens
259rotations, i.e.
260
261\f[ T = \left[ \begin{array}{cc}
262 T_1 & T_2 \\
263 0 & 0
264 \end{array} \right] \f]
265
266where \f$ T_1 \f$ is invertible. Then \f$ T^p \f$ is given by
267
268\f[ T^p = \left[ \begin{array}{cc}
269 T_1^p & T_1^{-1} T_1^p T_2 \\
270 0 & 0
271 \end{array}. \right] \f]
272
273\warning Fractional power of a matrix with a non-semisimple zero
274eigenvalue is not well-defined. We introduce an assertion failure
275against inaccurate result, e.g. \code
276#include <unsupported/Eigen/MatrixFunctions>
277#include <iostream>
278
279int main()
280{
281 Eigen::Matrix4d A;
282 A << 0, 0, 2, 3,
283 0, 0, 4, 5,
284 0, 0, 6, 7,
285 0, 0, 8, 9;
286 std::cout << A.pow(0.37) << std::endl;
287
288 // The 1 makes eigenvalue 0 non-semisimple.
289 A.coeffRef(0, 1) = 1;
290
291 // This fails if EIGEN_NO_DEBUG is undefined.
292 std::cout << A.pow(0.37) << std::endl;
293
294 return 0;
295}
296\endcode
297
Brian Silverman72890c22015-09-19 14:37:37 -0400298Details of the algorithm can be found in: Nicholas J. Higham and
299Lijing Lin, "A Schur-Pad&eacute; algorithm for fractional powers of a
300matrix," <em>SIAM J. %Matrix Anal. Applic.</em>,
301<b>32(3)</b>:1056&ndash;1078, 2011.
302
303Example: The following program checks that
304\f[ \left[ \begin{array}{ccc}
305 \cos1 & -\sin1 & 0 \\
306 \sin1 & \cos1 & 0 \\
307 0 & 0 & 1
308 \end{array} \right]^{\frac14\pi} = \left[ \begin{array}{ccc}
309 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
310 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
311 0 & 0 & 1
312 \end{array} \right]. \f]
313This corresponds to \f$ \frac14\pi \f$ rotations of 1 radian around
314the z-axis.
315
316\include MatrixPower.cpp
317Output: \verbinclude MatrixPower.out
318
319MatrixBase::pow() is user-friendly. However, there are some
320circumstances under which you should use class MatrixPower directly.
321MatrixPower can save the result of Schur decomposition, so it's
322better for computing various powers for the same matrix.
323
324Example:
325\include MatrixPower_optimal.cpp
326Output: \verbinclude MatrixPower_optimal.out
327
Austin Schuh189376f2018-12-20 22:11:15 +1100328\note \p M has to be a matrix of \c float, \c double, `long
329double`, \c complex<float>, \c complex<double>, or
330\c complex<long double> .
Brian Silverman72890c22015-09-19 14:37:37 -0400331
332\sa MatrixBase::exp(), MatrixBase::log(), class MatrixPower.
333
334
335\subsection matrixbase_matrixfunction MatrixBase::matrixFunction()
336
337Compute a matrix function.
338
339\code
340const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename internal::traits<Derived>::Scalar>::type f) const
341\endcode
342
343\param[in] M argument of matrix function, should be a square matrix.
344\param[in] f an entire function; \c f(x,n) should compute the n-th
345derivative of f at x.
346\returns expression representing \p f applied to \p M.
347
348Suppose that \p M is a matrix whose entries have type \c Scalar.
349Then, the second argument, \p f, should be a function with prototype
350\code
351ComplexScalar f(ComplexScalar, int)
352\endcode
353where \c ComplexScalar = \c std::complex<Scalar> if \c Scalar is
354real (e.g., \c float or \c double) and \c ComplexScalar =
355\c Scalar if \c Scalar is complex. The return value of \c f(x,n)
356should be \f$ f^{(n)}(x) \f$, the n-th derivative of f at x.
357
358This routine uses the algorithm described in:
359Philip Davies and Nicholas J. Higham,
360"A Schur-Parlett algorithm for computing matrix functions",
361<em>SIAM J. %Matrix Anal. Applic.</em>, <b>25</b>:464&ndash;485, 2003.
362
363The actual work is done by the MatrixFunction class.
364
365Example: The following program checks that
366\f[ \exp \left[ \begin{array}{ccc}
367 0 & \frac14\pi & 0 \\
368 -\frac14\pi & 0 & 0 \\
369 0 & 0 & 0
370 \end{array} \right] = \left[ \begin{array}{ccc}
371 \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
372 \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
373 0 & 0 & 1
374 \end{array} \right]. \f]
375This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
376the z-axis. This is the same example as used in the documentation
377of \ref matrixbase_exp "exp()".
378
379\include MatrixFunction.cpp
380Output: \verbinclude MatrixFunction.out
381
382Note that the function \c expfn is defined for complex numbers
383\c x, even though the matrix \c A is over the reals. Instead of
384\c expfn, we could also have used StdStemFunctions::exp:
385\code
386A.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
387\endcode
388
389
390
391\subsection matrixbase_sin MatrixBase::sin()
392
393Compute the matrix sine.
394
395\code
396const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
397\endcode
398
399\param[in] M a square matrix.
400\returns expression representing \f$ \sin(M) \f$.
401
Austin Schuh189376f2018-12-20 22:11:15 +1100402This function computes the matrix sine. Use ArrayBase::sin() for computing the entry-wise sine.
403
404The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
Brian Silverman72890c22015-09-19 14:37:37 -0400405
406Example: \include MatrixSine.cpp
407Output: \verbinclude MatrixSine.out
408
409
410
411\subsection matrixbase_sinh MatrixBase::sinh()
412
413Compute the matrix hyperbolic sine.
414
415\code
416MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
417\endcode
418
419\param[in] M a square matrix.
420\returns expression representing \f$ \sinh(M) \f$
421
422This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
423
424Example: \include MatrixSinh.cpp
425Output: \verbinclude MatrixSinh.out
426
427
428\subsection matrixbase_sqrt MatrixBase::sqrt()
429
430Compute the matrix square root.
431
432\code
433const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
434\endcode
435
436\param[in] M invertible matrix whose square root is to be computed.
437\returns expression representing the matrix square root of \p M.
438
439The matrix square root of \f$ M \f$ is the matrix \f$ M^{1/2} \f$
440whose square is the original matrix; so if \f$ S = M^{1/2} \f$ then
Austin Schuh189376f2018-12-20 22:11:15 +1100441\f$ S^2 = M \f$. This is different from taking the square root of all
442the entries in the matrix; use ArrayBase::sqrt() if you want to do the
443latter.
Brian Silverman72890c22015-09-19 14:37:37 -0400444
445In the <b>real case</b>, the matrix \f$ M \f$ should be invertible and
446it should have no eigenvalues which are real and negative (pairs of
447complex conjugate eigenvalues are allowed). In that case, the matrix
448has a square root which is also real, and this is the square root
449computed by this function.
450
451The matrix square root is computed by first reducing the matrix to
452quasi-triangular form with the real Schur decomposition. The square
453root of the quasi-triangular matrix can then be computed directly. The
454cost is approximately \f$ 25 n^3 \f$ real flops for the real Schur
455decomposition and \f$ 3\frac13 n^3 \f$ real flops for the remainder
456(though the computation time in practice is likely more than this
457indicates).
458
459Details of the algorithm can be found in: Nicholas J. Highan,
460"Computing real square roots of a real matrix", <em>Linear Algebra
461Appl.</em>, 88/89:405&ndash;430, 1987.
462
463If the matrix is <b>positive-definite symmetric</b>, then the square
464root is also positive-definite symmetric. In this case, it is best to
465use SelfAdjointEigenSolver::operatorSqrt() to compute it.
466
467In the <b>complex case</b>, the matrix \f$ M \f$ should be invertible;
468this is a restriction of the algorithm. The square root computed by
469this algorithm is the one whose eigenvalues have an argument in the
470interval \f$ (-\frac12\pi, \frac12\pi] \f$. This is the usual branch
471cut.
472
473The computation is the same as in the real case, except that the
474complex Schur decomposition is used to reduce the matrix to a
475triangular matrix. The theoretical cost is the same. Details are in:
476&Aring;ke Bj&ouml;rck and Sven Hammarling, "A Schur method for the
477square root of a matrix", <em>Linear Algebra Appl.</em>,
47852/53:127&ndash;140, 1983.
479
480Example: The following program checks that the square root of
481\f[ \left[ \begin{array}{cc}
482 \cos(\frac13\pi) & -\sin(\frac13\pi) \\
483 \sin(\frac13\pi) & \cos(\frac13\pi)
484 \end{array} \right], \f]
485corresponding to a rotation over 60 degrees, is a rotation over 30 degrees:
486\f[ \left[ \begin{array}{cc}
487 \cos(\frac16\pi) & -\sin(\frac16\pi) \\
488 \sin(\frac16\pi) & \cos(\frac16\pi)
489 \end{array} \right]. \f]
490
491\include MatrixSquareRoot.cpp
492Output: \verbinclude MatrixSquareRoot.out
493
494\sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
495 SelfAdjointEigenSolver::operatorSqrt().
496
497*/
498
499#endif // EIGEN_MATRIX_FUNCTIONS
500