Skip to content

Commit

Permalink
Throw an error if zero() is not defined for SparseMatrix type
Browse files Browse the repository at this point in the history
  • Loading branch information
simonster committed May 23, 2015
1 parent 2fedc27 commit 97a0a32
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 7 additions & 3 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
function SparseMatrixCSC(m::Integer, n::Integer, colptr::Vector{Ti}, rowval::Vector{Ti}, nzval::Vector{Tv})
m < 0 && throw(ArgumentError("number of rows (m) must be ≥ 0, got $m"))
n < 0 && throw(ArgumentError("number of columns (n) must be ≥ 0, got $n"))
try
zero(Tv)
catch e
isa(e, MethodError) && throw(ArgumentError("cannot construct a SparseMatrixCSC{$Tv} because zero(::$Tv) is not defined"))
rethrow(e)
end
new(Int(m), Int(n), colptr, rowval, nzval)
end
end
Expand Down Expand Up @@ -184,9 +190,7 @@ convert{T}(::Type{AbstractMatrix{T}}, A::SparseMatrixCSC) = convert(SparseMatrix
convert(::Type{Matrix}, S::SparseMatrixCSC) = full(S)

function full{Tv}(S::SparseMatrixCSC{Tv})
# Handle cases where zero(Tv) is not defined but the array is dense.
# (Should we really worry about this?)
A = length(S) == nnz(S) ? Array(Tv, S.m, S.n) : zeros(Tv, S.m, S.n)
A = zeros(Tv, S.m, S.n)
for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
A[S.rowval[k], col] = S.nzval[k]
end
Expand Down
6 changes: 4 additions & 2 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@ for rng in ([], [MersenneTwister()], [RandomDevice()])
X = T == Bool ? T[0,1] : T[0,1,2]
rand!(rng..., A) ::typeof(A)
rand!(rng..., A, X) ::typeof(A)
rand!(rng..., sparse(A)) ::typeof(sparse(A))
rand!(rng..., sparse(A), X) ::typeof(sparse(A))
if T != Char
rand!(rng..., sparse(A)) ::typeof(sparse(A))
rand!(rng..., sparse(A), X) ::typeof(sparse(A))
end
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,8 @@ let S = spzeros(5,1), I = [false,true,false,true,false]
end

# issue #9917
@test sparse([]') == reshape(sparse([]), 1, 0)
@test full(sparse([])) == zeros(0, 1)
@test_throws BoundsError sparse([])[1]
@test sparse(Int[]') == reshape(sparse(Int[]), 1, 0)
@test_throws BoundsError sparse(Int[])[1]
x = speye(100)
@test_throws BoundsError x[-10:10]

Expand Down

0 comments on commit 97a0a32

Please sign in to comment.