Skip to content

Commit d656a46

Browse files
committed
Add useful fallbacks
1 parent 9380013 commit d656a46

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/conversion.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ function Base.convert(::Type{M}, t::AbstractTerm) where M <: AbstractMonomialLik
3636
throw(InexactError(:convert, M, t))
3737
end
3838
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+
3949
function Base.convert(::Type{T}, p::AbstractPolynomial) where T <: AbstractTermLike
4050
if iszero(nterms(p))
4151
convert(T, zeroterm(p))

src/monomial.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Note that the variables of `m` does not necessarily have nonzero degree.
2525
For instance, `variables([x^2*y, y*z][1])` is usually `(x, y, z)` since the two monomials have been promoted to a common type.
2626
"""
2727
function variables end
28+
variables(t::AbstractTerm) = variables(monomial(t))
2829

2930
"""
3031
nvariables(p::AbstractPolynomialLike)
@@ -36,6 +37,7 @@ Returns the number of variables in `p`, i.e. `length(variables(p))`. It could be
3637
Calling `nvariables(x^2*y)` should return at least 2 and calling `nvariables(x)` should return at least 1.
3738
"""
3839
nvariables(::Union{AbstractVariable, Type{<:AbstractVariable}}) = 1
40+
nvariables(p::APL) = length(variables(p))
3941

4042
"""
4143
exponents(t::AbstractTermLike)

src/promote.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Base.promote_rule(::Type{RS}, ::Type{RT}) where {RS<:RationalPoly, RT<:RationalP
1515
Base.promote_rule(::Type{PT}, ::Type{RT}) where {PT<:APL, RT<:RationalPoly} = promote_rule_rational(PT, RT)
1616
Base.promote_rule(::Type{RT}, ::Type{PT}) where {PT<:APL, RT<:RationalPoly} = promote_rule_rational(PT, RT)
1717

18+
# Promotion with Term
19+
function Base.promote_rule(ST::Type{<:AbstractTermLike{S}}, TT::Type{<:AbstractTerm{T}}) where {S, T}
20+
U = promote_type(S, T)
21+
UT = termtype(ST, U)
22+
if UT != termtype(TT, U)
23+
error("Cannot promote `$ST` and `$TT` to the same type.")
24+
end
25+
return UT
26+
end
27+
1828
#promote_rule(::Type{Term{C, U}}, ::Type{RationalPoly{C, S, T}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}
1929
#promote_rule(::Type{RationalPoly{C, S, T}}, ::Type{Term{C, U}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}
2030
#promote_rule(::Type{Polynomial{C, U}}, ::Type{RationalPoly{C, S, T}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}

src/term.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Calling `coefficient` on ``4x^2y`` should return ``4``.
5151
Calling `coefficient(2x + 4y^2 + 3, y^2)` should return ``4``.
5252
Calling `coefficient(2x + 4y^2 + 3, x^2)` should return ``0``.
5353
"""
54+
function coefficient end
55+
coefficient(t::AbstractTerm) = t.coefficient # by convention, the field should be `coefficient`
5456
coefficient(m::AbstractMonomialLike) = 1
5557
function coefficient(p::AbstractPolynomialLike{T}, m::AbstractMonomialLike) where T
5658
for t in terms(p)
@@ -116,6 +118,7 @@ Returns the monomial of the term `t`.
116118
Calling `monomial` on ``4x^2y`` should return ``x^2y``.
117119
"""
118120
function monomial end
121+
monomial(t::AbstractTerm) = t.monomial # by convention, the field should be `monomial`.
119122
monomial(m::AbstractMonomial) = m
120123

121124
"""

src/variable.jl

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,6 @@ This operation is not type stable for the TypedPolynomials implementation if `nv
3535
"""
3636
variable(t::APL) = convert(variable_union_type(t), t)
3737

38-
_errormono2var() = error("Monomial cannot be converted to a variable")
39-
_mono2var() = _errormono2var()
40-
function _checknovar() end
41-
function _checknovar(ve, ves...)
42-
if iszero(ve[2])
43-
_checknovar(ves...)
44-
else
45-
_errormono2var()
46-
end
47-
end
48-
function _mono2var(ve, ves...)
49-
if iszero(ve[2])
50-
_mono2var(ves...)
51-
elseif isone(ve[2])
52-
_checknovar(ves...)
53-
ve[1]
54-
else
55-
_errormono2var()
56-
end
57-
end
58-
5938
"""
6039
name(v::AbstractVariable)::AbstractString
6140

test/commutativetests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include("mutable_arithmetics.jl")
1+
#include("mutable_arithmetics.jl")
22

33
include("zip.jl")
44
include("variable.jl")

0 commit comments

Comments
 (0)