Skip to content

Commit 80b6bac

Browse files
authored
Merge pull request #237 from mschauer/ms/expm2
Add complex matrix exponential for 2x2
2 parents 3fed14b + 4cfd95f commit 80b6bac

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/expm.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,28 @@ end
4040
(newtype)((m11, m21, m12, m22))
4141
end
4242

43-
# TODO add complex valued expm
43+
@inline function _expm(::Size{(2,2)}, A::StaticMatrix{<:Any,<:Any,<:Complex})
44+
T = typeof(exp(zero(eltype(A))))
45+
newtype = similar_type(A,T)
46+
47+
@inbounds a = A[1]
48+
@inbounds c = A[2]
49+
@inbounds b = A[3]
50+
@inbounds d = A[4]
51+
52+
z = sqrt((a - d)*(a - d) + 4*b*c )
53+
e = exp((a + d - z)/2)
54+
f = exp((a + d + z)/2)
55+
zr = inv(z)
56+
57+
m11 = (-e*(a - d - z) + f*(a - d + z)) * zr/2
58+
m12 = (f-e) * b * zr
59+
m21 = (f-e) * c * zr
60+
m22 = (-e*(-a + d - z) + f*(-a + d + z)) * zr/2
61+
62+
(newtype)((m11, m21, m12, m22))
63+
end
64+
4465
# TODO add special case for 3x3 matrices
4566

4667
@inline _expm(s::Size, A::StaticArray) = s(Base.expm(Array(A)))

test/expm.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
@test expm(@SMatrix [5 2; -2 1])::SMatrix expm([5 2; -2 1])
44
@test expm(@SMatrix [4 2; -2 1])::SMatrix expm([4 2; -2 1])
55
@test expm(@SMatrix [4 2; 2 1])::SMatrix expm([4 2; 2 1])
6+
@test expm(@SMatrix [ -3+1im -1+2im;-4-5im 5-2im])::SMatrix expm(Complex{Float64}[ -3+1im -1+2im;-4-5im 5-2im])
67
@test expm(@SMatrix [1 2 0; 2 1 0; 0 0 1])::SizedArray{Tuple{3,3}} expm([1 2 0; 2 1 0; 0 0 1])
78
end

0 commit comments

Comments
 (0)