# Extracting data vectors in constraint from sum notation in JuMP

**URL:** <https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 22, 2023, 2:44pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448 "2023-03-22T14:44:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [March 22, 2023, 2:44pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448/1 "2023-03-22T14:44:01Z")

</div>

Dear All,

I am working on an optimization problem where I have a variable y\_{i,j} where i,j \in [1:n] = \{1,2,\ldots,n\}. I have a constraint of the form

\sum\_{j \in [1:n]} y\_{j,i} - \sum\_{j \in [1:n]} y\_{i,j} \geq b\_i \ldots \ldots \texttt{con}[i]

for i \in \{1, 2, \ldots ,n\}. This is very easy to construct in `JuMP`. However, to create a robust formulation for this problem it is beneficial for me to concatenate all the y\_{i,j} in one variable x \equiv \{y\_{i,j}\}\_{i\in [1:n], j \in [1:n] }, and also write down the constraint \texttt{con}[i] in the form

\langle v\_i^{\textrm{in}} - v\_i^{\textrm{out}}; x\rangle \geq b\_i

where \sum\_{j \in [1:n]} y\_{j,i} = \langle v\_i^{\textrm{in}} ; x \rangle, and \sum\_{j \in [1:n]} y\_{i,j} = \langle v\_i^{\textrm{out}} ; x\rangle. I was wondering in `JuMP` if we can (i) create a concatenated decision variable x \equiv \{y\_{i,j}\}\_{i\in [1:n], j \in [1:n] }, and (ii) extract the vectors v\_i^{\textrm{in}}, v\_i^{\textrm{out}} if we create the constraints \texttt{con}[i] for i\in [1:n] in `JuMP`. Any help will be much appreciated!

---

<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:** [March 22, 2023, 7:05pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448/2 "2023-03-22T19:05:49Z")

</div>

I’m not sure I fully understand the question.

If you use `constraint_object(con[I]).func` you can get the `AffExpr` of the constraint. That contains the coefficients. But it isn’t a vector. You’d need to write that conversion yourself.

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [March 22, 2023, 7:31pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448/3 "2023-03-22T19:31:55Z")

</div>

Thanks for your response @odow ! What I meant is that if we concatenate all the variables into one in x \triangleq \{y\_{i,j}\}\_{i,\in [1:n]j\in[1:n]}, then all the constraints of the form

\sum\_{j \in [1:n]} y\_{j,i} - \sum\_{j \in [1:n]} y\_{i,j} \geq b\_i \ldots \ldots \texttt{con}[i]

can be written more compactly as

A x \geq b

for some matrix A \in \mathbb{R}^{n^2 \times n}. My question was is there a way to get the matrix A if I create the `JuMP` model using the constraints in the first form and is there a way to creat x \triangleq \{y\_{i,j}\}\_{i,\in [1:n]j\in[1:n]} programmatically in `JuMP`.

---

<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:** [March 22, 2023, 9:13pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448/4 "2023-03-22T21:13:46Z")

</div>

You can get the matrix following your PDHG question: [Connecting a simple first-order solver to solve standard form linear program to JuMP](https://discourse.julialang.org/t/connecting-a-simple-first-order-solver-to-solve-standard-form-linear-program-to-jump/95694)

You can use `x = all_variables(model)`.

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [March 22, 2023, 10:25pm UTC](https://discourse.julialang.org/t/extracting-data-vectors-in-constraint-from-sum-notation-in-jump/96448/5 "2023-03-22T22:25:27Z")

</div>

This is great, thanks @odow !
