# Performance issues with matrix multiplication in JuMP

**URL:** <https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [June 25, 2021, 4:54pm UTC](https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564 "2021-06-25T16:54:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Huy\_Nguyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huy_nguyen/32/19188_2.png) [@Huy\_Nguyen](https://discourse.julialang.org/u/Huy_Nguyen)\
**Post date:** [June 25, 2021, 4:54pm UTC](https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564/1 "2021-06-25T16:54:25Z")

</div>

I am trying to do linear constraint optimisation as follows:

```julia
model = Model(Tulip.Optimizer)
    # Set up variables
    @variable(model, x[1:length(VecA)+length(VecB)])
    # set up linear constraints
    workingweights = x' * Mat' + VecC'
    # Equalities
    for i in 1:length(VecA)
        @constraint(model, workingweights * Mat[:,i] == VecA[i])
    end
    # Inequalities
    for i in 1:length(VecB)
        xi = i+length(VecB)
        @constraint(model, workingweights * Mat[:,i] <= VecB[i])
    end

    optimize!(model)

```

Here, `Mat` is a matrix, and `VecA`, `VecB`, and `VecC` are vectors. The problem is I am getting the “addition operator has been used on JuMP expressions a large number of times…” I imagine the issue is in either the definition of `workingweights` or the `@constraints` declarations, but I am unsure how to fix this. This code is central in my program, and optimising this would help a lot.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 25, 2021, 5:19pm UTC](https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564/2 "2021-06-25T17:19:59Z")

</div>

It would help a lot if you could provide a complete, executable program so that other people can replicate your problem.

---

<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:** [June 25, 2021, 10:14pm UTC](https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564/3 "2021-06-25T22:14:52Z")

</div>

This is covered in the docs (but could probably be improved): [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)

Always construct expressions inside a JuMP macro:

```nohighlight
@expression(model, workingweights, x' * Mat' + VecC')

```

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [June 26, 2021, 1:32pm UTC](https://discourse.julialang.org/t/performance-issues-with-matrix-multiplication-in-jump/63564/4 "2021-06-26T13:32:46Z")

</div>

Can you give the types of the vectors involved in `workingweights = x' * Mat' + VecC'`, even if it is outside a macro, we also try to make it fast but it’s harder so we may be missing a method in MutableArithmetics.jl
