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.

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