# Simplify a nonlinear expression

**URL:** <https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 12, 2023, 11:12pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772 "2023-05-12T23:12:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [May 12, 2023, 11:12pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/1 "2023-05-12T23:12:41Z")

</div>

Hi,

Is there any way we can simplify a nonlinear expression automatically in JuMP. For example, if we have:

```julia
using JuMP, MadNLP
model = Model(MadNLP.Optimizer)
@variable(model, CA)
@variable(model, CB)
expr_str = :($(CA) + $(CA) + $(CB))
expr_NL = add_nonlinear_expression(model, expr_str)

```

Is it possible for JuMP to automatically convert` expr_str` to` :(2*$(CA) + $(CB))` instead of `:($(CA) + $(CA) + $(CB))`? Thanks for your help!

---

<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 13, 2023, 2:24am UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/2 "2023-05-13T02:24:54Z")

</div>

> Is it possible for JuMP to automatically convert

Nope. We use the expressions you pass as-is.

Although the new nonlinear rewrite that is in progress, [https://github.com/jump-dev/JuMP.jl/pull/3106](https://github.com/jump-dev/JuMP.jl/pull/3106), will automatically detect linear and quadratic expressions and simplify them.

---

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [May 13, 2023, 6:01pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/3 "2023-05-13T18:01:49Z")

</div>

Thanks for the answer. Will the rewrite able to simplify linear/quadratic expressions which are parts of a larger nonlinear expression? For example, `x + exp(x + x + x*y) + x` to `2*x + exp(2*x + x*y)`. 😀

---

<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 13, 2023, 10:48pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/4 "2023-05-13T22:48:44Z")

</div>

> Will the rewrite able to simplify linear/quadratic expressions which are parts of a larger nonlinear expression?

Mostly. But there are a few cases where we won’t:

```julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x);

julia> @variable(model, y);

julia> @expression(model, x + exp(x + x + x*y) + x)
+(x, exp(x*y + 2 x), x)

julia> @expression(model, x + x + exp(x + x + x*y))
+(2 x, exp(x*y + 2 x))

```

Because we build expressions from left to right, the first `@expression` doesn’t go back and realize that it could add the first and third terms together.

---

<div class="post-metadata">

**Author:** ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)\
**Post date:** [May 14, 2023, 6:20pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/5 "2023-05-14T18:20:13Z")

</div>

It might be worth giving MTK a try [Modeling Optimization Problems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/dev/tutorials/optimization/) if you have such expressions in constraints, then you can call `structural_simplify` on the `OptimizationSystem` though it doesn’t do it for the objective right now

---

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [May 15, 2023, 5:32pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/6 "2023-05-15T17:32:03Z")

</div>

Fantastic. I am looking forward to the feature’s release. Thanks for the great work!

---

<div class="post-metadata">

**Author:** ![VPBML](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vpbml/32/212617_2.png) [@VPBML](https://discourse.julialang.org/u/VPBML)\
**Post date:** [May 15, 2023, 5:35pm UTC](https://discourse.julialang.org/t/simplify-a-nonlinear-expression/98772/7 "2023-05-15T17:35:51Z")

</div>

Thanks for the suggestion. But I do not have such expressions in constraints or the objective function. They are some expressions that I would like to calculate their values at the optimal conditions.
