Skip to content

Commit

Permalink
Revert "Fix initialization of DF and DDF's. (#112)"
Browse files Browse the repository at this point in the history
This reverts commit 7bf56b5.
  • Loading branch information
pkofod committed Jan 27, 2020
1 parent e822369 commit b49c532
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/NLSolversBase.jl
Expand Up @@ -57,7 +57,7 @@ end
is_finitediff(autodiff) = autodiff (:central, :finite, :finiteforward, :finitecomplex)
is_forwarddiff(autodiff) = autodiff (:forward, :forwarddiff, true)

x_of_nans(x, Tf=eltype(x)) = fill!(Tf.(x), Tf(NaN))
x_of_nans(x) = fill!(similar(x), eltype(x)(NaN))

include("objective_types/inplace_factory.jl")
include("objective_types/abstract.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/objective_types/abstract.jl
Expand Up @@ -16,8 +16,8 @@ function make_fdf(x, F::Number, f, g!)
end

# Initialize an n-by-n Jacobian
alloc_DF(x, F) = fill(eltype(F)(NaN), length(F), length(x))
alloc_DF(x, F) = fill(eltype(x)(NaN), length(F), length(x))
# Initialize a gradient shaped like x
alloc_DF(x, F::T) where T<:Number = x_of_nans(x, T)
alloc_DF(x, F::Number) = x_of_nans(x)
# Initialize an n-by-n Hessian
alloc_H(x, F::T) where T<:Number = alloc_DF(x, T.(x))
alloc_H(x) = alloc_DF(x, x)
4 changes: 2 additions & 2 deletions src/objective_types/incomplete.jl
Expand Up @@ -83,7 +83,7 @@ const InPlaceFGH = InplaceObjective{<:Nothing,<:Nothing,TH,<:Nothing,<:Nothing}
const InPlaceFG_HV = InplaceObjective{<:Nothing,TFG,<:Nothing,THv,<:Nothing} where {TFG,THv}
const InPlaceFGHV = InplaceObjective{<:Nothing,<:Nothing,<:Nothing,<:Nothing,TFGHv} where {TFGHv}

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}
function TwiceDifferentiable(t::InPlaceFGH, x::AbstractArray, F::Real = real(zero(eltype(x))), G::AbstractArray = similar(x), H = alloc_H(x)) where {TH}
f = x -> t.fgh(F, nothing, nothing, x)
df = (G, x) -> t.fgh(nothing, G, nothing, x)
fdf = (G, x) -> t.fgh(F, G, nothing, x)
Expand All @@ -93,7 +93,7 @@ end

function TwiceDifferentiable(t::InPlaceFGH, x::AbstractVector, F::Real = real(zero(eltype(x))), G::AbstractVector = similar(x)) where {TH}

H = alloc_H(x, F)
H = alloc_H(x)
f = x -> t.fgh(F, nothing, nothing, x)
df = (G, x) -> t.fgh(nothing, G, nothing, x)
fdf = (G, x) -> t.fgh(F, G, nothing, x)
Expand Down
6 changes: 3 additions & 3 deletions src/objective_types/twicedifferentiable.jl
Expand Up @@ -15,7 +15,7 @@ mutable struct TwiceDifferentiable{T,TDF,TH,TX} <: AbstractObjective
h_calls::Vector{Int}
end
# compatibility with old constructor
function TwiceDifferentiable(f, g, fg, h, x::TX, F::T = real(zero(eltype(x))), G::TG = alloc_DF(x, F), H::TH = alloc_H(x, F); inplace = true) where {T, TG, TH, TX}
function TwiceDifferentiable(f, g, fg, h, x::TX, F::T = real(zero(eltype(x))), G::TG = similar(x), H::TH = alloc_H(x); inplace = true) where {T, TG, TH, TX}
x_f, x_df, x_h = x_of_nans(x), x_of_nans(x), x_of_nans(x)

g! = df!_from_df(g, F, inplace)
Expand All @@ -31,8 +31,8 @@ end
function TwiceDifferentiable(f, g, h,
x::AbstractVector{TX},
F::Real = real(zero(eltype(x))),
G = alloc_DF(x, F),
H = alloc_H(x, F); inplace = true) where {TX}
G = similar(x),
H = alloc_H(x); inplace = true) where {TX}

g! = df!_from_df(g, F, inplace)
h! = h!_from_h(h, F, inplace)
Expand Down
2 changes: 1 addition & 1 deletion test/interface.jl
Expand Up @@ -168,7 +168,7 @@
x = rand(prob.initial_x, length(prob.initial_x))
v = rand(prob.initial_x, length(prob.initial_x))
G = NLSolversBase.alloc_DF(x, 0.0)
H = NLSolversBase.alloc_H(x, 0.0)
H = NLSolversBase.alloc_H(x)
MVP.hessian(prob)(H, x)
@test hv_product!(ddf, x, v) == H*v
@test hv_product(ddf) == H*v
Expand Down

0 comments on commit b49c532

Please sign in to comment.