Skip to content

Commit ee75981

Browse files
authored
Merge pull request #31 from johnomotani/sparsecsr-dense-matrix
Add method for sparsecsr() that takes an AbstractMatrix
2 parents cb35edb + 9a27d99 commit ee75981

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/SparseMatrixCSR.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ Create a `SparseMatrixCSR` with `Bi`-based indexing (1 by default)
9393
from the same `args...` as one constructs a `SparseMatrixCSC`
9494
with the [`SparseArrays.sparse`](@extref) function.
9595
"""
96+
sparsecsr(A::AbstractMatrix{T}) where T = convert(SparseMatrixCSR{1,T,Int64}, A)
9697
sparsecsr(I,J,V) = SparseMatrixCSR(transpose(sparse(J,I,V,dimlub(J),dimlub(I))))
9798
sparsecsr(I,J,V,m,n) = SparseMatrixCSR(transpose(sparse(J,I,V,n,m)))
9899
sparsecsr(I,J,V,m,n,combine) = SparseMatrixCSR(transpose(sparse(J,I,V,n,m,combine)))
100+
sparsecsr(::Val{Bi},A::AbstractMatrix{T}) where {Bi,T} = convert(SparseMatrixCSR{Bi,T,Int64}, A)
99101
sparsecsr(::Val{Bi},I,J,V) where Bi = SparseMatrixCSR{Bi}(transpose(sparse(J,I,V,dimlub(J),dimlub(I))))
100102
sparsecsr(::Val{Bi},I,J,V,m,n) where Bi = SparseMatrixCSR{Bi}(transpose(sparse(J,I,V,n,m)))
101103
sparsecsr(::Val{Bi},I,J,V,m,n,combine) where Bi = SparseMatrixCSR{Bi}(transpose(sparse(J,I,V,n,m,combine)))

test/SparseMatrixCSR.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ function test_csr(Bi,Tv,Ti)
4747
end
4848
end
4949

50+
if Ti == Int64
51+
dense = rand(Tv, maxrows,maxcols)
52+
CSR = sparsecsr(Val(Bi), dense)
53+
@test isa(CSR,SparseMatrixCSR{Bi,Tv,Ti})
54+
@test CSR == dense
55+
if Bi == 1
56+
CSR = sparsecsr(dense)
57+
@test CSR == dense
58+
@test isa(CSR,SparseMatrixCSR{Bi,Tv,Ti})
59+
end
60+
end
61+
5062
CSC = sparse(I,J,V,maxrows,maxcols)
5163
if Bi == 1
5264
CSR = sparsecsr(I,J,V,maxrows,maxcols)

0 commit comments

Comments
 (0)