|
93 | 93 | const InPlaceFGH = InplaceObjective{<:Nothing,<:Nothing,TH,<:Nothing,<:Nothing} where {TH}
|
94 | 94 | const InPlaceFG_HV = InplaceObjective{<:Nothing,TFG,<:Nothing,THv,<:Nothing} where {TFG,THv}
|
95 | 95 | const InPlaceFGHV = InplaceObjective{<:Nothing,<:Nothing,<:Nothing,<:Nothing,TFGHv} where {TFGHv}
|
96 |
| - |
97 | 96 | function TwiceDifferentiable(t::InPlaceFGH, x::AbstractArray, F::Real = real(zero(eltype(x))), G::AbstractArray = alloc_DF(x, F), H = alloc_H(x, F)) where {TH}
|
98 | 97 | f = x -> t.fgh(F, nothing, nothing, x)
|
99 | 98 | df = (G, x) -> t.fgh(nothing, G, nothing, x)
|
100 | 99 | fdf = (G, x) -> t.fgh(F, G, nothing, x)
|
| 100 | + fdfh = (G, H, x) -> t.fgh(F, G, H, x) |
| 101 | + dfh = (G, H, x) -> t.fgh(nothing, G, H, x) |
101 | 102 | h = (H, x) -> t.fgh(F, nothing, H, x)
|
102 |
| - TwiceDifferentiable(f, df, fdf, h, x, F, G, H) |
103 |
| -end |
104 | 103 |
|
105 |
| -function TwiceDifferentiable(t::InPlaceFGH, x::AbstractVector, F::Real = real(zero(eltype(x))), G::AbstractVector = similar(x)) where {TH} |
| 104 | + x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x) |
106 | 105 |
|
107 |
| - H = alloc_H(x, F) |
| 106 | + TwiceDifferentiable(f, df, fdf, dfh, fdfh, h, |
| 107 | + copy(F), copy(G), copy(H), |
| 108 | + x_f, x_df, x_h, |
| 109 | + [0,], [0,], [0,]) |
| 110 | +end |
| 111 | +function TwiceDifferentiable(t::InPlaceFGH, x::AbstractVector{T}, F::Real = real(zero(eltype(x))), G::AbstractVector{Tx} = alloc_DF(x, F)) where {T, Tx} |
108 | 112 | f = x -> t.fgh(F, nothing, nothing, x)
|
109 | 113 | df = (G, x) -> t.fgh(nothing, G, nothing, x)
|
110 | 114 | fdf = (G, x) -> t.fgh(F, G, nothing, x)
|
| 115 | + fdfh = (G, H, x) -> t.fgh(F, G, H, x) |
| 116 | + dfh = (G, H, x) -> t.fgh(nothing, G, H, x) |
111 | 117 | h = (H, x) -> t.fgh(F, nothing, H, x)
|
112 |
| - TwiceDifferentiable(f, df, fdf, h, x, F, G, H) |
| 118 | + |
| 119 | + H = alloc_H(x, F) |
| 120 | + x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x) |
| 121 | + |
| 122 | + TwiceDifferentiable(f, df, fdf, dfh, fdfh, h, |
| 123 | + copy(F), copy(G), copy(H), |
| 124 | + x_f, x_df, x_h, |
| 125 | + [0,], [0,], [0,]) |
| 126 | +end |
| 127 | +function value_gradient_hessian!!(obj, x) |
| 128 | + obj.f_calls .+= 1 |
| 129 | + obj.df_calls .+= 1 |
| 130 | + obj.h_calls .+= 1 |
| 131 | + obj.x_f .= x |
| 132 | + obj.x_df .= x |
| 133 | + obj.x_h .= x |
| 134 | + if obj.fdfh === nothing |
| 135 | + obj.F = obj.fdf(obj.DF, x) |
| 136 | + obj.h(obj.H, x) |
| 137 | + else |
| 138 | + obj.F = obj.fdfh(obj.DF, obj.H, x) |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +function gradient_hessian!!(obj, x) |
| 143 | + if obj.dfh === nothing |
| 144 | + gradient!!(obj, x) |
| 145 | + hessian!!(obj, x) |
| 146 | + else |
| 147 | + obj.df_calls .+= 1 |
| 148 | + obj.h_calls .+= 1 |
| 149 | + obj.x_df .= x |
| 150 | + obj.x_h .= x |
| 151 | + obj.dfh(obj.DF, obj.H, x) |
| 152 | + end |
113 | 153 | end
|
114 | 154 |
|
115 | 155 | function TwiceDifferentiableHV(t::InPlaceFG_HV, x::AbstractVector)
|
|
0 commit comments