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

fill[stored]! for Symmetric and Hermitian matrices #25152

Merged
merged 1 commit into from
Dec 19, 2017
Merged
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
15 changes: 15 additions & 0 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ function copyto!(dest::Hermitian, src::Hermitian)
return dest
end

# fill[stored]!
fill!(A::HermOrSym, x) = fillstored!(A, x)
function fillstored!(A::HermOrSym{T}, x) where T
xT = convert(T, x)
if isa(A, Hermitian)
isreal(xT) || throw(ArgumentError("cannot fill Hermitian matrix with a nonreal value"))
end
if A.uplo == 'U'
fillband!(A.data, xT, 0, size(A,2)-1)
else # A.uplo == 'L'
fillband!(A.data, xT, 1-size(A,1), 0)
end
return A
end

function Base.isreal(A::HermOrSym)
n = size(A, 1)
@inbounds if A.uplo == 'U'
Expand Down
15 changes: 15 additions & 0 deletions test/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,18 @@ end
@test_throws TypeError Symmetric{Float64,Matrix{Float32}}(A, 'U')
@test_throws TypeError Hermitian{Float64,Matrix{Float32}}(A, 'U')
end

@testset "fill[stored]!" begin
for uplo in (:U, :L)
# Hermitian
A = Hermitian(fill(1.0+0im, 2, 2), uplo)
@test fill!(A, 2) == fill(2, 2, 2)
@test A.data == (uplo == :U ? [2 2; 1.0+0im 2] : [2 1.0+0im; 2 2])
@test_throws ArgumentError fill!(A, 2+im)

# Symmetric
A = Symmetric(fill(1.0+im, 2, 2), uplo)
@test fill!(A, 2) == fill(2, 2, 2)
@test A.data == (uplo == :U ? [2 2; 1.0+im 2] : [2 1.0+im; 2 2])
end
end