JuliaAlgebra / TypedPolynomials.jl

MultivariatePolynomials implementation using typed variables in Julia
Other
25 stars 6 forks source link

TypedPolynomials do not work with ImplicitPlots.jl #87

Open hurak opened 9 months ago

hurak commented 9 months ago

Plotting the solution set of $h(x)=0$ where the function $h(x)$ is a TypedPolynomial does not work with ImplicitPlots.jl:

julia> using Plots, ImplicitPlots

julia> using TypedPolynomials

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

julia> h = -(y[1]+1)^2 - (y[2]+1)^2 + 1
-1 - 2y₂ - 2y₁ - y₂² - y₁²

julia> implicit_plot!(h; xlims=(-2.5, 2.5), ylims=(-2.5, 2.5))
ERROR: MethodError: no method matching StaticPolynomials.Polynomial(::Vector{Int64}, ::Matrix{Int64}, ::Tuple{Symbol, Symbol}, ::Nothing, ::Nothing)

Closest candidates are:
  StaticPolynomials.Polynomial(::Vector, ::Matrix{<:Integer}, ::Vector{Symbol}, ::Union{Nothing, Matrix{<:Integer}}, ::Any)
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:63
  StaticPolynomials.Polynomial(::Vector{T}, ::StaticPolynomials.SExponents, ::Any, ::Union{Nothing, StaticPolynomials.SExponents}, ::Any, ::Vector{Int64}) where T
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:26
  StaticPolynomials.Polynomial(::Vector, ::Matrix{<:Integer}, ::Vector{Symbol}, ::Union{Nothing, Matrix{<:Integer}})
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:63
  ...

Stacktrace:
 [1] StaticPolynomials.Polynomial(p::VectorPolynomial{Int64, MultivariatePolynomials.Term{Int64, Monomial{(y₁, y₂), 2}}}, vars::Tuple{Variable{Symbol("y[1]")}, Variable{Symbol("y[2]")}}, params::Nothing)
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:58
 [2] StaticPolynomials.Polynomial(p::VectorPolynomial{Int64, MultivariatePolynomials.Term{Int64, Monomial{(y₁, y₂), 2}}}; parameters::Nothing, variables::Tuple{Variable{Symbol("y[1]")}, Variable{Symbol("y[2]")}})
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:32
 [3] StaticPolynomials.Polynomial(p::VectorPolynomial{Int64, MultivariatePolynomials.Term{Int64, Monomial{(y₁, y₂), 2}}})
   @ StaticPolynomials ~/.julia/packages/StaticPolynomials/gnM4J/src/polynomial.jl:31
 [4] ImplicitFunction(f::VectorPolynomial{Int64, MultivariatePolynomials.Term{Int64, Monomial{(y₁, y₂), 2}}})
   @ ImplicitPlots ~/.julia/packages/ImplicitPlots/mxASJ/src/ImplicitPlots.jl:24
 [5] implicit_plot!(f::VectorPolynomial{Int64, MultivariatePolynomials.Term{Int64, Monomial{(y₁, y₂), 2}}}; kwargs::Base.Pairs{Symbol, Tuple{Float64, Float64}, Tuple{Symbol, Symbol}, NamedTuple{(:xlims, :ylims), Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}}})
   @ ImplicitPlots ~/.julia/packages/ImplicitPlots/mxASJ/src/ImplicitPlots.jl:83
 [6] top-level scope
   @ REPL[5]:1

Maybe it does not even classify as an issue since such functionality was not promised anywhere, but the fact is, that the DynamicPolynomials.jl do work fine with ImplicitPlots.jl. And it is both documented as an example at the bottom of https://github.com/saschatimme/ImplicitPlots.jl and it is also relied upon in the example on barrier functions at https://jump.dev/SumOfSquares.jl/dev/generated/Systems%20and%20Control/barrier_certificate/. Now, when a user is introduced to multivariate polynomials in Julia, the message that they receive that DynamicPolynomial and TypedPolynomial types are more or less equivalent. Therefore it may easily happen that they may want to pick arbitrarily the latter (after all its README page states "Handling variables at the type level makes constructing variables, monomials, and terms more efficient than with DynamicPolynomials.jl") and then face the troubles that (implicit) plotting does not work.

blegat commented 9 months ago

I agree it's worth investigating and fixing. Maybe StaticPolynomials is doing something that's not part of the MultivariatePolynomials API ?