Skip to content

Commit 01b79ef

Browse files
Add methods for SparseMatrixLNK and ExtendableSparseMatrix to Base.copy (#15)
1 parent 27f4172 commit 01b79ef

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/ExtendableSparse.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ using Requires
66

77
using DocStringExtensions
88

9-
9+
import SparseArrays: rowvals, getcolptr, nonzeros
10+
import Base: copy
1011

1112
include("sparsematrixcsc.jl")
1213
include("sparsematrixlnk.jl")

src/extendable.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,10 @@ function SparseArrays.dropzeros!(ext::ExtendableSparseMatrix)
394394
end
395395

396396

397+
function copy(S::ExtendableSparseMatrix)
398+
if isnothing(S.lnkmatrix)
399+
ExtendableSparseMatrix(copy(S.cscmatrix), nothing, S.phash)
400+
else
401+
ExtendableSparseMatrix(copy(S.cscmatrix), copy(S.lnkmatrix), S.phash)
402+
end
403+
end

src/sparsematrixlnk.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,10 @@ function SparseArrays.SparseMatrixCSC(lnk::SparseMatrixLNK)::SparseMatrixCSC
409409
csc=spzeros(lnk.m,lnk.n)
410410
lnk+csc
411411
end
412+
413+
414+
rowvals(S::SparseMatrixLNK) = getfield(S, :rowval)
415+
getcolptr(S::SparseMatrixLNK) = getfield(S, :colptr)
416+
nonzeros(S::SparseMatrixLNK) = getfield(S, :nzval)
417+
418+
copy(S::SparseMatrixLNK) = SparseMatrixLNK(size(S, 1), size(S, 2), S.nnz, S.nentries, copy(getcolptr(S)), copy(rowvals(S)), copy(nonzeros(S)))

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ using IterativeSolvers
2626
@test test_constructors()
2727
end
2828

29+
##############################################################
30+
@testset "Copy-Methods" begin
31+
function test_copymethods()
32+
Xcsc = sprand(10_000,10_000,0.01)
33+
Xlnk = SparseMatrixLNK(Xcsc)
34+
Xext = ExtendableSparseMatrix(Xcsc)
35+
t0 = @elapsed copy(Xcsc)
36+
t1 = @elapsed copy(Xlnk)
37+
t2 = @elapsed copy(Xext)
38+
t1 / t0 < 10 && t0 / t2 < 10
39+
end
40+
@test test_copymethods()
41+
end
42+
2943
#################################################################
3044

3145
@testset "Updates" begin

0 commit comments

Comments
 (0)