# Can JuMP.jl create a sparse array variable that can be used as a full matrix?

**URL:** <https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [October 2, 2021, 2:15pm UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114 "2021-10-02T14:15:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iiitr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iiitr/32/24586_2.png) [@iiitr](https://discourse.julialang.org/u/iiitr)\
**Post date:** [October 2, 2021, 2:15pm UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/1 "2021-10-02T14:15:31Z")

</div>

Hi all  
Recently, I have encountered some problems in optimizing code speed. I guess it is because I always create dense array variables

I want to create a 4x4 array variables, but it only has a value at the index (1, 2), (2, 1), (2, 3), (3, 2) and the rest is 0. The current practice is to use the following code:

```julia
model = Model(Gurobi.Optimizer)
@variable(model, x[1:4, 1:4])
@constraints(model, begin
    x[1, 2] == 0
    x[2, 1] == 0
    x[2, 3] == 0
    x[3, 2] == 0
end)

```

When the array size is very small, this will not have any adverse impact, but when the array size is very large, many unnecessary variables and constraints will be created. I guess this reduces the speed of model construction and solver solving

What I want to achieve is to create a sparse array variable, but it can be used as a full matrix, including index, matrix multiplication and so on. It is as natural and convenient as MATLAB.

Thanks a lot !

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [October 2, 2021, 3:35pm UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/2 "2021-10-02T15:35:59Z")

</div>

One option would be as follows:

```julia
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> x = zeros(AffExpr, 3, 3)
3×3 Matrix{AffExpr}:
 0 0 0
 0 0 0
 0 0 0

julia> x[1, 1] = @variable(model, base_name = "x[1, 1]")
x[1, 1]

julia> x[2, 2] = @variable(model, base_name = "x[2, 2]")
x[2, 2]

julia> x[1, 3] = @variable(model, base_name = "x[1, 3]")
x[1, 3]

julia> x[3, 1] = @variable(model, base_name = "x[3, 1]")
x[3, 1]

julia> x[3, 3] = @variable(model, base_name = "x[3, 3]")
x[3, 3]

julia> x
3×3 Matrix{AffExpr}:
 x[1, 1] 0 x[1, 3]
 0 x[2, 2] 0
 x[3, 1] 0 x[3, 3]

```

Another option is as follows

```nohighlight
julia> using SparseArrays

julia> s = sparse([1, 3, 2, 1, 3], [1, 1, 2, 3, 3], @variable(model, y[1:5]))
3×3 SparseMatrixCSC{VariableRef, Int64} with 5 stored entries:
 y[1] ⋅ y[4]
 ⋅ y[3] ⋅
 y[2] ⋅ y[5]

julia> Matrix(s)
3×3 Matrix{AffExpr}:
 y[1] 0 y[4]
 0 y[3] 0
 y[2] 0 y[5]

```

---

<div class="post-metadata">

**Author:** ![iiitr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iiitr/32/24586_2.png) [@iiitr](https://discourse.julialang.org/u/iiitr)\
**Post date:** [October 3, 2021, 1:21am UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/4 "2021-10-03T01:21:15Z")

</div>

Thank you very much for your reply! The code you provided is very helpful.

On the basis of the solution you provided, I raised a new question. If you have time, I’d like to ask you for help!

> [@How to extend the JuMP @variable so that it can construct a sparse array which can be used as a full matrix?](https://discourse.julialang.org/t/how-to-extend-the-jump-variable-so-that-it-can-construct-a-sparse-array-which-can-be-used-as-a-full-matrix/69125):
>
> Hi all In fact, this question is a derivative of my last question. [https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/2?u=iiitr](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/2) I want to create a 4x4 array variables, but it only has a value at the index (1, 2), (2, 1), (2, 3), (3, 2) and the rest is 0. @blegat provide several solutions, one of which best meets my expectations, as follows: model = Model() x = zeros(AffExpr, 3, 3) x[1, 1] = @variable(model, base\_name = "x[1, …
