JuliaNLSolvers / NLsolve.jl

Julia solvers for systems of nonlinear equations and mixed complementarity problems
Other
330 stars 66 forks source link

Allow complex numbers #112

Closed ChrisRackauckas closed 5 years ago

ChrisRackauckas commented 7 years ago
using NLsolve

function f!(x, fvec)
    fvec[1] = (x[1]+3)*(x[2]^3-7)+18
    fvec[2] = sin(x[2]*exp(x[1])-1)
end

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

nlsolve(f!, g!, [ 0.1+im; 1.2])
MethodError: no method matching #nlsolve#12(::Symbol, ::Complex{Float64}, ::Complex{Float64}, ::Int64, ::Bool, ::Bool, ::Bool, ::NLsolve.#no_linesearch!, ::Complex{Float64}, ::Bool, ::Int64, ::Float64, ::NLsolve.#nlsolve, ::#f!, ::#g!, ::Array{Complex{Float64},1})
Closest candidates are:
  #nlsolve#12(::Symbol, !Matched::Real, !Matched::Real, ::Integer, ::Bool, ::Bool, ::Bool, ::Function, !Matched::Real, ::Bool, ::Integer, ::Real, ::Any, ::Function, ::Function, ::Array{T,1}) where T at C:\Users\Chris\.julia\v0.6\NLsolve\src\nlsolve_func_defs.jl:52
nlsolve(::Function, ::Function, ::Array{Complex{Float64},1}) at nlsolve_func_defs.jl:52
include_string(::String, ::String) at loading.jl:515
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##49#52{String,Int64,String})() at eval.jl:50
withpath(::Atom.##49#52{String,Int64,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
macro expansion at eval.jl:49 [inlined]
(::Atom.##48#51{Dict{String,Any}})() at task.jl:80

It looks like there are assumptions baked into there that it needs to be real numbers

https://github.com/JuliaNLSolvers/NLsolve.jl/blob/master/src/nlsolve_func_defs.jl#L5

Could those be removed?

sglyon commented 7 years ago

There's one way to find out ... have you tried it?

Although I'm a maintainer of this package, I didn't write all the code myself so I can't say with certainty if any parts of the code rely on the assumption of real numbers. I think some trial an error will be the quickest way to find out for sure.

mcawte commented 7 years ago

I will try it, as I am the one that raised the issue with Chris first. But it will take me some time as I am still new.

sglyon commented 7 years ago

OK great!

If you need any tips or get stuck reach out here or on the JuliaNLSolvers gitter

pkofod commented 7 years ago

Could those be removed?

Yes, I don't believe they serve any purpose. @mcawte if you get to it before me, it'll be merged sooner :)

mcawte commented 7 years ago

Hi, sorry for the long delay. I got stuck the last two times. I have had a talk with someone more knowledgeable than me about this kind of thing and I will give it another try now.

mcawte commented 7 years ago

I have made some progress, but in the process I have also totally destroyed type stability and caused some other failures. I will continue to work on it today.

julia> using NLsolve

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

julia> function g!(x, fjac)
           fjac[1, 1] = x[2]^3-7
           fjac[1, 2] = 3*x[2]^2*(x[1]+3)
           u = exp(x[1])*cos(x[2]*exp(x[1])-1)
           fjac[2, 1] = x[2]*u
           fjac[2, 2] = u
       end
g! (generic function with 1 method)

julia> nlsolve(f!, g!, [ 0.1+im; 1.2])
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: Complex{Float64}[0.1+1.0im, 1.2+0.0im]
 * Zero: Complex{Float64}[1.34322e-13-2.06963e-14im, 1.0-3.14007e-14im]
 * Inf-norm of residuals: 0.000000
 * Iterations: 6
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 7
 * Jacobian Calls (df/dx): 7

julia> Pkg.test("NLsolve")
INFO: Computing test dependencies for NLsolve...
INFO: No packages to install, update or remove
INFO: Testing NLsolve
Running tests:
Test Summary: | Pass  Total
2by2          |   21     21
Test Summary:     | Pass  Total
finite_difference |    1      1
Function                                          Dim    NFEV    NJEV   Final inf-norm     total time
--------------------------------------------------------------------------------------
Rosenbrock-trust_region                             2       4       4     2.220446e-15   7.221850e-01
Rosenbrock-trust_region-AD                          2       4       4     2.220446e-15   1.146676e+00
Rosenbrock-newton                                   2       6       5     8.881784e-15   9.136216e-02
Rosenbrock-newton-AD                                2       6       5     8.881784e-15   8.941346e-02
Powell singular-trust_region                        4      17      17     2.945101e-09   7.171153e-01
Powell singular-trust_region-AD                     4      17      17     2.945101e-09   1.016350e+00
Powell singular-newton                              4      20      19     2.945101e-09   8.158862e-02
Powell singular-newton-AD                           4      20      19     2.945101e-09   8.895896e-02
Powell badly scaled-trust_region                    2      14      14     4.540812e-14   7.221543e-01
Powell badly scaled-trust_region-AD                 2      14      14     4.540812e-14   8.563378e-01
Powell badly scaled-newton                          2      16      15     1.573341e-11   8.710659e-02
Powell badly scaled-newton-AD                       2      16      15     1.573341e-11   9.109193e-02
Wood-trust_region                                   4      13      13     2.385539e-09   7.291459e-01
Wood-trust_region-AD                                4      13      13     2.385539e-09   8.698214e-01
Wood-newton                                         4      18      17     6.972201e-14   8.867458e-02
Wood-newton-AD                                      4      18      17     4.884981e-14   9.001350e-02
Helical Valley-trust_region                         3      10      10     4.501998e-10   7.354898e-01
Helical Valley-trust_region-AD                      3      10      10     4.501998e-10   1.005046e+00
Helical Valley-newton                               3      14      13     1.145685e-14   8.948618e-02
Helical Valley-newton-AD                            3      14      13     1.145685e-14   8.964651e-02
Watson-trust_region                                 6      15      15     5.658483e-10   7.832036e-01
Watson-trust_region-AD                              6      15      15     5.658641e-10   1.164363e+00
Watson-newton                                       6      16      15     3.157266e-13   8.559092e-02
Watson-newton-AD                                    6      16      15     3.107306e-13   9.108928e-02
Watson-trust_region                                 9      17      17     1.615683e-12   1.686710e-04
Watson-trust_region-AD                              9      17      17     1.619032e-12   1.199813e+00
Watson-newton                                       9      17      16     5.399161e-15   1.618470e-04
Watson-newton-AD                                    9      17      16     1.418956e-14   1.019139e-01
Chebyquad-trust_region                              5       6       6     4.943032e-12   7.434617e-01
Chebyquad-trust_region-AD                           5       6       6     4.943032e-12   9.918313e-01
Chebyquad-trust_region                              6    1001      24    4.079941e+236   1.243661e-03
minpack: Test Failed
  Expression: converged(r)
Stacktrace:
 [1] macro expansion at /Users/mcawte/.julia/v0.6/NLsolve/test/minpack.jl:536 [inlined]
 [2] macro expansion at ./test.jl:860 [inlined]
 [3] anonymous at ./<missing>:?
Chebyquad-trust_region-AD                           6    1001      24    4.079941e+236   8.280616e-01
minpack: Test Failed
  Expression: converged(r_AD)
Stacktrace:
 [1] macro expansion at /Users/mcawte/.julia/v0.6/NLsolve/test/minpack.jl:547 [inlined]
 [2] macro expansion at ./test.jl:860 [inlined]
 [3] anonymous at ./<missing>:?
Chebyquad-trust_region                              7      10      10     6.957282e-14   4.907100e-05
Chebyquad-trust_region-AD                           7      10      10     6.957282e-14   9.949086e-01
Chebyquad-trust_region                              9    1001       8     2.004787e+93   1.797914e-03
minpack: Test Failed
  Expression: converged(r)
Stacktrace:
 [1] macro expansion at /Users/mcawte/.julia/v0.6/NLsolve/test/minpack.jl:536 [inlined]
 [2] macro expansion at ./test.jl:860 [inlined]
 [3] anonymous at ./<missing>:?
Chebyquad-trust_region-AD                           9    1001       8     2.004787e+93   8.374872e-01
minpack: Test Failed
  Expression: converged(r_AD)
Stacktrace:
 [1] macro expansion at /Users/mcawte/.julia/v0.6/NLsolve/test/minpack.jl:547 [inlined]
 [2] macro expansion at ./test.jl:860 [inlined]
 [3] anonymous at ./<missing>:?
Brown almost-linear-trust_region                   10       5       5     3.382330e-09   8.043306e-01
Brown almost-linear-trust_region-AD                10       5       5     3.382330e-09   1.143856e+00
Brown almost-linear-trust_region                   30       4       4     8.883264e-09   4.851379e-03
Brown almost-linear-trust_region-AD                30       4       4     8.883264e-09   3.299710e-04
Brown almost-linear-trust_region                   40       4       4     1.570837e-09   3.996820e-04
Brown almost-linear-trust_region-AD                40       4       4     1.570837e-09   4.536290e-04
Discrete boundary value-trust_region               10       4       4     2.636780e-16   7.480826e-01
Discrete boundary value-trust_region-AD            10       4       4     2.636780e-16   8.941065e-01
Discrete boundary value-newton                     10       7       6     2.636780e-16   8.575129e-02
Discrete boundary value-newton-AD                  10       7       6     2.636780e-16   9.097427e-02
Discrete integral equation-trust_region             1       4       4     8.537615e-14   7.522495e-01
Discrete integral equation-trust_region-AD          1       4       4     8.537615e-14   9.953919e-01
Discrete integral equation-newton                   1       7       6     8.537615e-14   8.387582e-02
Discrete integral equation-newton-AD                1       7       6     8.537615e-14   8.847913e-02
Discrete integral equation-trust_region            10       4       4     2.747802e-15   7.315500e-05
Discrete integral equation-trust_region-AD         10       4       4     2.747802e-15   9.142398e-01
Discrete integral equation-newton                  10       7       6     2.747802e-15   8.677200e-05
Discrete integral equation-newton-AD               10       7       6     2.747802e-15   8.880551e-02
Trigonometric-trust_region                         10       8       8     2.721379e-12   7.483983e-01
Trigonometric-trust_region-AD                      10       8       8     2.719602e-12   8.438471e-01
Variably dimensioned-trust_region                  10      15      15     1.323497e-12   7.575408e-01
Variably dimensioned-trust_region-AD               10      15      15     1.323497e-12   8.964982e-01
Variably dimensioned-newton                        10      18      17     1.323497e-12   8.400945e-02
Variably dimensioned-newton-AD                     10      18      17     1.323497e-12   9.052024e-02
Broyden tridiagonal-trust_region                   10       5       5     7.548402e-10   7.327109e-01
Broyden tridiagonal-trust_region-AD                10       5       5     7.548402e-10   8.555842e-01
Broyden tridiagonal-newton                         10       8       7     7.548402e-10   8.413761e-02
Broyden tridiagonal-newton-AD                      10       8       7     7.548402e-10   9.031228e-02
Broyden banded-trust_region                        10       6       6     9.359466e-09   7.815542e-01
Broyden banded-trust_region-AD                     10       6       6     9.359466e-09   8.708300e-01
Broyden banded-newton                              10       9       8     9.359466e-09   8.797210e-02
Broyden banded-newton-AD                           10       9       8     9.359466e-09   8.847328e-02
Test Summary: | Pass  Fail  Total
minpack       |   64     4     68
ERROR: LoadError: LoadError: Some tests did not pass: 64 passed, 4 failed, 0 errored, 0 broken.
while loading /Users/mcawte/.julia/v0.6/NLsolve/test/minpack.jl, in expression starting on line 16
while loading /Users/mcawte/.julia/v0.6/NLsolve/test/runtests.jl, in expression starting on line 34
=====================================================================[ ERROR: NLsolve ]======================================================================

failed process: Process(`/Applications/Julia-0.6.app/Contents/Resources/julia/bin/julia -Ccore2 -J/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib --compile=yes --depwarn=yes --check-bounds=yes --code-coverage=none --color=yes --compilecache=yes /Users/mcawte/.julia/v0.6/NLsolve/test/runtests.jl`, ProcessExited(1)) [1]

=============================================================================================================================================================
INFO: No packages to install, update or remove
ERROR: NLsolve had test errors
ChrisRackauckas commented 6 years ago

This will help this issue as well: https://github.com/JuliaNLSolvers/NLsolve.jl/issues/127