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

Allow sparse / sparsevec constructors to work for AbstractMatrix argument types. #10031

Merged
merged 2 commits into from
Feb 3, 2015
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
make sparse and sparsevec constructors work for AbstractMatrix subtypes
  • Loading branch information
jakebolewski committed Feb 2, 2015
commit 53e7cc6cafa04874eee840f3b3a09d532899f3da
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ function findn(A::StridedMatrix)
return (I, J)
end

function findnz{T}(A::StridedMatrix{T})
function findnz{T}(A::AbstractMatrix{T})
nnzA = countnz(A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function convert{Tv,TvS,TiS}(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC{TvS
end
end

function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, M::Matrix)
function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix)
m, n = size(M)
(I, J, V) = findnz(M)
return sparse_IJ_sorted!(convert(Vector{Ti},I),
Expand Down Expand Up @@ -231,7 +231,7 @@ sparse(a::Vector) = sparsevec(a)

## Construct a sparse matrix

sparse{Tv}(A::Matrix{Tv}) = convert(SparseMatrixCSC{Tv,Int}, A)
sparse{Tv}(A::AbstractMatrix{Tv}) = convert(SparseMatrixCSC{Tv,Int}, A)

sparse(S::SparseMatrixCSC) = copy(S)

Expand Down
12 changes: 12 additions & 0 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,15 @@ let S = sprand(10, 10, 0.1)
@test_throws BoundsError S[[0,2,1], [1,2]]
@test_throws BoundsError S[[2,1], [0,1,2]]
end

# Test that sparse / sparsevec constructors work for AbstractMatrix subtypes
let D = Diagonal(ones(10,10)),
sm = sparse(D),
sv = sparsevec(D)

@test countnz(sm) == 10
@test countnz(sv) == 10

@test countnz(sparse(Diagonal(Int[]))) == 0
@test countnz(sparsevec(Diagonal(Int[]))) == 0
end