Skip to content

Commit f1ed128

Browse files
Merge pull request #21 from JuliaDiffEq/high_level
add high level interface
2 parents e1d38b6 + 7d56482 commit f1ed128

12 files changed

+54
-35
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2019 Pankaj Mishra <[email protected]>
1+
Copyright (c) 2019 JuliaDiffEq
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Project.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
name = "SparseDiffTools"
22
uuid = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
3-
authors = ["Pankaj Mishra <[email protected]>"]
3+
authors = ["Pankaj Mishra <[email protected]>","Chris Rackauckas <[email protected]>"]
44
version = "0.1.0"
55

66
[deps]
77
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
88
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
9+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1011
VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f"
1112

1213
[compat]
1314
julia = "1"
1415

1516
[extras]
16-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
17-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1817
DiffEqDiffTools = "01453d9d-ee7c-5054-8395-0335cb756afa"
18+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1919

2020
[targets]
21-
test = ["Test", "LinearAlgebra", "DiffEqDiffTools"]
21+
test = ["Test", "DiffEqDiffTools"]

src/SparseDiffTools.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
module SparseDiffTools
22

3+
using SparseArrays, LinearAlgebra, LightGraphs, VertexSafeGraphs
4+
using ForwardDiff: Dual, jacobian, partials, DEFAULT_CHUNK_THRESHOLD
5+
36
export contract_color,
47
greedy_d1,
58
matrix2graph,
9+
matrix_colors,
610
forwarddiff_color_jacobian!,
711
ForwardColorJacCache
812

13+
include("coloring/high_level.jl")
914
include("coloring/contraction_coloring.jl")
1015
include("coloring/greedy_d1_coloring.jl")
11-
include("differentiation/matrix2graph.jl")
16+
include("coloring/matrix2graph.jl")
1217
include("differentiation/compute_jacobian_ad.jl")
1318

1419
end # module

src/coloring/bsc_algo.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#Backtracking Sequential Coloring algorithm
2-
using VertexSafeGraphs
1+
"""
2+
BSCColor
33
4-
function bsc_color(G::VSafeGraph)
4+
Backtracking Sequential Coloring algorithm
5+
"""
6+
function color_graph(G::VSafeGraph,::BSCColor)
57
V = nv(G)
68
F = zeros(Int64, V)
79
freeColors = [Vector{Int64}() for _ in 1:V] #set of free colors for each vertex

src/coloring/contraction_coloring.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using VertexSafeGraphs
2-
using LightGraphs
3-
41
"""
5-
colorGraph(g)
2+
ColorContraction
63
74
Find a coloring of the graph g such that no two vertices connected
85
by an edge have the same color.
96
"""
10-
function contract_color(G::VSafeGraph)
7+
function color_graph(G::VSafeGraph,::ContractionColor)
118

129
colornumber = 0
1310
V = nv(G)

src/coloring/greedy_d1_coloring.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
using VertexSafeGraphs
2-
31
"""
4-
5-
greedy_d1(G)
2+
GreedyD1 Coloring
63
74
Find a coloring of a given input graph such that
85
no two vertices connected by an edge have the same
96
color using greedy approach. The number of colors
107
used may be equal or greater than the chromatic
118
number χ(G) of the graph.
129
"""
13-
function greedy_d1(G::VSafeGraph)
10+
function color_graph(G::VSafeGraph,alg::GreedyD1Color)
1411
V = nv(G)
1512
result = zeros(Int64, V)
1613
result[1] = 1

src/coloring/high_level.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
abstract type ColoringAlgorithm end
2+
struct GreedyD1Color <: ColoringAlgorithm end
3+
struct BSCColor <: ColoringAlgorithm end
4+
struct ContractionColor <: ColoringAlgorithm end
5+
6+
"""
7+
matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color())
8+
9+
Returns the color vector for the matrix A using the chosen coloring
10+
algorithm. If a known analytical solution exists, that is used instead.
11+
The coloring defaults to a greedy distance-1 coloring.
12+
13+
"""
14+
function matrix_colors(A::AbstractMatrix,alg::ColoringAlgorithm = GreedyD1Color())
15+
_A = A isa SparseMatrixCSC ? A : sparse(A) # Avoid the copy
16+
A_graph = matrix2graph(_A)
17+
color_graph(A_graph,alg)
18+
end
19+
20+
function matrix_colors(A::Array)
21+
eachindex(size(A,2)) # Vector size matches number of rows
22+
end
23+
24+
function matrix_colors(A::Tridiagonal)
25+
repeat(1:3,div(size(A,2),3)+1)[1:size(A,2)]
26+
end

src/differentiation/matrix2graph.jl renamed to src/coloring/matrix2graph.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using SparseArrays
2-
using LightGraphs
3-
using VertexSafeGraphs
4-
51
"""
62
matrix2graph(SparseMatrix)
73

src/differentiation/compute_jacobian_ad.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using ForwardDiff: Dual, jacobian, partials, DEFAULT_CHUNK_THRESHOLD
2-
31
struct ForwardColorJacCache{T,T2,T3,T4,T5}
42
t::T
53
fx::T2

test/test_contraction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end
2323

2424
for i in 1:20
2525
g = test_graphs[i]
26-
out_colors = contract_color(g)
26+
out_colors = SparseDiffTools.color_graph(g,SparseDiffTools.ContractionColor())
2727
for v = 1:nv(g)
2828
color = out_colors[v]
2929
for j in inneighbors(g, v)

0 commit comments

Comments
 (0)