# Operations on NonlinearExpressions

**URL:** <https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453>\
**Category:** Optimization (Mathematical)\
**Created:** [August 16, 2021, 12:33am UTC](https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453 "2021-08-16T00:33:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonygem96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tonygem96/32/27564_2.png) [@tonygem96](https://discourse.julialang.org/u/tonygem96)\
**Post date:** [August 16, 2021, 12:33am UTC](https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453/1 "2021-08-16T00:33:37Z")

</div>

I am building a user defined function to be optimized in an NLP problem. However NLexpressions cannot be operated on without the @NLexpression macro (eg added, subtracted etc). Is there something wrong with lets say extending Base.+ to add 2 nonlinear expressions.

```julia
using JuMP
using Ipopt

model = Model(Ipopt.Optimizer)
@variable(model,x)

expr1 = @NLexpression(model,sin(x))
expr2 = @NLexpression(model,cos(x))

expr3 = expr1 + expr2 # This doesn't work 

import Base:+
+(a::NonlinearExpression,b::NonlinearExpression) = @NLexpression(model, a + b)

expr3 = expr1 + expr2 # Now it works  

```

This would save me having to include all the operations inside a macro.

Thanks!

---

<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:** [August 16, 2021, 3:58am UTC](https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453/2 "2021-08-16T03:58:29Z")

</div>

Operator overloading causes a large number of intermediate expressions to be generated:

```nohighlight
julia> using JuMP

julia> function Base.:+(a::NonlinearExpression, b::NonlinearExpression)
           return @NLexpression(model, a + b)
       end

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> expr = @NLexpression(model, sin(x))
"Reference to nonlinear expression #1"

julia> expr_2 = sum(expr for _ in 1:10)
"Reference to nonlinear expression #10"

```

The macros rewrite things behind the scenes for efficiency, which is critical for achieving performance in any realistic sized instances.

---

<div class="post-metadata">

**Author:** ![tonygem96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tonygem96/32/27564_2.png) [@tonygem96](https://discourse.julialang.org/u/tonygem96)\
**Post date:** [August 19, 2021, 8:39pm UTC](https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453/3 "2021-08-19T20:39:30Z")

</div>

Is this also the case with linear expressions? Should I avoid explicitly adding them for performance? (eg only use add\_to\_expression! or the appropriate macro)

---

<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:** [August 19, 2021, 9:22pm UTC](https://discourse.julialang.org/t/operations-on-nonlinearexpressions/66453/4 "2021-08-19T21:22:29Z")

</div>

Yes

[https://jump.dev/JuMP.jl/stable/tutorials/Getting%20started/performance\_tips/#Use-macros-to-build-expressions](https://jump.dev/JuMP.jl/stable/tutorials/Getting%20started/performance_tips/#Use-macros-to-build-expressions)
