@@ -19,14 +19,14 @@ function make_hessian_buffers(colorvec, x)
19
19
return (;ncolors, D, buffer, G1, G2)
20
20
end
21
21
22
- function ForwardColorHesCache (f,
23
- x:: AbstractVector{<:Number} ,
24
- colorvec:: AbstractVector{<:Integer} = eachindex (x),
22
+ function ForwardColorHesCache (f,
23
+ x:: AbstractVector{<:Number} ,
24
+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
25
25
sparsity:: Union{AbstractMatrix, Nothing} = nothing ,
26
26
g! = (G, x, grad_config) -> ForwardDiff. gradient! (G, f, x, grad_config))
27
27
ncolors, D, buffer, G, G2 = make_hessian_buffers (colorvec, x)
28
28
grad_config = ForwardDiff. GradientConfig (f, x)
29
-
29
+
30
30
# If user supplied their own gradient function, make sure it has the right
31
31
# signature (i.e. g!(G, x) or g!(G, x, grad_config::ForwardDiff.GradientConfig))
32
32
if ! hasmethod (g!, (typeof (G), typeof (G), typeof (grad_config)))
@@ -38,16 +38,16 @@ function ForwardColorHesCache(f,
38
38
else
39
39
g1! = g!
40
40
end
41
-
41
+
42
42
if sparsity === nothing
43
43
sparsity = sparse (ones (length (x), length (x)))
44
44
end
45
45
return ForwardColorHesCache (sparsity, colorvec, ncolors, D, buffer, g1!, grad_config, G, G2)
46
46
end
47
47
48
- function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
49
- f,
50
- x:: AbstractArray{<:Number} ,
48
+ function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
49
+ f,
50
+ x:: AbstractArray{<:Number} ,
51
51
hes_cache:: ForwardColorHesCache ;
52
52
safe = true )
53
53
ϵ = cbrt (eps (eltype (x)))
@@ -69,18 +69,18 @@ function numauto_color_hessian!(H::AbstractMatrix{<:Number},
69
69
return H
70
70
end
71
71
72
- function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
73
- f,
72
+ function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
73
+ f,
74
74
x:: AbstractArray{<:Number} ,
75
- colorvec:: AbstractVector{<:Integer} = eachindex (x),
75
+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
76
76
sparsity:: Union{AbstractMatrix, Nothing} = nothing )
77
77
hes_cache = ForwardColorHesCache (f, x, colorvec, sparsity)
78
78
numauto_color_hessian! (H, f, x, hes_cache)
79
79
return H
80
80
end
81
81
82
- function numauto_color_hessian (f,
83
- x:: AbstractArray{<:Number} ,
82
+ function numauto_color_hessian (f,
83
+ x:: AbstractArray{<:Number} ,
84
84
hes_cache:: ForwardColorHesCache )
85
85
H = convert .(eltype (x), hes_cache. sparsity)
86
86
numauto_color_hessian! (H, f, x, hes_cache)
89
89
90
90
function numauto_color_hessian (f,
91
91
x:: AbstractArray{<:Number} ,
92
- colorvec:: AbstractVector{<:Integer} = eachindex (x),
92
+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
93
93
sparsity:: Union{AbstractMatrix, Nothing} = nothing )
94
94
hes_cache = ForwardColorHesCache (f, x, colorvec, sparsity)
95
95
H = convert .(eltype (x), hes_cache. sparsity)
@@ -113,13 +113,13 @@ struct AutoAutoTag end
113
113
function ForwardAutoColorHesCache (f,
114
114
x:: AbstractVector{V} ,
115
115
colorvec:: AbstractVector{<:Integer} = eachindex (x),
116
- sparsity:: Union{AbstractMatrix,Nothing} = nothing ) where V
116
+ sparsity:: Union{AbstractMatrix,Nothing} = nothing ,
117
+ tag:: ForwardDiff.Tag = ForwardDiff. Tag (AutoAutoTag (), V)) where V
117
118
118
119
if sparsity === nothing
119
120
sparsity = sparse (ones (length (x), length (x)))
120
121
end
121
122
122
- tag = ForwardDiff. Tag (AutoAutoTag (), V)
123
123
chunksize = ForwardDiff. pickchunksize (maximum (colorvec))
124
124
chunk = ForwardDiff. Chunk (chunksize)
125
125
@@ -130,7 +130,7 @@ function ForwardAutoColorHesCache(f,
130
130
g! = (G, x) -> ForwardDiff. gradient! (G, f, x, gradient_config, Val (false ))
131
131
132
132
jac_cache = ForwardColorJacCache (g!, x; colorvec, sparsity, tag= outer_tag)
133
-
133
+
134
134
return ForwardAutoColorHesCache (jac_cache, g!, sparsity, colorvec)
135
135
end
136
136
0 commit comments