Matrix Functions
This package contains methods for computing special functions on matrices, such as exponential, logarithm, trigonometric functions, etc. Note they are not the same as applying the special function to each entry of the matrix. Instead, they can usually be defined as the limit of the Taylor series for the special function applied to the matrix.
Methods
================================ ============ Method Description ================================ ============ expm_ (A) Exponential of a matrix logm_ (A) Logarithm of a matrix cosm_ (A) Cosine of a matrix sinm_ (A) Sine of a matrix tanm_ (A) Tangent function of a matrix coshm_ (A) Hyperbolic cosine of a matrix sinhm_ (A) Hyperbolic sine of a matrix tanhm_ (A) Hyperbolic tangent of a matrix signm_ (A) Sign of a matrix sqrtm_ (A) Square root of a matrix ================================ ============
Description
.. _expm:
.. function:: expm(a)
:param a: Matrix<Float64>
:return: Matrix<Float64>
.. function:: expm(a)
:param a: Matrix<Complex64>
:return: Matrix<Complex64>
Computes the exponential of a matrix. The exponential function is defined
as the limit of the following series:
.. math::
\exp(A) = I + A + \frac{A^2}{2!} + \frac{A^3}{3!} + \cdots
The computation is performed using Padé approximants and dividing-and-squaring
to get the input for Padé approximation to be close to zero.
Example:
1. Compute the exponential of a 2-by-2 matrix.
::
var a1 = matrix<Float64>(
[[1.0, 2.0],
[-1.0, 3.0]]
)
expm(a1)
The result is:
::
matrix<Float64>(
[[-2.225352, 12.435353],
[-6.217676, 10.210000]]
)
.. _logm:
.. function:: logm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
Computes the logarithm of a matrix. Implementation follows Section 9.4 of
*Matrix Computations, 4th edition*.
The computation is performed using (3,3) Padé approximants of :math:`\log(A)`
around :math:`A = I`, given by the following formula
.. math::
\log(A) &\approx D(A)^{-1}N(A)
D(A) &= 60I + 90(A - I) + 36(A - I)^2 + 3(A - I)^3
N(A) &= 60(A - I) + 60(A - I)^2 + 11(A - I)^3
This is combined with inverse scaling and squaring to get the input for
Padé approximation close to identity.
Example:
1. Compute the logarithm of a 2-by-2 matrix.
::
var a = matrix<Float64>([
[1.0, 3.0],
[1.0, 4.0]
])
logm(a)
The result is:
::
matrix<Float64>([
[-1.02571087, 2.05142174],
[ 0.68380725, 1.02571087]
])
.. _cosm:
.. function:: cosm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: cosm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the cosine of a matrix. This is defined (and computed) using the
formula
.. math::
\cos(A) = \frac{\exp(iA) + \exp(-iA)}{2}
Example:
1. Compute the cosine of a 2-by-2 matrix.
::
var a = matrix<Float64>(
[[1.0, 2.0],
[-1.0, 3.0]]
)
cosm(a)
The result is:
::
matrix<Float64>(
[[0.4264593, -2.13721484],
[1.06860742, -1.71075555]]
)
.. _sinm:
.. function:: sinm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: sinm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the sine of a matrix. This is defined (and computed) using the
formula
.. math::
\sin(A) = \frac{\exp(iA) - \exp(-iA)}{2i}
Example:
1. Compute the sine of a 2-by-2 matrix.
::
var a = matrix<Float64>(
[[1.0, 2.0],
[-1.0, 3.0]]
)
sinm(a)
The result is:
::
matrix<Float64>(
[[1.89217551, -0.97811252],
[0.48905626, 0.91406299]]
)
.. _tanm:
.. function:: tanm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: tanm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the tangent of a matrix. This is defined (and computed) by
the formula
.. math::
\tan(A) = \cos(A)^{-1}\sin(A)
using solving linear equations rather than taking inverse directly.
.. _coshm:
.. function:: coshm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: coshm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the hyperbolic cosine of a matrix. This is defined (and computed)
by the formula
.. math::
\cosh(A) = \frac{e^A + e^{-A}}{2}
.. _sinhm:
.. function:: sinhm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: sinhm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the hyperbolic sine of a matrix. This is defined (and computed)
by the formula
.. math::
\sinh(A) = \frac{e^A - e^{-A}}{2}
.. _tanhm:
.. function:: tanhm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
.. function:: tanhm(A)
:param A: Matrix<Complex64>
:return: Matrix<Complex64>
Compute the hyperbolic tangent of a matrix. This is defined (and computed)
by the formula
.. math::
\tanh(A) = \cosh(A)^{-1}\sinh(A)
using solving linear equations rather than taking inverse directly.
.. _signm:
.. function:: signm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
Compute the matrix sign function. The implementation follows Section 9.4
of *Matrix Computations, 4th edition*. It uses an iteration with
:math:`S_0 = A` and
.. math::
S_{k+1} = \frac{S_k + S_k^{-1}}{2}
Example:
1. Compute the matrix sign function of a 3-by-3 matrix.
::
var a = matrix<Float64>([
[1.0, 2.0, 3.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 1.0]
])
signm(a)
The result is:
::
matrix<Float64>([
[-0.13127464, 0.15312833, 1.83967663],
[ 0.22292636, 0.96982489, -0.36252242],
[ 0.51565075, -0.06979803, 0.16144975]
])
.. _sqrtm:
.. function:: sqrtm(A)
:param A: Matrix<Float64>
:return: Matrix<Float64>
Compute the square root of a matrix. The implementation follows Section 9.4
of *Matrix Computations, 4th edition*. It uses an iteration with
:math:`X_0 = A`, :math:`Y_0 = I` and
.. math::
X_{k+1} = \frac{X_k + Y_k^{-1}}{2}, \quad Y_{k+1} = \frac{Y_k + X_k^{-1}}{2}
**Note.** This algorithm does not terminate in some cases, in particular when
taking the square root requires complex numbers.
Example:
1. Compute the square root of a 2-by-2 matrix.
::
var a = matrix<Float64>([
[1.0, 3.0],
[1.0, 4.0]
])
sqrtm(a)
The result is:
::
matrix<Float64>([
[ 0.75592895, 1.13389342],
[ 0.37796447, 1.88982237]
])