@@ -9,6 +9,43 @@ function Base.convert(::Type{P}, p::APL) where {T, P<:AbstractPolynomial{T}}
99 return convert (P, polynomial (p, T))
1010end
1111
12+ function Base. convert (:: Type{V} , mono:: AbstractMonomial ) where V <: AbstractVariable
13+ variable = nothing
14+ for v in variables (mono)
15+ d = degree (mono, v)
16+ if isone (d)
17+ if variable === nothing
18+ variable = v
19+ else
20+ throw (InexactError (:convert , V, mono))
21+ end
22+ elseif ! iszero (d)
23+ throw (InexactError (:convert , V, mono))
24+ end
25+ end
26+ if variable === nothing
27+ throw (InexactError (:convert , V, mono))
28+ end
29+ return variable
30+ end
31+
32+ function Base. convert (:: Type{M} , t:: AbstractTerm ) where M <: AbstractMonomialLike
33+ if isone (coefficient (t))
34+ return convert (M, monomial (t))
35+ else
36+ throw (InexactError (:convert , M, t))
37+ end
38+ end
39+ function Base. convert (:: Type{T} , p:: AbstractPolynomial ) where T <: AbstractTermLike
40+ if iszero (nterms (p))
41+ convert (T, zeroterm (p))
42+ elseif isone (nterms (p))
43+ convert (T, leadingterm (p))
44+ else
45+ throw (InexactError (:convert , T, p))
46+ end
47+ end
48+
1249MA. scaling (p:: AbstractPolynomialLike{T} ) where {T} = convert (T, p)
1350Base. convert (:: Type{Any} , p:: APL ) = p
1451# Conversion polynomial -> scalar
@@ -24,14 +61,4 @@ function Base.convert(S::Type{<:Union{Number, T}}, p::APL{T}) where T
2461 s
2562end
2663
27- # Fix ambiguity caused by above conversions
28- Base. convert (:: Type{P} , p:: APL ) where P<: APL = P (p)
29-
3064Base. convert (:: Type{PT} , p:: PT ) where {PT<: APL } = p
31- function Base. convert (:: Type{MT} , t:: AbstractTerm ) where {MT<: AbstractMonomial }
32- if isone (coefficient (t))
33- monomial (t)
34- else
35- error (" Cannot convert a term with a coefficient that is not one into a monomial" )
36- end
37- end
0 commit comments