JuliaNLSolvers / NLsolve.jl

Julia solvers for systems of nonlinear equations and mixed complementarity problems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can no longer feed Jacobian not_in_place after most recent update

mfariacastro opened this issue · comments

Hi, I updated today to 0.12.0 and noticed that I stopped being able to use nlsolve with a not_in_place Jacobian. Everything was working fine before.

Here is a MWE

### Test Jacobian
using NLsolve

function f(x::Vector{Float64})
    
    eq = Array{Float64}(length(x))
    
    eq[1] = 5 .* x[1] - x[2].^2
    eq[2] = 4 .* x[2] - x[1]
    
    return eq
end

function J(x::Vector{Float64})
    
    jmat = Array{Float64}(length(x),length(x))
    
    jmat[1,1] = 5
    jmat[1,2] = -2.*x[2]
    jmat[2,1] = -1
    jmat[2,2] = 4
    
    return jmat
end

x0 = [3.0, 12.0]
solution = nlsolve(not_in_place(f), not_in_place(J), x0)

It throws an error

`MethodError: no method matching (::NLsolve.#f!#20{#J})(::Array{Float64,1}, ::Array{Float64,2})
Closest candidates are:
  f!(::AbstractArray{T,1} where T, ::AbstractArray{T,1} where T) at C:\Users\miguel\.julia\v0.6\NLsolve\src\utils.jl:42

Stacktrace:
 [1] trust_region_(::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}}}}, ::Array{Float64,1}, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Float64, ::Bool) at C:\Users\miguel\.julia\v0.6\NLsolve\src\trust_region.jl:106
 [2] #nlsolve#12(::Symbol, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Function, ::Float64, ::Bool, ::Int64, ::Float64, ::NLsolve.#nlsolve, ::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}}}}, ::Array{Float64,1}) at C:\Users\miguel\.julia\v0.6\NLsolve\src\nlsolve_func_defs.jl:26
 [3] (::NLsolve.#kw##nlsolve)(::Array{Any,1}, ::NLsolve.#nlsolve, ::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{NLsolve.#f!#20{#f},Array{Float64,1}},NLsolve.#gvec!#28{NLsolve.#f!#20{#J},Array{Float64,1}}}}, ::Array{Float64,1}) at .\<missing>:0
 [4] #nlsolve#13(::Symbol, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Function, ::Float64, ::Bool, ::Int64, ::Float64, ::NLsolve.#nlsolve, ::NLsolve.#f!#20{#f}, ::NLsolve.#f!#20{#J}, ::Array{Float64,1}) at C:\Users\miguel\.julia\v0.6\NLsolve\src\nlsolve_func_defs.jl:52
 [5] nlsolve(::Function, ::Function, ::Array{Float64,1}) at C:\Users\miguel\.julia\v0.6\NLsolve\src\nlsolve_func_defs.jl:52
 [6] include_string(::String, ::String) at .\loading.jl:515`

I am able to evaluate both the function f(x0) and the Jacobian J(x0) at the initial guess.

From the code here it looks like you might try nlsolve(not_in_place(f, J), x0) instead of nlsolve(not_in_place(f), not_in_place(J), x0).

Does that work on v0.12.0?

That does it @sglyon , thanks a lot! I did not realize the previous syntax had been deprecated.

OK great, glad it works.

To be honest I didn't know it was deprecated either!

This is from #117 and I didn't realize that PR introduced a breaking change, although I did actually look at the README changes. I'll see if we can patch a better deprecation or at least mention it at the top of the readme. Thanks for reporting this!

I'm reopening until we have a more satisfying deprecation situation, so people will find the answer easier.

Edit: out of curiosity - you don't have Optim installed do you?

@pkofod thanks, I have Optim 0.10.0 installed

Can I see the output of your Pkg.status?

julia> Pkg.status()
7 required packages:
 - Distributions                 0.14.2
 - GR                            0.24.0
 - Interpolations                0.7.0
 - JLD                           0.8.3
 - NLsolve                       0.12.0
 - Plots                         0.13.1
 - QuantEcon                     0.13.0
58 additional packages:
 - AbstractFFTs                  0.2.0
 - AxisAlgorithms                0.2.0
 - BinDeps                       0.6.0
 - Blosc                         0.3.0
 - Calculus                      0.2.2
 - CodecZlib                     0.3.1
 - ColorTypes                    0.6.4
 - Colors                        0.8.1
 - CommonSubexpressions          0.0.1
 - Compat                        0.33.0
 - Contour                       0.4.0
 - DSP                           0.3.4
 - DataStructures                0.7.2
 - DiffBase                      0.3.2
 - DiffResults                   0.0.1
 - DiffRules                     0.0.1
 - Distances                     0.5.0
 - DualNumbers                   0.3.0
 - FFTW                          0.0.4
 - FileIO                        0.5.2
 - FixedPointNumbers             0.4.2
 - ForwardDiff                   0.6.0
 - HDF5                          0.8.6
 - JSON                          0.15.1
 - LegacyStrings                 0.2.2
 - LightGraphs                   0.11.0
 - LineSearches                  3.0.1
 - MacroTools                    0.4.0
 - MathProgBase                  0.6.4
 - Measures                      0.1.0
 - NLSolversBase                 3.1.0
 - NLopt                         0.3.6
 - NaNMath                       0.2.6
 - Optim                         0.10.0
 - PDMats                        0.7.0
 - Parameters                    0.8.0
 - PlotThemes                    0.1.4
 - PlotUtils                     0.4.4
 - Polynomials                   0.1.6
 - PositiveFactorizations        0.1.0
 - Primes                        0.2.0
 - QuadGK                        0.1.3
 - Ratios                        0.2.0
 - RecipesBase                   0.2.3
 - Reexport                      0.0.3
 - Requires                      0.4.3
 - Rmath                         0.3.0
 - SHA                           0.5.1
 - ShowItLikeYouBuildIt          0.2.0
 - Showoff                       0.1.1
 - SimpleTraits                  0.5.1
 - SpecialFunctions              0.3.4
 - StaticArrays                  0.6.4
 - StatsBase                     0.19.0
 - StatsFuns                     0.5.0
 - TranscodingStreams            0.3.1
 - URIParser                     0.2.0
 - WoodburyMatrices              0.2.2

Fixed by #122 - should add a test :)

Should mention that I really appreciate the bug report. It was very easy to locate the problem using your MWE!

Thanks, I'm glad it was useful!