# Use add\_to\_expression!() for multi-dimension expressions

**URL:** <https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [May 23, 2022, 7:48pm UTC](https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532 "2022-05-23T19:48:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xuqingyu](https://avatars.discourse-cdn.com/v4/letter/x/a9a28c/32.png) [@xuqingyu](https://discourse.julialang.org/u/xuqingyu)\
**Post date:** [May 23, 2022, 7:48pm UTC](https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532/1 "2022-05-23T19:48:02Z")

</div>

Hi all,  
May I have your suggestions on how to use add\_to\_expression!() to add one multi-dimension expressions to another?  
Say I have an expression A with dimension (n x m), and an expression B with dimension (n x m).  
I received the following errors if I coded `add_to_expression!(A, B)`:

```julia
ERROR: LoadError: MethodError: no method matching add_to_expression!(::Matrix{JuMP.AffExpr}, ::Matrix{JuMP.AffExpr})
Closest candidates are:
  add_to_expression!(::JuMP.GenericAffExpr{C, V}, ::V) where {C, V} at /Users/qingyuxu/.julia/packages/JuMP/zn6NT/src/aff_expr.jl:382
  add_to_expression!(::JuMP.GenericAffExpr{C, V}, ::V, ::Union{Number, LinearAlgebra.UniformScaling}) where {C, V} at /Users/qingyuxu/.julia/packages/JuMP/zn6NT/src/aff_expr.jl:408
  add_to_expression!(::JuMP.GenericQuadExpr{C, V}, ::Union{JuMP.GenericAffExpr{C, V}, V}, ::Union{Number, LinearAlgebra.UniformScaling}) where {C, V} at /Users/qingyuxu/.julia/packages/JuMP/zn6NT/src/quad_expr.jl:314
  ...

```

P.S. I used to do A +=B, but JuMP warns me ‘+=’ is less efficient. Also, if A and B are vectors, add\_to\_expressions!() seems to work well.  
P.S.2 I used `+=` to get rid of the loops in the first place, so I am afraid looping through dimensions cannot help me here.

---

<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 23, 2022, 8:39pm UTC](https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532/2 "2022-05-23T20:39:35Z")

</div>

Hi @xuqingyu,

There’s no explicit support for this, but you have a couple of options.

Assuming `A` and `B` have the same dimensions, then you can use broadcasting:

```julia
add_to_expression!.(A, B)

```

This will call `add_to_expression!` on each element pair of `A` and `B`.

Second, although I see your p.p.s., just use a loop! It won’t be any slower.

Third, if your problem is small enough, just use `+=`. It’s a warning, not an error. So if it’s fast enough for you, don’t worry about the “best” thing to do.

You might also be able to restructure your code to avoid constructing expressions outside the macros. Here’s an explanation in the docs for why we warn that `+=` is less efficient: [Performance tips · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/performance_tips/#Use-macros-to-build-expressions)

---

<div class="post-metadata">

**Author:** ![xuqingyu](https://avatars.discourse-cdn.com/v4/letter/x/a9a28c/32.png) [@xuqingyu](https://discourse.julialang.org/u/xuqingyu)\
**Post date:** [May 23, 2022, 9:10pm UTC](https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532/3 "2022-05-23T21:10:18Z")

</div>

Thanks @odow, this is just wonderful! I love the idea of broadcasting!  
For loop not being slower, is the reason because broadcasting or `+=` will undergo looping anyway?  
(the main reason I want to use += or add\_to\_expressions!, but not loop, is that I want to reduce the length of the code)

Thank you again!

---

<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 23, 2022, 9:52pm UTC](https://discourse.julialang.org/t/use-add-to-expression-for-multi-dimension-expressions/81532/4 "2022-05-23T21:52:51Z")

</div>

Something like:

```Julia
for (a, b) in zip(A, B)
    add_to_expression!(a, b)
end

```

is essentially equivalent to

```julia
add_to_expression!.(A, B)

```

There should be minimal performance differences.

> I love the idea of broadcasting!

And yes! Quite powerful and applicable to any Julia function, not just JuMP. This is one issue we struggle with in terms of documentation. We don’t provide explicit syntax in JuMP for a lot of operations because you can compose other Julia operators to the same effect.
