Skip to content

Commit 95dc00a

Browse files
authored
Merge pull request #83 from JuliaAlgebra/bl/fix82
Defines oneunit for AbstractVariable
2 parents b2a29d3 + 4059a7b commit 95dc00a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/monomial.jl

+8
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ mapexponents(f, m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponen
124124

125125
Base.one(::Type{TT}) where {TT<:AbstractMonomialLike} = constantmonomial(TT)
126126
Base.one(t::AbstractMonomialLike) = constantmonomial(t)
127+
# See https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/issues/82
128+
# By default, Base do oneunit(v::VT) = VT(one(v)).
129+
# This tries to convert a monomial to a variable which does not work.
130+
# The issue here is there is no way to represent the multiplicative identity
131+
# using the variable type. The best we can do is return a monomial even
132+
# if it does not exactly match the definition of oneunit.
133+
Base.oneunit(v::AbstractVariable) = one(v)
134+
Base.oneunit(VT::Type{<: AbstractVariable}) = one(VT)

test/variable.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import MultivariatePolynomials: AbstractVariable, similarvariable, @similarvaria
2020
@test iszero(zero(x))
2121
@test zero(x) isa AbstractPolynomial{Int}
2222
@inferred zero(x)
23-
@test one(x) == 1
24-
@test one(x) isa AbstractMonomial
25-
@inferred one(x)
2623

2724
typetests(x)
2825
@test (@inferred polynomial(x)) isa AbstractPolynomial{Int}
@@ -40,6 +37,18 @@ import MultivariatePolynomials: AbstractVariable, similarvariable, @similarvaria
4037

4138
@test divides(x, x) == true
4239
@test divides(x, y) == false
40+
41+
@testset "Issue #82" begin
42+
for v in (x, typeof(x))
43+
for fun in (oneunit, one)
44+
@test fun(v) == 1
45+
@test 1 == fun(v)
46+
#@test isone(fun(v)) # Enable when Julia v0.6 is dropped
47+
@test fun(v) isa AbstractMonomial
48+
@inferred fun(v)
49+
end
50+
end
51+
end
4352
end
4453
@testset "Create similar variable" begin
4554
Mod.@polyvar x y

0 commit comments

Comments
 (0)