You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
13 KiB

  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. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_MATRIX_FUNCTIONS
  10. #define EIGEN_MATRIX_FUNCTIONS
  11. #include <cfloat>
  12. #include <list>
  13. #include <functional>
  14. #include <iterator>
  15. #include <Eigen/Core>
  16. #include <Eigen/LU>
  17. #include <Eigen/Eigenvalues>
  18. /** \ingroup Unsupported_modules
  19. * \defgroup MatrixFunctions_Module Matrix functions module
  20. * \brief This module aims to provide various methods for the computation of
  21. * matrix functions.
  22. *
  23. * To use this module, add
  24. * \code
  25. * #include <unsupported/Eigen/MatrixFunctions>
  26. * \endcode
  27. * at the start of your source file.
  28. *
  29. * This module defines the following MatrixBase methods.
  30. * - \ref matrixbase_cos "MatrixBase::cos()", for computing the matrix cosine
  31. * - \ref matrixbase_cosh "MatrixBase::cosh()", for computing the matrix hyperbolic cosine
  32. * - \ref matrixbase_exp "MatrixBase::exp()", for computing the matrix exponential
  33. * - \ref matrixbase_log "MatrixBase::log()", for computing the matrix logarithm
  34. * - \ref matrixbase_matrixfunction "MatrixBase::matrixFunction()", for computing general matrix functions
  35. * - \ref matrixbase_sin "MatrixBase::sin()", for computing the matrix sine
  36. * - \ref matrixbase_sinh "MatrixBase::sinh()", for computing the matrix hyperbolic sine
  37. * - \ref matrixbase_sqrt "MatrixBase::sqrt()", for computing the matrix square root
  38. *
  39. * These methods are the main entry points to this module.
  40. *
  41. * %Matrix functions are defined as follows. Suppose that \f$ f \f$
  42. * is an entire function (that is, a function on the complex plane
  43. * that is everywhere complex differentiable). Then its Taylor
  44. * series
  45. * \f[ f(0) + f'(0) x + \frac{f''(0)}{2} x^2 + \frac{f'''(0)}{3!} x^3 + \cdots \f]
  46. * converges to \f$ f(x) \f$. In this case, we can define the matrix
  47. * function by the same series:
  48. * \f[ f(M) = f(0) + f'(0) M + \frac{f''(0)}{2} M^2 + \frac{f'''(0)}{3!} M^3 + \cdots \f]
  49. *
  50. */
  51. #include "src/MatrixFunctions/MatrixExponential.h"
  52. #include "src/MatrixFunctions/MatrixFunction.h"
  53. #include "src/MatrixFunctions/MatrixSquareRoot.h"
  54. #include "src/MatrixFunctions/MatrixLogarithm.h"
  55. /**
  56. \page matrixbaseextra MatrixBase methods defined in the MatrixFunctions module
  57. \ingroup MatrixFunctions_Module
  58. The remainder of the page documents the following MatrixBase methods
  59. which are defined in the MatrixFunctions module.
  60. \section matrixbase_cos MatrixBase::cos()
  61. Compute the matrix cosine.
  62. \code
  63. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
  64. \endcode
  65. \param[in] M a square matrix.
  66. \returns expression representing \f$ \cos(M) \f$.
  67. This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cos().
  68. \sa \ref matrixbase_sin "sin()" for an example.
  69. \section matrixbase_cosh MatrixBase::cosh()
  70. Compute the matrix hyberbolic cosine.
  71. \code
  72. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
  73. \endcode
  74. \param[in] M a square matrix.
  75. \returns expression representing \f$ \cosh(M) \f$
  76. This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::cosh().
  77. \sa \ref matrixbase_sinh "sinh()" for an example.
  78. \section matrixbase_exp MatrixBase::exp()
  79. Compute the matrix exponential.
  80. \code
  81. const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
  82. \endcode
  83. \param[in] M matrix whose exponential is to be computed.
  84. \returns expression representing the matrix exponential of \p M.
  85. The matrix exponential of \f$ M \f$ is defined by
  86. \f[ \exp(M) = \sum_{k=0}^\infty \frac{M^k}{k!}. \f]
  87. The matrix exponential can be used to solve linear ordinary
  88. differential equations: the solution of \f$ y' = My \f$ with the
  89. initial condition \f$ y(0) = y_0 \f$ is given by
  90. \f$ y(t) = \exp(M) y_0 \f$.
  91. The cost of the computation is approximately \f$ 20 n^3 \f$ for
  92. matrices of size \f$ n \f$. The number 20 depends weakly on the
  93. norm of the matrix.
  94. The matrix exponential is computed using the scaling-and-squaring
  95. method combined with Pad&eacute; approximation. The matrix is first
  96. rescaled, then the exponential of the reduced matrix is computed
  97. approximant, and then the rescaling is undone by repeated
  98. squaring. The degree of the Pad&eacute; approximant is chosen such
  99. that the approximation error is less than the round-off
  100. error. However, errors may accumulate during the squaring phase.
  101. Details of the algorithm can be found in: Nicholas J. Higham, "The
  102. scaling and squaring method for the matrix exponential revisited,"
  103. <em>SIAM J. %Matrix Anal. Applic.</em>, <b>26</b>:1179&ndash;1193,
  104. 2005.
  105. Example: The following program checks that
  106. \f[ \exp \left[ \begin{array}{ccc}
  107. 0 & \frac14\pi & 0 \\
  108. -\frac14\pi & 0 & 0 \\
  109. 0 & 0 & 0
  110. \end{array} \right] = \left[ \begin{array}{ccc}
  111. \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
  112. \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
  113. 0 & 0 & 1
  114. \end{array} \right]. \f]
  115. This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
  116. the z-axis.
  117. \include MatrixExponential.cpp
  118. Output: \verbinclude MatrixExponential.out
  119. \note \p M has to be a matrix of \c float, \c double, \c long double
  120. \c complex<float>, \c complex<double>, or \c complex<long double> .
  121. \section matrixbase_log MatrixBase::log()
  122. Compute the matrix logarithm.
  123. \code
  124. const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
  125. \endcode
  126. \param[in] M invertible matrix whose logarithm is to be computed.
  127. \returns expression representing the matrix logarithm root of \p M.
  128. The matrix logarithm of \f$ M \f$ is a matrix \f$ X \f$ such that
  129. \f$ \exp(X) = M \f$ where exp denotes the matrix exponential. As for
  130. the scalar logarithm, the equation \f$ \exp(X) = M \f$ may have
  131. multiple solutions; this function returns a matrix whose eigenvalues
  132. have imaginary part in the interval \f$ (-\pi,\pi] \f$.
  133. In the real case, the matrix \f$ M \f$ should be invertible and
  134. it should have no eigenvalues which are real and negative (pairs of
  135. complex conjugate eigenvalues are allowed). In the complex case, it
  136. only needs to be invertible.
  137. This function computes the matrix logarithm using the Schur-Parlett
  138. algorithm as implemented by MatrixBase::matrixFunction(). The
  139. logarithm of an atomic block is computed by MatrixLogarithmAtomic,
  140. which uses direct computation for 1-by-1 and 2-by-2 blocks and an
  141. inverse scaling-and-squaring algorithm for bigger blocks, with the
  142. square roots computed by MatrixBase::sqrt().
  143. Details of the algorithm can be found in Section 11.6.2 of:
  144. Nicholas J. Higham,
  145. <em>Functions of Matrices: Theory and Computation</em>,
  146. SIAM 2008. ISBN 978-0-898716-46-7.
  147. Example: The following program checks that
  148. \f[ \log \left[ \begin{array}{ccc}
  149. \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
  150. \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
  151. 0 & 0 & 1
  152. \end{array} \right] = \left[ \begin{array}{ccc}
  153. 0 & \frac14\pi & 0 \\
  154. -\frac14\pi & 0 & 0 \\
  155. 0 & 0 & 0
  156. \end{array} \right]. \f]
  157. This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
  158. the z-axis. This is the inverse of the example used in the
  159. documentation of \ref matrixbase_exp "exp()".
  160. \include MatrixLogarithm.cpp
  161. Output: \verbinclude MatrixLogarithm.out
  162. \note \p M has to be a matrix of \c float, \c double, \c long double
  163. \c complex<float>, \c complex<double>, or \c complex<long double> .
  164. \sa MatrixBase::exp(), MatrixBase::matrixFunction(),
  165. class MatrixLogarithmAtomic, MatrixBase::sqrt().
  166. \section matrixbase_matrixfunction MatrixBase::matrixFunction()
  167. Compute a matrix function.
  168. \code
  169. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename internal::traits<Derived>::Scalar>::type f) const
  170. \endcode
  171. \param[in] M argument of matrix function, should be a square matrix.
  172. \param[in] f an entire function; \c f(x,n) should compute the n-th
  173. derivative of f at x.
  174. \returns expression representing \p f applied to \p M.
  175. Suppose that \p M is a matrix whose entries have type \c Scalar.
  176. Then, the second argument, \p f, should be a function with prototype
  177. \code
  178. ComplexScalar f(ComplexScalar, int)
  179. \endcode
  180. where \c ComplexScalar = \c std::complex<Scalar> if \c Scalar is
  181. real (e.g., \c float or \c double) and \c ComplexScalar =
  182. \c Scalar if \c Scalar is complex. The return value of \c f(x,n)
  183. should be \f$ f^{(n)}(x) \f$, the n-th derivative of f at x.
  184. This routine uses the algorithm described in:
  185. Philip Davies and Nicholas J. Higham,
  186. "A Schur-Parlett algorithm for computing matrix functions",
  187. <em>SIAM J. %Matrix Anal. Applic.</em>, <b>25</b>:464&ndash;485, 2003.
  188. The actual work is done by the MatrixFunction class.
  189. Example: The following program checks that
  190. \f[ \exp \left[ \begin{array}{ccc}
  191. 0 & \frac14\pi & 0 \\
  192. -\frac14\pi & 0 & 0 \\
  193. 0 & 0 & 0
  194. \end{array} \right] = \left[ \begin{array}{ccc}
  195. \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
  196. \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
  197. 0 & 0 & 1
  198. \end{array} \right]. \f]
  199. This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
  200. the z-axis. This is the same example as used in the documentation
  201. of \ref matrixbase_exp "exp()".
  202. \include MatrixFunction.cpp
  203. Output: \verbinclude MatrixFunction.out
  204. Note that the function \c expfn is defined for complex numbers
  205. \c x, even though the matrix \c A is over the reals. Instead of
  206. \c expfn, we could also have used StdStemFunctions::exp:
  207. \code
  208. A.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
  209. \endcode
  210. \section matrixbase_sin MatrixBase::sin()
  211. Compute the matrix sine.
  212. \code
  213. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
  214. \endcode
  215. \param[in] M a square matrix.
  216. \returns expression representing \f$ \sin(M) \f$.
  217. This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
  218. Example: \include MatrixSine.cpp
  219. Output: \verbinclude MatrixSine.out
  220. \section matrixbase_sinh MatrixBase::sinh()
  221. Compute the matrix hyperbolic sine.
  222. \code
  223. MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
  224. \endcode
  225. \param[in] M a square matrix.
  226. \returns expression representing \f$ \sinh(M) \f$
  227. This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
  228. Example: \include MatrixSinh.cpp
  229. Output: \verbinclude MatrixSinh.out
  230. \section matrixbase_sqrt MatrixBase::sqrt()
  231. Compute the matrix square root.
  232. \code
  233. const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
  234. \endcode
  235. \param[in] M invertible matrix whose square root is to be computed.
  236. \returns expression representing the matrix square root of \p M.
  237. The matrix square root of \f$ M \f$ is the matrix \f$ M^{1/2} \f$
  238. whose square is the original matrix; so if \f$ S = M^{1/2} \f$ then
  239. \f$ S^2 = M \f$.
  240. In the <b>real case</b>, the matrix \f$ M \f$ should be invertible and
  241. it should have no eigenvalues which are real and negative (pairs of
  242. complex conjugate eigenvalues are allowed). In that case, the matrix
  243. has a square root which is also real, and this is the square root
  244. computed by this function.
  245. The matrix square root is computed by first reducing the matrix to
  246. quasi-triangular form with the real Schur decomposition. The square
  247. root of the quasi-triangular matrix can then be computed directly. The
  248. cost is approximately \f$ 25 n^3 \f$ real flops for the real Schur
  249. decomposition and \f$ 3\frac13 n^3 \f$ real flops for the remainder
  250. (though the computation time in practice is likely more than this
  251. indicates).
  252. Details of the algorithm can be found in: Nicholas J. Highan,
  253. "Computing real square roots of a real matrix", <em>Linear Algebra
  254. Appl.</em>, 88/89:405&ndash;430, 1987.
  255. If the matrix is <b>positive-definite symmetric</b>, then the square
  256. root is also positive-definite symmetric. In this case, it is best to
  257. use SelfAdjointEigenSolver::operatorSqrt() to compute it.
  258. In the <b>complex case</b>, the matrix \f$ M \f$ should be invertible;
  259. this is a restriction of the algorithm. The square root computed by
  260. this algorithm is the one whose eigenvalues have an argument in the
  261. interval \f$ (-\frac12\pi, \frac12\pi] \f$. This is the usual branch
  262. cut.
  263. The computation is the same as in the real case, except that the
  264. complex Schur decomposition is used to reduce the matrix to a
  265. triangular matrix. The theoretical cost is the same. Details are in:
  266. &Aring;ke Bj&ouml;rck and Sven Hammarling, "A Schur method for the
  267. square root of a matrix", <em>Linear Algebra Appl.</em>,
  268. 52/53:127&ndash;140, 1983.
  269. Example: The following program checks that the square root of
  270. \f[ \left[ \begin{array}{cc}
  271. \cos(\frac13\pi) & -\sin(\frac13\pi) \\
  272. \sin(\frac13\pi) & \cos(\frac13\pi)
  273. \end{array} \right], \f]
  274. corresponding to a rotation over 60 degrees, is a rotation over 30 degrees:
  275. \f[ \left[ \begin{array}{cc}
  276. \cos(\frac16\pi) & -\sin(\frac16\pi) \\
  277. \sin(\frac16\pi) & \cos(\frac16\pi)
  278. \end{array} \right]. \f]
  279. \include MatrixSquareRoot.cpp
  280. Output: \verbinclude MatrixSquareRoot.out
  281. \sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
  282. SelfAdjointEigenSolver::operatorSqrt().
  283. */
  284. #endif // EIGEN_MATRIX_FUNCTIONS