Skip to content

Conversation

@jishnub
Copy link
Member

@jishnub jishnub commented Mar 19, 2025

Currently, copying Symmetric/Hermitian matrices with mismatched uplos might access unset indices. In this PR, we fix this by ensuring that we always use copytrito!, and only access the triangular half of the parent that uplo corresponds to.
On master,

julia> using LinearAlgebra

julia> M = Matrix{Complex{BigFloat}}(undef, 2, 2)
2×2 Matrix{Complex{BigFloat}}:
 #undef  #undef
 #undef  #undef

julia> M[1,1] = M[2,2] = 3
3

julia> uplo1, uplo2 = :U, :L
(:U, :L)

julia> isupper = uplo1 == :U
true

julia> M[1+!isupper, 1+isupper] = 4+3im
4 + 3im

julia> M
2×2 Matrix{Complex{BigFloat}}:
 3.0+0.0im  4.0+3.0im
  #undef    3.0+0.0im

julia> H1 = Hermitian(M, uplo1)
2×2 Hermitian{Complex{BigFloat}, Matrix{Complex{BigFloat}}}:
 3.0+0.0im  4.0+3.0im
 4.0-3.0im  3.0+0.0im

julia> H2 = Hermitian(M', uplo2)
2×2 Hermitian{Complex{BigFloat}, Adjoint{Complex{BigFloat}, Matrix{Complex{BigFloat}}}}:
 3.0+0.0im  4.0+3.0im
 4.0-3.0im  3.0+0.0im

julia> copyto!(H1, H2)
ERROR: UndefRefError: access to undefined reference
[...]

After this PR,

julia> copyto!(H1, H2)
2×2 Hermitian{Complex{BigFloat}, Matrix{Complex{BigFloat}}}:
 3.0+0.0im  4.0+3.0im
 4.0-3.0im  3.0+0.0im

@jishnub jishnub merged commit 5e53d12 into master Apr 1, 2025
1 of 2 checks passed
@jishnub jishnub deleted the jishnub/copyto_sym branch April 1, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants