Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc example with complex numbers not working #780

Closed
luigiannelli opened this issue Jan 28, 2020 · 4 comments
Closed

Doc example with complex numbers not working #780

luigiannelli opened this issue Jan 28, 2020 · 4 comments

Comments

@luigiannelli
Copy link

the example at page https://julianlsolvers.github.io/Optim.jl/stable/#algo/complex/ fails.
the environment is:

  • julia 1.1.1
  • IJulia v1.20.2
  • Optim v0.20.0

minimal (not) working example from the docs of the page linked above:

using Random, LinearAlgebra, Optim
Random.seed!(0) # Set the seed for reproducibility
# μ is the strength of the quartic. μ = 0 is just a quadratic problem
n = 4
A = randn(n,n) + im*randn(n,n)
A = A'A + I
b = randn(n) + im*randn(n)
μ = 1.0

fcomplex(x) = real(dot(x,A*x)/2 - dot(b,x)) + μ*sum(abs.(x).^4)
gcomplex(x) = A*x-b + 4μ*(abs.(x).^2).*x
gcomplex!(stor,x) = copyto!(stor,gcomplex(x))

x0 = randn(n)+im*randn(n)

res = optimize(fcomplex, gcomplex!, x0, LBFGS())

output:

MethodError: no method matching ini_dec(::Complex{Float64}, ::Int64, ::Array{UInt8,1})
Closest candidates are:
  ini_dec(::Any, ::Any, !Matched::Int64, !Matched::String, !Matched::Int64, !Matched::Int64, !Matched::Char, !Matched::Any) at printf.jl:842
  ini_dec(!Matched::BigInt, ::Int64, ::Any) at printf.jl:1053
  ini_dec(!Matched::Integer, ::Int64, ::Any) at printf.jl:1011
  ...

Stacktrace:
 [1] ini_dec(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Complex{Float64}, ::Int64, ::String, ::Int64, ::Int64, ::Char, ::Array{UInt8,1}) at ./printf.jl:842
 [2] (::getfield(Optim, Symbol("##2#4")))(::Complex{Float64}) at ./printf.jl:0
 [3] iterate at ./generator.jl:47 [inlined]
 [4] collect(::Base.Generator{Base.Iterators.Take{Array{Complex{Float64},1}},getfield(Optim, Symbol("##2#4"))}) at ./array.jl:606
 [5] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Optim.MultivariateOptimizationResults{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,Array{Complex{Float64},1},Float64,Float64,Array{OptimizationState{Float64,LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))}},1},Bool}) at /home/luigi/.julia/packages/Optim/Q2XsG/src/types.jl:242
 [6] show at ./sysimg.jl:219 [inlined]
 [7] limitstringmime(::MIME{Symbol("text/plain")}, ::Optim.MultivariateOptimizationResults{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,Array{Complex{Float64},1},Float64,Float64,Array{OptimizationState{Float64,LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))}},1},Bool}) at /home/luigi/.julia/packages/IJulia/F1GUo/src/inline.jl:43
 [8] display_mimestring(::MIME{Symbol("text/plain")}, ::Optim.MultivariateOptimizationResults{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,Array{Complex{Float64},1},Float64,Float64,Array{OptimizationState{Float64,LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))}},1},Bool}) at /home/luigi/.julia/packages/IJulia/F1GUo/src/display.jl:67
 [9] display_dict(::Optim.MultivariateOptimizationResults{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,Array{Complex{Float64},1},Float64,Float64,Array{OptimizationState{Float64,LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))}},1},Bool}) at /home/luigi/.julia/packages/IJulia/F1GUo/src/display.jl:96
 [10] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [11] invokelatest at ./essentials.jl:741 [inlined]
 [12] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/luigi/.julia/packages/IJulia/F1GUo/src/execute_request.jl:112
 [13] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [14] invokelatest at ./essentials.jl:741 [inlined]
 [15] eventloop(::ZMQ.Socket) at /home/luigi/.julia/packages/IJulia/F1GUo/src/eventloop.jl:8
 [16] (::getfield(IJulia, Symbol("##15#18")))() at ./task.jl:259
@luigiannelli luigiannelli changed the title Manual example with complex number not working Doc example with complex number not working Jan 28, 2020
@luigiannelli luigiannelli changed the title Doc example with complex number not working Doc example with complex numbers not working Jan 28, 2020
@antoine-levitt
Copy link
Contributor

@luigiannelli
Copy link
Author

It's not a problem for me, i just wrote the code for Real variables, but it's not good that a Doc example does not work.

@pkofod
Copy link
Member

pkofod commented Jan 30, 2020

This is point 1 of #775

@pkofod
Copy link
Member

pkofod commented Sep 6, 2020

Fixed on the latest release

julia> res = optimize(fcomplex, gcomplex!, x0, LBFGS())
 * Status: success

 * Candidate solution
    Final objective value:     -1.706873e+00

 * Found with
    Algorithm:     L-BFGS

 * Convergence measures
    |x - x'|               = 4.23e-09 ≰ 0.0e+00
    |x - x'|/|x'|          = 6.91e-09 ≰ 0.0e+00
    |f(x) - f(x')|         = 2.22e-16 ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 1.30e-16 ≰ 0.0e+00
    |g(x)|                 = 1.23e-10 ≤ 1.0e-08

 * Work counters
    Seconds run:   0  (vs limit Inf)
    Iterations:    15
    f(x) calls:    44
    ∇f(x) calls:   44

@pkofod pkofod closed this as completed Sep 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants