Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing arbitrary entries in symmetric/Hermitian matrices #33071

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,28 @@ end
end

function setindex!(A::Symmetric, v, i::Integer, j::Integer)
i == j || throw(ArgumentError("Cannot set a non-diagonal index in a symmetric matrix"))
setindex!(A.data, v, i, j)
@boundscheck checkbounds(A, i, j)
@inbounds if i == j
setindex!(A.data, v, i, i)
elseif (A.uplo == 'U') == (i < j)
setindex!(A.data, v, i, j)
else
setindex!(A.data, transpose(v), j, i)
end
end

function setindex!(A::Hermitian, v, i::Integer, j::Integer)
if i != j
throw(ArgumentError("Cannot set a non-diagonal index in a Hermitian matrix"))
elseif !isreal(v)
throw(ArgumentError("Cannot set a diagonal entry in a Hermitian matrix to a nonreal value"))
else
@boundscheck checkbounds(A, i, j)

dvals(x::Number) = x
dvals(A::AbstractMatrix) = diag(A)
@inbounds if i == j
isreal(dvals(v)) || throw(ArgumentError("Cannot set a diagonal entry in a Hermitian matrix to a nonreal value"))
setindex!(A.data, v, i, i)
elseif (A.uplo == 'U') == (i < j)
setindex!(A.data, v, i, j)
else
setindex!(A.data, adjoint(v), j, i)
end
end

Expand Down
71 changes: 70 additions & 1 deletion stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ end

W[1,1] = 4
@test W == T([4 -1; -1 1])
@test_throws ArgumentError (W[1,2] = 2)

@test Y + I == T([2 -1; -1 2])
@test Y - I == T([0 -1; -1 0])
Expand Down Expand Up @@ -605,4 +604,74 @@ end
@test LinearAlgebra.hermitian_type(Int) == Int
end

@testset "setindex!" begin
# set non-real number on diagonal of Hermitian matrix
A = Hermitian([rand(1:7) + rand(1:7)*im for _ in 1:3, _ in 1:3])
@test_throws ArgumentError A[2, 2] = 1 + im

for T in (Symmetric, Hermitian)
for uplo in (:L, :U)
# set value on lower part of matrix
A = T(ones(Int, 3, 3), uplo)
A[2, 1] = 2
@test A == [1 2 1; 2 1 1; 1 1 1]

# set value on upper part of matrix
A = T(ones(Int, 3, 3), uplo)
A[1, 3] = 2
@test A == [1 1 2; 1 1 1; 2 1 1]

# set value on diagonal of matrix
A = T(ones(Int, 3, 3), uplo)
A[2, 2] = 2
@test A == [1 1 1; 1 2 1; 1 1 1]

# set value on upper part of block matrix
A = T([zeros(Complex, 2, 2) for _ in 1:4, _ in 1:4], uplo)
B = [1+1im 2+2im; 3+3im 4+4im]
A[1, 2] = B
expected = T == Symmetric ? [1+1im 3+3im; 2+2im 4+4im] : [1-1im 3-3im; 2-2im 4-4im]
@test A[1, 2] == B
@test A[2, 1] == expected
for cart in CartesianIndices(A)
i, j = cart[1], cart[2]
if (i, j) == (1, 2) || (i, j) == (2, 1)
continue
end
@test A[cart] == zeros(Complex, 2, 2)
end

# set value on lower part of block matrix
A = T([zeros(Complex, 2, 2) for _ in 1:4, _ in 1:4], uplo)
A[2, 1] = B
@test A[2, 1] == B
@test A[1, 2] == expected
for cart in CartesianIndices(A)
i, j = cart[1], cart[2]
if (i, j) == (1, 2) || (i, j) == (2, 1)
continue
end
@test A[cart] == zeros(Complex, 2, 2)
end

# set value on diagonal of block matrix
A = T([zeros(Complex, 2, 2) for _ in 1:4, _ in 1:4], uplo)
B = [1+1im 2+2im; 3+3im 4+4im]
if T == Hermitian
@test_throws ArgumentError A[3, 3] = B
B = [1+0im 2+2im; 3+3im 4+0im]
end
A[3, 3] = B
@test A[3, 3] == T(B, uplo)
for cart in CartesianIndices(A)
i, j = cart[1], cart[2]
if (i, j) == (3, 3)
continue
end
@test A[cart] == zeros(Complex, 2, 2)
end
end
end
end

end # module TestSymmetric
1 change: 0 additions & 1 deletion stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,6 @@ end

W[1,1] = 4
@test W == T(sparse([4 -1; -1 1]))
@test_throws ArgumentError (W[1,2] = 2)

@test Y + I == T(sparse([2 -1; -1 2]))
@test Y - I == T(sparse([0 -1; -1 0]))
Expand Down