# How to define this expression?

**URL:** <https://discourse.julialang.org/t/how-to-define-this-expression/83930>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, ipopt\
**Created:** [July 8, 2022, 3:35am UTC](https://discourse.julialang.org/t/how-to-define-this-expression/83930 "2022-07-08T03:35:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivzktch99](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@ivzktch99](https://discourse.julialang.org/u/ivzktch99)\
**Post date:** [July 8, 2022, 3:35am UTC](https://discourse.julialang.org/t/how-to-define-this-expression/83930/1 "2022-07-08T03:35:46Z")

</div>

Hello there

I want to solve the following model

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
n = 5
@variable(model, X[1:n, 1:n])
@variable(model, y[1:n])

u = ones(n)
z = @expression(model, X * u)

expr = (1/factorial(n))*z.^4 - (1/factorial(n+1)) * X * (z.^4)

@NLconstraint(model, sum(y[i]*expr[i] for i in 1:n) == 1 )

```

JuMP did not like the expr vector. So I wrapped it into @expression as

`expr = @expression(model, expr[i=1:n] ,(1/factorial(n))*z[i]^4 - (1/factorial(n+1)) * sum(X[i,j]*z[j]^4 for j in 1:i) for i in 1:n)`

But I got the following error

> Unexpected object Base.Generator{UnitRange{Int64}, var"#620#623"}(var"#620#623"(), 1:5) (of type Base.Generator{UnitRange{Int64}, var"#620#623"} in nonlinear expression.
> 
> Stacktrace:  
> [1] error(s::String)  
> @ Base ./error.jl:33  
> [2] \_parse\_NL\_expr\_runtime(m::Model, x::Base.Generator{UnitRange{Int64}, var"#620#623"}, tape::Vector{JuMP.\_Derivatives.NodeData}, parent::Int64, values::Vector{Float64})  
> @ JuMP ~/.julia/packages/JuMP/0C6kd/src/parse\_nlp.jl:510  
> [3] macro expansion  
> @ ~/.julia/packages/JuMP/0C6kd/src/parse\_nlp.jl:544 [inlined]  
> [4] macro expansion  
> @ ~/.julia/packages/JuMP/0C6kd/src/macros.jl:2121 [inlined]  
> [5] top-level scope  
> @ ./In[44]:14  
> [6] eval  
> @ ./boot.jl:373 [inlined]  
> [7] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
> @ Base ./loading.jl:1196

Is there any way to define **expr** vector needed for the constraint?

---

<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:** [July 8, 2022, 3:46am UTC](https://discourse.julialang.org/t/how-to-define-this-expression/83930/2 "2022-07-08T03:46:04Z")

</div>

One approach is this:

```julia
using JuMP, Ipopt
n = 5
n! = factorial(n)
model = Model(Ipopt.Optimizer)
@variable(model, X[1:n, 1:n])
@variable(model, y[1:n])
@expression(model, z[i=1:n], sum(X[i, :]))
@NLexpression(
    model,
    expr[i=1:n],
    z[i]^4 / n! - sum(X[i, j] * z[j]^4 for j in 1:n) / (n! * (n + 1)),
)
@NLconstraint(model, sum(y[i] * expr[i] for i in 1:n) == 1)

```

The trick is to realize that you can’t construct nonlinear expressions outside the macros, you can’t use linear algebra like `X * z`, and you can’t use function calls like `factorial(n)` inside the macro.
