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.

501 lines
17 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 <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, \c long double
  125. \c complex<float>, \c complex<double>, or \c 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, <tt>long
  170. double</tt>, \c complex<float>, \c complex<double>, or \c complex<long
  171. double> .
  172. \sa MatrixBase::exp(), MatrixBase::matrixFunction(),
  173. class MatrixLogarithmAtomic, MatrixBase::sqrt().
  174. \subsection matrixbase_pow MatrixBase::pow()
  175. Compute the matrix raised to arbitrary real power.
  176. \code
  177. const MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(RealScalar p) const
  178. \endcode
  179. \param[in] M base of the matrix power, should be a square matrix.
  180. \param[in] p exponent of the matrix power.
  181. The matrix power \f$ M^p \f$ is defined as \f$ \exp(p \log(M)) \f$,
  182. where exp denotes the matrix exponential, and log denotes the matrix
  183. logarithm. This is different from raising all the entries in the matrix
  184. to the p-th power. Use ArrayBase::pow() if you want to do the latter.
  185. If \p p is complex, the scalar type of \p M should be the type of \p
  186. p . \f$ M^p \f$ simply evaluates into \f$ \exp(p \log(M)) \f$.
  187. Therefore, the matrix \f$ M \f$ should meet the conditions to be an
  188. argument of matrix logarithm.
  189. If \p p is real, it is casted into the real scalar type of \p M. Then
  190. this function computes the matrix power using the Schur-Pad&eacute;
  191. algorithm as implemented by class MatrixPower. The exponent is split
  192. into integral part and fractional part, where the fractional part is
  193. in the interval \f$ (-1, 1) \f$. The main diagonal and the first
  194. super-diagonal is directly computed.
  195. If \p M is singular with a semisimple zero eigenvalue and \p p is
  196. positive, the Schur factor \f$ T \f$ is reordered with Givens
  197. rotations, i.e.
  198. \f[ T = \left[ \begin{array}{cc}
  199. T_1 & T_2 \\
  200. 0 & 0
  201. \end{array} \right] \f]
  202. where \f$ T_1 \f$ is invertible. Then \f$ T^p \f$ is given by
  203. \f[ T^p = \left[ \begin{array}{cc}
  204. T_1^p & T_1^{-1} T_1^p T_2 \\
  205. 0 & 0
  206. \end{array}. \right] \f]
  207. \warning Fractional power of a matrix with a non-semisimple zero
  208. eigenvalue is not well-defined. We introduce an assertion failure
  209. against inaccurate result, e.g. \code
  210. #include <unsupported/Eigen/MatrixFunctions>
  211. #include <iostream>
  212. int main()
  213. {
  214. StormEigen::Matrix4d A;
  215. A << 0, 0, 2, 3,
  216. 0, 0, 4, 5,
  217. 0, 0, 6, 7,
  218. 0, 0, 8, 9;
  219. std::cout << A.pow(0.37) << std::endl;
  220. // The 1 makes eigenvalue 0 non-semisimple.
  221. A.coeffRef(0, 1) = 1;
  222. // This fails if EIGEN_NO_DEBUG is undefined.
  223. std::cout << A.pow(0.37) << std::endl;
  224. return 0;
  225. }
  226. \endcode
  227. Details of the algorithm can be found in: Nicholas J. Higham and
  228. Lijing Lin, "A Schur-Pad&eacute; algorithm for fractional powers of a
  229. matrix," <em>SIAM J. %Matrix Anal. Applic.</em>,
  230. <b>32(3)</b>:1056&ndash;1078, 2011.
  231. Example: The following program checks that
  232. \f[ \left[ \begin{array}{ccc}
  233. \cos1 & -\sin1 & 0 \\
  234. \sin1 & \cos1 & 0 \\
  235. 0 & 0 & 1
  236. \end{array} \right]^{\frac14\pi} = \left[ \begin{array}{ccc}
  237. \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
  238. \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
  239. 0 & 0 & 1
  240. \end{array} \right]. \f]
  241. This corresponds to \f$ \frac14\pi \f$ rotations of 1 radian around
  242. the z-axis.
  243. \include MatrixPower.cpp
  244. Output: \verbinclude MatrixPower.out
  245. MatrixBase::pow() is user-friendly. However, there are some
  246. circumstances under which you should use class MatrixPower directly.
  247. MatrixPower can save the result of Schur decomposition, so it's
  248. better for computing various powers for the same matrix.
  249. Example:
  250. \include MatrixPower_optimal.cpp
  251. Output: \verbinclude MatrixPower_optimal.out
  252. \note \p M has to be a matrix of \c float, \c double, <tt>long
  253. double</tt>, \c complex<float>, \c complex<double>, or \c complex<long
  254. double> .
  255. \sa MatrixBase::exp(), MatrixBase::log(), class MatrixPower.
  256. \subsection matrixbase_matrixfunction MatrixBase::matrixFunction()
  257. Compute a matrix function.
  258. \code
  259. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename internal::traits<Derived>::Scalar>::type f) const
  260. \endcode
  261. \param[in] M argument of matrix function, should be a square matrix.
  262. \param[in] f an entire function; \c f(x,n) should compute the n-th
  263. derivative of f at x.
  264. \returns expression representing \p f applied to \p M.
  265. Suppose that \p M is a matrix whose entries have type \c Scalar.
  266. Then, the second argument, \p f, should be a function with prototype
  267. \code
  268. ComplexScalar f(ComplexScalar, int)
  269. \endcode
  270. where \c ComplexScalar = \c std::complex<Scalar> if \c Scalar is
  271. real (e.g., \c float or \c double) and \c ComplexScalar =
  272. \c Scalar if \c Scalar is complex. The return value of \c f(x,n)
  273. should be \f$ f^{(n)}(x) \f$, the n-th derivative of f at x.
  274. This routine uses the algorithm described in:
  275. Philip Davies and Nicholas J. Higham,
  276. "A Schur-Parlett algorithm for computing matrix functions",
  277. <em>SIAM J. %Matrix Anal. Applic.</em>, <b>25</b>:464&ndash;485, 2003.
  278. The actual work is done by the MatrixFunction class.
  279. Example: The following program checks that
  280. \f[ \exp \left[ \begin{array}{ccc}
  281. 0 & \frac14\pi & 0 \\
  282. -\frac14\pi & 0 & 0 \\
  283. 0 & 0 & 0
  284. \end{array} \right] = \left[ \begin{array}{ccc}
  285. \frac12\sqrt2 & -\frac12\sqrt2 & 0 \\
  286. \frac12\sqrt2 & \frac12\sqrt2 & 0 \\
  287. 0 & 0 & 1
  288. \end{array} \right]. \f]
  289. This corresponds to a rotation of \f$ \frac14\pi \f$ radians around
  290. the z-axis. This is the same example as used in the documentation
  291. of \ref matrixbase_exp "exp()".
  292. \include MatrixFunction.cpp
  293. Output: \verbinclude MatrixFunction.out
  294. Note that the function \c expfn is defined for complex numbers
  295. \c x, even though the matrix \c A is over the reals. Instead of
  296. \c expfn, we could also have used StdStemFunctions::exp:
  297. \code
  298. A.matrixFunction(StdStemFunctions<std::complex<double> >::exp, &B);
  299. \endcode
  300. \subsection matrixbase_sin MatrixBase::sin()
  301. Compute the matrix sine.
  302. \code
  303. const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
  304. \endcode
  305. \param[in] M a square matrix.
  306. \returns expression representing \f$ \sin(M) \f$.
  307. This function computes the matrix sine. Use ArrayBase::sin() for computing the entry-wise sine.
  308. The implementation calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sin().
  309. Example: \include MatrixSine.cpp
  310. Output: \verbinclude MatrixSine.out
  311. \subsection matrixbase_sinh MatrixBase::sinh()
  312. Compute the matrix hyperbolic sine.
  313. \code
  314. MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
  315. \endcode
  316. \param[in] M a square matrix.
  317. \returns expression representing \f$ \sinh(M) \f$
  318. This function calls \ref matrixbase_matrixfunction "matrixFunction()" with StdStemFunctions::sinh().
  319. Example: \include MatrixSinh.cpp
  320. Output: \verbinclude MatrixSinh.out
  321. \subsection matrixbase_sqrt MatrixBase::sqrt()
  322. Compute the matrix square root.
  323. \code
  324. const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
  325. \endcode
  326. \param[in] M invertible matrix whose square root is to be computed.
  327. \returns expression representing the matrix square root of \p M.
  328. The matrix square root of \f$ M \f$ is the matrix \f$ M^{1/2} \f$
  329. whose square is the original matrix; so if \f$ S = M^{1/2} \f$ then
  330. \f$ S^2 = M \f$. This is different from taking the square root of all
  331. the entries in the matrix; use ArrayBase::sqrt() if you want to do the
  332. latter.
  333. In the <b>real case</b>, the matrix \f$ M \f$ should be invertible and
  334. it should have no eigenvalues which are real and negative (pairs of
  335. complex conjugate eigenvalues are allowed). In that case, the matrix
  336. has a square root which is also real, and this is the square root
  337. computed by this function.
  338. The matrix square root is computed by first reducing the matrix to
  339. quasi-triangular form with the real Schur decomposition. The square
  340. root of the quasi-triangular matrix can then be computed directly. The
  341. cost is approximately \f$ 25 n^3 \f$ real flops for the real Schur
  342. decomposition and \f$ 3\frac13 n^3 \f$ real flops for the remainder
  343. (though the computation time in practice is likely more than this
  344. indicates).
  345. Details of the algorithm can be found in: Nicholas J. Highan,
  346. "Computing real square roots of a real matrix", <em>Linear Algebra
  347. Appl.</em>, 88/89:405&ndash;430, 1987.
  348. If the matrix is <b>positive-definite symmetric</b>, then the square
  349. root is also positive-definite symmetric. In this case, it is best to
  350. use SelfAdjointEigenSolver::operatorSqrt() to compute it.
  351. In the <b>complex case</b>, the matrix \f$ M \f$ should be invertible;
  352. this is a restriction of the algorithm. The square root computed by
  353. this algorithm is the one whose eigenvalues have an argument in the
  354. interval \f$ (-\frac12\pi, \frac12\pi] \f$. This is the usual branch
  355. cut.
  356. The computation is the same as in the real case, except that the
  357. complex Schur decomposition is used to reduce the matrix to a
  358. triangular matrix. The theoretical cost is the same. Details are in:
  359. &Aring;ke Bj&ouml;rck and Sven Hammarling, "A Schur method for the
  360. square root of a matrix", <em>Linear Algebra Appl.</em>,
  361. 52/53:127&ndash;140, 1983.
  362. Example: The following program checks that the square root of
  363. \f[ \left[ \begin{array}{cc}
  364. \cos(\frac13\pi) & -\sin(\frac13\pi) \\
  365. \sin(\frac13\pi) & \cos(\frac13\pi)
  366. \end{array} \right], \f]
  367. corresponding to a rotation over 60 degrees, is a rotation over 30 degrees:
  368. \f[ \left[ \begin{array}{cc}
  369. \cos(\frac16\pi) & -\sin(\frac16\pi) \\
  370. \sin(\frac16\pi) & \cos(\frac16\pi)
  371. \end{array} \right]. \f]
  372. \include MatrixSquareRoot.cpp
  373. Output: \verbinclude MatrixSquareRoot.out
  374. \sa class RealSchur, class ComplexSchur, class MatrixSquareRoot,
  375. SelfAdjointEigenSolver::operatorSqrt().
  376. */
  377. #endif // EIGEN_MATRIX_FUNCTIONS