# Monomials with variable of a model

**URL:** <https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 30, 2022, 12:56pm UTC](https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048 "2022-11-30T12:56:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![irezvyakova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/irezvyakova/32/39063_2.png) [@irezvyakova](https://discourse.julialang.org/u/irezvyakova)\
**Post date:** [November 30, 2022, 12:56pm UTC](https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048/1 "2022-11-30T12:56:04Z")

</div>

Does there exist a nice way to use a certain variable of a model (say,

model = Model(COSMO.Optimizer)  
@variable(model, u)  
@variable(model, t)

)  
to get a vector of monomials of [u,t] up to certain degree (say, 10) with some nice command like  
monomials([u,t], 0:10)?

The error I get is “no method matching monomials(::VariableRef, :: Vector{int64}) Closest candidates are monomials(::MultivariatePolynomials.AbstractVariable, ::Any…)

It would be nice to have such a short command and not to write a vector of monomials by induction (since I have more than 2 variables in monomial vector).

Moreover, if I already have a vector of polynomials with the variable “u” of the model (say,  
p =Array{Polynomial}(undef, 2)) and if I want to write a product p\*p’_A, where A is a matrix 2_2 and A is matrix variable of a model (@variable, A[1:2, 1:2], PSD), then I get an error: no method matching \*(::AffExpr, ::Polynomial{Float64}).

All in all, the only thing I need is to put into constraint of the model an expression which is the trace of the product of a matrix which is a variable of the model by the matrix which is obtained from monomial or polynomial vector of a variable “u” of the model.

---

<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:** [November 30, 2022, 8:10pm UTC](https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048/2 "2022-11-30T20:10:39Z")

</div>

> Does there exist a nice way

Have you seen [Variables · SumOfSquares](https://jump.dev/SumOfSquares.jl/stable/variables/)?

If that isn’t what you want, and you want a vector of monomials of JuMP variables, like `[u, u^2, u^3, ...]`, use:

```julia
@NLexpression(model, [i=0:10], u^i)

```

But this will create a vector of nonlinear expressions, so you cannot use a conic solver like COSMO.

> All in all, the only thing I need

So to clarify, you want something like this?

```julia
using JuMP, LinearAlgebra
model = Model()
@variable(model, u)
@variable(model, A[1:2, 1:2], PSD)
p = [u, u^2]
@constraint(model, tr(p * p' * A) == 1)

```

---

<div class="post-metadata">

**Author:** ![irezvyakova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/irezvyakova/32/39063_2.png) [@irezvyakova](https://discourse.julialang.org/u/irezvyakova)\
**Post date:** [December 2, 2022, 12:49pm UTC](https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048/3 "2022-12-02T12:49:00Z")

</div>

Yes, I need smth. like this (but with much more variables ) :

using JuMP, LinearAlgebra  
model = Model()  
@variable(model, u)  
@variable(model, A[1:3, 1:3], PSD)  
p = [1, u, u^2]  
@constraint(model, tr(p \* p’ \* A) == 1)

---

<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:** [December 3, 2022, 3:13am UTC](https://discourse.julialang.org/t/monomials-with-variable-of-a-model/91048/4 "2022-12-03T03:13:38Z")

</div>

JuMP isn’t setup to solve problems like this. You _could_ write:

```julia
using JuMP
model = Model()
@variable(model, u)
@variable(model, A[1:3, 1:3], PSD)
@NLexpression(model, p[i=1:3], u^(i-1))
@NLconstraint(
    model, 
    sum(sum(p[i] * p[j] * A[j, i] for j in 1:3) for i in 1:3) == 1
)

```

but now you’ll need a solver that supports PSD constraints and nonlinear equality constraints.

You could get rid of the explicit PSD constraint using Cholesky:

```julia
using JuMP, LinearAlgebra
model = Model()
@variable(model, u)
@variable(model, L[1:3, 1:3], Symmetric)
A = Matrix(LowerTriangular(L)) * Matrix(LowerTriangular(L))'
@NLexpression(model, p[i=1:3], u^(i-1))
@NLconstraint(
    model, 
    sum(p[i] * sum(p[j] * A[j, i] for j in 1:3) for i in 1:3) == 1
)

```

but this will be really hard to solve.

Where does the problem come from? How do other people solve it?
