Skip to content

Commit f4adf4e

Browse files
committed
Fix leading for zero polynomial
1 parent 088305f commit f4adf4e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/polynomial.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ Returns the coefficient of the leading term, i.e. `first(terms(p))`.
251251
Calling `leadingterm` on ``4x^2y + xy + 2x`` should return ``4x^2y``.
252252
"""
253253
function leadingterm(p::AbstractPolynomialLike)
254-
first(terms(p))
254+
if iszero(p)
255+
zeroterm(p)
256+
else
257+
first(terms(p))
258+
end
255259
end
256260
leadingterm(t::AbstractTermLike) = term(t)
257261

test/monovec.jl

+4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
@test x > y
44
@test x^2 > y^2
55
X = [x^2, x*y, y^2]
6+
#@test isempty(@inferred monomials((x, y), 1:0))
67
for (i, m) in enumerate(monomials((x, y), 2))
78
@test m == X[i]
89
end
10+
for (i, m) in enumerate(monomials((x, y), 2:2))
11+
@test m == X[i]
12+
end
913
X = [x^2, y^2]
1014
for (i, m) in enumerate(monomials((x, y), 2, m -> m != x*y))
1115
@test m == X[i]

test/polynomial.jl

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
@test polynomial(1 + x) == 1 + x
99
@test leadingterm(1 + x) == x
10+
@test leadingterm(x - x) == 0
11+
@test leadingmonomial(x - x) == 1
12+
@test leadingcoefficient(x - x) == 0
1013
@test one(1 + x) == one(1.0 + x) == 1
1114
@test zero(1 + x) == zero(1.0 + x) == 0
1215
@test 1 != 1 + x

0 commit comments

Comments
 (0)