# Why are the entries identical in \`zeros(JuMP.AffExpr, N)\`

**URL:** <https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, zeros\
**Created:** [July 18, 2025, 11:30pm UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860 "2025-07-18T23:30:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 18, 2025, 11:30pm UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/1 "2025-07-18T23:30:16Z")

</div>

My understanding about the `zeros` function in julia is that it has independent entries.  
But today I find a weird behavior which I cannot explain why.  
Compare the following unexpected outcome `🔴` and the expected outcome `✅`

```julia
julia> vec = zeros(Int, 3)
3-element Vector{Int64}:
 0
 0
 0

julia> for i = 1:3
           vec[i] += i
       end

julia> vec
3-element Vector{Int64}:
 1
 2
 3

julia> using JuMP

julia> model = Model();

julia> vec = zeros(JuMP.AffExpr, 3)
3-element Vector{AffExpr}:
 0
 0
 0

julia> for i = 1:3
           x = JuMP.@variable(model)
           println("i = $i, x = $x")
           add_to_expression!(vec[i], x)
       end
i = 1, x = _[1]
i = 2, x = _[2]
i = 3, x = _[3]

julia> vec # 🔴
3-element Vector{AffExpr}:
 _[1] + _[2] + _[3]
 _[1] + _[2] + _[3]
 _[1] + _[2] + _[3]

julia> model = Model();

julia> vec = [JuMP.AffExpr(0) for _ = 1:3]
3-element Vector{AffExpr}:
 0
 0
 0

julia> for i = 1:3
           x = JuMP.@variable(model)
           println("i = $i, x = $x")
           add_to_expression!(vec[i], x)
       end
i = 1, x = _[1]
i = 2, x = _[2]
i = 3, x = _[3]

julia> vec # ✅
3-element Vector{AffExpr}:
 _[1]
 _[2]
 _[3]

```

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [July 19, 2025, 1:30am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/2 "2025-07-19T01:30:04Z")

</div>

They behave differently because of how JuMP’s `AffExpr` objects are created and stored in the array.

```julia
vec = zeros(JuMP.AffExpr, 3)

```

This line creates an array of three references to the _same_ default-constructed `AffExpr()` object.

So when you do:

```julia
add_to_expression!(vec[i], x)

```

you’re modifying **the same object** every time. The result is that all three elements in `vec` appear identical and contain _all_ the variables added across iterations.

```julia
vec = [JuMP.AffExpr(0) for _ = 1:3]

```

This explicitly creates **three distinct `AffExpr` objects** initialized with zero. Now:

```julia
add_to_expression!(vec3[i], x)

```

modifies a unique object each time, so each element in `vec` holds a different variable — as you intended.

Avoid `zeros(JuMP.AffExpr, n)` — use list comprehensions to ensure unique instances:

```julia
vec = [JuMP.AffExpr(0) for _ in 1:n]

```

This pattern is common for JuMP modeling when dealing with arrays of expressions.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 19, 2025, 1:46am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/3 "2025-07-19T01:46:03Z")

</div>

> [@technocrat](#):
>
> This line creates an array of three references to the _same_ default-constructed `AffExpr()` object.

Why is the behavior defined to be this?  
I thought this would pertain to `fill`.

Otherwise why we need to devise the function `zeros` and `ones`? They could have been overshadowed by `fill`.

```julia
julia> model = JuMP.Model();

julia> vec = fill(JuMP.AffExpr(0), 3)
3-element Vector{AffExpr}:
 0
 0
 0

julia> x = JuMP.@variable(model)
_[1]

julia> JuMP.add_to_expression!(vec[2], x)
_[1]

julia> vec
3-element Vector{AffExpr}:
 _[1]
 _[1]
 _[1]

```

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [July 19, 2025, 2:02am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/4 "2025-07-19T02:02:45Z")

</div>

> [@WalterMadelim](#):
>
> Why is the behavior defined to be this?  
> I thought this would pertain to `fill`.
> 
> Otherwise why we need to devise the function `zeros` and `ones`? They could have been overshadowed by `fill`.

Yeah, it’s counterintuitive.

Short answer is \* `zeros(T, n)` → calls `fill(zero(T), n)`

- For **mutable types** , this means all elements are the _same_ object
- For **immutable types** , this is safe and expected
- `fill(f(), n)` reuses the result of `f()` once — does _not_ call `f()` `n` times
- Therefore, if you need _distinct mutable objects_, use a comprehension:

```julia

[AffExpr(0) for _ in 1:n]

```

---

<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:** [July 19, 2025, 2:41am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/5 "2025-07-19T02:41:27Z")

</div>

This is documented at

> **[Expressions · JuMP](https://jump.dev/JuMP.jl/stable/manual/expressions/#Initializing-arrays)**
>
> Documentation for JuMP.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 19, 2025, 3:15am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/6 "2025-07-19T03:15:35Z")

</div>

Although the present behavior of `zeros` is confusing, I’ll remember this.

We do not this redundant function in julia (I think), we suffice to have

```julia
julia> fill(rand(), 3)
3-element Vector{Float64}:
 0.7694423098899204
 0.7694423098899204
 0.7694423098899204

julia> [rand() for _ = 1:3]
3-element Vector{Float64}:
 0.5962687295988872
 0.46835512943912494
 0.30210548834623685

```

---

<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:** [July 19, 2025, 3:29am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/7 "2025-07-19T03:29:22Z")

</div>

Yes, this is a Julia problem. It is not specific to JuMP. It applies to all types.

---

<div class="post-metadata">

**Author:** ![araujoms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/araujoms/32/217734_2.png) [@araujoms](https://discourse.julialang.org/u/araujoms)\
**Post date:** [July 19, 2025, 6:59am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/8 "2025-07-19T06:59:46Z")

</div>

I think the docs should mention `[AffExpr(0) for _ in 1:n]`, that’s much more convenient than the method explained there.

---

<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:** [July 19, 2025, 7:06am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/9 "2025-07-19T07:06:19Z")

</div>

PRs accepted

---

<div class="post-metadata">

**Author:** ![araujoms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/araujoms/32/217734_2.png) [@araujoms](https://discourse.julialang.org/u/araujoms)\
**Post date:** [July 19, 2025, 8:04am UTC](https://discourse.julialang.org/t/why-are-the-entries-identical-in-zeros-jump-affexpr-n/130860/10 "2025-07-19T08:04:07Z")

</div>

PR submitted.
