From 7980baf4ac024975c2176238b9178a3f723fda40 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 19 Sep 2025 01:37:16 +0530 Subject: [PATCH] Remove zeroing in `similar` for `Hermitian` --- src/symmetric.jl | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/symmetric.jl b/src/symmetric.jl index 2cc74763..aa4df3d4 100644 --- a/src/symmetric.jl +++ b/src/symmetric.jl @@ -330,14 +330,8 @@ isdiag(A::HermOrSym) = applytri(isdiag, A) # For A<:Union{Symmetric,Hermitian}, similar(A[, neweltype]) should yield a matrix with the same # symmetry type, uplo flag, and underlying storage type as A. The following methods cover these cases. -similar(A::Symmetric, ::Type{T}) where {T} = Symmetric(similar(parent(A), T), ifelse(A.uplo == 'U', :U, :L)) -# If the Hermitian constructor's check ascertaining that the wrapped matrix's -# diagonal is strictly real is removed, the following method can be simplified. -function similar(A::Hermitian, ::Type{T}) where T - B = similar(parent(A), T) - for i in 1:size(B, 1) B[i, i] = 0 end - return Hermitian(B, ifelse(A.uplo == 'U', :U, :L)) -end +similar(A::Symmetric, ::Type{T}) where {T} = Symmetric(similar(parent(A), T), sym_uplo(A.uplo)) +similar(A::Hermitian, ::Type{T}) where {T} = Hermitian(similar(parent(A), T), sym_uplo(A.uplo)) # On the other hand, similar(A, [neweltype,] shape...) should yield a matrix of the underlying # storage type of A (not wrapped in a symmetry type). The following method covers these cases. similar(A::Union{Symmetric,Hermitian}, ::Type{T}, dims::Dims{N}) where {T,N} = similar(parent(A), T, dims)