Skip to content

Commit

Permalink
fill[stored]! for Symmetric and Hermitian matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Dec 18, 2017
1 parent 7fb5d27 commit 447307a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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 LinAlg.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

0 comments on commit 447307a

Please sign in to comment.