Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit b0c1df7

Browse files
Merge pull request #145 from sjdaines/matrix_colors_storedzeros
matrix_colors include sparse matrix stored zeros (structural nonzeros)
2 parents e798411 + cc77afa commit b0c1df7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/coloring/high_level.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ struct AcyclicColoring <: SparseDiffToolsColoringAlgorithm end
1212
Return the colorvec vector for the matrix A using the chosen coloring
1313
algorithm. If a known analytical solution exists, that is used instead.
1414
The coloring defaults to a greedy distance-1 coloring.
15+
16+
Note that if A isa SparseMatrixCSC, the sparsity pattern is defined by structural nonzeroes,
17+
ie includes explicitly stored zeros.
1518
"""
1619
function ArrayInterface.matrix_colors(A::AbstractMatrix, alg::SparseDiffToolsColoringAlgorithm = GreedyD1Color(); partition_by_rows::Bool = false)
1720
_A = A isa SparseMatrixCSC ? A : sparse(A) # Avoid the copy

src/coloring/matrix2graph.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ A utility function to generate a graph from input
3232
sparse matrix, columns are represented with vertices
3333
and 2 vertices are connected with an edge only if
3434
the two columns are mutually orthogonal.
35+
36+
Note that the sparsity pattern is defined by structural nonzeroes, ie includes
37+
explicitly stored zeros.
3538
"""
3639
function matrix2graph(sparse_matrix::SparseMatrixCSC{<:Number, Int}, partition_by_rows::Bool=true)
37-
38-
dropzeros!(sparse_matrix)
40+
3941
(rows_index, cols_index, _) = findnz(sparse_matrix)
4042

4143
ncols = size(sparse_matrix, 2)

test/test_matrix2graph.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ for i in 1:20
4040
@test pr != 0
4141
end
4242
end
43+
44+
@info "stored zeros"
45+
for i in 1:20
46+
matrix = matrices[i]
47+
g = matrix2graph(matrix)
48+
# recalculate graph with stored zeros
49+
matrix_sz = copy(matrix)
50+
fill!(matrix_sz, 0.0)
51+
g_sz = matrix2graph(matrix_sz)
52+
# check that graphs are the same
53+
@test nv(g) == nv(g_sz)
54+
@test ne(g) == ne(g_sz)
55+
for e in edges(g)
56+
@test has_edge(g_sz, e)
57+
end
58+
for e in edges(g_sz)
59+
@test has_edge(g, e)
60+
end
61+
end

0 commit comments

Comments
 (0)