From f6bb3d0be888291ca1b1fd5d4013322121e2ee30 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Fri, 4 Aug 2023 14:45:02 -0500 Subject: [PATCH 1/2] add constructors and tests --- src/SparseMatrixCSR.jl | 14 ++++++++++++++ test/SparseMatrixCSR.jl | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/SparseMatrixCSR.jl b/src/SparseMatrixCSR.jl index 58f31f0..b3731bd 100644 --- a/src/SparseMatrixCSR.jl +++ b/src/SparseMatrixCSR.jl @@ -55,6 +55,20 @@ function SparseMatrixCSR(a::Transpose{Tv,<:SparseMatrixCSC} where Tv) SparseMatrixCSR{1}(size(a,1),size(a,2),at.colptr,rowvals(at),nonzeros(at)) end +""" + SparseMatrixCSR(a::SparseMatrixCSC} + +Build a 1-based `SparseMatrixCSR` from a `SparseMatrixCSC`. +""" +SparseMatrixCSR(a::SparseMatrixCSC) = SparseMatrixCSR(transpose(sparse(transpose(a)))) + +""" + SparseMatrixCSR(a::AbstractMatrix} + +Build a 1-based `SparseMatrixCSR` from an `AbstractMatrix`. +""" +SparseMatrixCSR(a::AbstractMatrix) = SparseMatrixCSR(sparse(a)) + """ SparseMatrixCSR{Bi}(a::Transpose{Tv,<:SparseMatrixCSC} where Tv) where Bi diff --git a/test/SparseMatrixCSR.jl b/test/SparseMatrixCSR.jl index f9ef797..37c8d57 100644 --- a/test/SparseMatrixCSR.jl +++ b/test/SparseMatrixCSR.jl @@ -110,6 +110,9 @@ function test_csr(Bi,Tv,Ti) @test out === CSR @test _CSR ≈ -1*CSR + # test constructors + @test CSR == SparseMatrixCSR(CSC) + @test CSR == SparseMatrixCSR(Matrix(CSC)) end function test_lu(Bi,I,J,V) From 1a93650c5bbb3cefdf2caea4bf257bf24b456f78 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Sun, 6 Aug 2023 21:10:17 -0500 Subject: [PATCH 2/2] fix test --- test/SparseMatrixCSR.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/SparseMatrixCSR.jl b/test/SparseMatrixCSR.jl index 37c8d57..79363a4 100644 --- a/test/SparseMatrixCSR.jl +++ b/test/SparseMatrixCSR.jl @@ -105,14 +105,15 @@ function test_csr(Bi,Tv,Ti) mul!(z,CSC,x) @test y ≈ z + # test constructors + @test CSR == SparseMatrixCSR(CSC) + @test CSR == SparseMatrixCSR(Matrix(CSC)) + _CSR = copy(CSR) out = LinearAlgebra.rmul!(CSR,-1) @test out === CSR @test _CSR ≈ -1*CSR - # test constructors - @test CSR == SparseMatrixCSR(CSC) - @test CSR == SparseMatrixCSR(Matrix(CSC)) end function test_lu(Bi,I,J,V)