Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when creating an array with Monomial and other objects #169

Open
shashi opened this issue Aug 5, 2021 · 3 comments
Open

Error when creating an array with Monomial and other objects #169

shashi opened this issue Aug 5, 2021 · 3 comments

Comments

@shashi
Copy link

shashi commented Aug 5, 2021

using SymbolicUtils

julia> @syms(a); b = SymbolicUtils.to_mpoly(a^2)
(a², OrderedCollections.OrderedDict{Sym, Any}(a => a))

julia> c = b[1]
a²

julia> typeof(c)
DynamicPolynomials.Monomial{true}

julia> [c, a]
ERROR: MethodError: no method matching one(::Type{Any})
Closest candidates are:
  one(::Type{Union{Missing, T}}) where T at missing.jl:105
  one(::Union{Type{T}, T}) where T<:AbstractString at strings/basic.jl:262
  one(::Union{Type{P}, P}) where P<:Dates.Period at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:54
  ...
Stacktrace:
 [1] one(#unused#::Type{Any})
   @ Base ./missing.jl:106
 [2] convert(#unused#::Type{DynamicPolynomials.Term{true, Any}}, x::DynamicPolynomials.Monomial{true})
   @ DynamicPolynomials ~/.julia/packages/DynamicPolynomials/5p8Ga/src/term.jl:18
 [3] setindex!(A::Vector{DynamicPolynomials.Term{true, Any}}, x::DynamicPolynomials.Monomial{true}, i1::Int64)
   @ Base ./array.jl:839
 [4] copyto!(dest::Vector{DynamicPolynomials.Term{true, Any}}, src::Tuple{DynamicPolynomials.Monomial{true}, Sym{Number, Nothing}})
   @ Base ./abstractarray.jl:840
 [5] vect(::DynamicPolynomials.Monomial{true}, ::Vararg{Any, N} where N)
   @ Base ./array.jl:129
 [6] top-level scope
   @ REPL[193]:1
@blegat
Copy link
Member

blegat commented Aug 5, 2021

In MultivariatePolynomials, everything that is not a AbstractPolynomialLike is considered a constant/coefficient.
So here, MultivariatePolynomials tries to create a vector with terms for which the coefficients are symbolic expressions.
The issue is that:

julia> typeof(a)
SymbolicUtils.Sym{Number, Nothing}

julia> promote_type(Int, typeof(a))
Any

So we try instead to create an array of terms with coefficients Any.
Would it make sense for promote_type(Int, typeof(a)) to be a symbolic expression instead ?

@shashi
Copy link
Author

shashi commented Aug 10, 2021

Would it make sense for promote_type(Int, typeof(a)) to be a symbolic expression instead ?

No I don't think so, the correct answer is Any here imo because 1 and Sym are different types with only Any as a common ancestor.

In MultivariatePolynomials, everything that is not a AbstractPolynomialLike is considered a constant/coefficient.

I think this is a completely fine behavior when it's actually dealing with these terms, but here we are trying to build a plain julia vector of objects, so I feel the reasoning that anything else should be treated as a coefficient should not be leaking outside the package. Not surprisingly it creates surprising problems. It could even happen in packages like Folds or something, which just deal with collections of objects and expect things like vect to just work.

@shashi
Copy link
Author

shashi commented Aug 10, 2021

The problem boils down to:

p = some polynomial
@syms a b
julia> Base.promote_typeof(p, a*b)
DynamicPolynomials.Polynomial{true, Any}

It should not give a type for which convert is not defined. It should just go Any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants