diff --git a/src/differentiable_vectors/autodiff.jl b/src/differentiable_vectors/autodiff.jl index a80598e..3457e13 100644 --- a/src/differentiable_vectors/autodiff.jl +++ b/src/differentiable_vectors/autodiff.jl @@ -1,4 +1,4 @@ -function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = :central) +function OnceDifferentiable(f!, x::AbstractArray, F::AbstractArray, autodiff::Union{Symbol, Bool} = :central) if autodiff == :central function fj!(F, J, x) f!(F, x) @@ -13,7 +13,7 @@ function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = : F = similar(x) fj!(F, J, x) end - return OnceDifferentiable(f!, j!, fj!, similar(x), similar(x)) + return OnceDifferentiable(f!, j!, fj!, x, x) elseif autodiff == :forward || autodiff == true jac_cfg = ForwardDiff.JacobianConfig(f!, x, x) ForwardDiff.checktag(jac_cfg, f!, x) @@ -28,7 +28,7 @@ function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = : DiffBase.value(jac_res) end - return OnceDifferentiable(f!, g!, fg!, similar(x), x) + return OnceDifferentiable(f!, g!, fg!, x, x) else error("The autodiff value $(autodiff) is not supported. Use :central or :forward.") end diff --git a/src/nlsolve/nlsolve.jl b/src/nlsolve/nlsolve.jl index de35785..43b8701 100644 --- a/src/nlsolve/nlsolve.jl +++ b/src/nlsolve/nlsolve.jl @@ -49,7 +49,7 @@ function nlsolve(f!, autoscale::Bool = true, m::Integer = 0, beta::Real = 1.0) where T - nlsolve(OnceDifferentiable(f!, j!, similar(initial_x), initial_x), + nlsolve(OnceDifferentiable(f!, j!, initial_x, initial_x), initial_x, method = method, xtol = xtol, ftol = ftol, iterations = iterations, store_trace = store_trace, show_trace = show_trace, extended_trace = extended_trace, diff --git a/src/solvers/anderson.jl b/src/solvers/anderson.jl index 4c471e2..34e2a8e 100644 --- a/src/solvers/anderson.jl +++ b/src/solvers/anderson.jl @@ -28,7 +28,7 @@ for n = 1:iterations # fixed-point iteration - value!(df, fx, xs[:,1]) + value!!(df, fx, xs[:,1]) gs[:,1] .= xs[:,1] .+ β.*fx diff --git a/src/solvers/mcp.jl b/src/solvers/mcp.jl index e476eb1..3c609c6 100644 --- a/src/solvers/mcp.jl +++ b/src/solvers/mcp.jl @@ -16,7 +16,7 @@ function mcp_smooth(df::OnceDifferentiable, lower::Vector, upper::Vector) function f!(F, x) - value!(df, F, x) + value!!(df, F, x) for i = 1:length(x) if isfinite.(upper[i]) F[i] += (x[i]-upper[i]) + sqrt(F[i]^2+(x[i]-upper[i])^2) @@ -29,7 +29,7 @@ function mcp_smooth(df::OnceDifferentiable, function j!(J, x) F = similar(x) - value_jacobian!(df, F, J, x) + value_jacobian!!(df, F, J, x) # Derivatives of phiplus sqplus = sqrt.(F.^2 .+ (x .- upper).^2) @@ -74,7 +74,7 @@ function mcp_smooth(df::OnceDifferentiable, J[i,i] += dminus_dv[i] + dminus_du[i]*dplus_dv[i] end end - return OnceDifferentiable(f!, j!, similar(df.F), similar(df.x_f)) + return OnceDifferentiable(f!, j!, similar(df.x_f), similar(df.F)) end # Generate a function whose roots are the solutions of the MCP. @@ -87,7 +87,7 @@ end function mcp_minmax(df::OnceDifferentiable, lower::Vector, upper::Vector) function f!(F, x) - value!(df, F, x) + value!!(df, F, x) for i = 1:length(x) if F[i] < x[i]-upper[i] F[i] = x[i]-upper[i] @@ -100,7 +100,7 @@ function mcp_minmax(df::OnceDifferentiable, function j!(J, x) F = similar(x) - value_jacobian!(df, F, J, x) + value_jacobian!!(df, F, J, x) for i = 1:length(x) if F[i] < x[i]-upper[i] || F[i] > x[i]-lower[i] for j = 1:length(x) @@ -109,5 +109,5 @@ function mcp_minmax(df::OnceDifferentiable, end end end - return OnceDifferentiable(f!, j!, similar(df.F), similar(df.x_f)) + return OnceDifferentiable(f!, j!, similar(df.x_f), similar(df.F)) end diff --git a/src/solvers/mcp_func_defs.jl b/src/solvers/mcp_func_defs.jl index 3137b77..02945bd 100644 --- a/src/solvers/mcp_func_defs.jl +++ b/src/solvers/mcp_func_defs.jl @@ -52,7 +52,7 @@ function mcpsolve{T}(f!, linesearch! = LineSearches.BackTracking(), factor::Real = one(T), autoscale::Bool = true) - @reformulate OnceDifferentiable(f!, j!, similar(initial_x), initial_x) + @reformulate OnceDifferentiable(f!, j!, initial_x, initial_x) nlsolve(rf, initial_x, method = method, xtol = xtol, ftol = ftol, iterations = iterations, store_trace = store_trace, diff --git a/src/solvers/newton.jl b/src/solvers/newton.jl index 764296b..fbc3eaa 100644 --- a/src/solvers/newton.jl +++ b/src/solvers/newton.jl @@ -85,7 +85,7 @@ function newton_{T}(df::OnceDifferentiable, go!(storage, xlin) vecdot(value(df), value(df)) / 2 end - dfo = OnceDifferentiable(fo, go!, fgo!, real(zero(T)), x) + dfo = OnceDifferentiable(fo, go!, fgo!, x, real(zero(T))) while !converged && it < iterations diff --git a/test/minpack.jl b/test/minpack.jl index 1846d14..d36490f 100644 --- a/test/minpack.jl +++ b/test/minpack.jl @@ -535,7 +535,7 @@ for (df, initial, name) in alltests r.f_calls, r.g_calls, r.residual_norm, tot_time) @test converged(r) # with autodiff - tot_time = @elapsed r_AD = nlsolve(df.f, initial, method = method, autodiff = true) + tot_time2 = @elapsed r_AD = nlsolve(df.f, initial, method = method, autodiff = true) @printf("%-45s %5d %5d %5d %14e %10e\n", name*"-"*string(method)*"-AD", length(initial), r_AD.f_calls, r_AD.g_calls, r_AD.residual_norm, tot_time) if PRINT_FILE diff --git a/test/sparse.jl b/test/sparse.jl index 8ea6824..472703b 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -16,7 +16,7 @@ function g_sparse!(J, x) J[2, 2] = u end -df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), spzeros(2, 2), rand(2)) +df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), rand(2), spzeros(2, 2)) # Test trust region r = nlsolve(df, [ -0.5; 1.4], method = :trust_region, autoscale = true) @@ -39,7 +39,7 @@ r = mcpsolve(df, [-Inf;-Inf], [Inf; Inf], [-0.5; 1.4], reformulation = :minmax) @test norm(r.zero - [ 0; 1]) < 1e-8 # Test given sparse -df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), spzeros(2, 2), rand(2)) +df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), rand(2), spzeros(2, 2)) r = nlsolve(df, [ -0.5; 1.4], method = :trust_region, autoscale = true) @test converged(r) @test norm(r.zero - [ 0; 1]) < 1e-8