Hi @JPG, welcome to the forum.
You need to use the spzeros(type, m, n)
method:
help?> SparseArrays.spzeros
spzeros([type,]m[,n])
Create a sparse vector of length m or sparse matrix of size m x n. This sparse array will not contain any nonzero
values. No storage will be allocated for nonzero values during construction. The type defaults to Float64 if not
specified.
Examples
β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘
julia> spzeros(3, 3)
3Γ3 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
β
β
β
β
β
β
β
β
β
julia> spzeros(Float32, 4)
4-element SparseVector{Float32, Int64} with 0 stored entries
Hereβs an example:
julia> Q = SparseArrays.spzeros(QuadExpr, 3, 3)
3Γ3 SparseArrays.SparseMatrixCSC{QuadExpr, Int64} with 0 stored entries:
β
β
β
β
β
β
β
β
β
julia> model = Model();
julia> @variable(model, x)
x
julia> Q[1, 1] = 2 * x^2 + 1
2 xΒ² + 1
julia> Q
3Γ3 SparseArrays.SparseMatrixCSC{QuadExpr, Int64} with 1 stored entry:
2 xΒ² + 1 β
β
β
β
β
β
β
β
But it looks like you might have a complex-valued affine expression, not a quadratic expression?
In that case, youβd need
julia> Q = SparseArrays.spzeros(GenericAffExpr{ComplexF64, VariableRef}, 3, 3)
3Γ3 SparseArrays.SparseMatrixCSC{GenericAffExpr{ComplexF64, VariableRef}, Int64} with 0 stored entries:
β
β
β
β
β
β
β
β
β
julia> Q[1, 1] = (2.0 + 3.0im) * x
(2 + 3im) x
julia> Q
3Γ3 SparseArrays.SparseMatrixCSC{GenericAffExpr{ComplexF64, VariableRef}, Int64} with 1 stored entry:
(2 + 3im) x β
β
β
β
β
β
β
β