Skip to content

Commit

Permalink
Specialize copy and fix conj
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Feb 1, 2024
1 parent b736a90 commit 2c5bd94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ Hermitian{T,S}(A::Hermitian) where {T,S<:AbstractMatrix{T}} = Hermitian{T,S}(con
AbstractMatrix{T}(A::Hermitian) where {T} = Hermitian(convert(AbstractMatrix{T}, A.data), sym_uplo(A.uplo))
AbstractMatrix{T}(A::Hermitian{T}) where {T} = copy(A)

copy(A::Symmetric{T,S}) where {T,S} = (B = copy(A.data); Symmetric{T,typeof(B)}(B,A.uplo))
copy(A::Hermitian{T,S}) where {T,S} = (B = copy(A.data); Hermitian{T,typeof(B)}(B,A.uplo))
copy(A::Symmetric) = (Symmetric(parentof_applytri(copy, B), sym_uplo(A.uplo)))
copy(A::Hermitian) = (Hermitian(parentof_applytri(copy, B), sym_uplo(A.uplo)))

function copyto!(dest::Symmetric, src::Symmetric)
if src.uplo == dest.uplo
Expand Down Expand Up @@ -422,7 +422,8 @@ Base.copy(A::Transpose{<:Any,<:Hermitian}) =
tr(A::Symmetric) = tr(A.data) # to avoid AbstractMatrix fallback (incl. allocations)
tr(A::Hermitian) = real(tr(A.data))

Base.conj(A::HermOrSym) = typeof(A)(parentof_applytri(conj, A), A.uplo)
Base.conj(A::Symmetric) = Symmetric(parentof_applytri(conj, A), sym_uplo(A.uplo))
Base.conj(A::Hermitian) = Hermitian(parentof_applytri(conj, A), sym_uplo(A.uplo))
Base.conj!(A::HermOrSym) = typeof(A)(parentof_applytri(conj!, A), A.uplo)

# tril/triu
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,11 @@ end
end
end

@testset "conj for immutable" begin
S = Symmetric(reshape((1:16)*im, 4, 4))
@test conj(S) == conj(Array(S))
H = Hermitian(reshape((1:16)*im, 4, 4))
@test conj(H) == conj(Array(H))
end

end # module TestSymmetric

0 comments on commit 2c5bd94

Please sign in to comment.