1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2009 Jitse Niesen <jitse@maths.leeds.ac.uk>
57faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// Copyright (C) 2012 Chen-Pang He <jdh8@ms63.hinet.net>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_MATRIX_FUNCTIONS
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATRIX_FUNCTIONS
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <cfloat>
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <list>
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <functional>
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <iterator>
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <Eigen/Core>
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <Eigen/LU>
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <Eigen/Eigenvalues>
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez/**
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \defgroup MatrixFunctions_Module Matrix functions module
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \brief This module aims to provide various methods for the computation of
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * matrix functions. 
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * To use this module, add 
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \code
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * #include <unsupported/Eigen/MatrixFunctions>
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \endcode
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * at the start of your source file.
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This module defines the following MatrixBase methods.
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_cos "MatrixBase::cos()", for computing the matrix cosine
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_cosh "MatrixBase::cosh()", for computing the matrix hyperbolic cosine
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_exp "MatrixBase::exp()", for computing the matrix exponential
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_log "MatrixBase::log()", for computing the matrix logarithm
397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  *  - \ref matrixbase_pow "MatrixBase::pow()", for computing the matrix power
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_matrixfunction "MatrixBase::matrixFunction()", for computing general matrix functions
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_sin "MatrixBase::sin()", for computing the matrix sine
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_sinh "MatrixBase::sinh()", for computing the matrix hyperbolic sine
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *  - \ref matrixbase_sqrt "MatrixBase::sqrt()", for computing the matrix square root
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * These methods are the main entry points to this module. 
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * %Matrix functions are defined as follows.  Suppose that \f$ f \f$
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * is an entire function (that is, a function on the complex plane
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * that is everywhere complex differentiable).  Then its Taylor
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * series
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \f[ f(0) + f'(0) x + \frac{f''(0)}{2} x^2 + \frac{f'''(0)}{3!} x^3 + \cdots \f]
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * converges to \f$ f(x) \f$. In this case, we can define the matrix
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * function by the same series:
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \f[ f(M) = f(0) + f'(0) M + \frac{f''(0)}{2} M^2 + \frac{f'''(0)}{3!} M^3 + \cdots \f]
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "src/MatrixFunctions/MatrixExponential.h"
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "src/MatrixFunctions/MatrixFunction.h"
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "src/MatrixFunctions/MatrixSquareRoot.h"
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "src/MatrixFunctions/MatrixLogarithm.h"
627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#include "src/MatrixFunctions/MatrixPower.h"
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** 
667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\page matrixbaseextra_page
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\ingroup MatrixFunctions_Module
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\section matrixbaseextra MatrixBase methods defined in the MatrixFunctions module
707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe remainder of the page documents the following MatrixBase methods
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathwhich are defined in the MatrixFunctions module.
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_cos MatrixBase::cos()
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix cosine.
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  a square matrix.
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns  expression representing \f$ \cos(M) \f$.
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cos().
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\sa \ref matrixbase_sin "sin()" for an example.
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_cosh MatrixBase::cosh()
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix hyberbolic cosine.
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  a square matrix.
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns  expression representing \f$ \cosh(M) \f$
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cosh().
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\sa \ref matrixbase_sinh "sinh()" for an example.
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_exp MatrixBase::exp()
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix exponential.
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  matrix whose exponential is to be computed.
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns    expression representing the matrix exponential of \p M.
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix exponential of \f$ M \f$ is defined by
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \exp(M) = \sum_{k=0}^\infty \frac{M^k}{k!}. \f]
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix exponential can be used to solve linear ordinary
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathdifferential equations: the solution of \f$ y' = My \f$ with the
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinitial condition \f$ y(0) = y_0 \f$ is given by
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f$ y(t) = \exp(M) y_0 \f$.
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe cost of the computation is approximately \f$ 20 n^3 \f$ for
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathmatrices of size \f$ n \f$. The number 20 depends weakly on the
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnorm of the matrix.
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix exponential is computed using the scaling-and-squaring
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathmethod combined with Pad&eacute; approximation. The matrix is first
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathrescaled, then the exponential of the reduced matrix is computed
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathapproximant, and then the rescaling is undone by repeated
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathsquaring. The degree of the Pad&eacute; approximant is chosen such
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththat the approximation error is less than the round-off
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamatherror. However, errors may accumulate during the squaring phase.
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathDetails of the algorithm can be found in: Nicholas J. Higham, "The
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathscaling and squaring method for the matrix exponential revisited,"
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath<em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179&ndash;1193,
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath2005.
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: The following program checks that
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \exp \left[ \begin{array}{ccc}
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & \frac14\pi & 0 \\
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      -\frac14\pi & 0 & 0 \\
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 0
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right] = \left[ \begin{array}{ccc}
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 1
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right]. \f]
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis corresponds to a rotation of \f$ \frac14\pi \f$ radians around
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththe z-axis.
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\include MatrixExponential.cpp
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixExponential.out
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\note \p M has to be a matrix of \c float, \c double, \c long double
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\c complex<float>, \c complex<double>, or \c complex<long double> .
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_log MatrixBase::log()
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix logarithm.
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  invertible matrix whose logarithm is to be computed.
174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns    expression representing the matrix logarithm root of \p M.
175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix logarithm of \f$ M \f$ is a matrix \f$ X \f$ such that 
177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f$ \exp(X) = M \f$ where exp denotes the matrix exponential. As for
178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththe scalar logarithm, the equation \f$ \exp(X) = M \f$ may have
179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathmultiple solutions; this function returns a matrix whose eigenvalues
180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathhave imaginary part in the interval \f$ (-\pi,\pi] \f$.
181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathIn the real case, the matrix \f$ M \f$ should be invertible and
183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathit should have no eigenvalues which are real and negative (pairs of
184c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcomplex conjugate eigenvalues are allowed). In the complex case, it
185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathonly needs to be invertible.
186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
187c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis function computes the matrix logarithm using the Schur-Parlett
188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathalgorithm as implemented by MatrixBase::matrixFunction(). The
189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathlogarithm of an atomic block is computed by MatrixLogarithmAtomic,
190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathwhich uses direct computation for 1-by-1 and 2-by-2 blocks and an
191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinverse scaling-and-squaring algorithm for bigger blocks, with the
192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathsquare roots computed by MatrixBase::sqrt().
193c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathDetails of the algorithm can be found in Section 11.6.2 of:
195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathNicholas J. Higham,
196c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath<em>Functions of Matrices: Theory and Computation</em>,
197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSIAM 2008. ISBN 978-0-898716-46-7.
198c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: The following program checks that
200c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \log \left[ \begin{array}{ccc} 
201c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 1
204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right] = \left[ \begin{array}{ccc}
205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & \frac14\pi & 0 \\ 
206c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      -\frac14\pi & 0 & 0 \\
207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 0 
208c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right]. \f]
209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis corresponds to a rotation of \f$ \frac14\pi \f$ radians around
210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththe z-axis. This is the inverse of the example used in the
211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathdocumentation of \ref matrixbase_exp "exp()".
212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\include MatrixLogarithm.cpp
214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixLogarithm.out
215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\note \p M has to be a matrix of \c float, \c double, <tt>long
2177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezdouble</tt>, \c complex<float>, \c complex<double>, or \c complex<long
2187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezdouble> .
219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\sa MatrixBase::exp(), MatrixBase::matrixFunction(), 
221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    class MatrixLogarithmAtomic, MatrixBase::sqrt().
222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_pow MatrixBase::pow()
2257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezCompute the matrix raised to arbitrary real power.
2277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\code
2297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezconst MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(RealScalar p) const
2307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\endcode
2317faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\param[in]  M  base of the matrix power, should be a square matrix.
2337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\param[in]  p  exponent of the matrix power, should be real.
2347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezThe matrix power \f$ M^p \f$ is defined as \f$ \exp(p \log(M)) \f$,
2367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezwhere exp denotes the matrix exponential, and log denotes the matrix
2377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezlogarithm.
2387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezThe matrix \f$ M \f$ should meet the conditions to be an argument of
2407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezmatrix logarithm. If \p p is not of the real scalar type of \p M, it
2417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezis casted into the real scalar type of \p M.
2427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezThis function computes the matrix power using the Schur-Pad&eacute;
2447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezalgorithm as implemented by class MatrixPower. The exponent is split
2457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezinto integral part and fractional part, where the fractional part is
2467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezin the interval \f$ (-1, 1) \f$. The main diagonal and the first
2477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezsuper-diagonal is directly computed.
2487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezDetails of the algorithm can be found in: Nicholas J. Higham and
2507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezLijing Lin, "A Schur-Pad&eacute; algorithm for fractional powers of a
2517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezmatrix," <em>SIAM J. %Matrix Anal. Applic.</em>,
2527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez<b>32(3)</b>:1056&ndash;1078, 2011.
2537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezExample: The following program checks that
2557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\f[ \left[ \begin{array}{ccc}
2567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      \cos1 & -\sin1 & 0 \\
2577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      \sin1 & \cos1 & 0 \\
2587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      0 & 0 & 1
2597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    \end{array} \right]^{\frac14\pi} = \left[ \begin{array}{ccc}
2607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
2617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
2627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      0 & 0 & 1
2637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    \end{array} \right]. \f]
2647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezThis corresponds to \f$ \frac14\pi \f$ rotations of 1 radian around
2657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezthe z-axis.
2667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\include MatrixPower.cpp
2687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezOutput: \verbinclude MatrixPower.out
2697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezMatrixBase::pow() is user-friendly. However, there are some
2717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezcircumstances under which you should use class MatrixPower directly.
2727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezMatrixPower can save the result of Schur decomposition, so it's
2737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezbetter for computing various powers for the same matrix.
2747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezExample:
2767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\include MatrixPower_optimal.cpp
2777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezOutput: \verbinclude MatrixPower_optimal.out
2787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\note \p M has to be a matrix of \c float, \c double, <tt>long
2807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezdouble</tt>, \c complex<float>, \c complex<double>, or \c complex<long
2817faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezdouble> .
2827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\sa MatrixBase::exp(), MatrixBase::log(), class MatrixPower.
2847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2867faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_matrixfunction MatrixBase::matrixFunction()
287c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
288c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute a matrix function.
289c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
290c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
291c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename internal::traits<Derived>::Scalar>::type f) const
292c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
293c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
294c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  argument of matrix function, should be a square matrix.
295c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  f  an entire function; \c f(x,n) should compute the n-th
296c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathderivative of f at x.
297c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns  expression representing \p f applied to \p M.
298c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
299c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSuppose that \p M is a matrix whose entries have type \c Scalar. 
300c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThen, the second argument, \p f, should be a function with prototype
301c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code 
302c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathComplexScalar f(ComplexScalar, int) 
303c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
304c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathwhere \c ComplexScalar = \c std::complex<Scalar> if \c Scalar is
305c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathreal (e.g., \c float or \c double) and \c ComplexScalar =
306c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\c Scalar if \c Scalar is complex. The return value of \c f(x,n)
307c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathshould be \f$ f^{(n)}(x) \f$, the n-th derivative of f at x.
308c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
309c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis routine uses the algorithm described in:
310c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathPhilip Davies and Nicholas J. Higham, 
311c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath"A Schur-Parlett algorithm for computing matrix functions", 
312c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath<em>SIAM J. %Matrix Anal. Applic.</em>, <b>25</b>:464&ndash;485, 2003.
313c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
314c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe actual work is done by the MatrixFunction class.
315c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
316c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: The following program checks that
317c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \exp \left[ \begin{array}{ccc} 
318c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & \frac14\pi & 0 \\ 
319c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      -\frac14\pi & 0 & 0 \\
320c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 0 
321c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right] = \left[ \begin{array}{ccc}
322c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
323c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
324c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      0 & 0 & 1
325c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right]. \f]
326c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis corresponds to a rotation of \f$ \frac14\pi \f$ radians around
327c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththe z-axis. This is the same example as used in the documentation
328c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathof \ref matrixbase_exp "exp()".
329c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
330c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\include MatrixFunction.cpp
331c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixFunction.out
332c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
333c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathNote that the function \c expfn is defined for complex numbers 
334c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\c x, even though the matrix \c A is over the reals. Instead of
335c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\c expfn, we could also have used StdStemFunctions::exp:
336c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
337c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathA.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
338c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
339c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
340c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
341c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_sin MatrixBase::sin()
343c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
344c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix sine.
345c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
346c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
347c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
348c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
349c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
350c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  a square matrix.
351c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns  expression representing \f$ \sin(M) \f$.
352c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
353c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
354c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
355c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: \include MatrixSine.cpp
356c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixSine.out
357c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
358c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
359c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_sinh MatrixBase::sinh()
361c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
362c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix hyperbolic sine.
363c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
364c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
365c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathMatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
366c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
367c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
368c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  a square matrix.
369c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns  expression representing \f$ \sinh(M) \f$
370c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
371c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThis function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
372c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
373c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: \include MatrixSinh.cpp
374c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixSinh.out
375c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
376c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez\subsection matrixbase_sqrt MatrixBase::sqrt()
378c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
379c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathCompute the matrix square root.
380c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
381c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\code
382c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
383c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\endcode
384c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
385c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\param[in]  M  invertible matrix whose square root is to be computed.
386c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\returns    expression representing the matrix square root of \p M.
387c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
388c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix square root of \f$ M \f$ is the matrix \f$ M^{1/2} \f$
389c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathwhose square is the original matrix; so if \f$ S = M^{1/2} \f$ then
390c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f$ S^2 = M \f$. 
391c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
392c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathIn the <b>real case</b>, the matrix \f$ M \f$ should be invertible and
393c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathit should have no eigenvalues which are real and negative (pairs of
394c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcomplex conjugate eigenvalues are allowed). In that case, the matrix
395c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathhas a square root which is also real, and this is the square root
396c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcomputed by this function. 
397c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
398c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe matrix square root is computed by first reducing the matrix to
399c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathquasi-triangular form with the real Schur decomposition. The square
400c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathroot of the quasi-triangular matrix can then be computed directly. The
401c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcost is approximately \f$ 25 n^3 \f$ real flops for the real Schur
402c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathdecomposition and \f$ 3\frac13 n^3 \f$ real flops for the remainder
403c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath(though the computation time in practice is likely more than this
404c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathindicates).
405c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
406c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathDetails of the algorithm can be found in: Nicholas J. Highan,
407c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath"Computing real square roots of a real matrix", <em>Linear Algebra
408c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathAppl.</em>, 88/89:405&ndash;430, 1987.
409c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
410c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathIf the matrix is <b>positive-definite symmetric</b>, then the square
411c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathroot is also positive-definite symmetric. In this case, it is best to
412c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathuse SelfAdjointEigenSolver::operatorSqrt() to compute it.
413c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
414c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathIn the <b>complex case</b>, the matrix \f$ M \f$ should be invertible;
415c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththis is a restriction of the algorithm. The square root computed by
416c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamaththis algorithm is the one whose eigenvalues have an argument in the
417c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinterval \f$ (-\frac12\pi, \frac12\pi] \f$. This is the usual branch
418c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcut.
419c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
420c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathThe computation is the same as in the real case, except that the
421c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcomplex Schur decomposition is used to reduce the matrix to a
422c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtriangular matrix. The theoretical cost is the same. Details are in:
423c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath&Aring;ke Bj&ouml;rck and Sven Hammarling, "A Schur method for the
424c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathsquare root of a matrix", <em>Linear Algebra Appl.</em>,
425c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath52/53:127&ndash;140, 1983.
426c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
427c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathExample: The following program checks that the square root of
428c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \left[ \begin{array}{cc} 
429c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              \cos(\frac13\pi) & -\sin(\frac13\pi) \\
430c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              \sin(\frac13\pi) & \cos(\frac13\pi)
431c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right], \f]
432c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathcorresponding to a rotation over 60 degrees, is a rotation over 30 degrees:
433c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\f[ \left[ \begin{array}{cc} 
434c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              \cos(\frac16\pi) & -\sin(\frac16\pi) \\
435c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              \sin(\frac16\pi) & \cos(\frac16\pi)
436c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    \end{array} \right]. \f]
437c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
438c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\include MatrixSquareRoot.cpp
439c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathOutput: \verbinclude MatrixSquareRoot.out
440c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
441c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath\sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
442c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    SelfAdjointEigenSolver::operatorSqrt().
443c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
444c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath*/
445c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
446c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_MATRIX_FUNCTIONS
447c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
448