Skip to content

Commit 3715384

Browse files
stevengjKristofferC
authored andcommitted
bugfix in Hermitian + complex*I (#32001)
(cherry picked from commit b05c0c7)
1 parent 9fa5ea8 commit 3715384

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

stdlib/LinearAlgebra/src/uniformscaling.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,21 @@ end
141141
# matrix breaks the hermiticity, if the UniformScaling is non-real.
142142
# However, to preserve type stability, we do not special-case a
143143
# UniformScaling{<:Complex} that happens to be real.
144-
function (+)(A::Hermitian{T,S}, J::UniformScaling{<:Complex}) where {T,S}
145-
A_ = copytri!(copy(parent(A)), A.uplo)
146-
B = convert(AbstractMatrix{Base._return_type(+, Tuple{eltype(A), typeof(J)})}, A_)
147-
@inbounds for i in diagind(B)
148-
B[i] += J
144+
function (+)(A::Hermitian, J::UniformScaling{<:Complex})
145+
TS = Base._return_type(+, Tuple{eltype(A), typeof(J)})
146+
B = copytri!(copy_oftype(parent(A), TS), A.uplo, true)
147+
for i in diagind(B)
148+
B[i] = A[i] + J
149149
end
150150
return B
151151
end
152152

153-
function (-)(J::UniformScaling{<:Complex}, A::Hermitian{T,S}) where {T,S}
154-
A_ = copytri!(copy(parent(A)), A.uplo)
155-
B = convert(AbstractMatrix{Base._return_type(+, Tuple{eltype(A), typeof(J)})}, A_)
156-
@inbounds for i in eachindex(B)
157-
B[i] = -B[i]
158-
end
159-
@inbounds for i in diagind(B)
160-
B[i] += J
153+
function (-)(J::UniformScaling{<:Complex}, A::Hermitian)
154+
TS = Base._return_type(+, Tuple{eltype(A), typeof(J)})
155+
B = copytri!(copy_oftype(parent(A), TS), A.uplo, true)
156+
B .= .-B
157+
for i in diagind(B)
158+
B[i] = J - A[i]
161159
end
162160
return B
163161
end

stdlib/LinearAlgebra/test/uniformscaling.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,17 @@ let
180180
@test @inferred(J - T) == J - Array(T)
181181
@test @inferred(T\I) == inv(T)
182182

183-
if isa(A, Array)
184-
T = Hermitian(randn(3,3))
185-
else
186-
T = Hermitian(view(randn(3,3), 1:3, 1:3))
183+
for elty in (Float64, ComplexF64)
184+
if isa(A, Array)
185+
T = Hermitian(randn(elty, 3,3))
186+
else
187+
T = Hermitian(view(randn(elty, 3,3), 1:3, 1:3))
188+
end
189+
@test @inferred(T + J) == Array(T) + J
190+
@test @inferred(J + T) == J + Array(T)
191+
@test @inferred(T - J) == Array(T) - J
192+
@test @inferred(J - T) == J - Array(T)
187193
end
188-
@test @inferred(T + J) == Array(T) + J
189-
@test @inferred(J + T) == J + Array(T)
190-
@test @inferred(T - J) == Array(T) - J
191-
@test @inferred(J - T) == J - Array(T)
192194

193195
@test @inferred(I\A) == A
194196
@test @inferred(A\I) == inv(A)

0 commit comments

Comments
 (0)