Skip to content

Commit c1729f2

Browse files
Complete colorvec transition
1 parent c04f12c commit c1729f2

10 files changed

+50
-50
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ sparsity_pattern = sparsity!(f,output,input)
4444
jac = Float64.(sparse(sparsity_pattern))
4545
```
4646

47-
Now we call `matrix_colors` to get the color vector for that matrix:
47+
Now we call `matrix_colors` to get the colorvec vector for that matrix:
4848

4949
```julia
5050
colors = matrix_colors(jac)
@@ -54,14 +54,14 @@ Since `maximum(colors)` is 3, this means that finite differencing can now
5454
compute the Jacobian in just 4 `f`-evaluations:
5555

5656
```julia
57-
DiffEqDiffTools.finite_difference_jacobian!(jac, f, rand(30), color=colors)
57+
DiffEqDiffTools.finite_difference_jacobian!(jac, f, rand(30), colorvec=colors)
5858
@show fcalls # 4
5959
```
6060

6161
In addition, a faster forward-mode autodiff call can be utilized as well:
6262

6363
```julia
64-
forwarddiff_color_jacobian!(jac, f, x, color = colors)
64+
forwarddiff_color_jacobian!(jac, f, x, colorvec = colors)
6565
```
6666

6767
If one only need to compute products, one can use the operators. For example,
@@ -102,7 +102,7 @@ requires an `f` call to `maximum(colors)+1`, or reduces automatic differentiatio
102102
to using `maximum(colors)` partials. Since normally these values are `length(x)`,
103103
this can be significant savings.
104104

105-
The API for computing the color vector is:
105+
The API for computing the colorvec vector is:
106106

