JuMP: declaring a vector of PSD matrices

in JuMP it’s very straightforward to declare a PSD matrix variable. If S \succeq 0, I can simply say

@variable(m, S[1:4,1:4], PSD)

But if I now want a set, or vector, of them, like, S_i \succeq 0, i \in [1,\ldots,k] I’m not sure of how to achieve this. Is it possible?

Clearly

@variable(m, S[1:k,1:4,1:4], PSD)

doesn’t work.

Many thanks.

Hi @healyp

Do something like:

julia> using JuMP

julia> model = Model();

julia> S = [@variable(model, [1:4,1:4], PSD, base_name = "S$k") for k in 1:3]
3-element Vector{LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}}}:
 [S1[1,1] S1[1,2] S1[1,3] S1[1,4]; S1[1,2] S1[2,2] S1[2,3] S1[2,4]; S1[1,3] S1[2,3] S1[3,3] S1[3,4]; S1[1,4] S1[2,4] S1[3,4] S1[4,4]]
 [S2[1,1] S2[1,2] S2[1,3] S2[1,4]; S2[1,2] S2[2,2] S2[2,3] S2[2,4]; S2[1,3] S2[2,3] S2[3,3] S2[3,4]; S2[1,4] S2[2,4] S2[3,4] S2[4,4]]
 [S3[1,1] S3[1,2] S3[1,3] S3[1,4]; S3[1,2] S3[2,2] S3[2,3] S3[2,4]; S3[1,3] S3[2,3] S3[3,3] S3[3,4]; S3[1,4] S3[2,4] S3[3,4] S3[4,4]]

julia> S[1]
4×4 LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}}:
 S1[1,1]  S1[1,2]  S1[1,3]  S1[1,4]
 S1[1,2]  S1[2,2]  S1[2,3]  S1[2,4]
 S1[1,3]  S1[2,3]  S1[3,3]  S1[3,4]
 S1[1,4]  S1[2,4]  S1[3,4]  S1[4,4]

A useful tip to remember is that JuMP’s built-in data structures are there to help in common cases, but you don’t need to use only them. You can, for example, build your own vector of JuMP matrices, or a dictionary, or whatever is the most appropriate data structure for your problem.