Skip to content

Commit 65b7a26

Browse files
authored
Merge pull request #8 from gridap/implement_convert
Implement convert
2 parents da95497 + 57ab05f commit 65b7a26

5 files changed

+49
-2
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseMatricesCSR"
22
uuid = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1"
33
authors = ["Víctor Sande <vsande@cimne.upc.edu>","Francesc Verdugo <fverdugo@cimne.upc.edu>"]
4-
version = "0.6.0"
4+
version = "0.6.1"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/SparseMatrixCSR.jl

+27
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ sparsecsr(::Val{Bi},I,J,V,m,n) where Bi = SparseMatrixCSR{Bi}(transpose(sparse(J
8787
sparsecsr(::Val{Bi},I,J,V,m,n,combine) where Bi = SparseMatrixCSR{Bi}(transpose(sparse(J,I,V,n,m,combine)))
8888
dimlub(I) = isempty(I) ? 0 : Int(maximum(I))
8989

90+
Base.convert(::Type{T},a::T) where T<:SparseMatrixCSR = a
91+
function Base.convert(
92+
::Type{SparseMatrixCSR{Bi,Tv,Ti}},a::SparseMatrixCSR{Bi}) where {Bi,Tv,Ti}
93+
rowptr = convert(Vector{Ti},a.rowptr)
94+
colval = convert(Vector{Ti},a.colval)
95+
nzval = convert(Vector{Tv},a.nzval)
96+
SparseMatrixCSR{Bi}(a.m,a.n,rowptr,colval,nzval)
97+
end
98+
function Base.convert(
99+
::Type{SparseMatrixCSR{Bi,Tv,Ti}},a::SparseMatrixCSR{Bj}) where {Bi,Tv,Ti,Bj}
100+
rowptr = Vector{Ti}(undef,length(a.rowptr))
101+
colval = Vector{Ti}(undef,length(a.colval))
102+
rowptr .= a.rowptr .+ (Bi-Bj)
103+
colval .= a.colval .+ (Bi-Bj)
104+
nzval = convert(Vector{Tv},a.nzval)
105+
SparseMatrixCSR{Bi}(a.m,a.n,rowptr,colval,nzval)
106+
end
107+
function Base.convert(
108+
::Type{SparseMatrixCSR{Bi,Tv,Ti}},a::Transpose{Tu,<:SparseMatrixCSC} where Tu) where {Bi,Tv,Ti}
109+
convert(SparseMatrixCSR{Bi,Tv,Ti},SparseMatrixCSR{Bi}(a))
110+
end
111+
function Base.convert(
112+
::Type{SparseMatrixCSR{Bi,Tv,Ti}},a::AbstractMatrix) where {Bi,Tv,Ti}
113+
at = convert(SparseMatrixCSC{Tv,Ti},transpose(a))
114+
convert(SparseMatrixCSR{Bi,Tv,Ti},transpose(at))
115+
end
116+
90117
size(S::SparseMatrixCSR) = (S.m, S.n)
91118
IndexStyle(::Type{<:SparseMatrixCSR}) = IndexCartesian()
92119
function getindex(A::SparseMatrixCSR{Bi,T}, i0::Integer, i1::Integer) where {Bi,T}

src/SymSparseMatrixCSR.jl

+6
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ function show(io::IO, ::MIME"text/plain", S::SymSparseMatrixCSR)
187187
end
188188
show(io::IO, S::SymSparseMatrixCSR) = show(io, S.uppertrian)
189189

190+
Base.convert(::Type{T},a::T) where T<:SymSparseMatrixCSR = a
191+
function Base.convert(
192+
::Type{SymSparseMatrixCSR{Bi,Tv,Ti}},a::SymSparseMatrixCSR) where {Bi,Tv,Ti}
193+
utrian = convert(SparseMatrixCSR{Bi,Tv,Ti},a.uppertrian)
194+
SymSparseMatrixCSR(utrian)
195+
end

test/SparseMatrixCSR.jl

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ function test_csr(Bi,Tv,Ti)
3838
CSR = sparsecsr(Val(Bi),I,J,V,maxrows,maxcols)
3939
@test CSR == CSC
4040

41+
CSR2 = convert(typeof(CSR),CSR)
42+
@test CSR2 === CSR
43+
CSR2 = convert(SparseMatrixCSR{0,Float64,Int32},CSR)
44+
@test CSR2 == CSR
45+
CSR2 = convert(SparseMatrixCSR{0,Float64,Int32},CSC)
46+
@test CSR2 == CSC
47+
CSR2 = convert(SparseMatrixCSR{0,Float64,Int32},collect(CSC))
48+
@test CSR2 == CSC
49+
4150
@test size(CSR) == size(CSC)
4251
@test eltype(CSR) == Tv
4352
@test isa(CSR,SparseMatrixCSR{Bi,Tv,Ti})

test/SymSparseMatrixCSR.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module SparseMatrixCSRTests
1+
module SymSparseMatrixCSRTests
22

33
using Test
44
using SparseMatricesCSR
@@ -45,6 +45,11 @@ function test_csr(Bi,Tv,Ti)
4545
CSR = symsparsecsr(Val(Bi),copy(I),copy(J),copy(V),maxrows,maxcols;symmetrize=true)
4646
@test CSR == CSC
4747

48+
CSR2 = convert(typeof(CSR),CSR)
49+
@test CSR2 === CSR
50+
CSR2 = convert(SymSparseMatrixCSR{0,Float64,Int32},CSR)
51+
@test CSR2 == CSR
52+
4853
@test size(CSR) == size(CSC)
4954
@test eltype(CSR) == Tv
5055
@test isa(CSR,SymSparseMatrixCSR{Bi,Tv,Ti})

0 commit comments

Comments
 (0)