Skip to content

Commit

Permalink
Fix F-dimension issue and add test (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
iewaij authored and pkofod committed Aug 10, 2018
1 parent 18220aa commit 98894c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/objective_types/oncedifferentiable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, DF::AbstractA
F = similar(x)
fj!(F, J, x)
end
return OnceDifferentiable(f, j!, fj!, x, x, DF)
return OnceDifferentiable(f, j!, fj!, x, F, DF)
elseif autodiff == :forward || autodiff == true
jac_cfg = ForwardDiff.JacobianConfig(f, F, x, chunk)
ForwardDiff.checktag(jac_cfg, f, x)
Expand All @@ -113,7 +113,7 @@ function OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, DF::AbstractA
DiffResults.value(jac_res)
end

return OnceDifferentiable(f, g!, fg!, x, x, DF)
return OnceDifferentiable(f, g!, fg!, x, F, DF)
else
error("The autodiff value $(autodiff) is not supported. Use :finite or :forward.")
end
Expand Down Expand Up @@ -180,8 +180,5 @@ function OnceDifferentiable(f, df, fdf,

x_f, x_df = x_of_nans(x), x_of_nans(x)

OnceDifferentiable(f, df!, fdf!,
copy(F), copy(DF),
x_f, x_df,
[0,], [0,])
OnceDifferentiable(f, df!, fdf!, copy(F), copy(DF), x_f, x_df, [0,], [0,])
end
31 changes: 23 additions & 8 deletions test/autodiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,30 @@ end
@test gradient(odad1) 2 .* a .* (x_seed .+ 1.0)
end

@testset "jacobian of residual function" begin
@testset "residual function" begin
function f!(F, x)
F[1] = (x[1]^3 - 7.0 * x[1]^2 + 14.0 * x[1] - 8)* x[2]^2 * exp(-x[2])
F[2] = 2.0 * (1.0 - 8.0 * x[1] + 7.0 * x[1]^2 - (7.0 / 3.0) * x[1]^3 + (1.0 / 4.0) * x[1]^4) * x[2] * exp(-x[2]) - (1.0 - 8.0 * x[1] + 7.0 * x[1]^2 - (7.0 / 3.0) * x[1]^3 + (1.0 / 4.0) * x[1]^4) * x[2]^2 * exp(-x[2])
F[1] = 2x[1]+x[2]
F[2] = x[1]+x[2]^2
F[3] = x[1]^2+x[2]^2
F
end

x = zeros(4)
F = zeros(2)
od = OnceDifferentiable(f!, x, F)
# This failed before, now it runs!
value_jacobian!!(od, [0., 8., 1., 1.])
function j!(J, x)
J[1, 1] = 2
J[1, 2] = 1
J[2, 1] = 1
J[2, 2] = 2 * x[2]
J[3, 1] = 2 * x[1]
J[3, 2] = 2 * x[2]
J
end

F = zeros(3)
J = zeros(3, 2)
x_init = [1., 1.]
od = OnceDifferentiable(f!, x_init, F)
value_jacobian!(od, x_init)
@test length(value(od)) == length(F)
@test value(od) f!(F, x_init)
@test jacobian(od) j!(J, x_init)
end

0 comments on commit 98894c6

Please sign in to comment.