107107
```julia
108108
matrix_colors(A::AbstractMatrix,alg::ColoringAlgorithm = GreedyD1Color();
@@ -113,31 +113,31 @@ The first argument is the abstract matrix which represents the sparsity pattern
113113
of the Jacobian. The second argument is the optional choice of coloring algorithm.
114114
It will default to a greedy distance 1 coloring, though if your special matrix
115115
type has more information, like is a `Tridiagonal` or `BlockBandedMatrix`, the
116-
color vector will be analytically calculated instead. The keyword argument
116+
colorvec vector will be analytically calculated instead. The keyword argument
117117
`partition_by_rows` allows you to partition the Jacobian on the basis of rows instead
118118
of columns and generate a corresponding coloring vector which can be used for
119119
reverse-mode AD. Default value is false.
120120

121-
The result is a vector which assigns a color to each column (or row) of the matrix.
121+
The result is a vector which assigns a colorvec to each column (or row) of the matrix.
122122

123-
### Color-Assisted Differentiation
123+
### Colorvec-Assisted Differentiation
124124

125-
Color-assisted differentiation for numerical differentiation is provided by
125+
Colorvec-assisted differentiation for numerical differentiation is provided by
126126
DiffEqDiffTools.jl and for automatic differentiation is provided by
127127
ForwardDiff.jl.
128128

129-
For DiffEqDiffTools.jl, one simply has to use the provided `color` keyword
129+
For DiffEqDiffTools.jl, one simply has to use the provided `colorvec` keyword
130130
argument. See [the DiffEqDiffTools Jacobian documentation](https://github.com/JuliaDiffEq/DiffEqDiffTools.jl#jacobians) for more details.
131131

132-
For forward-mode automatic differentiation, use of a color vector is provided
132+
For forward-mode automatic differentiation, use of a colorvec vector is provided
133133
by the following function:
134134

135135
```julia
136136
forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
137137
f,
138138
x::AbstractArray{<:Number};
139139
dx = nothing,
140-
color = eachindex(x),
140+
colorvec = eachindex(x),
141141
sparsity = nothing)
142142
```
143143

@@ -147,7 +147,7 @@ cache, construct the cache in advance:
147147
```julia
148148
ForwardColorJacCache(f,x,_chunksize = nothing;
149149
dx = nothing,
150-
color=1:length(x),
150+
colorvec=1:length(x),
151151
sparsity = nothing)
152152
```
153153

src/coloring/backtracking_coloring.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor)
2020

2121
start = 1
2222

23-
#optimal color number
23+
#optimal chromatic number
2424
opt = v + 1
2525

2626
#current vertex to be colored
@@ -135,7 +135,7 @@ end
135135
opt::Integer)
136136
137137
Returns set of free colors of x which are less
138-
than optimal color number (opt)
138+
than optimal chromatic number (opt)
139139
140140
Arguments:
141141
@@ -192,7 +192,7 @@ end
192192
least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer)
193193
194194
Returns least index i such that color of vertex
195-
A[i] is equal to `opt` (optimal color number)
195+
A[i] is equal to `opt` (optimal chromatic number)
196196
"""
197197
function least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer)
198198
for i in eachindex(A)
@@ -218,7 +218,7 @@ end
218218
remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer)
219219
220220
Remove all the colors which are greater than or
221-
equal to the `opt` (optimal color number) from
221+
equal to the `opt` (optimal chromatic number) from
222222
the set of colors U
223223
"""
224224
function remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer)

src/coloring/greedy_star1_coloring.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@
2323
"""
2424
function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color)
2525
v = nv(g)
26-
color = zeros(Int, v)
26+
colorvec = zeros(Int, v)
2727

2828
forbidden_colors = zeros(Int, v+1)
2929

3030
for vertex_i = vertices(g)
3131

3232
for w in inneighbors(g, vertex_i)
33-
if color[w] != 0
34-
forbidden_colors[color[w]] = vertex_i
33+
if colorvec[w] != 0
34+
forbidden_colors[colorvec[w]] = vertex_i
3535
end
3636

3737
for x in inneighbors(g, w)
38-
if color[x] != 0
39-
if color[w] == 0
40-
forbidden_colors[color[x]] = vertex_i
38+
if colorvec[x] != 0
39+
if colorvec[w] == 0
40+
forbidden_colors[colorvec[x]] = vertex_i
4141
else
4242
for y in inneighbors(g, x)
43-
if color[y] != 0
44-
if y != w && color[y] == color[w]
45-
forbidden_colors[color[x]] = vertex_i
43+
if colorvec[y] != 0
44+
if y != w && colorvec[y] == colorvec[w]
45+
forbidden_colors[colorvec[x]] = vertex_i
4646
break
4747
end
4848
end
@@ -52,10 +52,10 @@ function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color)
5252
end
5353
end
5454

55-
color[vertex_i] = find_min_color(forbidden_colors, vertex_i)
55+
colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i)
5656
end
5757

58-
color
58+
colorvec
5959
end
6060

6161
function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer)

src/coloring/greedy_star2_coloring.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,32 @@
2626
"""
2727
function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color)
2828
v = nv(g)
29-
color = zeros(Int, v)
29+
colorvec = zeros(Int, v)
3030

3131
forbidden_colors = zeros(Int, v+1)
3232

3333
for vertex_i = vertices(g)
3434

3535
for w in inneighbors(g, vertex_i)
36-
if color[w] != 0
37-
forbidden_colors[color[w]] = vertex_i
36+
if colorvec[w] != 0
37+
forbidden_colors[colorvec[w]] = vertex_i
3838
end
3939

4040
for x in inneighbors(g, w)
41-
if color[x] != 0
42-
if color[w] == 0
43-
forbidden_colors[color[x]] = vertex_i
41+
if colorvec[x] != 0
42+
if colorvec[w] == 0
43+
forbidden_colors[colorvec[x]] = vertex_i
4444
else
45-
if color[x] < color[w]
46-
forbidden_colors[color[x]] = vertex_i
45+
if colorvec[x] < colorvec[w]
46+
forbidden_colors[colorvec[x]] = vertex_i
4747
end
4848
end
4949
end
5050
end
5151
end
5252

53-
color[vertex_i] = find_min_color(forbidden_colors, vertex_i)
53+
colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i)
5454
end
5555

56-
color
56+
colorvec
5757
end

src/coloring/high_level.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct GreedyStar2Color <: SparseDiffToolsColoringAlgorithm end
88
"""
99
matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color())
1010
11-
Returns the color vector for the matrix A using the chosen coloring
11+
Returns the colorvec vector for the matrix A using the chosen coloring
1212
algorithm. If a known analytical solution exists, that is used instead.
1313
The coloring defaults to a greedy distance-1 coloring.
1414

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if GROUP == "All"
99
@time @safetestset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end
1010
@time @safetestset "Greedy star coloring" begin include("test_greedy_star.jl") end
1111
@time @safetestset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end
12-
@time @safetestset "AD using color vector" begin include("test_ad.jl") end
12+
@time @safetestset "AD using colorvec vector" begin include("test_ad.jl") end
1313
@time @safetestset "Integration test" begin include("test_integration.jl") end
1414
@time @safetestset "Special matrices" begin include("test_specialmatrices.jl") end
1515
@time @safetestset "Jac Vecs and Hes Vecs" begin include("test_jaches_products.jl") end

test/test_ad.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ _J = sparse(J)
3333

3434
fcalls = 0
3535
_J1 = similar(_J)
36-
forwarddiff_color_jacobian!(_J1, f, x, color = repeat(1:3,10))
36+
forwarddiff_color_jacobian!(_J1, f, x, colorvec = repeat(1:3,10))
3737
@test _J1 J
3838
@test fcalls == 1
3939

4040
fcalls = 0
41-
jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1)
41+
jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1)
4242
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
4343
@test _J1 J
4444
@test fcalls == 1
4545

4646
fcalls = 0
4747
_J1 = similar(_J)
4848
_denseJ1 = collect(_J1)
49-
forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _J1)
49+
forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1)
5050
@test _denseJ1 J
5151
@test fcalls == 1
5252

5353
fcalls = 0
5454
_J1 = similar(_J)
5555
_denseJ1 = collect(_J1)
56-
jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1)
56+
jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1)
5757
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
5858
@test _denseJ1 J
5959
@test fcalls == 1
6060

6161
_Jt = similar(Tridiagonal(J))
62-
forwarddiff_color_jacobian!(_Jt, f, x, color = repeat(1:3,10), sparsity = _Jt)
62+
forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3,10), sparsity = _Jt)
6363
@test _Jt J

test/test_gpu_ad.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ x = cu(rand(30))
1313
CuArrays.allowscalar(false)
1414
forwarddiff_color_jacobian!(_denseJ1, f, x)
1515
forwarddiff_color_jacobian!(_denseJ1, f, x, sparsity = _J1)
16-
forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _J1)
16+
forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1)
1717
_Jt = similar(Tridiagonal(_J1))
18-
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _Jt)
18+
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _Jt)

test/test_integration.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ J = finite_difference_jacobian(f, rand(30))
3939
#Jacobian computed with coloring vectors
4040
fcalls = 0
4141
_J = similar(true_jac)
42-
finite_difference_jacobian!(_J, f, rand(30), color = colors)
42+
finite_difference_jacobian!(_J, f, rand(30), colorvec = colors)
4343
@test fcalls == 4
4444
@test _J J
4545

4646
fcalls = 0
4747
_J = similar(true_jac)
4848
_denseJ = collect(_J)
49-
finite_difference_jacobian!(_denseJ, f, rand(30), color = colors, sparsity=_J)
49+
finite_difference_jacobian!(_denseJ, f, rand(30), colorvec = colors, sparsity=_J)
5050
@test fcalls == 4
5151
@test _denseJ J

test/test_specialmatrices.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ bandedblockbanded3 = BandedBlockBandedMatrix(Zeros(2 * 4 , 2 * 4), ([4, 4] ,[4,
4141
function _testvalidity(A)
4242
colorvec=matrix_colors(A)
4343
ncolor=maximum(colorvec)
44-
for color in 1:ncolor
45-
subA=A[:,findall(x->x==color,colorvec)]
44+
for colorvec in 1:ncolor
45+
subA=A[:,findall(x->x==colorvec,colorvec)]
4646
@test maximum(sum(subA,dims=2))<=1.0
4747
end
4848
end

0 commit comments

Comments
 (0)