# Vectorize Julia JuMP code

**URL:** https://discourse.julialang.org/t/vectorize-julia-jump-code/109844
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [February 6, 2024, 11:24pm UTC](https://discourse.julialang.org/t/vectorize-julia-jump-code/109844 "2024-02-06T23:24:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [February 6, 2024, 11:24pm UTC](https://discourse.julialang.org/t/vectorize-julia-jump-code/109844/1 "2024-02-06T23:24:16Z")

</div>

Hi all,

I have the code below. How can we write this code in a vectorized way?

```julia
using JuMP
using SparseArrays

model = Model()
D = [1,2,3]
L = [1,2,3,4,5]
line2dem = sparse([1, 2, 3], [1, 2, 3], [1, 1, 1], 8, 3)
lineIReal = [1,2,1,1,2]
demIReal = [1,2,1]
@variable(model, demIV[D])
@variable(model, genIReal[D])
@variable(model, genIImag[D])

@objective(model, Min, sum(demIReal[d] * (genIReal[d]^2 + genIImag[d]^2)^2 + lineIReal[l] for d in D, l in L))

@constraint(model, eDemIR[d in D], demIV[d] + demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)

```

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: [February 6, 2024, 11:39pm UTC](https://discourse.julialang.org/t/vectorize-julia-jump-code/109844/2 "2024-02-06T23:39:38Z")

</div>

First answer: you don’t need to. The scalar version of JuMP will be just as fast (and potentially even a little faster).

Second answer: something like this:

```Julia
using JuMP
using SparseArrays

lineIReal = [1, 2, 1, 1, 2]
demIReal = [1, 2, 1]
D = length(demIReal)
L = length(lineIReal)
line2dem = sparse([1, 2, 3], [1, 2, 3], [1, 1, 1], L, D)

model = Model()
@variable(model, demIV[1:D])
@variable(model, genIReal[1:D])
@variable(model, genIImag[1:D])
@objective(model, Min, demIReal' * (genIReal.^2 .+ genIImag.^2).^2 + sum(lineIReal))
@constraint(model, demIV .+ demIReal .+ line2dem' * lineIReal .== 0)

```

I changed the objective though. Did you mean to have the sum over both `D` and `L`? The terms were unrelated.

Also, your final constraint is just a fixed variable bound. You could instead do

```kulia
fix.(demIV, -(demIReal .+ line2dem' * lineIReal))

```
