Skip to content

Commit c1a4ba6

Browse files
authored
Merge pull request #2 from gridap/add/SymSparseMatrixCSR
Add/sym sparse matrix csr
2 parents 0d1922f + dc7e02e commit c1a4ba6

8 files changed

+96
-11
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseMatricesCSR"
22
uuid = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1"
33
authors = ["Víctor Sande <[email protected]>"]
4-
version = "0.1.0"
4+
version = "0.2.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,32 @@
44
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://gridap.github.io/SparseMatricesCSR.jl/dev)
55
[![Build Status](https://travis-ci.com/gridap/SparseMatricesCSR.jl.svg?branch=master)](https://travis-ci.com/gridap/SparseMatricesCSR.jl)
66
[![Codecov](https://codecov.io/gh/gridap/SparseMatricesCSR.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/gridap/SparseMatricesCSR.jl)
7+
8+
Compressed Sparse Row (CSR) sparse matrix implementations.
9+
10+
It includes `SparseMatrixCSR` and `SymSparseMatrixCSR` wrappers.
11+
12+
CSR wrappers relies on `SparseMatrixCSC` native Julia type.
13+
14+
## Basic Usage
15+
16+
```julia
17+
julia> push_coo!(SymSparseMatrixCSR,I,J,V,i1,j1,v1)
18+
julia> ...
19+
julia> push_coo!(SymSparseMatrixCSR,I,J,V,in,jn,vn)
20+
julia> finalize_coo!(SparseMatrixCSR,I,J,V,m,n)
21+
julia> CSR= sparsecsr(I,J,V,m,n)
22+
23+
```
24+
25+
## Installation
26+
27+
**SparseMatricesCSR** itself is installed when you add and use it into another project.
28+
29+
To include into your project from Julia REPL, use the following commands:
30+
31+
```
32+
pkg> add SparseMatricesCSR
33+
julia> using SparseMatricesCSR
34+
```
35+

src/SparseMatricesCSR.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import SparseArrays: nnz, getnzval, nzvalview, nonzeros, nzrange, findnz
88

99
export SparseMatrixCSR
1010
export SymSparseMatrixCSR
11-
export push_coo!, sparsecsr, symsparsecsr, getrowptr, getcolval
11+
export push_coo!, finalize_coo!, sparsecsr, symsparsecsr, getrowptr, getcolval
1212

1313
include("SparseMatrixCSC.jl")
1414
include("SparseMatrixCSR.jl")

src/SparseMatrixCSC.jl

+24
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,28 @@ function push_coo!(::Type{SparseMatrixCSC},I::Vector{Integer},J::Vector{Integer}
77
(push!(I, ik), push!(J, jk), push!(V, vk))
88
end
99

10+
"""
11+
function push_coo!(I,J,V,ik,jk,vk)
12+
13+
Inserts entries in COO vectors for further building a SparseMatrixCSC.
14+
"""
15+
function push_coo!(I::Vector{Integer},J::Vector{Integer},V::Vector{Number},ik::Integer,jk::Integer,vk::Number)
16+
push_coo!(SparseMatrixCSC,J,V,ik,jk,vk)
17+
end
18+
19+
"""
20+
function finalize_coo!(::Type{SparseMatrixCSC},I,J,V,m,n)
1021
22+
Check and insert diagonal entries in COO vectors if needed.
23+
"""
24+
function finalize_coo!(::Type{SparseMatrixCSC},I::Vector{Integer},J::Vector{Integer},V::Vector{Number},m::Integer,n::Integer)
25+
end
26+
27+
"""
28+
function finalize_coo!(I,J,V,m,n)
29+
30+
Check and insert diagonal entries in COO vectors if needed.
31+
"""
32+
function finalize_coo!(I::Vector{Integer},J::Vector{Integer},V::Vector{Number},m::Integer,n::Integer)
33+
finalize_coo!(SparseMatrixCSC,I,J,V,m,n)
34+
end

src/SparseMatrixCSR.jl

+9
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,14 @@ function push_coo!(::Type{SparseMatrixCSR},I::Vector,J::Vector,V::Vector,ik::Int
8383
(push!(I, ik), push!(J, jk), push!(V, vk))
8484
end
8585

86+
"""
87+
function finalize_coo!(::Type{SparseMatrixCSR},I,J,V,m,n)
88+
89+
Check and insert diagonal entries in COO vectors if needed.
90+
"""
91+
function finalize_coo!(::Type{SparseMatrixCSR},I::Vector,J::Vector,V::Vector,m::Integer,n::Integer)
92+
end
93+
94+
8695

8796

src/SymSparseMatrixCSR.jl

+25-4
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ end
8080

8181
function symsparsecsr(I,J,V,m,n,combine)
8282
m == n || throw(DimensionMismatch("matrix is not square: dimensions are ($m, $n)"))
83-
indices = [ i for i in 1:length(I) if !(I[i]<J[i])]
84-
# Explicitly store diagonal zeros if needed
85-
SymSparseMatrixCSR(sparse(vcat(J[indices],1:m),vcat(I[indices],1:m),vcat(V[indices],zeros(eltype(V),m)),m,m,combine))
83+
SymSparseMatrixCSR(sparse(J,I,V,m,n,combine))
8684
end
8785

8886

8987
"""
90-
function push_coo!(::Type{SparseMatrixCSR},I,J,V,ik,jk,vk)
88+
function push_coo!(::Type{SymSparseMatrixCSR},I,J,V,ik,jk,vk)
9189
9290
Inserts entries in COO vectors for further building a SymSparseMatrixCSR.
9391
"""
@@ -96,3 +94,26 @@ function push_coo!(::Type{SymSparseMatrixCSR},I::Vector,J::Vector,V::Vector,ik::
9694
(push!(I, ik), push!(J, jk), push!(V, vk))
9795
end
9896

97+
"""
98+
function finalize_coo!(::Type{SymSparseMatrixCSR},I,J,V,m,n)
99+
100+
Check and insert diagonal entries in COO vectors if needed.
101+
"""
102+
function finalize_coo!(T::Type{SymSparseMatrixCSR},I::Vector,J::Vector,V::Vector, m::Integer, n::Integer)
103+
m == n || throw(DimensionMismatch("matrix is not square: dimensions are ($m, $n)"))
104+
touched = zeros(Bool,m)
105+
for k in 1:length(I)
106+
Ik = I[k]
107+
Jk = J[k]
108+
if Ik == Jk
109+
touched[Ik] = true
110+
end
111+
end
112+
for k in 1:m
113+
if ! touched[k]
114+
push_coo!(T,I,J,V,k,k,zero(eltype(V)))
115+
end
116+
end
117+
end
118+
119+

test/SparseMatrixCSR.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
for (ik, jk, vk) in zip(rand(1:maxrows, maxnz), rand(1:maxcols, maxnz), rand(1:T(maxnz), maxnz))
1111
push_coo!(SparseMatrixCSR,I,J,V,ik,jk,vk)
1212
end
13-
CSC = sparse(I, J, V)
14-
CSR = sparsecsr(I, J, V)
13+
finalize_coo!(SparseMatrixCSR,I,J,V,maxcols,maxrows)
14+
CSC = sparse(I, J, V, maxcols,maxrows)
15+
CSR = sparsecsr(I, J, V,maxcols,maxrows)
1516

1617
@test CSC == CSR
1718

1819
@test nnz(CSC) == count(i->(i!=0), CSC) == nnz(CSR) == count(i->(i!=0), CSR)
1920

20-
TCSC = sparse(J, I, V)
21-
TCSR = sparsecsr(J, I, V)
21+
TCSC = sparse(J, I, V, maxrows, maxcols)
22+
TCSR = sparsecsr(J, I, V, maxrows, maxcols)
2223

2324
@test size(CSC)==size(CSR)==reverse(size(CSR.transpose))
2425
@test size(CSC)==size(CSR)==reverse(size(TCSC))==reverse(size(TCSC))

test/SymSparseMatrixCSR.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
I = Vector{Int}()
88
J = Vector{Int}()
99
V = Vector{T}()
10-
for (ik, jk, vk) in zip(vcat(rand(1:maxrows, maxnz),1:maxrows), vcat(rand(1:maxcols, maxnz),1:maxcols), vcat(rand(1:T(maxnz), maxnz), zeros(T,maxnz)))
10+
for (ik, jk, vk) in zip(rand(1:maxrows, maxnz), rand(1:maxcols, maxnz), rand(1:T(maxnz), maxnz) )
1111
push_coo!(SymSparseMatrixCSR,I,J,V,ik,jk,vk)
1212
end
13+
finalize_coo!(SymSparseMatrixCSR,I,J,V,maxrows, maxcols)
1314
SYMCSC = Symmetric(sparse(I, J, V, maxrows, maxcols),:L)
1415
SYMCSR = symsparsecsr(I, J, V, maxrows, maxcols)
1516

0 commit comments

Comments
 (0)