Skip to content

Commit de63e5c

Browse files
authored
Merge pull request #13 from gridap/adding_lu_to_CSR
Implemented support for lu, lu! + SparseMatrixCSR ...
2 parents 0d6a94d + e6b18d7 commit de63e5c

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name = "SparseMatricesCSR"
22
uuid = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1"
33
authors = ["Víctor Sande <[email protected]>", "Francesc Verdugo <[email protected]>"]
4-
version = "0.6.2"
4+
version = "0.6.3"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
9+
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
910

1011
[compat]
1112
julia = "1"

src/SparseMatricesCSR.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module SparseMatricesCSR
22

33
using SparseArrays
44
using LinearAlgebra
5+
using SuiteSparse
56

67
import Base: convert, copy, size, getindex, show, count, *, IndexStyle
7-
import LinearAlgebra: mul!
8+
import LinearAlgebra: mul!, lu, lu!
89
import SparseArrays: nnz, getnzval, nonzeros, nzrange
910
import SparseArrays: findnz, rowvals, getnzval, issparse
1011

src/SparseMatrixCSR.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ function Base.copy(a::SparseMatrixCSR{Bi}) where Bi
118118
SparseMatrixCSR{Bi}(a.m,a.n,copy(a.rowptr),copy(a.colval),copy(a.nzval))
119119
end
120120

121+
_copy_and_increment(x) = copy(x) .+ 1
122+
123+
function LinearAlgebra.lu(a::SparseMatrixCSR{0})
124+
rowptr = _copy_and_increment(a.rowptr)
125+
colval = _copy_and_increment(a.colval)
126+
Transpose(lu(SparseMatrixCSC(a.m,a.n,rowptr,colval,a.nzval)))
127+
end
128+
129+
function LinearAlgebra.lu(a::SparseMatrixCSR{1})
130+
Transpose(lu(SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
131+
end
132+
133+
function LinearAlgebra.lu!(
134+
translu::Transpose{T,<:SuiteSparse.UMFPACK.UmfpackLU{T}},
135+
a::SparseMatrixCSR{1}) where {T}
136+
Transpose(lu!(translu.parent,SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
137+
end
138+
139+
function LinearAlgebra.lu!(
140+
translu::Transpose{T,<:SuiteSparse.UMFPACK.UmfpackLU{T}},
141+
a::SparseMatrixCSR{0}) where {T}
142+
rowptr = _copy_and_increment(a.rowptr)
143+
colval = _copy_and_increment(a.colval)
144+
Transpose(lu!(translu.parent,SparseMatrixCSC(a.m,a.n,rowptr,colval,a.nzval)))
145+
end
146+
121147
size(S::SparseMatrixCSR) = (S.m, S.n)
122148
IndexStyle(::Type{<:SparseMatrixCSR}) = IndexCartesian()
123149
function getindex(A::SparseMatrixCSR{Bi,T}, i0::Integer, i1::Integer) where {Bi,T}

test/SparseMatrixCSR.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,21 @@ function test_csr(Bi,Tv,Ti)
8282
@test y z
8383

8484
@test CSR*x CSC*x
85+
end
8586

87+
function test_lu(Bi,I,J,V)
88+
CSR=sparsecsr(Val(Bi),I,J,V)
89+
CSC=sparse(I,J,V)
90+
x=rand(3)
91+
@test norm(CSR\x-CSC\x) < 1.0e-14
92+
fact=lu(CSR)
93+
lu!(fact,CSR)
94+
y=similar(x)
95+
ldiv!(y,fact,x)
96+
@test norm(y-CSC\x) < 1.0e-14
8697
end
8798

99+
88100
for Bi in (0,1)
89101
for Tv in (Float32,Float64)
90102
for Ti in (Int32,Int64)
@@ -93,4 +105,10 @@ for Bi in (0,1)
93105
end
94106
end
95107

108+
I = [1,1,2,2,2,3,3]
109+
J = [1,2,1,2,3,2,3]
110+
V = [4.0,1.0,-1.0,4.0,1.0,-1.0,4.0]
111+
test_lu(0,I,J,V)
112+
test_lu(1,I,J,V)
113+
96114
end # module

0 commit comments

Comments
 (0)