Skip to content

Commit

Permalink
Use copy over similar to fix BigFloat support (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkofod authored Aug 14, 2020
1 parent 4a49424 commit d036d44
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/nlsolve/fixedpoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ function fixedpoint(f,
f(out, x);
out .-= x;
end
dg = OnceDifferentiable(g!, initial_x, similar(initial_x), autodiff)
dg = OnceDifferentiable(g!, initial_x, copy(initial_x), autodiff)
else
g(x) = f(x) - x;
dg = OnceDifferentiable(g, initial_x, similar(initial_x); autodiff = autodiff, inplace = inplace)
dg = OnceDifferentiable(g, initial_x, copy(initial_x); autodiff = autodiff, inplace = inplace)
end

return nlsolve(dg,
Expand Down
12 changes: 6 additions & 6 deletions src/nlsolve/nlsolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function nlsolve(f,
inplace = !applicable(f, initial_x),
kwargs...)
if method in (:anderson, :broyden)
df = NonDifferentiable(f, initial_x, similar(initial_x); inplace=inplace)
df = NonDifferentiable(f, initial_x, copy(initial_x); inplace=inplace)
else
df = OnceDifferentiable(f, initial_x, similar(initial_x); autodiff=autodiff, inplace=inplace)
df = OnceDifferentiable(f, initial_x, copy(initial_x); autodiff=autodiff, inplace=inplace)
end

nlsolve(df, initial_x; method = method, kwargs...)
Expand All @@ -59,9 +59,9 @@ function nlsolve(f,
inplace = !applicable(f, initial_x),
kwargs...)
if inplace
df = OnceDifferentiable(f, j, initial_x, similar(initial_x))
df = OnceDifferentiable(f, j, initial_x, copy(initial_x))
else
df = OnceDifferentiable(not_in_place(f, j)..., initial_x, similar(initial_x))
df = OnceDifferentiable(not_in_place(f, j)..., initial_x, copy(initial_x))
end

nlsolve(df, initial_x; kwargs...)
Expand All @@ -74,9 +74,9 @@ function nlsolve(f,
inplace = !applicable(f, initial_x),
kwargs...)
if inplace
df = OnceDifferentiable(f, j, fj, initial_x, similar(initial_x))
df = OnceDifferentiable(f, j, fj, initial_x, copy(initial_x))
else
df = OnceDifferentiable(not_in_place(f, j, fj)..., initial_x, similar(initial_x))
df = OnceDifferentiable(not_in_place(f, j, fj)..., initial_x, copy(initial_x))
end

nlsolve(df, initial_x; kwargs...)
Expand Down
10 changes: 5 additions & 5 deletions src/solvers/anderson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ struct AndersonCache{Tx,To,Tdg,Tg,TQ,TR} <: AbstractSolverCache
end

function AndersonCache(df, m)
x = similar(df.x_f)
g = similar(x)
x = copy(df.x_f)
g = copy(x)

if m > 0
fxold = similar(x)
gold = similar(x)
fxold = copy(x)
gold = copy(x)

# maximum size of history
mmax = min(length(x), m)

# buffer storing the differences between g of the iterates, from oldest to newest
Δgs = [similar(x) for _ in 1:mmax]
Δgs = [copy(x) for _ in 1:mmax]

T = eltype(x)
γs = Vector{T}(undef, mmax) # coefficients obtained from the least-squares problem
Expand Down
8 changes: 4 additions & 4 deletions src/solvers/broyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ function broyden_(df::Union{NonDifferentiable, OnceDifferentiable},
value!!(df, x)

vecvalue = vec(value(df))
fold, xold = similar(vecvalue), similar(x)
fold, xold = copy(vecvalue), copy(x)
fold .= T(0)
xold .= T(0)

p = similar(x)
g = similar(x)
p = copy(x)
g = copy(x)
Jinv = Matrix{T}(I, n, n)
check_isfinite(value(df))
it = 0
Expand Down Expand Up @@ -108,7 +108,7 @@ function broyden_(df::Union{NonDifferentiable, OnceDifferentiable},
@broydentrace sqeuclidean(x, xold)
end
return SolverResults("broyden without line-search",
initial_x, copyto!(similar(initial_x), x), norm(value(df), Inf),
initial_x, copyto!(copy(initial_x), x), norm(value(df), Inf),
it, x_converged, xtol, f_converged, ftol, tr,
first(df.f_calls), 0)
end
Expand Down
12 changes: 6 additions & 6 deletions src/solvers/mcp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ function mcp_smooth(df::OnceDifferentiable,
end

function j!(J, x)
F = similar(x)
F = copy(x)
value_jacobian!!(df, F, J, x)

# Derivatives of phiplus
sqplus = sqrt.(F.^2 .+ (x .- upper).^2)

dplus_du = 1 .+ F./sqplus

dplus_dv = similar(x)
dplus_dv = copy(x)
for i = 1:length(x)
if isfinite.(upper[i])
dplus_dv[i] = 1 + (x[i]-upper[i])/sqplus[i]
Expand All @@ -57,7 +57,7 @@ function mcp_smooth(df::OnceDifferentiable,

dminus_du = 1 .- phiplus ./ sqminus

dminus_dv = similar(x)
dminus_dv = copy(x)
for i = 1:length(x)
if isfinite.(lower[i])
dminus_dv[i] = 1 - (x[i]-lower[i])/sqminus[i]
Expand All @@ -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.x_f), similar(df.F))
return OnceDifferentiable(f!, j!, copy(df.x_f), copy(df.F))
end

# Generate a function whose roots are the solutions of the MCP.
Expand All @@ -99,7 +99,7 @@ function mcp_minmax(df::OnceDifferentiable,
end

function j!(J, x)
F = similar(x)
F = copy(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]
Expand All @@ -109,5 +109,5 @@ function mcp_minmax(df::OnceDifferentiable,
end
end
end
return OnceDifferentiable(f!, j!, similar(df.x_f), similar(df.F))
return OnceDifferentiable(f!, j!, copy(df.x_f), copy(df.F))
end
8 changes: 4 additions & 4 deletions src/solvers/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ struct NewtonCache{Tx} <: AbstractSolverCache
g::Tx
end
function NewtonCache(df)
x = similar(df.x_f)
xold = similar(x)
p = similar(x)
g = similar(x)
x = copy(df.x_f)
xold = copy(x)
p = copy(x)
g = copy(x)
NewtonCache(x, xold, p, g)
end

Expand Down
22 changes: 11 additions & 11 deletions src/solvers/trust_region.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ struct NewtonTrustRegionCache{Tx} <: AbstractSolverCache
d::Tx
end
function NewtonTrustRegionCache(df)
x = similar(df.x_f) # Current point
xold = similar(x) # Old point
r = similar(df.F) # Current residual
r_predict = similar(x) # predicted residual
p = similar(x) # Step
p_c = similar(x) # Cauchy point
pi = similar(x)
d = similar(x) # Scaling vector
x = copy(df.x_f) # Current point
xold = copy(x) # Old point
r = copy(df.F) # Current residual
r_predict = copy(x) # predicted residual
p = copy(x) # Step
p_c = copy(x) # Cauchy point
pi = copy(x)
d = copy(x) # Scaling vector
NewtonTrustRegionCache(x, xold, r, r_predict, p, p_c, pi, d)
end
macro trustregiontrace(stepnorm)
Expand All @@ -44,9 +44,9 @@ macro trustregiontrace(stepnorm)
end)
end

function dogleg!(p::AbstractArray{T}, p_c::AbstractArray{T}, p_i,
r::AbstractArray{T}, d::AbstractArray{T}, J::AbstractMatrix{T},
delta::Real) where T
function dogleg!(p, p_c, p_i,
r, d, J, delta::Real)
T = eltype(d)
try
copyto!(p_i, J \ vec(r)) # Gauss-Newton step
catch e
Expand Down
16 changes: 16 additions & 0 deletions test/iface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,20 @@ r = nlsolve(n_ary(f), [ -0.5; 1.4], autodiff = :forward)

r = nlsolve(df, [ 0.01; .99], method = :anderson, m = 10, beta=.01, show_trace=true)
end
@testset "#247" begin
function f!(F, x)
F[1] = (x[1]+3)*(x[2]^3-7)+18
F[2] = sin(x[2]*exp(x[1])-1)
end

function j!(J, x)
J[1, 1] = x[2]^3-7
J[1, 2] = 3*x[2]^2*(x[1]+3)
u = exp(x[1])*cos(x[2]*exp(x[1])-1)
J[2, 1] = x[2]*u
J[2, 2] = u
end

nlsolve(f!, j!, [ BigFloat(0.1);BigFloat( 1.2)])
end
end

0 comments on commit d036d44

Please sign in to comment.