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

Define copy!, setindex!, and arithmetic for Symmetric and Hermitian #19228

Merged
merged 4 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Define copy! and setindex! for Symmetric and Hermitian
  • Loading branch information
ararslan committed Nov 7, 2016
commit 374cc7bf89fc01b88e51ed80b490915b9452f22e
37 changes: 37 additions & 0 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ end
r
end

function setindex!(A::Symmetric, v, i::Integer, j::Integer)
if (A.uplo == 'U' && i >= j) || (A.uplo == 'L' && i <= j)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. It should only be allowed to modify the diagonal. Modifying off-diagonals will effectively change two values at a time so I think it should be an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Then I'm back to defining specific methods for things, since only being able to set the diagonal doesn't allow more general operations, but maybe that's fine.

setindex!(A.data, v, i, j)
else
setindex!(A.data, v, j, i)
end
end

function setindex!(A::Hermitian, v, i::Integer, j::Integer)
if i == j && !isreal(v)
throw(ArgumentError("Cannot set a nonreal value to the diagonal in a Hermitian matrix"))
elseif (A.uplo == 'U' && i >= j) || (A.uplo == 'L' && i <= j)
setindex!(A.data, v, i, j)
else
setindex!(A.data, v, j, i)
end
end

similar{T}(A::Symmetric, ::Type{T}) = Symmetric(similar(A.data, T))
# Hermitian version can be simplified when check for imaginary part of
# diagonal in Hermitian has been removed
Expand All @@ -133,6 +151,25 @@ convert{T}(::Type{AbstractMatrix{T}}, A::Hermitian) = Hermitian(convert(Abstract

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

function copy!(dest::Symmetric, src::Symmetric)
if src.uplo == dest.uplo
copy!(dest.data, src.data)
else
transpose!(dest.data, src.data)
end
return dest
end

function copy!(dest::Hermitian, src::Hermitian)
if src.uplo == dest.uplo
copy!(dest.data, src.data)
else
ctranspose!(dest.data, src.data)
end
return dest
end

ishermitian(A::Hermitian) = true
ishermitian{T<:Real,S}(A::Symmetric{T,S}) = true
ishermitian{T<:Complex,S}(A::Symmetric{T,S}) = all(imag(A.data) .== 0)
Expand Down
25 changes: 25 additions & 0 deletions test/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,28 @@ let a = randn(2,2)
cc = copy(c)
@test conj!(c) == conj(Array(c))
end

# 19225
let X = sparse([1 -1; -1 1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should to into the sparse tests for modularity. However, you might want to test the functionality with dense arrays as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the sparse wrapper for these tests, then copied a simplified version of what's currently here to the sparse tests. Let me know if the format looks okay.

for T in (Symmetric, Hermitian)
Y = T(copy(X))
_Y = similar(Y)
copy!(_Y, Y)
@test _Y == Y

W = T(copy(X), :L)
copy!(W, Y)
@test W.data == Y.data
@test W.uplo != Y.uplo

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

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

@test_throws ArgumentError Hermitian(X) + 2im*I
@test_throws ArgumentError Hermitian(X) - 2im*I
end