1- struct ForwardColorHesCache{THS,THC,TI<: Integer ,TD,TGF,TGC,TG}
1+ struct ForwardColorHesCache{THS, THC, TI<: Integer , TD, TGF, TGC, TG}
22 sparsity:: THS
33 colors:: THC
44 ncolors:: TI
@@ -16,40 +16,40 @@ function make_hessian_buffers(colorvec, x)
1616 buffer = similar (D)
1717 G1 = similar (x)
1818 G2 = similar (x)
19- return (; ncolors, D, buffer, G1, G2)
19+ return (;ncolors, D, buffer, G1, G2)
2020end
2121
22- function ForwardColorHesCache (f,
23- x:: AbstractVector{<:Number} ,
24- colorvec:: AbstractVector{<:Integer} = eachindex (x),
25- sparsity:: Union{AbstractMatrix,Nothing} = nothing ,
26- (g!) = (G, x, grad_config) -> ForwardDiff. gradient! (G, f, x, grad_config))
22+ function ForwardColorHesCache (f,
23+ x:: AbstractVector{<:Number} ,
24+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
25+ sparsity:: Union{AbstractMatrix, Nothing} = nothing ,
26+ g! = (G, x, grad_config) -> ForwardDiff. gradient! (G, f, x, grad_config))
2727 ncolors, D, buffer, G, G2 = make_hessian_buffers (colorvec, x)
2828 grad_config = ForwardDiff. GradientConfig (f, x)
29-
29+
3030 # If user supplied their own gradient function, make sure it has the right
3131 # signature (i.e. g!(G, x) or g!(G, x, grad_config::ForwardDiff.GradientConfig))
32- if ! hasmethod (g!, (typeof (G), typeof (G), typeof (grad_config)))
33- if ! hasmethod (g!, (typeof (G), typeof (G)))
32+ if ! hasmethod (g!, (typeof (G), typeof (G), typeof (grad_config)))
33+ if ! hasmethod (g!, (typeof (G), typeof (G)))
3434 throw (ArgumentError (" Signature of `g!` must be either `g!(G, x)` or `g!(G, x, grad_config::ForwardDiff.GradientConfig)`" ))
3535 end
3636 # define new method that takes a GradientConfig but doesn't use it
3737 g1! (G, x, grad_config) = g! (G, x)
3838 else
3939 g1! = g!
4040 end
41-
41+
4242 if sparsity === nothing
4343 sparsity = sparse (ones (length (x), length (x)))
4444 end
4545 return ForwardColorHesCache (sparsity, colorvec, ncolors, D, buffer, g1!, grad_config, G, G2)
4646end
4747
48- function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
49- f,
50- x:: AbstractArray{<:Number} ,
51- hes_cache:: ForwardColorHesCache ;
52- safe= true )
48+ function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
49+ f,
50+ x:: AbstractArray{<:Number} ,
51+ hes_cache:: ForwardColorHesCache ;
52+ safe = true )
5353 ϵ = cbrt (eps (eltype (x)))
5454 for j in 1 : hes_cache. ncolors
5555 x .+ = ϵ .* @view hes_cache. D[:, j]
@@ -69,28 +69,28 @@ function numauto_color_hessian!(H::AbstractMatrix{<:Number},
6969 return H
7070end
7171
72- function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
73- f,
74- x:: AbstractArray{<:Number} ,
75- colorvec:: AbstractVector{<:Integer} = eachindex (x),
76- sparsity:: Union{AbstractMatrix,Nothing} = nothing )
72+ function numauto_color_hessian! (H:: AbstractMatrix{<:Number} ,
73+ f,
74+ x:: AbstractArray{<:Number} ,
75+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
76+ sparsity:: Union{AbstractMatrix, Nothing} = nothing )
7777 hes_cache = ForwardColorHesCache (f, x, colorvec, sparsity)
7878 numauto_color_hessian! (H, f, x, hes_cache)
7979 return H
8080end
8181
82- function numauto_color_hessian (f,
83- x:: AbstractArray{<:Number} ,
84- hes_cache:: ForwardColorHesCache )
82+ function numauto_color_hessian (f,
83+ x:: AbstractArray{<:Number} ,
84+ hes_cache:: ForwardColorHesCache )
8585 H = convert .(eltype (x), hes_cache. sparsity)
8686 numauto_color_hessian! (H, f, x, hes_cache)
8787 return H
8888end
8989
9090function numauto_color_hessian (f,
91- x:: AbstractArray{<:Number} ,
92- colorvec:: AbstractVector{<:Integer} = eachindex (x),
93- sparsity:: Union{AbstractMatrix,Nothing} = nothing )
91+ x:: AbstractArray{<:Number} ,
92+ colorvec:: AbstractVector{<:Integer} = eachindex (x),
93+ sparsity:: Union{AbstractMatrix, Nothing} = nothing )
9494 hes_cache = ForwardColorHesCache (f, x, colorvec, sparsity)
9595 H = convert .(eltype (x), hes_cache. sparsity)
9696 numauto_color_hessian! (H, f, x, hes_cache)
103103
104104mutable struct ForwardAutoColorHesCache{TJC,TG,TS,TC}
105105 jac_cache:: TJC
106- grad!TG
106+ grad!:: TG
107107 sparsity:: TS
108108 colorvec:: TC
109109end
0 commit comments