API reference
The docstrings on this page define the public API of the package.
SparseMatrixColorings.SparseMatrixColorings
— ModuleSparseMatrixColorings
SparseMatrixColorings.jl
Coloring algorithms for sparse Jacobian and Hessian matrices.
Getting started
To install this package, run the following in a Julia Pkg REPL:
pkg> add SparseMatrixColorings
Background
The algorithms implemented in this package are taken from the following articles:
- What Color Is Your Jacobian? Graph Coloring for Computing Derivatives, Gebremedhin et al. (2005)
- New Acyclic and Star Coloring Algorithms with Application to Computing Hessians, Gebremedhin et al. (2007)
- Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation, Gebremedhin et al. (2009)
- ColPack: Software for graph coloring and related problems in scientific computing, Gebremedhin et al. (2013)
Some parts of the articles (like definitions) are thus copied verbatim in the documentation.
Alternatives
- ColPack.jl: a Julia interface to the C++ library ColPack
- SparseDiffTools.jl: contains Julia implementations of some coloring algorithms
Citing
Please cite this software using the Zenodo DOI of the version you used. The link https://zenodo.org/doi/10.5281/zenodo.11314275 resolves to the latest version.
Exports
Main function
SparseMatrixColorings.coloring
— Functioncoloring(
+API reference · SparseMatrixColorings.jl API reference
The docstrings on this page define the public API of the package.
SparseMatrixColorings.SparseMatrixColorings
— ModuleSparseMatrixColorings
SparseMatrixColorings.jl
Coloring algorithms for sparse Jacobian and Hessian matrices.
Getting started
To install this package, run the following in a Julia Pkg REPL:
pkg> add SparseMatrixColorings
Background
The algorithms implemented in this package are taken from the following articles:
- What Color Is Your Jacobian? Graph Coloring for Computing Derivatives, Gebremedhin et al. (2005)
- New Acyclic and Star Coloring Algorithms with Application to Computing Hessians, Gebremedhin et al. (2007)
- Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation, Gebremedhin et al. (2009)
- ColPack: Software for graph coloring and related problems in scientific computing, Gebremedhin et al. (2013)
Some parts of the articles (like definitions) are thus copied verbatim in the documentation.
Alternatives
- ColPack.jl: a Julia interface to the C++ library ColPack
- SparseDiffTools.jl: contains Julia implementations of some coloring algorithms
Citing
Please cite this software using the Zenodo DOI of the version you used. The link https://zenodo.org/doi/10.5281/zenodo.11314275 resolves to the latest version.
Exports
sourceMain function
SparseMatrixColorings.coloring
— Functioncoloring(
S::AbstractMatrix,
problem::ColoringProblem,
algo::GreedyColoringAlgorithm;
@@ -32,9 +32,9 @@
3-element Vector{Vector{Int64}}:
[1, 2, 4]
[3, 5]
- [6]
See also
sourceSparseMatrixColorings.ColoringProblem
— TypeColoringProblem{structure,partition}
Selector type for the coloring problem to solve, enabling multiple dispatch.
It is passed as an argument to the main function coloring
.
Constructors
ColoringProblem{structure,partition}()
-ColoringProblem(; structure=:nonsymmetric, partition=:column)
structure::Symbol
: either :nonsymmetric
or :symmetric
partition::Symbol
: either :column
, :row
or :bidirectional
Warning The second constructor (based on keyword arguments) is type-unstable.
Link to automatic differentiation
Matrix coloring is often used in automatic differentiation, and here is the translation guide:
matrix mode structure
partition
implemented Jacobian forward :nonsymmetric
:column
yes Jacobian reverse :nonsymmetric
:row
yes Jacobian mixed :nonsymmetric
:bidirectional
no Hessian - :symmetric
:column
yes Hessian - :symmetric
:row
no
sourceSparseMatrixColorings.GreedyColoringAlgorithm
— TypeGreedyColoringAlgorithm{decompression} <: ADTypes.AbstractColoringAlgorithm
Greedy coloring algorithm for sparse matrices which colors columns or rows one after the other, following a configurable order.
It is passed as an argument to the main function coloring
.
Constructors
GreedyColoringAlgorithm{decompression}(order=NaturalOrder())
-GreedyColoringAlgorithm(order=NaturalOrder(); decompression=:direct)
order::AbstractOrder
: the order in which the columns or rows are colored, which can impact the number of colors.decompression::Symbol
: either :direct
or :substitution
. Usually :substitution
leads to fewer colors, at the cost of a more expensive coloring (and decompression). When :substitution
is not applicable, it falls back on :direct
decompression.
Warning The second constructor (based on keyword arguments) is type-unstable.
ADTypes coloring interface
GreedyColoringAlgorithm
is a subtype of ADTypes.AbstractColoringAlgorithm
, which means the following methods are also applicable:
See their respective docstrings for details.
See also
sourceSparseMatrixColorings.ConstantColoringAlgorithm
— TypeConstantColoringAlgorithm{partition} <: ADTypes.AbstractColoringAlgorithm
Coloring algorithm which always returns the same precomputed vector of colors. Useful when the optimal coloring of a matrix can be determined a priori due to its specific structure (e.g. banded).
It is passed as an argument to the main function coloring
, but will only work if the associated problem
has :nonsymmetric
structure. Indeed, for symmetric coloring problems, we need more than just the vector of colors to allow fast decompression.
Constructors
ConstantColoringAlgorithm{partition}(matrix_template, color)
+ [6]
See also
sourceSparseMatrixColorings.ColoringProblem
— TypeColoringProblem{structure,partition}
Selector type for the coloring problem to solve, enabling multiple dispatch.
It is passed as an argument to the main function coloring
.
Constructors
ColoringProblem{structure,partition}()
+ColoringProblem(; structure=:nonsymmetric, partition=:column)
structure::Symbol
: either :nonsymmetric
or :symmetric
partition::Symbol
: either :column
, :row
or :bidirectional
Warning The second constructor (based on keyword arguments) is type-unstable.
Link to automatic differentiation
Matrix coloring is often used in automatic differentiation, and here is the translation guide:
matrix mode structure
partition
implemented Jacobian forward :nonsymmetric
:column
yes Jacobian reverse :nonsymmetric
:row
yes Jacobian mixed :nonsymmetric
:bidirectional
no Hessian - :symmetric
:column
yes Hessian - :symmetric
:row
no
sourceSparseMatrixColorings.GreedyColoringAlgorithm
— TypeGreedyColoringAlgorithm{decompression} <: ADTypes.AbstractColoringAlgorithm
Greedy coloring algorithm for sparse matrices which colors columns or rows one after the other, following a configurable order.
It is passed as an argument to the main function coloring
.
Constructors
GreedyColoringAlgorithm{decompression}(order=NaturalOrder())
+GreedyColoringAlgorithm(order=NaturalOrder(); decompression=:direct)
order::AbstractOrder
: the order in which the columns or rows are colored, which can impact the number of colors.decompression::Symbol
: either :direct
or :substitution
. Usually :substitution
leads to fewer colors, at the cost of a more expensive coloring (and decompression). When :substitution
is not applicable, it falls back on :direct
decompression.
Warning The second constructor (based on keyword arguments) is type-unstable.
ADTypes coloring interface
GreedyColoringAlgorithm
is a subtype of ADTypes.AbstractColoringAlgorithm
, which means the following methods are also applicable:
See their respective docstrings for details.
See also
sourceSparseMatrixColorings.ConstantColoringAlgorithm
— TypeConstantColoringAlgorithm{partition} <: ADTypes.AbstractColoringAlgorithm
Coloring algorithm which always returns the same precomputed vector of colors. Useful when the optimal coloring of a matrix can be determined a priori due to its specific structure (e.g. banded).
It is passed as an argument to the main function coloring
, but will only work if the associated problem
has :nonsymmetric
structure. Indeed, for symmetric coloring problems, we need more than just the vector of colors to allow fast decompression.
Constructors
ConstantColoringAlgorithm{partition}(matrix_template, color)
ConstantColoringAlgorithm(matrix_template, color; partition=:column)
partition::Symbol
: either :row
or :column
.matrix_template::AbstractMatrix
: matrix for which the vector of colors was precomputed (the algorithm will only accept matrices of the exact same size).color::Vector{Int}
: vector of integer colors, one for each row or column (depending on partition
).
Warning The second constructor (based on keyword arguments) is type-unstable.
We do not necessarily verify consistency between the matrix template and the vector of colors, this is the responsibility of the user.
Example
julia> using SparseMatrixColorings, LinearAlgebra
julia> matrix_template = Diagonal(ones(Bool, 5))
@@ -65,7 +65,7 @@
1
1
1
- 1
ADTypes coloring interface
ConstantColoringAlgorithm
is a subtype of ADTypes.AbstractColoringAlgorithm
, which means the following methods are also applicable (although they will error if the kind of coloring demanded not consistent):
sourceResult analysis
SparseMatrixColorings.AbstractColoringResult
— TypeAbstractColoringResult{structure,partition,decompression}
Abstract type for the result of a coloring algorithm.
It is the supertype of the object returned by the main function coloring
.
Type parameters
Combination between the type parameters of ColoringProblem
and GreedyColoringAlgorithm
:
structure::Symbol
: either :nonsymmetric
or :symmetric
partition::Symbol
: either :column
, :row
or :bidirectional
decompression::Symbol
: either :direct
or :substitution
Applicable methods
column_colors
and column_groups
(for a :column
or :bidirectional
partition) row_colors
and row_groups
(for a :row
or :bidirectional
partition)sparsity_pattern
compress
, decompress
, decompress!
, decompress_single_color!
Warning Unlike the methods above, the concrete subtypes of AbstractColoringResult
are not part of the public API and may change without notice.
sourceSparseMatrixColorings.column_colors
— Functioncolumn_colors(result::AbstractColoringResult)
Return a vector color
of integer colors, one for each column of the colored matrix.
sourceSparseMatrixColorings.row_colors
— Functionrow_colors(result::AbstractColoringResult)
Return a vector color
of integer colors, one for each row of the colored matrix.
sourceSparseMatrixColorings.column_groups
— Functioncolumn_groups(result::AbstractColoringResult)
Return a vector group
such that for every color c
, group[c]
contains the indices of all columns that are colored with c
.
sourceSparseMatrixColorings.row_groups
— Functionrow_groups(result::AbstractColoringResult)
Return a vector group
such that for every color c
, group[c]
contains the indices of all rows that are colored with c
.
sourceSparseMatrixColorings.sparsity_pattern
— Functionsparsity_pattern(result::AbstractColoringResult)
Return the matrix that was initially passed to coloring
, without any modifications.
Note This matrix is not necessarily a SparseMatrixCSC
, nor does it necessarily have Bool
entries.
sourceDecompression
SparseMatrixColorings.compress
— Functioncompress(A, result::AbstractColoringResult)
Compress A
given a coloring result
of the sparsity pattern of A
.
- If
result
comes from a :column
(resp. :row
) partition, the output is a single matrix B
compressed by column (resp. by row). - If
result
comes from a :bidirectional
partition, the output is a tuple of matrices (Br, Bc)
, where Br
is compressed by row and Bc
by column.
Compression means summing either the columns or the rows of A
which share the same color. It is undone by calling decompress
or decompress!
.
Warning At the moment, :bidirectional
partitions are not implemented.
Example
julia> using SparseMatrixColorings, SparseArrays
+ 1
ADTypes coloring interface
ConstantColoringAlgorithm
is a subtype of ADTypes.AbstractColoringAlgorithm
, which means the following methods are also applicable (although they will error if the kind of coloring demanded not consistent):
sourceResult analysis
SparseMatrixColorings.AbstractColoringResult
— TypeAbstractColoringResult{structure,partition,decompression}
Abstract type for the result of a coloring algorithm.
It is the supertype of the object returned by the main function coloring
.
Type parameters
Combination between the type parameters of ColoringProblem
and GreedyColoringAlgorithm
:
structure::Symbol
: either :nonsymmetric
or :symmetric
partition::Symbol
: either :column
, :row
or :bidirectional
decompression::Symbol
: either :direct
or :substitution
Applicable methods
column_colors
and column_groups
(for a :column
or :bidirectional
partition) row_colors
and row_groups
(for a :row
or :bidirectional
partition)sparsity_pattern
compress
, decompress
, decompress!
, decompress_single_color!
Warning Unlike the methods above, the concrete subtypes of AbstractColoringResult
are not part of the public API and may change without notice.
sourceSparseMatrixColorings.column_colors
— Functioncolumn_colors(result::AbstractColoringResult)
Return a vector color
of integer colors, one for each column of the colored matrix.
sourceSparseMatrixColorings.row_colors
— Functionrow_colors(result::AbstractColoringResult)
Return a vector color
of integer colors, one for each row of the colored matrix.
sourceSparseMatrixColorings.column_groups
— Functioncolumn_groups(result::AbstractColoringResult)
Return a vector group
such that for every color c
, group[c]
contains the indices of all columns that are colored with c
.
sourceSparseMatrixColorings.row_groups
— Functionrow_groups(result::AbstractColoringResult)
Return a vector group
such that for every color c
, group[c]
contains the indices of all rows that are colored with c
.
sourceSparseMatrixColorings.sparsity_pattern
— Functionsparsity_pattern(result::AbstractColoringResult)
Return the matrix that was initially passed to coloring
, without any modifications.
Note This matrix is not necessarily a SparseMatrixCSC
, nor does it necessarily have Bool
entries.
sourceDecompression
SparseMatrixColorings.compress
— Functioncompress(A, result::AbstractColoringResult)
Compress A
given a coloring result
of the sparsity pattern of A
.
- If
result
comes from a :column
(resp. :row
) partition, the output is a single matrix B
compressed by column (resp. by row). - If
result
comes from a :bidirectional
partition, the output is a tuple of matrices (Br, Bc)
, where Br
is compressed by row and Bc
by column.
Compression means summing either the columns or the rows of A
which share the same color. It is undone by calling decompress
or decompress!
.
Warning At the moment, :bidirectional
partitions are not implemented.
Example
julia> using SparseMatrixColorings, SparseArrays
julia> A = sparse([
0 0 4 6 0 9
@@ -87,7 +87,7 @@
6 4 9
1 7 0
2 8 0
- 3 5 0
See also
sourceSparseMatrixColorings.decompress
— Functiondecompress(B::AbstractMatrix, result::AbstractColoringResult)
Decompress B
into a new matrix A
, given a coloring result
of the sparsity pattern of A
. The in-place alternative is decompress!
.
Compression means summing either the columns or the rows of A
which share the same color. It is done by calling compress
.
Example
julia> using SparseMatrixColorings, SparseArrays
+ 3 5 0
See also
sourceSparseMatrixColorings.decompress
— Functiondecompress(B::AbstractMatrix, result::AbstractColoringResult)
Decompress B
into a new matrix A
, given a coloring result
of the sparsity pattern of A
. The in-place alternative is decompress!
.
Compression means summing either the columns or the rows of A
which share the same color. It is done by calling compress
.
Example
julia> using SparseMatrixColorings, SparseArrays
julia> A = sparse([
0 0 4 6 0 9
@@ -119,7 +119,7 @@
⋅ 3 5 ⋅ ⋅ ⋅
julia> decompress(B, result) == A
-true
See also
sourceSparseMatrixColorings.decompress!
— Functionsource SparseMatrixColorings.decompress!
— Functiondecompress!(
A::AbstractMatrix, B::AbstractMatrix,
result::AbstractColoringResult, [uplo=:F]
)
Decompress B
in-place into A
, given a coloring result
of the sparsity pattern of A
. The out-of-place alternative is decompress
.
Note In-place decompression is faster when A isa SparseMatrixCSC
.
Compression means summing either the columns or the rows of A
which share the same color. It is done by calling compress
.
For :symmetric
coloring results (and for those only), an optional positional argument uplo in (:U, :L, :F)
can be passed to specify which part of the matrix A
should be updated: the Upper triangle, the Lower triangle, or the Full matrix. When A isa SparseMatrixCSC
, using the uplo
argument requires a target matrix which only stores the relevant triangle(s).
Example
julia> using SparseMatrixColorings, SparseArrays
@@ -156,7 +156,7 @@
⋅ 3 5 ⋅ ⋅ ⋅
julia> A2 == A
-true
See also
sourceSparseMatrixColorings.decompress_single_color!
— Functionsource SparseMatrixColorings.decompress_single_color!
— Functiondecompress_single_color!(
A::AbstractMatrix, b::AbstractVector, c::Integer,
result::AbstractColoringResult, [uplo=:F]
)
Decompress the vector b
corresponding to color c
in-place into A
, given a :direct
coloring result
of the sparsity pattern of A
(it will not work with a :substitution
coloring).
- If
result
comes from a :nonsymmetric
structure with :column
partition, this will update the columns of A
that share color c
(whose sum makes up b
). - If
result
comes from a :nonsymmetric
structure with :row
partition, this will update the rows of A
that share color c
(whose sum makes up b
). - If
result
comes from a :symmetric
structure with :column
partition, this will update the coefficients of A
whose value is deduced from color c
.
Warning This function will only update some coefficients of A
, without resetting the rest to zero.
For :symmetric
coloring results (and for those only), an optional positional argument uplo in (:U, :L, :F)
can be passed to specify which part of the matrix A
should be updated: the Upper triangle, the Lower triangle, or the Full matrix. When A isa SparseMatrixCSC
, using the uplo
argument requires a target matrix which only stores the relevant triangle(s).
Example
julia> using SparseMatrixColorings, SparseArrays
@@ -193,4 +193,4 @@
⋅ 0 5 ⋅ ⋅ ⋅
julia> A2[:, [3, 5]] == A[:, [3, 5]]
-true
See also
sourceOrders
SparseMatrixColorings.AbstractOrder
— TypeAbstractOrder
Abstract supertype for the vertex order used inside GreedyColoringAlgorithm
.
In this algorithm, the rows and columns of a matrix form a graph, and the vertices are colored one after the other in a greedy fashion. Depending on how the vertices are ordered, the number of colors necessary may vary.
Subtypes
sourceSparseMatrixColorings.NaturalOrder
— TypeNaturalOrder()
Instance of AbstractOrder
which sorts vertices using their index in the provided graph.
sourceSparseMatrixColorings.RandomOrder
— TypeRandomOrder(rng=default_rng())
Instance of AbstractOrder
which sorts vertices using a random permutation.
sourceSparseMatrixColorings.LargestFirst
— TypeLargestFirst()
Instance of AbstractOrder
which sorts vertices using their degree in the provided graph: the largest degree comes first.
sourceSettings
This document was generated with Documenter.jl version 1.7.0 on Friday 4 October 2024. Using Julia version 1.10.5.
+true
See also
Orders
SparseMatrixColorings.AbstractOrder
— TypeAbstractOrder
Abstract supertype for the vertex order used inside GreedyColoringAlgorithm
.
In this algorithm, the rows and columns of a matrix form a graph, and the vertices are colored one after the other in a greedy fashion. Depending on how the vertices are ordered, the number of colors necessary may vary.
Subtypes
SparseMatrixColorings.NaturalOrder
— TypeNaturalOrder()
Instance of AbstractOrder
which sorts vertices using their index in the provided graph.
SparseMatrixColorings.RandomOrder
— TypeRandomOrder(rng=default_rng())
Instance of AbstractOrder
which sorts vertices using a random permutation.
SparseMatrixColorings.LargestFirst
— TypeLargestFirst()
Instance of AbstractOrder
which sorts vertices using their degree in the provided graph: the largest degree comes first.