MatrixFunctions 17 KB

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