Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the function bicoloring #93

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ADTypes.AbstractColoringAlgorithm
ADTypes.column_coloring
ADTypes.row_coloring
ADTypes.symmetric_coloring
ADTypes.bicoloring
ADTypes.NoColoringAlgorithm
```

Expand Down
2 changes: 1 addition & 1 deletion src/ADTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export jacobian_sparsity, hessian_sparsity

# Matrix coloring
export AbstractColoringAlgorithm
export column_coloring, row_coloring, symmetric_coloring
export column_coloring, row_coloring, symmetric_coloring, bicoloring
@public coloring_algorithm
@public NoColoringAlgorithm

Expand Down
19 changes: 19 additions & 0 deletions src/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Abstract supertype for Jacobian/Hessian coloring algorithms.
- [`column_coloring`](@ref)
- [`row_coloring`](@ref)
- [`symmetric_coloring`](@ref)
- [`bicoloring`](@ref)

# Note

Expand Down Expand Up @@ -154,6 +155,23 @@ The result is a coloring vector `c` of length `size(M, 1) == size(M, 2)` such th
"""
function symmetric_coloring end

"""
bicoloring(M::AbstractMatrix, ca::ColoringAlgorithm)::Tuple{AbstractVector{<:Integer},AbstractVector{<:Integer}}

Use algorithm `ca` to construct a structurally orthogonal partition of both the rows and columns of the matrix `M`, ensuring no two non-zero entries in the same row or column share the same color.

The result is a tuple of coloring vectors `(cr, cc)` of lengths `size(M, 1)` and `size(M, 2)`, respectively.
The vector `cr` provides a color assignment for each row, and `cc` provides a color assignment for each column.
For each non-zero entry `M[i, j]` in `M`, the following conditions are met:
amontoison marked this conversation as resolved.
Show resolved Hide resolved

- row `i` is the only row with color `cr[i]` that has a non-zero entry in column `j`;
- column `j` is the only column with color `cc[j]` that has a non-zero entry in row `i`.

A neutral color `0` may be assigned to rows or columns, indicating that they are not needed to retrieve all non-zero entries in `M`.
This occurs when the entries in a row (or column) are fully determined by the non-zero entries in the columns (or rows).
"""
function bicoloring end

"""
NoColoringAlgorithm <: AbstractColoringAlgorithm

Expand All @@ -168,6 +186,7 @@ struct NoColoringAlgorithm <: AbstractColoringAlgorithm end
column_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 2)
row_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 1)
symmetric_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 1)
bicoloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 1), 1:size(M, 2)

## Sparse backend

Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ using ADTypes: dense_ad,
coloring_algorithm,
column_coloring,
row_coloring,
symmetric_coloring
symmetric_coloring,
bicoloring
using Aqua: Aqua
using ChainRulesCore: ChainRulesCore, RuleConfig,
HasForwardsMode, HasReverseMode,
Expand Down
9 changes: 9 additions & 0 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ end
@test sv isa AbstractVector{<:Integer}
@test length(sv) == size(M, 1) == size(M, 2)
@test allunique(sv)

M = rand(3, 4)
brv, bcv = bicoloring(M, ca)
@test brv isa AbstractVector{<:Integer}
@test bcv isa AbstractVector{<:Integer}
@test length(brv) == size(M, 1)
@test length(bcv) == size(M, 2)
@test allunique(brv)
@test allunique(bcv)
end