JuliaNLSolvers / NLsolve.jl

Julia solvers for systems of nonlinear equations and mixed complementarity problems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Simple Example" fails

Sleort opened this issue · comments

NLsolve used to work fine, but now (after an update?)... I'm not quite sure what's going on here:

troels@troels-700Z7C:~$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.2 (2017-12-13 18:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> using NLsolve

julia> function f!(F, x)
           F[1] = (x[1]+3)*(x[2]^3-7)+18
           F[2] = sin(x[2]*exp(x[1])-1)
       end
f! (generic function with 1 method)

julia> 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
j! (generic function with 1 method)

julia> nlsolve(f!, j!, [ 0.1; 1.2])
ERROR: BoundsError: attempt to access 2-element Array{Float64,1} at index [1, 2]
Stacktrace:
 [1] j!(::Array{Float64,1}, ::Array{Float64,2}) at ./REPL[3]:3
 [2] (::NLsolve.#fg!#1{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}}})(::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,2}) at /home/troels/.julia/v0.6/NLsolve/src/differentiable_functions.jl:26
 [3] trust_region_(::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}}}}, ::Array{Float64,1}, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Float64, ::Bool) at /home/troels/.julia/v0.6/NLsolve/src/trust_region.jl:106
 [4] #nlsolve#12(::Symbol, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Function, ::Float64, ::Bool, ::Int64, ::Float64, ::NLsolve.#nlsolve, ::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}}}}, ::Array{Float64,1}) at /home/troels/.julia/v0.6/NLsolve/src/nlsolve_func_defs.jl:26
 [5] (::NLsolve.#kw##nlsolve)(::Array{Any,1}, ::NLsolve.#nlsolve, ::NLsolve.DifferentiableMultivariateFunction{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}},NLsolve.#fg!#1{NLsolve.#fvec!#27{#f!,Array{Float64,1}},NLsolve.#gvec!#28{#j!,Array{Float64,1}}}}, ::Array{Float64,1}) at ./<missing>:0
 [6] #nlsolve#13(::Symbol, ::Float64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::Function, ::Float64, ::Bool, ::Int64, ::Float64, ::NLsolve.#nlsolve, ::#f!, ::#j!, ::Array{Float64,1}) at /home/troels/.julia/v0.6/NLsolve/src/nlsolve_func_defs.jl:52
 [7] nlsolve(::Function, ::Function, ::Array{Float64,1}) at /home/troels/.julia/v0.6/NLsolve/src/nlsolve_func_defs.jl:52

furthermore, if I try the finite differencing solver,

julia> nlsolve(f!, [ 0.1; 1.2])
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [0.1, 1.2]
 * Zero: [-3.0, -0.841471]
 * Inf-norm of residuals: 0.000000
 * Iterations: 0
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 1
 * Jacobian Calls (df/dx): 1

The algorithm clearly doesn't converge to a zero!

Finally, the automatic differentiation version also fails:

julia> nlsolve(f!, [ 0.1; 1.2], autodiff= :forward)
ERROR: TypeError: non-boolean (Symbol) used in boolean context
Stacktrace:
 [1] (::NLsolve.#kw##nlsolve)(::Array{Any,1}, ::NLsolve.#nlsolve, ::Function, ::Array{Float64,1}) at ./<missing>:0

I believe my (relevant) packages should be up to date, but just in case, here's the result of Pkg.status():

julia> Pkg.status()
45 required packages:
 - ArgCheck                      0.4.2
 - Atom                          0.6.6+             master
 - BenchmarkTools                0.2.3
 - Bootstrap                     1.1.0
 - CatViews                      0.1.0+             master
 - DifferentialEquations         3.1.0
 - DoubleDouble                  0.2.1
 - DynamicalBilliards            1.6.3+             master
 - Flux                          0.4.0
 - ForwardDiff                   0.7.0
 - GLMNet                        0.3.0
 - GLVisualize                   0.6.1+             master
 - GR                            0.25.0
 - Gtk                           0.13.1
 - ImageMagick                   0.5.0
 - Images                        0.12.0
 - IterativeSolvers              0.4.2
 - JLD                           0.8.3
 - JLD2                          0.0.5
 - KernelDensity                 0.4.0
 - LightGraphs                   0.11.1
 - LsqFit                        0.3.0
 - MCMCDiagnostics               0.1.0
 - MNIST                         0.0.2
 - Memoize                       0.2.0
 - NLsolve                       0.13.0
 - NearestNeighbors              0.3.0
 - NumericIO                     0.2.0
 - PackageCompiler               0.2.0
 - ParallelDataTransfer          0.3.0+             master
 - PkgDev                        0.1.6
 - PlotRecipes                   0.3.0
 - PlotlyJS                      0.7.1              d7eae25e (dirty)
 - Plots                         0.15.0
 - Polynomials                   0.2.0
 - ProfileView                   0.3.0
 - ProgressMeter                 0.5.3
 - PyPlot                        2.3.2
 - ReverseDiff                   0.2.0
 - Revise                        0.1.1
 - Roots                         0.5.0
 - Rsvg                          0.1.0
 - StatPlots                     0.6.0
 - StaticArrays                  0.6.6
 - UnicodePlots                  0.2.6
209 additional packages:
 - ASTInterpreter2               0.1.1
 - AbstractFFTs                  0.2.0
 - AbstractNumbers               0.1.1
 - AlgebraicDiffEq               0.1.0
 - AxisAlgorithms                0.2.0
 - AxisArrays                    0.2.0
 - BandedMatrices                0.3.1
 - BinDeps                       0.8.5
 - BinaryProvider                0.1.3
 - Blink                         0.6.0
 - Blosc                         0.3.0
 - BoundaryValueDiffEq           0.3.0
 - BufferedStreams               0.3.3
 - Cairo                         0.4.0
 - Calculus                      0.2.2
 - CatIndices                    0.1.0
 - ChunkedArrays                 0.1.1
 - CodeTools                     0.4.7
 - CodecZlib                     0.4.2
 - Codecs                        0.4.0
 - ColorBrewer                   0.3.1
 - ColorTypes                    0.6.6
 - ColorVectorSpace              0.5.2
 - Colors                        0.8.2
 - Combinatorics                 0.5.0
 - CommonSubexpressions          0.0.1
 - Compat                        0.46.0
 - ComputationalResources        0.2.0
 - Conda                         0.7.1              master
 - Contour                       0.4.0
 - CoordinateTransformations     0.4.1
 - CustomUnitRanges              0.1.0
 - DataArrays                    0.6.2
 - DataFlow                      0.3.0
 - DataFrames                    0.10.1
 - DataStructures                0.7.4
 - DataValues                    0.3.1
 - DebuggerFramework             0.1.2
 - DelayDiffEq                   1.11.0
 - DiffBase                      0.3.2
 - DiffEqBase                    2.10.1
 - DiffEqBiological              0.3.0
 - DiffEqCallbacks               0.8.0
 - DiffEqDevTools                0.11.3
 - DiffEqDiffTools               0.2.0
 - DiffEqFinancial               0.3.0
 - DiffEqJump                    0.7.0
 - DiffEqMonteCarlo              0.9.1
 - DiffEqNoiseProcess            0.8.0
 - DiffEqPDEBase                 0.3.5
 - DiffEqParamEstim              0.7.2
 - DiffEqPhysics                 0.3.0
 - DiffEqSensitivity             0.3.0
 - DiffEqUncertainty             0.0.1
 - DiffResults                   0.0.3
 - DiffRules                     0.0.2
 - DimensionalPlotRecipes        0.0.2
 - Distances                     0.5.0
 - DistributedArrays             0.4.0
 - Distributions                 0.15.0+            master
 - DocSeeker                     0.1.0
 - DocStringExtensions           0.4.1
 - DualNumbers                   0.3.0
 - EllipsisNotation              0.3.0
 - FFTViews                      0.1.0
 - FFTW                          0.0.4
 - FactCheck                     0.4.3
 - FerrenbergSwendsenReweighting 0.0.0-             update_to_new_NLsolve_API (unregistered, dirty)
 - FileIO                        0.6.1
 - FiniteElementDiffEq           0.4.0
 - FixedPointNumbers             0.4.3
 - Fontconfig                    0.1.1
 - FreeType                      1.3.0
 - FreeTypeAbstraction           0.1.0
 - FunctionWrappers              0.1.0
 - GLAbstraction                 0.6.1
 - GLFW                          1.5.0
 - GLWindow                      0.7.1
 - GZip                          0.3.0
 - GenericSVD                    0.1.0
 - GeometryTypes                 0.4.4
 - Graphics                      0.2.0
 - GtkReactive                   0.4.0
 - HDF5                          0.8.8
 - Hiccup                        0.1.1
 - HttpCommon                    0.3.0
 - HttpParser                    0.3.0
 - HttpServer                    0.2.0
 - IdentityRanges                0.1.0
 - ImageAxes                     0.4.0
 - ImageCore                     0.5.0
 - ImageFiltering                0.2.3
 - ImageMetadata                 0.4.0
 - ImageMorphology               0.0.2
 - ImageTransformations          0.4.1
 - IndirectArrays                0.3.0
 - Interact                      0.6.3
 - Interpolations                0.7.3
 - IntervalSets                  0.1.1
 - IterTools                     0.2.0
 - IterableTables                0.6.1
 - IteratorInterfaceExtensions   0.0.2
 - Iterators                     0.3.1
 - JSON                          0.16.4
 - Juno                          0.3.2
 - LNR                           0.0.2
 - LaTeXStrings                  0.3.0
 - Lazy                          0.12.0
 - Learn                         0.0.0-             master (unregistered)
 - LearnBase                     0.1.6
 - LearningStrategies            0.2.0
 - LegacyStrings                 0.3.0
 - Libz                          0.2.4
 - LineSearches                  3.2.3
 - LinearMaps                    1.0.4
 - Loess                         0.3.0
 - LossFunctions                 0.2.0
 - MLDataPattern                 0.3.0
 - MLDataUtils                   0.2.0
 - MLLabelUtils                  0.2.0
 - MLMetrics                     0.0.0-             master (unregistered)
 - MacroTools                    0.4.0
 - Makie                         0.0.0-             master (unregistered)
 - MappedArrays                  0.0.7
 - MbedTLS                       0.5.4
 - Measures                      0.1.0
 - Media                         0.3.0
 - MeshIO                        0.1.1
 - Missings                      0.2.4
 - ModernGL                      0.2.1
 - MuladdMacro                   0.0.2
 - MultiScaleArrays              0.5.0
 - Mustache                      0.3.0
 - Mux                           0.2.3
 - NLSolversBase                 3.1.0
 - NNlib                         0.2.1
 - NaNMath                       0.3.0
 - NamedTuples                   4.0.0
 - NetworkLayout                 0.1.1
 - Nullables                     0.0.3
 - ObjectiveFunctions            0.0.0-             master (unregistered)
 - OffsetArrays                  0.4.2
 - OnlineStats                   0.15.0
 - OnlineStatsBase               0.4.0
 - Optim                         0.11.0
 - OptimBase                     0.1.0
 - OrdinaryDiffEq                2.35.1
 - PDMats                        0.8.0
 - Packing                       0.1.0
 - PaddedViews                   0.2.0
 - ParameterizedFunctions        2.3.1
 - Parameters                    0.8.1
 - PenaltyFunctions              0.0.2              master
 - PlotThemes                    0.2.0
 - PlotUtils                     0.4.4
 - PositiveFactorizations        0.1.0
 - Primes                        0.2.0
 - PyCall                        1.15.0
 - QuadGK                        0.2.0
 - Quaternions                   0.3.0
 - RandomNumbers                 0.1.1
 - RangeArrays                   0.2.0
 - Ratios                        0.2.0
 - Reactive                      0.6.0
 - RecipesBase                   0.2.3
 - RecursiveArrayTools           0.14.3
 - Reexport                      0.1.0
 - Requires                      0.4.3
 - ResettableStacks              0.3.0
 - Rmath                         0.3.1
 - Rotations                     0.6.1
 - RoundingIntegers              0.0.3
 - SHA                           0.5.3
 - SIUnits                       0.1.0
 - ShowItLikeYouBuildIt          0.2.0
 - Showoff                       0.1.1
 - SignedDistanceFields          0.2.0
 - SimpleTraits                  0.5.1
 - SnoopCompile                  0.2.0
 - SortingAlgorithms             0.2.0
 - SpecialFunctions              0.3.8
 - StatsBase                     0.19.5
 - StatsFuns                     0.5.0
 - SteadyStateDiffEq             0.1.0
 - StochasticDiffEq              2.23.2
 - StochasticOptimization        0.0.0-             master (unregistered)
 - StringDistances               0.2.0
 - SugarBLAS                     0.0.4
 - Sundials                      1.0.0
 - SweepOperator                 0.1.0
 - SymEngine                     0.3.0
 - TableTraits                   0.1.0
 - TableTraitsUtils              0.1.3
 - TexExtensions                 0.1.0
 - TiledIteration                0.1.0
 - Tokenize                      0.4.2
 - TranscodingStreams            0.4.1
 - Transformations               0.0.0-             master (unregistered)
 - URIParser                     0.3.0
 - UnicodeFun                    0.1.0
 - VectorizedRoutines            0.0.2
 - WangLandau                    0.0.0-             master (unregistered, dirty)
 - WebSockets                    0.4.0
 - WoodburyMatrices              0.2.2

Any ideas?

I think that you used the new syntax, but the latest tagged version doesn't have those changes yet (v0.13.0 is from November). Did you try on master? (Pkg.checkout("NLsolve"))
For v0.13.0 the arguments of the function are reversed and for autodiff you have to use true. (See 150f861)

Ah, I see. So the documentation is just a bit ahead of schedule (which is not optimal / the user should be warned about...?)

Anyway, Pkg.checkout("NLsolve") did the trick, and now everything works again (with the new syntax). Thanks!

Ah, I see. So the documentation is just a bit ahead of schedule (which is not optimal / the user should be warned about...?)

Anyway, Pkg.checkout("NLsolve") did the trick, and now everything works again (with the new syntax). Thanks!

Yes, this is unfortunate, but Github works that way. We cannot provide for example the readme for the latest tag, it will always show the README.md for the current version. I should maybe put a notice and a link to the old README. Thanks for bringing this to my attention.

I hope the current README removes some of the confusion.

It sure does. On behalf of some other could-have-been-confused-but-now-they'll-never-know users: Thank you! 😄