Skip to content

Commit e77e3ef

Browse files
committed
Use expm1 for added precision
1 parent b086235 commit e77e3ef

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/expm.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ end
5050
@inbounds d = A[4]
5151

5252
z = sqrt((a - d)*(a - d) + 4*b*c )
53-
e = exp((a + d - z)/2)
54-
f = exp((a + d + z)/2)
53+
e = expm1((a + d - z) / 2)
54+
f = expm1((a + d + z) / 2)
5555
ϵ = eps()
5656
g = abs2(z) < ϵ^2 ? exp((a + d) / 2) * (1 + z^2 / 24) : (f - e) / z
5757

58-
m11 = (g * (a - d) + f + e) / 2
58+
m11 = (g * (a - d) + f + e) / 2 + 1
5959
m12 = g * b
6060
m21 = g * c
61-
m22 = (-g * (a - d) + f + e) / 2
61+
m22 = (-g * (a - d) + f + e) / 2 + 1
6262

6363
(newtype)((m11, m21, m12, m22))
6464
end

test/expm.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using StaticArrays, Test, LinearAlgebra
88
@test exp(@SMatrix [ -3+1im -1+2im;-4-5im 5-2im])::SMatrix exp(Complex{Float64}[ -3+1im -1+2im;-4-5im 5-2im])
99
# test for case `(a[1,1] - a[2,2])^2 + 4*a[2,1]*a[1,2] == 0`
1010
@test exp(@SMatrix [2+2im 1-1im; -2-2im 6+2im])::SMatrix exp(Complex{Float64}[2+2im 1-1im; -2-2im 6+2im])
11+
# test for zeros matrix
12+
@test exp(@SMatrix zeros(Complex{Float64}, 2, 2))::SMatrix Complex{Float64}[1 0; 0 1]
1113
@test exp(@SMatrix [1 2 0; 2 1 0; 0 0 1]) exp([1 2 0; 2 1 0; 0 0 1])
1214

1315
for sz in (3,4), typ in (Float64, Complex{Float64})

0 commit comments

Comments
 (0)