# How to pass a SymPy expression supported by JuMP

**URL:** https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109
**Category:** Optimization (Mathematical)
**Tags:** jump, sympy
**Created:** [May 31, 2021, 6:21am UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109 "2021-05-31T06:21:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [May 31, 2021, 6:21am UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/1 "2021-05-31T06:21:15Z")

</div>

I would like to convert a SymPy expression in order to use as an objetive function in JuMP. Suppose my variable involves two variables p [1,1] and p [1,2].

```julia
using SymPy
using JuMP, Ipopt

# expr is my SymPy expression
fn = lambdify( expr )

```

Now, my model

```julia
model = Model(Ipopt.Optimizer)
l = zeros(1,2)
@variable(model, p[j = 1:S] >= 0 )

#obj function
@objective(model,Min,fn)
print(model)
The objective function `#99` is not supported by JuMP.

```

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [May 31, 2021, 7:11am UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/2 "2021-05-31T07:11:45Z")

</div>

I’m guessing that your function is nonlinear, in which case you should use `@NLobjective`.

It might also be necessary to [register](https://jump.dev/JuMP.jl/stable/manual/nlp/#Register-a-function) the function.

---

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [May 31, 2021, 1:59pm UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/3 "2021-05-31T13:59:27Z")

</div>

Hi, I do this

```julia
x = Sym("x")
y = Sym("y")
expr = x^2 + y
fn = lambdify( expr )

```

and the model

```julia
model = Model(Ipopt.Optimizer)
l = zeros(1,2)
@variable(model, x[j = 1:2] >= 0 )

register(model, :fn,2, fn, autodiff = true)

#obj function
@NLobjective(model,Min,fn)
print(model)
Unexpected object #99 (of type SymPy.var"#99#100"{SymPy.var"###792"} in nonlinear expression.

Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] _parse_NL_expr_runtime(::Model, ::Function, ::Array{JuMP._Derivatives.NodeData,1}, ::Int64, ::Array{Float64,1}) at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\parse_nlp.jl:223
 [3] top-level scope at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\parse_nlp.jl:247
 [4] top-level scope at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\macros.jl:1368
 [5] top-level scope at In[92]:8
 [6] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [May 31, 2021, 2:08pm UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/4 "2021-05-31T14:08:43Z")

</div>

Sorry, I don’t know what SymPy’s `lambdify` does in detail and expected that it would simply return a callable function.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 31, 2021, 8:10pm UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/5 "2021-05-31T20:10:51Z")

</div>

It needs to be `@NLobjective(model, Min, fn(x[1], x[2]))`.

```nohighlight
using JuMP, SymPy, Ipopt
x = Sym("x")
y = Sym("y")
expr = x^2 + y
fn = lambdify(expr)
model = Model(Ipopt.Optimizer)
@variable(model, x[1:2] >= 0)
register(model, :fn, 2, fn; autodiff = true)
@NLobjective(model, Min, fn(x[1], x[2]))
optimize!(model)

```

---

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [June 1, 2021, 3:41pm UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/6 "2021-06-01T15:41:07Z")

</div>

I will often need to optimize a function with many variables. I wouldn’t want to have to write variable by variable. Is there any way to avoid this for automation issues?

```julia
model = Model(Ipopt.Optimizer)
@variable(model, p[1:S] >= 0)
register(model, :fn, 2, fn; autodiff = true)
@NLobjective(model, Min, fn( p[1:S] ) )
Incorrect number of arguments for "fn" in nonlinear expression.

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [June 1, 2021, 8:31pm UTC](https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/7 "2021-06-01T20:31:16Z")

</div>

This is explained in the documentation:  
[https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-functions-with-vector-inputs](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-functions-with-vector-inputs)

```nohighlight
@NLobjective(model, Min, fn(p...))

```
