# Question about matrix

**URL:** <https://discourse.julialang.org/t/question-about-matrix/26010>\
**Category:** New to Julia\
**Created:** [July 4, 2019, 1:36pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010 "2019-07-04T13:36:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 4, 2019, 1:36pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/1 "2019-07-04T13:36:42Z")

</div>

@variable(model,Y[1:n], lower\_bound=0)

L = Int64.(model.L)

D = Float64.(model.TD)

T = Float64.(model.TP)

n = 2  
m = 3

@objective(model,Max,sum(Y[i]\*L[i] for i in 1:2))

Yy = transpose(Y)

global a = Int64

global b = Int64

a = 1

b = n

for j in 1:m

```
@constraint(model,Yy[1:n].*T[a:b] .<= D[j])

global a = a+n

global b = b+n

```

end

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 4, 2019, 1:52pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/2 "2019-07-04T13:52:55Z")

</div>

Please

1. format you code with backticks,
2. make an MWE,
3. ask an actual question.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 4, 2019, 2:37pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/6 "2019-07-04T14:37:41Z")

</div>

```julia
for j in 1:m
@constraint(model,Yy[1:n].*T[a:b] .<= D[j])
global a = a+n
global b = b+n
end

#model is bound to a 2x2 dataframe

print(model)

output
Subject to
Y[1] >= 0.0
Y[2] >= 0.0
0.1 Y[1] <= 15.0
0.4 Y[2] <= 15.0
0.1 Y[1] <= 22.0
0.75 Y[2] <= 22.0
0.25 Y[1] <= 36.0
0.5 Y[2] <= 36.0

#The program returns me is output, but I need an output in the form of matrix 3x2

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 4, 2019, 9:13pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/7 "2019-07-04T21:13:58Z")

</div>

The question still isn’t clear to me, and this is not an MWE. Have you read through the link posted above? You’ve asked a few similar questions and you’d make your own life and that of people on here trying to help you a lot easier if you followed the advice above.

From what you have posted now it seems to me like you are printing a Jump model and somehow want the output (the constraints?) in a matrix. Could you show what output you’re looking for?

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 5, 2019, 1:18pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/8 "2019-07-05T13:18:38Z")

</div>

Yes, I’m printing a JuMP template, and I need the array constraints

```julia
#I am using these packages
using JuMP
using Cbc
using DataFrames
using CSV

# When the code is compiled, I get this message

Max 12 Y [1] + 60 Y [2]
Subject to
 Y [1]> = 0.0
 Y [2]> = 0.0
 0.1 Y [1] <= 15.0
 0.4 Y [2] <= 15.0
 0.1 Y [1] <= 22.0
 0.75 and [2] <= 22.0
 0.25 Y [1] <= 36.0
 0.5 Y [2] <= 36.0

#my goal is to receive this message

Max 12 Y [1] + 60 Y [2]
Subject to
 Y [1]> = 0.0
 Y [2]> = 0.0
0.1 Y [1] + 0.4 Y [2] <= 15.0
0.1 Y [1] + 0.75 Y [2] <= 22.0
0.25 Y [1] + 0.5 Y [2] <= 36.0

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 5, 2019, 4:05pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/9 "2019-07-05T16:05:28Z")

</div>

Okay I think I’m getting there, but this still isn’t an MWE which means it’s still unnecessarily hard to answer this question, and involves some guesswork on my part. It originally seemed like you’re unhappy with the format in which JuMP prints its models, but now it looks rather like you have an issue in how you’re constructing your constraints.

Consider the following (note that this is an MWE, so you can try it out yourself by copy-pasting my code below!):

```julia
using JuMP, GLPK

model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, Y[1:2] >= 0)
@objective(model, Max, 12*Y[1] + 60*Y[2])

@constraint(model, 0.1*Y[1] <= 15.0)
@constraint(model, 0.4*Y[2] <= 15.0)

```

Which yields:

```julia
julia> print(model)
Max 12 Y[1] + 60 Y[2]
Subject to
 Y[1] >= 0.0
 Y[2] >= 0.0
 0.1 Y[1] <= 15.0
 0.4 Y[2] <= 15.0

```

However, if we do:

```julia
model2 = Model(with_optimizer(GLPK.Optimizer))
@variable(model2, Y[1:2] >= 0)
@objective(model2, Max, 12*Y[1] + 60*Y[2])

@constraint(model2, 0.1*Y[1] + 0.4*Y[2] <= 15.0)

```

we get:

```julia
julia> print(model2)
Max 12 Y[1] + 60 Y[2]
Subject to
 Y[1] >= 0.0
 Y[2] >= 0.0
 0.1 Y[1] + 0.4 Y[2] <= 15.0

```

