MatrixFunctions 17 KB

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