# StackOverflowError

**URL:** <https://discourse.julialang.org/t/stackoverflowerror/7773>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [December 14, 2017, 9:08pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773 "2017-12-14T21:08:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [December 14, 2017, 9:08pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/1 "2017-12-14T21:08:57Z")

</div>

First time using JuMP. Below is my JuMP specific code. I am getting very vague error when I try to solve the model and so I am not sure how to follow up. I’m not even sure how specific I should get:

```julia
m = Model(solver=NLoptSolver(algorithm=:LD_MMA))
  JuMP.register(m, :f, 2, f, autodiff=true)
  @variable(m, lambda3)
  @variable(m, lambda4)
  setlowerbound(lambda3, -0.25)
  setlowerbound(lambda4, -0.25)
  @NLobjective(m, Min, f(lambda3, lambda4))

```

```julia
julia> solve(m)
ERROR: StackOverflowError:

```

note `f` is a user-defined function that is quite complicated, but I can certainly put it here if that is the next place to check.

Some suspicions i have:

`f` is a function defined within a function and references some variables that are in the scope of that function (its a closure, is that the correct term?). Could that be the issue? If so, how do I get around that?

`f` calls some other functions within it. Do they all have to be registered?

I might have narrowed it down to:

```julia
julia> f(ForwardDiff.Dual(1.2),ForwardDiff.Dual(3.4))
ERROR: StackOverflowError:

julia> f(1.2,3.4)
4.688062610165983

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 14, 2017, 9:50pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/2 "2017-12-14T21:50:46Z")

</div>

If you’re seeing the error just calling `f` with `Dual` arguments, then it’s not an issue with function registration in JuMP, so that simplifies the problem.

How much can you simplify `f` while still seeing the stack overflow? Can you post a minimal example? Stack overflow generally happens when you have infinite recursion, something like:

```julia
julia> f(x) = f(-x)
f (generic function with 1 method)

julia> f(1)
ERROR: StackOverflowError:
Stacktrace:
 [1] f(::Int64) at ./REPL[1]:1 (repeats 80000 times)

```

or something more complicated like:

```julia
f(x) = g(x)
g(x) = h(x)
h(x) = f(x)

```

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [December 14, 2017, 9:51pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/3 "2017-12-14T21:51:17Z")

</div>

Narrowed it down to the beta function.

```julia
beta(ForwardDiff.Dual(1.2), ForwardDiff.Dual(3.4))
ERROR: StackOverflowError:

```

So where does that leave me?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 14, 2017, 9:56pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/4 "2017-12-14T21:56:02Z")

</div>

What version of ForwardDiff are you on? (`Pkg.status()`).

It works on 0.7.0

```julia
julia> beta(ForwardDiff.Dual(1.2), ForwardDiff.Dual(3.4))
Dual{Void}(0.20455811064350196)

```

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [December 14, 2017, 9:56pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/5 "2017-12-14T21:56:49Z")

</div>

JuMP pins it back to 0.5 or something =((((

```julia
 - ForwardDiff 0.5.0

```

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [December 14, 2017, 9:59pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/6 "2017-12-14T21:59:29Z")

</div>

I will just use beta(x,y) =(gamma(x)\*gamma(y))/ gamma(x+y) until JuMP updates to ForwardDiff 0.7

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 14, 2017, 10:05pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/7 "2017-12-14T22:05:23Z")

</div>

You can commit some minor type piracy and do:

```julia
Beta.beta(x::ForwardDiff.Dual, y::ForwardDiff.Dual) = ...

```

so that you won’t interfere with usage of `beta()` on other types.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [December 17, 2017, 8:11pm UTC](https://discourse.julialang.org/t/stackoverflowerror/7773/9 "2017-12-17T20:11:56Z")

</div>

The update to ForwardDiff 0.7 is incoming: [https://github.com/JuliaLang/METADATA.jl/pull/12529](https://github.com/JuliaLang/METADATA.jl/pull/12529)
