diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 3c2a57844661b..f40f864802164 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -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 @@ -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 diff --git a/test/random.jl b/test/random.jl index 19e9c90858d29..aaca6b729ed29 100644 --- a/test/random.jl +++ b/test/random.jl @@ -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 diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index 5eb02cf81e588..f797e40c6d62e 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -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]