jump-dev / SumOfSquares.jl

Sum of Squares Programming for Julia
Other
115 stars 24 forks source link

Inconsistent behavior of Poly with DynamicPolynomials and TypedPolynomials. #255

Open hurak opened 2 years ago

hurak commented 2 years ago

I am trying to reproduce the example on Lyapunov function search from the documentation. The example uses DynamicPolynomials. Since the degree of the polynomials is fixed here, my understanding was that I can freely switch to TypedPolynomials. But then the example fails. Below is the snippet of code relevant to reproduce the failure. First, with DynamicPolynomials:

julia> using DynamicPolynomials

julia> @polyvar x[1:3]
(PolyVar{true}[x₁, x₂, x₃],)

julia> using SumOfSquares

julia> monosX = x.^2
3-element Vector{Monomial{true}}:
 x₁²
 x₂²
 x₃²

julia> Poly(monosX)
Poly{MonomialBasis{Monomial{true}, Vector{Monomial{true}}}}(MonomialBasis{Monomial{true}, Vector{Monomial{true}}}(Monomial{true}[x₁², x₂², x₃²]))

And now (after restarting the REPL) the same code with TypedPolynomials:

julia> using TypedPolynomials

julia> @polyvar y[1:3]
(y₁, y₂, y₃)

julia> using SumOfSquares

julia> monosY = y.^2
(y₁², y₂², y₃²)

julia> Poly(monosY)
ERROR: MethodError: no method matching Poly(::Tuple{Monomial{(y₁,), 1}, Monomial{(y₂,), 1}, Monomial{(y₃,), 1}})
Closest candidates are:
  Poly(::AbstractVector{<:MultivariatePolynomials.AbstractPolynomialLike}) at ~/.julia/packages/PolyJuMP/pghif/src/variable.jl:36
  Poly(::PB) where PB<:AbstractPolynomialBasis at ~/.julia/packages/PolyJuMP/pghif/src/variable.jl:34
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

Is this a feature or a bug? I confess I still do not get the difference between the two polynomial types perfectly.

blegat commented 1 year ago

Sorry for the late reply, I missed the notification. You need to give a vector while here you are giving a tuple.

monosY = [v^2 for v in y]

should fix it. Let's leave this issue open, we could add support for tuple input to ensure consistency.