Skip to content

Commit b1903f6

Browse files
committed
Fix #617
1 parent ebffb69 commit b1903f6

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/eigen.jl

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ end
112112

113113

114114
@inline function _eig(s::Size, A::StaticMatrix, permute, scale)
115-
# Only cover the hermitian branch, for now ast least
116-
# This also solves some type-stability issues such as arise in Base
115+
# Only cover the hermitian branch, for now at least
116+
# This also solves some type-stability issues that arise in Base
117117
if ishermitian(A)
118118
return _eig(s, Hermitian(A), permute, scale)
119119
else
@@ -136,19 +136,7 @@ end
136136
TA = eltype(A)
137137

138138
@inbounds if A.uplo == 'U'
139-
if a[3] == 0 # A is diagonal
140-
A11 = a[1]
141-
A22 = a[4]
142-
if A11 < A22
143-
vals = SVector(A11, A22)
144-
vecs = @SMatrix [convert(TA, 1) convert(TA, 0);
145-
convert(TA, 0) convert(TA, 1)]
146-
else # A22 <= A11
147-
vals = SVector(A22, A11)
148-
vecs = @SMatrix [convert(TA, 0) convert(TA, 1);
149-
convert(TA, 1) convert(TA, 0)]
150-
end
151-
else # A is not diagonal
139+
if !iszero(a[3]) # A is not diagonal
152140
t_half = real(a[1] + a[4]) / 2
153141
d = real(a[1] * a[4] - a[3]' * a[3]) # Should be real
154142

@@ -168,22 +156,11 @@ end
168156

169157
vecs = @SMatrix [ v11 v21 ;
170158
v12 v22 ]
159+
160+
return (vals, vecs)
171161
end
172-
return (vals, vecs)
173162
else # A.uplo == 'L'
174-
if a[2] == 0 # A is diagonal
175-
A11 = a[1]
176-
A22 = a[4]
177-
if A11 < A22
178-
vals = SVector(A11, A22)
179-
vecs = @SMatrix [convert(TA, 1) convert(TA, 0);
180-
convert(TA, 0) convert(TA, 1)]
181-
else # A22 <= A11
182-
vals = SVector(A22, A11)
183-
vecs = @SMatrix [convert(TA, 0) convert(TA, 1);
184-
convert(TA, 1) convert(TA, 0)]
185-
end
186-
else # A is not diagonal
163+
if !iszero(a[2]) # A is not diagonal
187164
t_half = real(a[1] + a[4]) / 2
188165
d = real(a[1] * a[4] - a[2]' * a[2]) # Should be real
189166

@@ -203,9 +180,24 @@ end
203180

204181
vecs = @SMatrix [ v11 v21 ;
205182
v12 v22 ]
183+
184+
return (vals,vecs)
206185
end
207-
return (vals,vecs)
208186
end
187+
188+
# A must be diagonal if we reached this point; treatment of uplo 'L' and 'U' is then identical
189+
A11 = real(a[1])
190+
A22 = real(a[4])
191+
if A11 < A22
192+
vals = SVector(A11, A22)
193+
vecs = @SMatrix [convert(TA, 1) convert(TA, 0);
194+
convert(TA, 0) convert(TA, 1)]
195+
else # A22 <= A11
196+
vals = SVector(A22, A11)
197+
vecs = @SMatrix [convert(TA, 0) convert(TA, 1);
198+
convert(TA, 1) convert(TA, 0)]
199+
end
200+
return (vals,vecs)
209201
end
210202

211203
# A small part of the code in the following method was inspired by works of David

test/eigen.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ using StaticArrays, Test, LinearAlgebra
6868
@test eigvals(m) sort(m_d)
6969
@test eigvals(Hermitian(m)) sort(m_d)
7070

71+
m_c = complex(m) # issue 614 (diagonal complex Hermitian)
72+
(vals, vecs) = eigen(Hermitian(m_c))
73+
@test vals::SVector sort(m_d)
74+
(vals, vecs) = eigen(Hermitian(m_c, :L))
75+
@test vals::SVector sort(m_d)
76+
@test eigvals(m_c) sort(m_d)
77+
@test eigvals(Hermitian(m_c)) sort(m_d)
78+
7179
# issue #523
7280
for (i, j) in ((1, 2), (2, 1)), uplo in (:U, :L)
7381
A = SMatrix{2,2,Float64}((i, 0, 0, j))

0 commit comments

Comments
 (0)