# When adding a SumOfSquares constraint, is it possible to get the JuMP variable corresponding to the Gram matrix?

**URL:** <https://discourse.julialang.org/t/when-adding-a-sumofsquares-constraint-is-it-possible-to-get-the-jump-variable-corresponding-to-the-gram-matrix/127966>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [April 11, 2025, 9:12am UTC](https://discourse.julialang.org/t/when-adding-a-sumofsquares-constraint-is-it-possible-to-get-the-jump-variable-corresponding-to-the-gram-matrix/127966 "2025-04-11T09:12:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaspeel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaspeel/32/216308_2.png) [@aaspeel](https://discourse.julialang.org/u/aaspeel)\
**Post date:** [April 11, 2025, 9:12am UTC](https://discourse.julialang.org/t/when-adding-a-sumofsquares-constraint-is-it-possible-to-get-the-jump-variable-corresponding-to-the-gram-matrix/127966/1 "2025-04-11T09:12:51Z")

</div>

Hello!

I have an optimization model with an SOS constraint and I would like to add extra constraints on the corresponding Gram matrix. Unfortunately, I could not find a way to obtain the JuMP variable corresponding to the Gram matrix (the function ‘gram\_matrix’ can only be called after the model has been optimized).

Behind the hood, a constrain `p in SOSCone()` must be converted in `p = Phi^T * G * Phi`, with G≥0 (in the psd sense) and Phi being the polynomial basis. I would like to add extra constraints on the Gram matrix G. More precisely, I would like to constraint the condition number of G to be not greater than a user-defined κ. To this end, I want to add the constraint λ_I ≤ G ≤ λ_κ\*I, where λ≥0 is an optimization variable.

The code would be something like this:

```julia
using JuMP
using SumOfSquares
using CSDP
using LinearAlgebra

model = SOSModel(CSDP.Optimizer)

@polyvar x
@variable(model, a)

p = x^2 + a

@objective(model, Min, a)
cons = @constraint(model, p in SOSCone())

κ = 1e4 # upper bound on the condition number of the Gram matrix

G = gram_matrix(cons) # this is throwing an error: OptimizeNotCalled()

@variable(model, λ≥0)
@constraint(model, G-λ*I in PSDCone())
@constraint(model, λ*κ*I - G in PSDCone())

optimize!(model)

```

Ideally, it would be possible to do the same thing when a domain is used in the SOS constraint, i.e.,

```julia
cons = @constraint(model, p in SOSCone(), domain = @set(x>=1))

```

In that case, there are several Gram matrices associated with that constraint.

Is there a way to do that?

PS: It is my first post here, I am happy to edit it if I am not following the guidelines properly 🙂

---

<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:** [April 11, 2025, 9:30am UTC](https://discourse.julialang.org/t/when-adding-a-sumofsquares-constraint-is-it-possible-to-get-the-jump-variable-corresponding-to-the-gram-matrix/127966/2 "2025-04-11T09:30:27Z")

</div>

Hi @aaspeel, welcome to the forum 😄 you’ve followed the guidelines perfectly.

I don’t know the answer, but @blegat can help.

---

<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:** [April 15, 2025, 12:28pm UTC](https://discourse.julialang.org/t/when-adding-a-sumofsquares-constraint-is-it-possible-to-get-the-jump-variable-corresponding-to-the-gram-matrix/127966/3 "2025-04-15T12:28:54Z")

</div>

At the moment, getting this matrix of variables is unfortunately not supported. For future reference, I have opened an [issue](https://github.com/jump-dev/SumOfSquares.jl/issues/399) to track this feature.  
As a workaround, you can do

```julia
@variable(model, q, SOSPoly(monomials(x, 0:1)))
@constraint(model, p == q)
G = value_matrix(q)

```
