# Optim: MethodError: no method matching zero

**URL:** <https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139>\
**Category:** Optimization (Mathematical)\
**Created:** [August 18, 2020, 2:57am UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139 "2020-08-18T02:57:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 18, 2020, 2:57am UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139/1 "2020-08-18T02:57:12Z")

</div>

When I do optimization calculations with the Optim package, an error occurred. The type of A1,A2,A3,A4 is ::Array{Complex{Float64},2}. I don’t know how to solve it.  
Is there a way to make optimize() support this type?Or what data type can replace this type and serve as optimize()?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b37e66552b31ee82bc0538fdde8f1d43c9cd0f6.png)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 18, 2020, 2:08pm UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139/2 "2020-08-18T14:08:57Z")

</div>

Please don’t post screenshots, and include an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![laikq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laikq/32/51949_2.png) [@laikq](https://discourse.julialang.org/u/laikq)\
**Post date:** [May 6, 2024, 4:44pm UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139/3 "2024-05-06T16:44:03Z")

</div>

If anyone else finds this, I encountered this issue when `u0` was (erroneously) set to `Vector{Vector}`. If someone finds the time, it would be great to implement a more descriptive error message, if possible.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [May 7, 2024, 1:37pm UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139/4 "2024-05-07T13:37:12Z")

</div>

Can you be a little more specific？

---

<div class="post-metadata">

**Author:** ![laikq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laikq/32/51949_2.png) [@laikq](https://discourse.julialang.org/u/laikq)\
**Post date:** [May 8, 2024, 9:24am UTC](https://discourse.julialang.org/t/optim-methoderror-no-method-matching-zero/45139/5 "2024-05-08T09:24:59Z")

</div>

MWE:

```julia
using Optimization, OptimizationOptimJL

my_func(x, p) = x[1]^2 + x[2]^2
my_grad!(G, x, p) = begin
    G[1] = 2*x[1]
    G[2] = 2*x[2]
end
of = OptimizationFunction(my_func; grad=my_grad!)
op = OptimizationProblem(of, [[1.], [1.]], ())
solve(op, LBFGS())

```

yields

```julia
ERROR: MethodError: no method matching zero(::Type{Vector{Float64}})

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Dates.Time})
   @ Dates /nix/store/psyzpzlbdyv09mbww56c4i1cd7yccygi-julia-bin-1.10.2/share/julia/stdlib/v1.10/Dates/src/types.jl:440
  zero(::Type{LibGit2.GitHash})
   @ LibGit2 /nix/store/psyzpzlbdyv09mbww56c4i1cd7yccygi-julia-bin-1.10.2/share/julia/stdlib/v1.10/LibGit2/src/oid.jl:221
  ...

Stacktrace:
 [1] __solve(cache::OptimizationCache{…})
   @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/fdBis/src/OptimizationOptimJL.jl:196
 [2] solve!
   @ ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:188 [inlined]
 [3] #solve#627
   @ ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:96 [inlined]
 [4] solve(::OptimizationProblem{…}, ::LBFGS{…})
   @ SciMLBase ~/.julia/packages/SciMLBase/QEvkv/src/solve.jl:93
 [5] top-level scope
   @ REPL[13]:1
Some type information was truncated. Use `show(err)` to see complete types.

```

The fix is

```julia
op = OptimizationProblem(of, [1., 1.], ())

```

because the given `u0` was of wrong type.