The output looks like you’re desired output (I’ve reduced the number of constraints for brevity), however note that the difference in outputs is because of different constraints - your original model restricts `0.1*Y[1]` and `0.4*Y[2]` to **each** be smaller or equal to 15, while your desired output only restricts **the sum** of both to be below 15. You might want to go through the JuMP documentation in detail to ensure that you understand how constraints are specified.

Now the guesswork part: you asked some questions previously about errors you got when trying to define constraints in a loop, which is probably how you ended up with a bunch of separate univariate constraints that you didn’t actually want. This is the whole point of the MWE: if you had provided a simple code snippet saying “here’s the loop I run to define constraints, here’s the resulting model and here’s what I would have wanted my model to look like”, you probably would have had answers to this as well as your last two ([1](https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442), [2](https://discourse.julialang.org/t/question-about-arrays/25405)) questions within 5 minutes.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 5, 2019, 6:30pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/10 "2019-07-05T18:30:26Z")

</div>

Thank you for your help!

I will put here exactly what I need so that at last there are no more attempts to guess.  
I am using this packages, I am developing a mathematical model and for this I needed to create dataframes and a database, which is the csv.

```julia
using JuMP
using GLPK
using DataFrames
using CSV

model = Model ()

```

In the tables are the information that will be used to construct the constraints. This information is the code output response values

```julia
Toy = CSV.read #dataframe 2x2, column "TypeToy" type String and column "LC" Int64
Process = CSV.read #dataframe 3x2, column "TypeProcess" type String and column "TD" Float64
Toy_Process = CSV.read #dataframe 6x3, column "TypeToy" and "TypeProcess" are String and column "TP" Float64

```

At this stage I need the program to recognize the size of the table by pulling this information from the database

```julia
n = size (Toy, 1)
m = size (Process, 1)

@variable (model, Y [1: n], lower_bound = 0)

```

Here I take only the values ​​that will be used in the constraints

```julia
L = Int64 (Toy.LC)
D = Float64. (Process.TD)
T = Float64. (Toy_Process.TP)

@objective (model, Max, sum (Y [i] * L [i] for i in 1: 2))

```

In order to calculate the matrix, I used the “transpose”

```julia
Yy = transpose (Y)

```

Finally, the loop will cycle through the tables in the database and print the response in matrix form

```julia
global a = Int64
global b = Int64
a = 1
b = n

for j in 1: m
    @constraint (model, Yy [1: n]. * T [a: b]. <= D [j])
    global a = a + n
    global b = b + n
end

print (model)

```

Taking the information from the CSV file, which works like the database, the code should give me this answer:

```julia
Max 12 Y [1] + 60 Y [2]
Subject to
 Y [1]> = 0.0
 Y [2]> = 0.0
0.1 Y [1] + 0.4 Y [2] <= 15.0
0.1 Y [1] + 0.75 Y [2] <= 22.0
0.25 Y [1] + 0.5 Y [2] <= 36.0

```

But unfortunately I can not get the answer in matrix form

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 5, 2019, 7:23pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/11 "2019-07-05T19:23:52Z")

</div>

Okay again this isn’t really an MWE, for two reasons:

1. The code relies on reading csv files which are only available on your computer; and
2. There are errors in there which prevent the code from running.

That notwithstanding here’s my best guess for a solution:

```julia
using JuMP, GLPK, DataFrames

# Create some dummy data - this is reverse engineered from your desired solution
# If you post questions here that rely on data you have on your own machine make 
# sure you include something similar
toy = DataFrame(LC = [12, 60])
process = DataFrame(TD = [15.0, 22.0, 36.0])
toy_process = DataFrame(TP = [0.1, 0.4, 0.1, 0.75, 0.25, 0.5])

model = Model() # your code has spaces before almost all parentheses which cause errors

n = size(toy, 1)
m = size(process, 1)

@variable(model, y[1:n], lower_bound = 0)

# Note that your columns are typed already so no need to convert types
l = toy.LC 
# also note variables are usually lowercase in Julia with uppercase reserved for Types
d = process.TD 
t = toy_process.TP

@objective(model, Max, sum(y[i]*l[i] for i ∈ 1: 2))

y′ = transpose(y)

for i ∈ 1:m
  @constraint(model, y′ * t[2*i-1:2*i] <= d[i])
end

print(model)

```

Should give:

```julia
Max 12 y[1] + 60 y[2]
Subject to
 y[1] >= 0.0
 y[2] >= 0.0
 0.1 y[1] + 0.4 y[2] <= 15.0
 0.1 y[1] + 0.75 y[2] <= 22.0
 0.25 y[1] + 0.5 y[2] <= 36.0

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 8, 2019, 12:31pm UTC](https://discourse.julialang.org/t/question-about-matrix/26010/12 "2019-07-08T12:31:19Z")

</div>

It worked perfectly, thank you!
