Skip to content

Commit 9380013

Browse files
committed
Fallback on conversion
1 parent f935957 commit 9380013

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

src/conversion.jl

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@ function Base.convert(::Type{P}, p::APL) where {T, P<:AbstractPolynomial{T}}
99
return convert(P, polynomial(p, T))
1010
end
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+
1249
MA.scaling(p::AbstractPolynomialLike{T}) where {T} = convert(T, p)
1350
Base.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
2562
end
2663

27-
# Fix ambiguity caused by above conversions
28-
Base.convert(::Type{P}, p::APL) where P<:APL = P(p)
29-
3064
Base.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

src/term.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@ export constantterm, term, termtype, zeroterm, coefficient, monomial
44
term(p::AbstractPolynomialLike)
55
66
Converts the polynomial `p` to a term.
7-
When applied on a polynomial, it throws an error if it has more than one term.
7+
When applied on a polynomial, it throws an `InexactError` if it has more than one term.
88
When applied to a term, it is the identity and does not copy it.
99
When applied to a monomial, it create a term of type `AbstractTerm{Int}`.
1010
"""
11-
function term(p::APL{T}) where T
12-
if nterms(p) == 0
13-
zeroterm(p)
14-
elseif nterms(p) > 1
15-
error("A polynomial is more than one term cannot be converted to a term.")
16-
else
17-
leadingterm(p)
18-
end
19-
end
20-
term(t::AbstractTerm) = t
21-
term(m::AbstractMonomialLike) = 1 * m
11+
term(p::APL) = convert(termtype(p), p)
2212

2313
"""
2414
termtype(p::AbstractPolynomialLike)

src/variable.jl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,19 @@ function variable_union_type end
2121
"""
2222
variable(p::AbstractPolynomialLike)
2323
24-
Converts `p` to a variable. Throws an error if it is not possible.
24+
Converts `p` to a variable. Throws `InexactError` if it is not possible.
2525
2626
### Examples
2727
2828
Calling `variable(x^2 + x - x^2)` should return the variable `x` and
2929
calling `variable(1.0y)` should return the variable `y` however calling
30-
`variable(2x)` or `variable(x + y)` should throw an error.
30+
`variable(2x)` or `variable(x + y)` should throw `InexactError`.
3131
3232
### Note
3333
3434
This operation is not type stable for the TypedPolynomials implementation if `nvariables(p) > 1` but is type stable for DynamicPolynomials.
3535
"""
36-
variable(m::AbstractMonomialLike) = _mono2var(powers(m)...)
37-
variable(v::AbstractVariable) = v
38-
function variable(t::AbstractTermLike)
39-
if isone(coefficient(t))
40-
variable(monomial(t))
41-
else
42-
error("A term with non-one coefficient cannot be converted into a variable")
43-
end
44-
end
45-
variable(p::APL) = variable(term(p))
36+
variable(t::APL) = convert(variable_union_type(t), t)
4637

4738
_errormono2var() = error("Monomial cannot be converted to a variable")
4839
_mono2var() = _errormono2var()

test/monomial.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const MP = MultivariatePolynomials
2525
@test degree(x * y[2]^2, y[1]) == 0
2626
@test degree(x * y[2]^2, y[2]) == 2
2727

28-
@test_throws ErrorException variable(x^2)
29-
@test_throws ErrorException variable(x*y[1])
30-
@test_throws ErrorException variable(constantmonomial(typeof(x)))
28+
@test_throws InexactError variable(x^2)
29+
@test_throws InexactError variable(x*y[1])
30+
@test_throws InexactError variable(constantmonomial(typeof(x)))
3131

3232
@test x != constantmonomial(typeof(x))
3333
@test constantmonomial(typeof(x)) != x
@@ -46,7 +46,7 @@ const MP = MultivariatePolynomials
4646
@test variable(x^2 + x - x^2) isa AbstractVariable
4747
@test variable(1.0x) == x
4848
@test variable(1.0x) isa AbstractVariable
49-
@test_throws ErrorException variable(x + 2x) == x
49+
@test_throws InexactError variable(x + 2x) == x
5050

5151
@test monic(x^2) == x^2
5252

test/polynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const MP = MultivariatePolynomials
3737
@test term(x + x^2 - x) == x^2
3838
@test term(x - x) isa AbstractTerm
3939
@test iszero(term(x - x))
40-
@test_throws ErrorException term(x + x^2)
40+
@test_throws InexactError term(x + x^2)
4141

4242
Mod.@polyvar y
4343

test/term.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
@test (@inferred polynomial(t, Float64)) isa AbstractPolynomial{Float64}
4848

4949
@test_throws InexactError push!([1], 2x)
50-
@test_throws ErrorException push!([x^2], 2x)
50+
@test_throws InexactError push!([x^2], 2x)
5151

5252

5353
@testset "Effective variables" begin

0 commit comments

Comments
 (0)