Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change storage and evaluation point order. #5

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end
function _unchecked_gradient!(obj, x)
obj.g_calls .+= 1
copy!(obj.last_x_g, x)
obj.g!(x, obj.g)
obj.g!(obj.g, x)
end
function gradient!(obj::AbstractObjective, x)
if x != obj.last_x_g
Expand All @@ -35,7 +35,7 @@ function value_gradient!(obj::AbstractObjective, x)
obj.g_calls .+= 1
copy!(obj.last_x_f, x)
copy!(obj.last_x_g, x)
obj.f_x = obj.fg!(x, obj.g)
obj.f_x = obj.fg!(obj.g, x)
elseif x != obj.last_x_f
_unchecked_value!(obj, x)
elseif x != obj.last_x_g
Expand All @@ -47,7 +47,7 @@ end
function _unchecked_hessian!(obj::AbstractObjective, x)
obj.h_calls .+= 1
copy!(obj.last_x_h, x)
obj.h!(x, obj.H)
obj.h!(obj.H, x)
end
function hessian!(obj::AbstractObjective, x)
if x != obj.last_x_h
Expand Down
14 changes: 7 additions & 7 deletions src/objective_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ end
# The user friendly/short form OnceDifferentiable constructor
function OnceDifferentiable(f, g!, fg!, x_seed)
g = similar(x_seed)
g!(x_seed, g)
g!(g, x_seed)
OnceDifferentiable(f, g!, fg!, f(x_seed), g, copy(x_seed), copy(x_seed), [1], [1])
end

# Automatically create the fg! helper function if only f and g! is provided
function OnceDifferentiable(f, g!, x_seed)
function fg!(x, storage)
g!(x, storage)
function fg!(storage, x)
g!(storage, x)
return f(x)
end
return OnceDifferentiable(f, g!, fg!, x_seed)
Expand All @@ -59,8 +59,8 @@ function TwiceDifferentiable{T}(f, g!, fg!, h!, x_seed::Array{T})
n_x = length(x_seed)
g = similar(x_seed)
H = Array{T}(n_x, n_x)
g!(x_seed, g)
h!(x_seed, H)
g!(g, x_seed)
h!(H, x_seed)
TwiceDifferentiable(f, g!, fg!, h!, f(x_seed),
g, H, copy(x_seed),
copy(x_seed), copy(x_seed), [1], [1], [1])
Expand All @@ -71,8 +71,8 @@ function TwiceDifferentiable{T}(f,
g!,
h!,
x_seed::Array{T})
function fg!(x::Vector, storage::Vector)
g!(x, storage)
function fg!(storage::Vector, x::Vector)
g!(storage, x)
return f(x)
end
return TwiceDifferentiable(f, g!, fg!, h!, x_seed)
Expand Down
4 changes: 2 additions & 2 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
return exp((2.0 - x[1])^2) + exp((3.0 - x[2])^2)
end

function exponential_gradient!(x::Vector, storage::Vector)
function exponential_gradient!(storage::Vector, x::Vector)
storage[1] = -2.0 * (2.0 - x[1]) * exp((2.0 - x[1])^2)
storage[2] = -2.0 * (3.0 - x[2]) * exp((3.0 - x[2])^2)
end

function exponential_hessian!(x::Vector, storage::Matrix)
function exponential_hessian!(storage::Matrix, x::Vector)
storage[1, 1] = 2.0 * exp((2.0 - x[1])^2) * (2.0 * x[1]^2 - 8.0 * x[1] + 9)
storage[1, 2] = 0.0
storage[2, 1] = 0.0
Expand Down
4 changes: 2 additions & 2 deletions test/objective_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
return exp((2.0 - x[1])^2) + exp((3.0 - x[2])^2)
end

function exponential_gradient!(x::Vector, storage::Vector)
function exponential_gradient!(storage::Vector, x::Vector)
storage[1] = -2.0 * (2.0 - x[1]) * exp((2.0 - x[1])^2)
storage[2] = -2.0 * (3.0 - x[2]) * exp((3.0 - x[2])^2)
end

function exponential_hessian!(x::Vector, storage::Matrix)
function exponential_hessian!(storage::Matrix, x::Vector)
storage[1, 1] = 2.0 * exp((2.0 - x[1])^2) * (2.0 * x[1]^2 - 8.0 * x[1] + 9)
storage[1, 2] = 0.0
storage[2, 1] = 0.0
Expand Down