@@ -9,6 +9,53 @@ 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 (TT:: Type{<:AbstractTerm{T}} , m:: AbstractMonomialLike ) where T
40+ return convert (TT, one (T) * m)
41+ end
42+ function Base. convert (TT:: Type{<:AbstractTerm{T}} , t:: AbstractTerm ) where T
43+ return convert (TT, convert (T, coefficient (t)) * monomial (t))
44+ end
45+ function Base. convert (:: Type{T} , t:: T ) where T <: AbstractTerm
46+ return t
47+ end
48+
49+ function Base. convert (:: Type{T} , p:: AbstractPolynomial ) where T <: AbstractTermLike
50+ if iszero (nterms (p))
51+ convert (T, zeroterm (p))
52+ elseif isone (nterms (p))
53+ convert (T, leadingterm (p))
54+ else
55+ throw (InexactError (:convert , T, p))
56+ end
57+ end
58+
1259MA. scaling (p:: AbstractPolynomialLike{T} ) where {T} = convert (T, p)
1360Base. convert (:: Type{Any} , p:: APL ) = p
1461# Conversion polynomial -> scalar
@@ -24,14 +71,4 @@ function Base.convert(S::Type{<:Union{Number, T}}, p::APL{T}) where T
2471 s
2572end
2673
27- # Fix ambiguity caused by above conversions
28- Base. convert (:: Type{P} , p:: APL ) where P<: APL = P (p)
29-
3074Base. 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