Skip to content

Commit e5777b0

Browse files
authored
Merge pull request #5 from gridap/add/convert
Sparse matrices conversion using convert method
2 parents a0e1847 + 3e0c024 commit e5777b0

6 files changed

+72
-2
lines changed

src/SparseMatricesCSR.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ This module contains:
2424
+ `nzrange`: Return the range of indices for a particular row or column (Depending on the SparseMatrix type)
2525
+ `findnz`: Return a tuple (I, J, V) where I and J are the row and column indices.
2626
+ `rowvals`: Return row indices or raises an error (Depending on the SparseMatrix type)
27+
+ `convert`: Type conversion
2728
2829
"""
2930
module SparseMatricesCSR
3031

3132
using SparseArrays
3233
using LinearAlgebra
3334

34-
import Base: size, getindex, show, count, *
35+
import Base: convert, size, getindex, show, count, *
3536
import LinearAlgebra: mul!
3637
import SparseArrays: nnz, getnzval, nonzeros, nzrange, findnz, rowvals
3738

src/SparseMatrixCSR.jl

+38
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,41 @@ getptr(S::SparseMatrixCSR) = S.rowptr
285285
Return column indices.
286286
"""
287287
getindices(S::SparseMatrixCSR) = colvals(S)
288+
289+
290+
"""
291+
function convert(::Type{SparseMatrixCSR}, x::AbstractSparseMatrix)
292+
293+
Convert x to a value of type SymSparseMatrixCSR.
294+
"""
295+
convert(::Type{SparseMatrixCSR}, x::AbstractSparseMatrix) = convert(SparseMatrixCSR{1}, x)
296+
297+
function convert(::Type{SparseMatrixCSR{Bi}}, x::SparseMatrixCSR{Bj}) where {Bi,Bj}
298+
if Bi == Bj
299+
return x
300+
else
301+
return SparseMatrixCSR{Bi}( x.m,
302+
x.n,
303+
copy(getptr(x)).-x.offset,
304+
copy(getindices(x)).-x.offset,
305+
copy(nonzeros(x)))
306+
end
307+
end
308+
309+
function convert(::Type{SparseMatrixCSR{Bi}}, x::SparseMatrixCSC) where {Bi}
310+
A = sparse(transpose(x))
311+
(m, n) = size(A)
312+
return SparseMatrixCSR{Bi}(m, n, getptr(A), getindices(A), nonzeros(A))
313+
end
314+
315+
"""
316+
function convert(::Type{SparseMatrixCSC}, x::SparseMatrixCSR)
317+
318+
Convert x to a value of type SparseMatrixCSC.
319+
"""
320+
function convert(::Type{SparseMatrixCSC}, x::SparseMatrixCSR{Bi}) where {Bi}
321+
A = sparse(transpose(x))
322+
(m, n) = size(A)
323+
return SparseMatrixCSR{Bi}(m, n, getptr(A), getindices(A), nonzeros(A))
324+
end
325+

src/SymSparseMatrixCSR.jl

+14
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,17 @@ Return column indices.
214214
"""
215215
getindices(S::SymSparseMatrixCSR) = colvals(S)
216216

217+
"""
218+
function convert(::Type{SymSparseMatrixCSR}, x::SymSparseMatrixCSR)
219+
220+
Convert x to a value of type SymSparseMatrixCSR.
221+
"""
222+
convert(::Type{SymSparseMatrixCSR}, x::SymSparseMatrixCSR) = convert(SymSparseMatrixCSR{1}, x)
223+
224+
function convert(::Type{SymSparseMatrixCSR{Bi}}, x::SymSparseMatrixCSR{Bj}) where {Bi,Bj}
225+
if Bi == Bj
226+
return x
227+
else
228+
return SymSparseMatrixCSR(convert(SparseMatrixCSR{Bi}, x.uppertrian))
229+
end
230+
end

test/SparseMatrixCSC.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
finalize_coo!(I,J,V,maxcols,maxrows)
1414
CSC = sparse(I, J, V, maxcols,maxrows)
1515

16-
@test hasrowmajororder(CSC) == false
16+
@test convert(SparseMatrixCSC, CSC) === CSC
17+
18+
@test hasrowmajororder(CSC) == false
1719
@test hascolmajororder(CSC) == true
1820
@test getptr(CSC) == CSC.colptr
1921
@test getindices(CSC) == rowvals(CSC)

test/SparseMatrixCSR.jl

+9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
CSC = sparse(I, J, V, maxrows,maxcols)
1616
CSR = sparsecsr(SparseMatrixCSR{Bi},I, J, V,maxrows,maxcols)
1717

18+
display(CSR)
19+
1820
@test CSC == CSR
21+
22+
@test convert(SparseMatrixCSR{Bi}, CSR) === CSR
23+
24+
CSRC = convert(SparseMatrixCSR{CSR.offset}, CSR)
25+
@test CSRC == CSR
26+
@test CSRC !== CSR
27+
1928
@test nnz(CSC) == count(!iszero, CSC) == nnz(CSR) == count(!iszero, CSR)
2029

2130
@test hasrowmajororder(CSR) == true

test/SymSparseMatrixCSR.jl

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
@test size(SYMCSC)==size(SYMCSR)
1919
@test SYMCSC == SYMCSR
2020

21+
@test convert(SymSparseMatrixCSR{Bi}, SYMCSR) === SYMCSR
22+
23+
SYMCSRC = convert(SymSparseMatrixCSR{SYMCSR.uppertrian.offset}, SYMCSR)
24+
@test SYMCSRC == SYMCSR
25+
@test SYMCSRC !== SYMCSR
26+
2127
@test hasrowmajororder(SYMCSR) == true
2228
@test hascolmajororder(SYMCSR) == false
2329
@test getptr(SYMCSR) == SYMCSR.uppertrian.rowptr

0 commit comments

Comments
 (0)