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

Implemented support for lu, lu! + SparseMatrixCSR ... #13

Merged
merged 5 commits into from
Oct 6, 2021
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name = "SparseMatricesCSR"
uuid = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1"
authors = ["Víctor Sande <vsande@cimne.upc.edu>", "Francesc Verdugo <fverdugo@cimne.upc.edu>"]
version = "0.6.2"
version = "0.6.3"

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

[compat]
julia = "1"
Expand Down
3 changes: 2 additions & 1 deletion src/SparseMatricesCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module SparseMatricesCSR

using SparseArrays
using LinearAlgebra
using SuiteSparse

import Base: convert, copy, size, getindex, show, count, *, IndexStyle
import LinearAlgebra: mul!
import LinearAlgebra: mul!, lu, lu!
import SparseArrays: nnz, getnzval, nonzeros, nzrange
import SparseArrays: findnz, rowvals, getnzval, issparse

Expand Down
26 changes: 26 additions & 0 deletions src/SparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ function Base.copy(a::SparseMatrixCSR{Bi}) where Bi
SparseMatrixCSR{Bi}(a.m,a.n,copy(a.rowptr),copy(a.colval),copy(a.nzval))
end

_copy_and_increment(x) = copy(x) .+ 1

function LinearAlgebra.lu(a::SparseMatrixCSR{0})
rowptr = _copy_and_increment(a.rowptr)
colval = _copy_and_increment(a.colval)
Transpose(lu(SparseMatrixCSC(a.m,a.n,rowptr,colval,a.nzval)))
end

function LinearAlgebra.lu(a::SparseMatrixCSR{1})
Transpose(lu(SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
end

function LinearAlgebra.lu!(
translu::Transpose{T,<:SuiteSparse.UMFPACK.UmfpackLU{T}},
a::SparseMatrixCSR{1}) where {T}
Transpose(lu!(translu.parent,SparseMatrixCSC(a.m,a.n,a.rowptr,a.colval,a.nzval)))
end

function LinearAlgebra.lu!(
translu::Transpose{T,<:SuiteSparse.UMFPACK.UmfpackLU{T}},
a::SparseMatrixCSR{0}) where {T}
rowptr = _copy_and_increment(a.rowptr)
colval = _copy_and_increment(a.colval)
Transpose(lu!(translu.parent,SparseMatrixCSC(a.m,a.n,rowptr,colval,a.nzval)))
end

size(S::SparseMatrixCSR) = (S.m, S.n)
IndexStyle(::Type{<:SparseMatrixCSR}) = IndexCartesian()
function getindex(A::SparseMatrixCSR{Bi,T}, i0::Integer, i1::Integer) where {Bi,T}
Expand Down
18 changes: 18 additions & 0 deletions test/SparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,21 @@ function test_csr(Bi,Tv,Ti)
@test y ≈ z

@test CSR*x ≈ CSC*x
end

function test_lu(Bi,I,J,V)
CSR=sparsecsr(Val(Bi),I,J,V)
CSC=sparse(I,J,V)
x=rand(3)
@test norm(CSR\x-CSC\x) < 1.0e-14
fact=lu(CSR)
lu!(fact,CSR)
y=similar(x)
ldiv!(y,fact,x)
@test norm(y-CSC\x) < 1.0e-14
end


for Bi in (0,1)
for Tv in (Float32,Float64)
for Ti in (Int32,Int64)
Expand All @@ -93,4 +105,10 @@ for Bi in (0,1)
end
end

I = [1,1,2,2,2,3,3]
J = [1,2,1,2,3,2,3]
V = [4.0,1.0,-1.0,4.0,1.0,-1.0,4.0]
test_lu(0,I,J,V)
test_lu(1,I,J,V)

end # module