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

Adding setindex! #18

Merged
merged 2 commits into from
Nov 26, 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
2 changes: 1 addition & 1 deletion src/SparseMatricesCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using SparseArrays
using LinearAlgebra
using SuiteSparse

import Base: convert, copy, size, getindex, show, count, *, IndexStyle
import Base: convert, copy, size, getindex, setindex!, show, count, *, IndexStyle
import LinearAlgebra: mul!, lu, lu!
import SparseArrays: nnz, getnzval, nonzeros, nzrange
import SparseArrays: findnz, rowvals, getnzval, issparse
Expand Down
20 changes: 20 additions & 0 deletions src/SparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ function getindex(A::SparseMatrixCSR{Bi,T}, i0::Integer, i1::Integer) where {Bi,
k = searchsortedfirst(colvals(A), i1o, r1, r2, Base.Order.Forward)
((k > r2) || (colvals(A)[k] != i1o)) ? zero(T) : nonzeros(A)[k]
end
function setindex!(A::SparseMatrixCSR{Bi,Tv,Ti}, _v, _i0::Integer, _i1::Integer) where {Bi,Tv,Ti}
errmsg = "Trying to set an entry outside sparsity pattern"
v = convert(Tv,_v)
i0 = convert(Ti,_i0)
i1 = convert(Ti,_i1)
if !(1 <= i0 <= size(A, 1) && 1 <= i1 <= size(A, 2)); throw(BoundsError()); end
o = getoffset(A)
r1 = Int(getrowptr(A)[i0]+o)
r2 = Int(getrowptr(A)[i0+1]-Bi)
(r1 > r2) && throw(ArgumentError(errmsg))
i1o = i1-o
k = searchsortedfirst(colvals(A), i1o, r1, r2, Base.Order.Forward)
if ((k > r2) || (colvals(A)[k] != i1o))
throw(ArgumentError(errmsg))
end
A.nzval[k]=v
end




getrowptr(S::SparseMatrixCSR) = S.rowptr
getnzval(S::SparseMatrixCSR) = S.nzval
Expand Down
3 changes: 3 additions & 0 deletions src/SymSparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ IndexStyle(::Type{<:SymSparseMatrixCSR}) = IndexCartesian()
function getindex(A::SymSparseMatrixCSR, x::Integer, y::Integer)
getindex(A.uppertrian,min(x,y),max(x,y))
end
function setindex!(A::SymSparseMatrixCSR, v, x::Integer, y::Integer)
setindex!(A.uppertrian,v,min(x,y),max(x,y))
end

getrowptr(S::SymSparseMatrixCSR) = getrowptr(S.uppertrian)
getnzval(S::SymSparseMatrixCSR) = getnzval(S.uppertrian)
Expand Down
16 changes: 16 additions & 0 deletions test/SparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,27 @@ function test_csr(Bi,Tv,Ti)
@test copy(CSR) == CSC
end
CSR = sparsecsr(Val(Bi),I,J,V)
show(CSR)
@test CSR == CSC
@test copy(CSR) == CSC
@test eltype(CSR) == Tv
@test isa(CSR,SparseMatrixCSR{Bi,Tv,Ti})

for i=1:size(CSR,1)
for j=1:size(CSR,2)
if (i,j) in zip(I,J)
CSR[i,j] = eltype(V)(i+j)
@test CSR[i,j] ≈ eltype(V)(i+j)
else
try
CSR[i,j] = eltype(V)(i+j)
catch e
@test isa(e,ArgumentError)
end
end
end
end

CSC = sparse(I,J,V,maxrows,maxcols)
if Bi == 1
CSR = sparsecsr(I,J,V,maxrows,maxcols)
Expand Down
15 changes: 15 additions & 0 deletions test/SymSparseMatrixCSR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ function test_csr(Bi,Tv,Ti)
@test eltype(CSR) == Tv
@test isa(CSR,SymSparseMatrixCSR{Bi,Tv,Ti})

for i=1:size(CSR,1)
for j=1:size(CSR,2)
if (i,j) in zip(I,J)
CSR[i,j] = eltype(V)(i+j)
@test CSR[i,j] ≈ eltype(V)(i+j)
else
try
CSR[i,j] = eltype(V)(i+j)
catch e
@test isa(e,ArgumentError)
end
end
end
end

CSC = sparse(I,J,V,maxrows,maxcols)
if Bi == 1
CSR = symsparsecsr(I_up,J_up,V_up,maxrows,maxcols)
Expand Down