Skip to content

Commit

Permalink
remove ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jul 18, 2022
1 parent 089d03b commit a7b4cc2
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion src/polynomials/ChebyshevT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/polynomials/ImmutablePolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/polynomials/LaurentPolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
##
Expand Down
2 changes: 1 addition & 1 deletion src/polynomials/factored_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/polynomials/mutable-arithmetics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/polynomials/standard-basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))



Expand Down
1 change: 1 addition & 0 deletions src/rational-functions/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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<:Polynomials.AbstractRationalFunction, P<:(Polynomials.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}}
Expand Down

0 comments on commit a7b4cc2

Please sign in to comment.