# How to produce sparse array with conditional array comprehension

**URL:** <https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, array, sparse\
**Created:** [September 3, 2024, 2:39pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996 "2024-09-03T14:39:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juan\_hb](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@juan\_hb](https://discourse.julialang.org/u/juan_hb)\
**Post date:** [September 3, 2024, 2:39pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/1 "2024-09-03T14:39:00Z")

</div>

When creating variable or constraint containers in JuMP, one can use conditionals and a sparse array is produced, where the entries that do not met the conditions are left empty. For example:

```julia
typeof(@variable(model, [i in 1:10, j in 1:10; i != j]))
# JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}}

```

Nevertheless, in base Julia:

```julia
typeof([(i, j) for i in 1:10, j in 1:10 if i != j])
# Vector{Tuple{Int64, Int64}} instead of sparse matrix

```

Is there a way to create sparse arrays using conditionals as above?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 3, 2024, 3:24pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/2 "2024-09-03T15:24:56Z")

</div>

Your example is not particularly sparse, but a hacky solution could be:

```julia
sparse((getindex.(d,i) for i in 1:3 for d in [[(i,j, 2.0*i) for i in 1:100 for j in 1:100 if i==2j]])...)

```

I changed everything slightly here to get something sparse and not all `Int`.

---

<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 3, 2024, 9:46pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/3 "2024-09-03T21:46:00Z")

</div>

Hi @juan_hb, welcome to the forum.

The `JuMP.Containers.SparseAxisArray` is a type specific to JuMP, but it isn’t restricted to holding variables. You can do:

```julia
julia> using JuMP

julia> Containers.@container([i in 1:10, j in 1:10; i != j], (i, j))
JuMP.Containers.SparseAxisArray{Tuple{Int64, Int64}, 2, Tuple{Int64, Int64}} with 90 entries:
  [1, 2] = (1, 2)

```

What are you trying to achieve? Do you mean you want a `SparseArrays.SparseMatrixCSC` instead?

---

<div class="post-metadata">

**Author:** ![juan\_hb](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@juan\_hb](https://discourse.julialang.org/u/juan_hb)\
**Post date:** [September 3, 2024, 10:38pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/4 "2024-09-03T22:38:18Z")

</div>

I do not particularly want to store the values efficiently, what I want is to create a structure that reminds entries where there shouldn’t be any values. Maybe something like `Matrix{Union(MyType, Nothing)}` and storing `nothing` in those entries would be more adequate?

---

<div class="post-metadata">

**Author:** ![juan\_hb](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@juan\_hb](https://discourse.julialang.org/u/juan_hb)\
**Post date:** [September 3, 2024, 10:40pm UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/5 "2024-09-03T22:40:13Z")

</div>

Thank you! I was thinking of something similar to that but that’s more concise

---

<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 4, 2024, 12:37am UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/6 "2024-09-04T00:37:06Z")

</div>

> what I want is to create a structure that reminds entries where there shouldn’t be any values

Why do you need this, specifically? What are you trying to achieve?

See also: [Containers · JuMP](https://jump.dev/JuMP.jl/stable/manual/containers/#Looping-3)

---

<div class="post-metadata">

**Author:** ![juan\_hb](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@juan\_hb](https://discourse.julialang.org/u/juan_hb)\
**Post date:** [September 4, 2024, 7:46am UTC](https://discourse.julialang.org/t/how-to-produce-sparse-array-with-conditional-array-comprehension/118996/7 "2024-09-04T07:46:57Z")

</div>

I wanted to create containers of variables and constraints capable of being appended rows to iteratively solve a problem increasing one by one the units of time.

I was considering

```julia
l = [s != k ? @variable(model, Bin) : nothing for s in 1:S, k in 1:S, _ in 1:T-1]

l = cat(l, [s != k ? @variable(model, Bin) : nothing for s in 1:S, k in 1:S], dims=3)

```

but I found it not very elegant
