# MethodError: no method matching add\_to\_expression!(::JuMP.AffExpr, ::JuMP.QuadExpr)

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-add-to-expression-jump-affexpr-jump-quadexpr/107379>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [December 10, 2023, 1:59pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-add-to-expression-jump-affexpr-jump-quadexpr/107379 "2023-12-10T13:59:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)\
**Post date:** [December 10, 2023, 1:59pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-add-to-expression-jump-affexpr-jump-quadexpr/107379/1 "2023-12-10T13:59:45Z")

</div>

I got a model like

```julia
using JuMP, Ipopt
m = Model(Ipopt.Optimizer)
@variable(m, x[i=1:2]>=0)
@expression(m, e1[i=1:2], i*m[:x][i]) ## Affine
@expression(m, e2[i=1:2], m[:x][i]^i) ## At most quadric
add_to_expression!.(m[:e1], m[:e2])

```

The last `add_to_expression!.` should fail while non-inplace way of `m[:e1] += m[:e2]` works. But I got plent of expressions like `e3, e4, e5, e6...` which are built upon `e1, e2`, making every following epression construction inefficient using `+=`. Will `add_to_expression` support different combinations of AffExpr and QuadExpr expressions?

---

<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:** [December 10, 2023, 9:08pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-add-to-expression-jump-affexpr-jump-quadexpr/107379/2 "2023-12-10T21:08:00Z")

</div>

> Will `add_to_expression` support different combinations of AffExpr and QuadExpr expressions?

No, because you cannot add a quadratic expression to an affine expression without changing the type of `e1`.

Just write your code differently:

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x[1:2] >= 0)
@expression(model, e1[i in 1:2], i * x[i])
@expression(model, e2[i in 1:2], x[i]^i)
@expression(model, e3[i in 1:2], e1[i] + e2[i])

```

My general rule of thumb is that if you feel like you are fighting the JuMP syntax, there is probably a better data structure that you could be using. If you post a large snippet of your code, people may have suggestions.

---

<div class="post-metadata">

**Author:** ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)\
**Post date:** [December 15, 2023, 12:18pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-add-to-expression-jump-affexpr-jump-quadexpr/107379/3 "2023-12-15T12:18:21Z")

</div>

Thanks @odow, I got a large project to maintain so sorry that I can’t po all my codes here till it’s public.
