-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@inline expm(A::StaticMatrix) = _expm(Size(A), A) | ||
|
||
@inline function _expm(::Size{(1,1)}, A::StaticMatrix) | ||
T = typeof(exp(zero(eltype(A)))) | ||
newtype = similar_type(A,T) | ||
|
||
(newtype)((exp(A[1]), )) | ||
end | ||
|
||
@inline function _expm{S<:Real}(::Size{(2,2)}, A::StaticMatrix{S}) | ||
T = typeof(exp(zero(eltype(A)))) | ||
newtype = similar_type(A,T) | ||
|
||
@inbounds a = A[1] | ||
@inbounds c = A[2] | ||
@inbounds b = A[3] | ||
@inbounds d = A[4] | ||
|
||
v = (a-d)^2 + 4*b*c | ||
|
||
if v > 0 | ||
z = sqrt(v) | ||
z1 = cosh(z / 2) | ||
z2 = sinh(z / 2) / z | ||
elseif v < 0 | ||
z = sqrt(-v) | ||
z1 = cos(z / 2) | ||
z2 = sin(z / 2) / z | ||
else # if v == 0 | ||
z1 = T(1.0) | ||
z2 = T(0.5) | ||
end | ||
|
||
r = exp((a + d) / 2) | ||
m11 = r * (z1 + (a - d) * z2) | ||
m12 = r * 2b * z2 | ||
m21 = r * 2c * z2 | ||
m22 = r * (z1 - (a - d) * z2) | ||
|
||
(newtype)((m11, m21, m12, m22)) | ||
end | ||
|
||
# TODO add complex valued expm | ||
# TODO add special case for 3x3 matrices | ||
|
||
@inline _expm(s::Size, A::StaticArray) = s(Base.expm(Array(A))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@testset "Matrix exponential" begin | ||
@test expm(@SMatrix [2])::SMatrix ≈ SMatrix{1,1}(expm(2)) | ||
@test expm(@SMatrix [5 2; -2 1])::SMatrix ≈ expm([5 2; -2 1]) | ||
@test expm(@SMatrix [4 2; -2 1])::SMatrix ≈ expm([4 2; -2 1]) | ||
@test expm(@SMatrix [4 2; 2 1])::SMatrix ≈ expm([4 2; 2 1]) | ||
@test expm(@SMatrix [1 2 0; 2 1 0; 0 0 1])::SizedArray{(3,3)} ≈ expm([1 2 0; 2 1 0; 0 0 1]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters