Skip to content

Commit 5440787

Browse files
committed
Add complex matrix exponential for 2x2
1 parent 2e9feff commit 5440787

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/expm.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,27 @@ 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.0*b*c )
53+
e = exp(a/2.0 + d/2.0 - z/2.0)
54+
f = exp(a/2.0 + d/2.0 + z/2.0)
55+
56+
m11 = -(e*(a - d - z))/(2.0* z) + (f*(a - d + z))/(2.0* z)
57+
m12 = -((e * b)/z) + (f * b)/z
58+
m21 = -((e * c)/z) + (f * c)/z
59+
m22 = -(e*(-a + d - z))/(2.0* z) + (f*(-a + d + z))/(2.0* z)
60+
61+
(newtype)((m11, m21, m12, m22))
62+
end
63+
4464
# TODO add special case for 3x3 matrices
4565

4666
@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)