# Print all ellements of a variable-vector in once (broadcasting)

**URL:** https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884
**Category:** Optimization (Mathematical)
**Tags:** jump, vector
**Created:** [September 15, 2023, 9:38am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884 "2023-09-15T09:38:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Fabian1](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fabian1](https://discourse.julialang.org/u/Fabian1)
#### Post date: [September 15, 2023, 9:38am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/1 "2023-09-15T09:38:21Z")

</div>

Hi,  
I’ve just started with Julia, and I would like to print each element of a vector in one time for a variable (see line 37) in my optimization model under JuMP. It is already working for a defined parameter (D, see line 31).  
This would save me writing 3 lines of code (see line 33 to 35).  
Does anyone have any ideas?  
Thank you in advance  
Fabian

1 using JuMP  
2 using LinearAlgebra  
3 using HiGHS

5 model = Model(HiGHS.Optimizer)

7 # Parameter  
8 D = [10, 10, 10]  
9 C\_Hydro = [1, 10, 1]  
10 C\_gas = [10, 1, 10]  
11 t = [1, 1, 1]

13 # decision variables

15 @ variable(model , q\_out\_hydro[i=1:3] \>= 0 ) # vector with 3 elements, there is a space between variable and ‘@’ to avoid tagging people  
16 @ variable(model , q\_in\_gas[i=1:3] \>= 0) # vector with 3 elements

18 # constraint  
19 e2 = 10 \* q\_in\_gas  
20 e1 = 2 \* q\_out\_hydro  
21 @ constraint(model, energy, D \<= e1 .\* t + e2 .\* t)  
22 Cope = dot(C\_Hydro, (q\_out\_hydro .\* t)) + dot(C\_gas, (q\_in\_gas .\* t))

24 # objective function

26 @ objective(model, Min, Cope)

28 optimize!(model)

30 # print results  
31 print(“D:”, D) # output: D:[10, 10, 10]

33 print(“; q\_out\_hydro[1]:”, value(q\_out\_hydro[1]) ) # output: q\_out\_hydro[1]:5.0  
34 print(“; q\_out\_hydro[2]:”, value(q\_out\_hydro[2]) ) # output: q\_out\_hydro[2]:0.0  
35 print(“; q\_out\_hydro[3]:”, value(q\_out\_hydro[3]) ) # output: q\_out\_hydro[3]:5.0

37 print(“; Q\_hydro:”, q\_out\_hydro[1:3]) # output: Q\_hydro:VariableRef[q\_out\_hydro[1], q\_out\_hydro[2], q\_out\_hydro[3]]

39 # Output in the Terminal  
40 # Running HiGHS 1.5.3 [date: 1970-01-01, git hash: 45a127b78]  
41 # Copyright (c) 2023 HiGHS under MIT licence terms  
42 # Presolving model  
43 # 3 rows, 4 cols, 4 nonzeros  
44 # 0 rows, 0 cols, 0 nonzeros  
45 # Presolve : Reductions: rows 0(-3); columns 0(-6); elements 0(-6) - Reduced to empty  
46 # Solving the original LP from the solution after postsolve  
47 # Model status : Optimal  
48 # Objective value : 1.1000000000e+01  
49 # HiGHS run time : 0.02  
50 # D:[10, 10, 10]; q\_out\_hydro[1]:5.0; q\_out\_hydro[2]:0.0; q\_out\_hydro[3]:5.0; Q\_hydro:VariableRef[q\_out\_hydro[1], q\_out\_hydro[2], q\_out\_hydro[3]]

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [September 15, 2023, 10:19am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/2 "2023-09-15T10:19:58Z")

</div>

You can query the values of a vector variable using a dot. For instance,

```julia
q_out_hydro_vals = value.(q_out_hydro)

```

You can also use the right-hand-side directly in the print function similar as in your line 31.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [September 15, 2023, 10:20am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/3 "2023-09-15T10:20:03Z")

</div>

Hi Fabian,

You probably want to use “broadcasting” which means putting `.` after the function to indicate you want to apply it to all elements:

```julia
value.(q_out_hydro)

```

Hope that helps.

There is also a specific category here on Julia Discourse that the JuMP developers actively watch that is a good place to ask JuMP questions:

> **[Optimization (Mathematical)](https://discourse.julialang.org/c/domain/opt/13)**
>
> For discussion of mathematical optimization software in Julia. This includes modeling software and solvers for linear optimization, mixed-integer optimization, convex optimization, nonlinear optimization, semidefinite optimization, and related topics.

---

<div class="post-metadata">

### Author: ![Fabian1](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fabian1](https://discourse.julialang.org/u/Fabian1)
#### Post date: [September 15, 2023, 10:38am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/4 "2023-09-15T10:38:02Z")

</div>

Thank you very much! It was very helpful

---

<div class="post-metadata">

### Author: ![Fabian1](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fabian1](https://discourse.julialang.org/u/Fabian1)
#### Post date: [September 15, 2023, 10:38am UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/5 "2023-09-15T10:38:38Z")

</div>

Thank you very much! Finally it worked! Have a nice day!

---

<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: [September 15, 2023, 10:58pm UTC](https://discourse.julialang.org/t/print-all-ellements-of-a-variable-vector-in-once-broadcasting/103884/6 "2023-09-15T22:58:05Z")

</div>

> [@jd-foster](#):
>
> There is also a specific category here on Julia Discourse that the JuMP developers actively watch that is a good place to ask JuMP questions:

Hi @Fabian1, I’ve moved this question to the optimization section. Feel free to open any other questions 😄
