From 6eb56ac2a48ed3abe67299508bf063cbc37a6d37 Mon Sep 17 00:00:00 2001 From: Moritz Drechsel-Grau Date: Thu, 24 Feb 2022 22:03:11 +0100 Subject: [PATCH] Add methods for SparseMatrixLNK and ExtendableSparseMatrix to Base.copy --- src/ExtendableSparse.jl | 3 ++- src/extendable.jl | 7 +++++++ src/sparsematrixlnk.jl | 7 +++++++ test/runtests.jl | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ExtendableSparse.jl b/src/ExtendableSparse.jl index 92dab8f..4f03c2e 100644 --- a/src/ExtendableSparse.jl +++ b/src/ExtendableSparse.jl @@ -6,7 +6,8 @@ using Requires using DocStringExtensions - +import SparseArrays: rowvals, getcolptr, nonzeros +import Base: copy include("sparsematrixcsc.jl") include("sparsematrixlnk.jl") diff --git a/src/extendable.jl b/src/extendable.jl index 8d27735..2f139e9 100644 --- a/src/extendable.jl +++ b/src/extendable.jl @@ -394,3 +394,10 @@ function SparseArrays.dropzeros!(ext::ExtendableSparseMatrix) end +function copy(S::ExtendableSparseMatrix) + if isnothing(S.lnkmatrix) + ExtendableSparseMatrix(copy(S.cscmatrix), nothing, S.phash) + else + ExtendableSparseMatrix(copy(S.cscmatrix), copy(S.lnkmatrix), S.phash) + end +end \ No newline at end of file diff --git a/src/sparsematrixlnk.jl b/src/sparsematrixlnk.jl index db67f8f..90ae256 100644 --- a/src/sparsematrixlnk.jl +++ b/src/sparsematrixlnk.jl @@ -409,3 +409,10 @@ function SparseArrays.SparseMatrixCSC(lnk::SparseMatrixLNK)::SparseMatrixCSC csc=spzeros(lnk.m,lnk.n) lnk+csc end + + +rowvals(S::SparseMatrixLNK) = getfield(S, :rowval) +getcolptr(S::SparseMatrixLNK) = getfield(S, :colptr) +nonzeros(S::SparseMatrixLNK) = getfield(S, :nzval) + +copy(S::SparseMatrixLNK) = SparseMatrixLNK(size(S, 1), size(S, 2), S.nnz, S.nentries, copy(getcolptr(S)), copy(rowvals(S)), copy(nonzeros(S))) \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index d101e65..9fd5a8b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,6 +26,20 @@ using IterativeSolvers @test test_constructors() end +############################################################## +@testset "Copy-Methods" begin + function test_copymethods() + Xcsc = sprand(10_000,10_000,0.01) + Xlnk = SparseMatrixLNK(Xcsc) + Xext = ExtendableSparseMatrix(Xcsc) + t0 = @elapsed copy(Xcsc) + t1 = @elapsed copy(Xlnk) + t2 = @elapsed copy(Xext) + t1 / t0 < 10 && t0 / t2 < 10 + end + @test test_copymethods() +end + ################################################################# @testset "Updates" begin