TypedPolynomials.jl icon indicating copy to clipboard operation
TypedPolynomials.jl copied to clipboard

Override dot with scalars

Open matbesancon opened this issue 4 years ago • 1 comments

@polyvar X[1:N]
current = 1
xs = [xi * c for xi in x for c in current]
10-element Vector{Term{Int64, M} where M<:Monomial}:
 X₁
 X₂
 X₃
 X₄
 ⋮
 X₈
 X₉
 X₁₀

julia> dot(xs, rand(10))
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T at missing.jl:105
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:53
  zero(::Union{Monomial, Polynomial, Term, Variable}) at /home/mbesancon/.julia/packages/TypedPolynomials/0HQcW/src/operators.jl:5
  ...
Stacktrace:
 [1] zero(#unused#::Type{Any})
   @ Base ./missing.jl:106
 [2] zero(#unused#::Type{Term{Int64, M} where M<:Monomial})
   @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/bsuXE/src/term.jl:152
 [3] promote_operation(op::typeof(*), #unused#::Type{Term{Int64, M} where M<:Monomial}, #unused#::Type{Float64})
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/bPWR4/src/interface.jl:24
 [4] promote_sum_mul(T::Type, S::Type)
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/bPWR4/src/linear_algebra.jl:169
 [5] operate(#unused#::typeof(dot), x::Vector{Term{Int64, M} where M<:Monomial}, y::Vector{Float64})
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/bPWR4/src/linear_algebra.jl:430
 [6] dot(lhs::Vector{Term{Int64, M} where M<:Monomial}, rhs::Vector{Float64})
   @ MutableArithmetics ~/.julia/packages/MutableArithmetics/bPWR4/src/dispatch.jl:13

matbesancon avatar Feb 26 '21 21:02 matbesancon

The issue is that the eltype of xs is not concrete so zero does not work. This is because the vector comprehension widen the eltype instead of promoting the elements to a common type. Anotating the eltype to the promoted type should make it work

blegat avatar Feb 27 '21 13:02 blegat