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 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Compiler improvements
Library improvements
--------------------

* `sparse(A)` now takes any `AbstractMatrix` A as an argument. ([#10031])

* Factorization api is now type-stable, functions dispatch on `Val{false}` or `Val{true}` instead of a boolean value ([#9575]).

* `convert` now checks for overflow when truncating integers or converting between
Expand Down
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
2 changes: 1 addition & 1 deletion doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ Sparse matrices support much of the same set of operations as dense matrices. Th

.. function:: sparse(A)

Convert a dense matrix ``A`` into a sparse matrix.
Convert an AbstractMatrix ``A`` into a sparse matrix.

.. function:: sparsevec(A)

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