diff --git a/src/common.jl b/src/common.jl index 2bdba7c4..f753eabd 100644 --- a/src/common.jl +++ b/src/common.jl @@ -851,6 +851,7 @@ function Base.:+(p::P, c::S) where {T,X, P<:AbstractPolynomial{T,X}, S} q + R(c) end + # polynomial + polynomial when different types function Base.:+(p::P, q::Q) where {T,X,P <: AbstractPolynomial{T,X}, S,Y,Q <: AbstractPolynomial{S,Y}} isconstant(p) && return constantterm(p) + q @@ -944,6 +945,12 @@ function scalar_mult(c::S, p::P) where {S, T, X, P<:AbstractPolynomial{T, X}} 𝐏([c * pᵢ for pᵢ ∈ coeffs(p)]) end +scalar_mult(c::S, p::Union{P, R}) where { + S<: AbstractPolynomial, + T, X, + P<:AbstractPolynomial{T, X}, + R <:AbstractPolynomial{T} +} = throw(DomainError()) # avoid ambiguity, issue #435 function Base.:/(p::P, c::S) where {P <: AbstractPolynomial,S} _convert(p, coeffs(p) ./ c) @@ -1082,6 +1089,11 @@ function Base.isapprox(p1::AbstractPolynomial{T}, return isapprox(p1, _convert(p1, [n])) end +Base.isapprox(::AbstractPolynomial{T}, ::Missing, args...; kwargs...) where T = + missing +Base.isapprox(::Missing, ::AbstractPolynomial{T}, args...; kwargs...) where T = + missing + Base.isapprox(n::S, p1::AbstractPolynomial{T}; rtol::Real = (Base.rtoldefault(T, S, 0)), diff --git a/src/polynomials/ChebyshevT.jl b/src/polynomials/ChebyshevT.jl index c80d688d..52ff818a 100644 --- a/src/polynomials/ChebyshevT.jl +++ b/src/polynomials/ChebyshevT.jl @@ -222,7 +222,8 @@ function Base.:+(p::ChebyshevT{T,X}, c::S) where {T,X, S<:Number} cs[1] += c ChebyshevT{R,X}(cs) end -function Base.:+(p::P, c::T) where {T,X,P<:ChebyshevT{T,X}} + +function Base.:+(p::P, c::T) where {T <: Number,X,P<:ChebyshevT{T,X}} cs = collect(T, values(p)) cs[1] += c P(cs) diff --git a/src/polynomials/ImmutablePolynomial.jl b/src/polynomials/ImmutablePolynomial.jl index c9c68f51..874268e9 100644 --- a/src/polynomials/ImmutablePolynomial.jl +++ b/src/polynomials/ImmutablePolynomial.jl @@ -203,7 +203,7 @@ function scalar_mult(c::S, p::ImmutablePolynomial{T,X,N}) where {T, X,N, S <: Nu return ImmutablePolynomial(cs, X) end -function Base.:/(p::ImmutablePolynomial{T,X,N}, c::S) where {T,X,N,S} +function Base.:/(p::ImmutablePolynomial{T,X,N}, c::S) where {T,X,N,S<:Number} R = eltype(one(T)/one(S)) P = ImmutablePolynomial{R,X} (N == 0 || isinf(c)) && return zero(P) diff --git a/src/polynomials/LaurentPolynomial.jl b/src/polynomials/LaurentPolynomial.jl index 2137fac4..e009280a 100644 --- a/src/polynomials/LaurentPolynomial.jl +++ b/src/polynomials/LaurentPolynomial.jl @@ -151,10 +151,15 @@ function Base.convert(::Type{P}, q::StandardBasisPolynomial{S}) where {P <:Laure ⟒(P){T,X}([q[i] for i in eachindex(q)], firstindex(q)) end -function Base.convert(::Type{P}, q::AbstractPolynomial) where {P <:LaurentPolynomial} +function Base.convert(::Type{P}, q::AbstractPolynomial{T,X}) where {T,X,P <:LaurentPolynomial} convert(P, convert(Polynomial, q)) end +# Ambiguity, issue #435 +function Base.convert(𝑷::Type{P}, p::ArnoldiFit{T, M, X}) where {P<:LaurentPolynomial, T, M, X} + convert(𝑷, convert(Polynomial, p)) +end + ## ## generic functions ## diff --git a/src/polynomials/factored_polynomial.jl b/src/polynomials/factored_polynomial.jl index 38915b4a..7110e9b9 100644 --- a/src/polynomials/factored_polynomial.jl +++ b/src/polynomials/factored_polynomial.jl @@ -273,7 +273,7 @@ function scalar_mult(p::P, c::S) where {S<:Number, T, X, P <: FactoredPolynomial end # scalar division -function Base.:/(p::P, c::S) where {S, T, X, P <: FactoredPolynomial{T, X}} +function Base.:/(p::P, c::S) where {S<:Number, T, X, P <: FactoredPolynomial{T, X}} p * (1/c) end diff --git a/src/polynomials/mutable-arithmetics.jl b/src/polynomials/mutable-arithmetics.jl index c39527b9..0e106d9c 100644 --- a/src/polynomials/mutable-arithmetics.jl +++ b/src/polynomials/mutable-arithmetics.jl @@ -65,3 +65,7 @@ macro register_mutable_arithmetic(name) end end end + +## Ambiguities. Issue #435 +Base.:+(p::P, ::MutableArithmetics.Zero) where {T, X, P<:AbstractPolynomial{T, X}} = p +Base.:+(p::P, ::T) where {T<:MutableArithmetics.Zero, P<:StandardBasisPolynomial{T}} = p diff --git a/src/polynomials/standard-basis.jl b/src/polynomials/standard-basis.jl index 8bcd2e58..1ffaa70d 100644 --- a/src/polynomials/standard-basis.jl +++ b/src/polynomials/standard-basis.jl @@ -644,7 +644,7 @@ evalpoly(x, p::ArnoldiFit) = polyvalA(p.coeffs, p.H, x) fit(::Type{ArnoldiFit}, x::AbstractVector{T}, y::AbstractVector{T}, deg::Int=length(x)-1; var=:x, kwargs...) where{T} = polyfitA(x, y, deg; var=var) -Base.convert(::Type{P}, p::ArnoldiFit) where {P <: AbstractPolynomial} = p(variable(P,indeterminate(p))) +Base.convert(::Type{P}, p::ArnoldiFit{T,M,X}) where {P <: AbstractPolynomial,T,M,X} = p(variable(P,indeterminate(p))) diff --git a/src/rational-functions/common.jl b/src/rational-functions/common.jl index 580400ab..f92cdc80 100644 --- a/src/rational-functions/common.jl +++ b/src/rational-functions/common.jl @@ -298,6 +298,7 @@ Base.:+(p::Number, q::AbstractRationalFunction) = q + p Base.:+(p::AbstractRationalFunction, q::Number) = p + q*one(p) Base.:+(p::AbstractPolynomial, q::AbstractRationalFunction) = q + p Base.:+(p::AbstractRationalFunction, q::AbstractPolynomial) = p + (q//one(q)) +Base.:+(p::P, q::T) where {T<: AbstractRationalFunction, P<:StandardBasisPolynomial{T}} = throw(DomainError()) # avoid ambiguity (issue #435. Base.:+(p::AbstractRationalFunction, q::AbstractRationalFunction) = sum(promote(p,q)) # type should implement this function Base.:+(p::R, q::R) where {T,X,P,R <: AbstractRationalFunction{T,X,P}}