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

One option would be as follows:

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

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]
2 Likes