Skip to content

Commit 047e699

Browse files
jishnubdkarrasch
andauthored
Copy parent in copy_similar for Symmetric/Hermitian (#54473)
This should be equivalent to copying the `Symmetric`/`Hermitian` matrix, as only one triangular half is used. However, this reduces latency when the parent is a `Matrix` (and possibly others too, although I've not checked this). ```julia julia> using LinearAlgebra julia> A = rand(2,2); H = Hermitian(A); julia> @time eigen(H); 0.342207 seconds (440.61 k allocations: 22.926 MiB, 99.95% compilation time) # nightly 0.260913 seconds (326.62 k allocations: 16.926 MiB, 99.93% compilation time) # This PR ``` Copying the parent is also marginally faster, although this doesn't really matter in `eigen`: ```julia julia> A = rand(1000,1000); H = Hermitian(A); julia> @Btime LinearAlgebra.copy_similar($H, Float64); 1.839 ms (3 allocations: 7.63 MiB) # nightly 1.760 ms (3 allocations: 7.63 MiB) # This PR ``` --------- Co-authored-by: Daniel Karrasch <[email protected]>
1 parent b5e91af commit 047e699

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

stdlib/LinearAlgebra/src/symmetriceigen.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
# preserve HermOrSym wrapper
4-
eigencopy_oftype(A::Hermitian, S) = Hermitian(copy_similar(A, S), sym_uplo(A.uplo))
5-
eigencopy_oftype(A::Symmetric, S) = Symmetric(copy_similar(A, S), sym_uplo(A.uplo))
4+
# Call `copytrito!` instead of `copy_similar` to only copy the matching triangular half
5+
eigencopy_oftype(A::Hermitian, S) = Hermitian(copytrito!(similar(parent(A), S, size(A)), A.data, A.uplo), sym_uplo(A.uplo))
6+
eigencopy_oftype(A::Symmetric, S) = Symmetric(copytrito!(similar(parent(A), S, size(A)), A.data, A.uplo), sym_uplo(A.uplo))
67

78
# Eigensolvers for symmetric and Hermitian matrices
89
eigen!(A::RealHermSymComplexHerm{<:BlasReal,<:StridedMatrix}; sortby::Union{Function,Nothing}=nothing) =

0 commit comments

Comments
 (0)