Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/hessian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ function hessian!(result::AbstractArray, f, x::AbstractArray, cfg::HessianConfig
return result
end


mutable struct GradF{R,CFG,F}
result::R
cfg::CFG
f::F # Add tag check on constructino of GradF???
end
function (x::GradF)(y, z)
inner_result = DiffResult(zero(eltype(y)), y)
gradient!(inner_result, x.f, z, x.cfg.gradient_config, Val{false}())
x.result = DiffResults.value!(x.result, value(DiffResults.value(inner_result)))
return y
end

"""
ForwardDiff.hessian!(result::DiffResult, f, x::AbstractArray, cfg::HessianConfig = HessianConfig(f, result, x), check=Val{true}())

Expand All @@ -45,14 +58,9 @@ Set `check` to `Val{false}()` to disable tag checking. This can lead to perturba
"""
function hessian!(result::DiffResult, f, x::AbstractArray, cfg::HessianConfig{T} = HessianConfig(f, result, x), ::Val{CHK}=Val{true}()) where {T,CHK}
CHK && checktag(T, f, x)
∇f! = (y, z) -> begin
inner_result = DiffResult(zero(eltype(y)), y)
gradient!(inner_result, f, z, cfg.gradient_config, Val{false}())
result = DiffResults.value!(result, value(DiffResults.value(inner_result)))
return y
end
∇f! = GradF(result, cfg, f)
jacobian!(DiffResults.hessian(result), ∇f!, DiffResults.gradient(result), x, cfg.jacobian_config, Val{false}())
return result
return ∇f!.result
end

hessian(f, x::SArray) = jacobian(y -> gradient(f, y), x)
Expand Down