# Correct way to define vectorized variables and constraints with JuMP

**URL:** <https://discourse.julialang.org/t/correct-way-to-define-vectorized-variables-and-constraints-with-jump/112401>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [April 2, 2024, 6:56am UTC](https://discourse.julialang.org/t/correct-way-to-define-vectorized-variables-and-constraints-with-jump/112401 "2024-04-02T06:56:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 2, 2024, 6:56am UTC](https://discourse.julialang.org/t/correct-way-to-define-vectorized-variables-and-constraints-with-jump/112401/1 "2024-04-02T06:56:50Z")

</div>

```julia
using JuMP

# Block1
M = Model()
@variable(M, x[1:3] >= 1)

# Block2
M = Model()
@variable(M, x[1:3] .>= 1)

# Block3
M = Model()
@variable(M, x[1:3])
@constraint(M, x[1:3] >= 1)

# Block4
M = Model()
@variable(M, x[1:3])
@constraint(M, x[1:3] .>= 1)

```

Block1: OK.  
Block2: OK.  
Block3: Not OK.  
Block4: OK.

Why? And what is the most correct way to define vectorized variables and constraints with `JuMP`?

Many 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:** [April 2, 2024, 7:03pm UTC](https://discourse.julialang.org/t/correct-way-to-define-vectorized-variables-and-constraints-with-jump/112401/2 "2024-04-02T19:03:24Z")

</div>

1. This is the recommended way. It creates a vector `x` of three variables and sets the lower bound to `1`
2. I didn’t think this worked, but I guess it does. It is identical to (1)
3. This does not work, because `x[1:3] >= 1` has a vector `x[1:3]` on the left-hand side and a constant on the right-hand side
4. This has a vector `x[1:3]` on the left-hand side, and a constant on the right-hand side, but it uses broadcasting to add three constraints. This does not modify variable bounds, but instead adds three new rows to the constraint matrix, effectively creating `[1 0 0; 0 1 0; 0 0 1] * x >= [1, 1, 1]`
