Skip to content

Commit 0a708a6

Browse files
Adding support for lu/lu! + SparseMatrixCSR{1}
1 parent b22eaa8 commit 0a708a6

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.6.2"
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

+2-1
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

+14
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ 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+
function LinearAlgebra.lu(a::SparseMatrixCSR{0})
122+
@assert false "Base.lu(a::SparseMatrixCSR{0}) not yet implemented"
123+
end
124+
125+
function LinearAlgebra.lu(a::SparseMatrixCSR{1})
126+
Transpose(lu(SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
127+
end
128+
129+
function LinearAlgebra.lu!(
130+
translu::Transpose{T,<:SuiteSparse.UMFPACK.UmfpackLU{T}},
131+
a::SparseMatrixCSR{1}) where {T}
132+
Transpose(lu!(translu.parent,SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
133+
end
134+
121135
size(S::SparseMatrixCSR) = (S.m, S.n)
122136
IndexStyle(::Type{<:SparseMatrixCSR}) = IndexCartesian()
123137
function getindex(A::SparseMatrixCSR{Bi,T}, i0::Integer, i1::Integer) where {Bi,T}

test/SparseMatrixCSR.jl

+13-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ function test_csr(Bi,Tv,Ti)
8282
@test y z
8383

8484
@test CSR*x CSC*x
85-
8685
end
8786

8887
for Bi in (0,1)
@@ -93,4 +92,17 @@ for Bi in (0,1)
9392
end
9493
end
9594

95+
I = [1,1,2,2,2,3,3]
96+
J = [1,2,1,2,3,2,3]
97+
V = [4.0,1.0,-1.0,4.0,1.0,-1.0,4.0]
98+
CSR=sparsecsr(I,J,V)
99+
CSC=sparse(I,J,V)
100+
x=rand(3)
101+
@test norm(CSR\x-CSC\x) < 1.0e-14
102+
fact=lu(CSR)
103+
lu!(fact,CSR)
104+
y=similar(x)
105+
ldiv!(y,fact,x)
106+
@test norm(y-CSC\x) < 1.0e-14
107+
96108
end # module

0 commit comments

Comments
 (0)