From 22d37cae22b9f1b2387f212a38e3db9025ed7979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 19 Jan 2026 14:42:02 +0100 Subject: [PATCH 1/4] Fix respectful_similar with SparsityPatternCSC --- src/matrices.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/matrices.jl b/src/matrices.jl index 453e3062..f761275f 100644 --- a/src/matrices.jl +++ b/src/matrices.jl @@ -49,6 +49,17 @@ respectful_similar(A::AbstractMatrix) = respectful_similar(A, eltype(A)) respectful_similar(A::AbstractMatrix, ::Type{T}) where {T} = similar(A, T) +# Needed if using `coloring(::SparsityPatternCSC, ...)` +function respectful_similar(A::SparsityPatternCSC, ::Type{T}) where {T} + return SparseArrays.SparseMatrixCSC( + A.m, + A.n, + A.colptr, + A.rowval, + similar(A.rowval, T), + ) +end + function respectful_similar(A::Transpose, ::Type{T}) where {T} return transpose(respectful_similar(parent(A), T)) end From b511c9c30a9a8780f6699e417da506a4fd2e56fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 29 Jan 2026 09:30:50 +0100 Subject: [PATCH 2/4] Add test --- src/graph.jl | 11 +++++++++++ src/matrices.jl | 11 ----------- test/structured.jl | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/graph.jl b/src/graph.jl index b93a864b..b6ad5201 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -32,6 +32,17 @@ SparseArrays.nnz(S::SparsityPatternCSC) = length(S.rowval) SparseArrays.rowvals(S::SparsityPatternCSC) = S.rowval SparseArrays.nzrange(S::SparsityPatternCSC, j::Integer) = S.colptr[j]:(S.colptr[j + 1] - 1) +# Needed if using `coloring(::SparsityPatternCSC, ...)` +function Base.similar(A::SparsityPatternCSC, ::Type{T}) where {T} + return SparseArrays.SparseMatrixCSC( + A.m, + A.n, + A.colptr, + A.rowval, + similar(A.rowval, T), + ) +end + """ transpose(S::SparsityPatternCSC) diff --git a/src/matrices.jl b/src/matrices.jl index f761275f..453e3062 100644 --- a/src/matrices.jl +++ b/src/matrices.jl @@ -49,17 +49,6 @@ respectful_similar(A::AbstractMatrix) = respectful_similar(A, eltype(A)) respectful_similar(A::AbstractMatrix, ::Type{T}) where {T} = similar(A, T) -# Needed if using `coloring(::SparsityPatternCSC, ...)` -function respectful_similar(A::SparsityPatternCSC, ::Type{T}) where {T} - return SparseArrays.SparseMatrixCSC( - A.m, - A.n, - A.colptr, - A.rowval, - similar(A.rowval, T), - ) -end - function respectful_similar(A::Transpose, ::Type{T}) where {T} return transpose(respectful_similar(parent(A), T)) end diff --git a/test/structured.jl b/test/structured.jl index 23ba967f..c046cd31 100644 --- a/test/structured.jl +++ b/test/structured.jl @@ -56,3 +56,19 @@ end; test_structured_coloring_decompression(A) end end; + +# See https://github.com/gdalle/SparseMatrixColorings.jl/pull/299 +@testset "SparsityPatternCSC" begin + S = sparse([ + 0 0 1 1 0 1 + 1 0 0 0 1 0 + 0 1 0 0 1 0 + 0 1 1 0 0 0 + ]) + P = SparseMatrixColorings.SparsityPatternCSC(S) + problem = ColoringProblem() + algo = GreedyColoringAlgorithm() + result = coloring(P, problem, algo) + B = compress(S, result) + @test decompress(B, result) isa SparseMatrixCSC{Int,Int} +end; From 1f08edb1781f848317376a66ea0367c96b2723c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 29 Jan 2026 09:35:04 +0100 Subject: [PATCH 3/4] Fix format --- src/graph.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/graph.jl b/src/graph.jl index b6ad5201..39df611c 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -34,13 +34,7 @@ SparseArrays.nzrange(S::SparsityPatternCSC, j::Integer) = S.colptr[j]:(S.colptr[ # Needed if using `coloring(::SparsityPatternCSC, ...)` function Base.similar(A::SparsityPatternCSC, ::Type{T}) where {T} - return SparseArrays.SparseMatrixCSC( - A.m, - A.n, - A.colptr, - A.rowval, - similar(A.rowval, T), - ) + return SparseArrays.SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, similar(A.rowval, T)) end """ From 77d32d53c214ba308133a31be9f43dc65957b713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 29 Jan 2026 10:07:18 +0100 Subject: [PATCH 4/4] Add test --- test/structured.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/structured.jl b/test/structured.jl index c046cd31..5c7b5874 100644 --- a/test/structured.jl +++ b/test/structured.jl @@ -2,6 +2,7 @@ using ArrayInterface: ArrayInterface using BandedMatrices: BandedMatrix, brand using BlockBandedMatrices: BandedBlockBandedMatrix, BlockBandedMatrix using LinearAlgebra +using SparseArrays using SparseMatrixColorings using Test @@ -58,8 +59,8 @@ end; end; # See https://github.com/gdalle/SparseMatrixColorings.jl/pull/299 -@testset "SparsityPatternCSC" begin - S = sparse([ +@testset "SparsityPatternCSC $T" for T in [Int, Float32] + S = sparse(T[ 0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 1 0 @@ -70,5 +71,5 @@ end; algo = GreedyColoringAlgorithm() result = coloring(P, problem, algo) B = compress(S, result) - @test decompress(B, result) isa SparseMatrixCSC{Int,Int} + @test decompress(B, result) isa SparseMatrixCSC{T,Int} end;