Skip to content

Commit aa58e7b

Browse files
committed
Fix differentiation of zero polynomial
1 parent 9a0f7bf commit aa58e7b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/differentiation.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ differentiate(α::T, v::AbstractVariable) where T = zero(T)
3737
differentiate(v1::AbstractVariable, v2::AbstractVariable) = v1 == v2 ? 1 : 0
3838
differentiate(t::AbstractTermLike, v::AbstractVariable) = coefficient(t) * differentiate(monomial(t), v)
3939
# The polynomial function will take care of removing the zeros
40-
differentiate(p::APL, v::AbstractVariable) = polynomial!(differentiate.(terms(p), v), SortedState())
40+
function differentiate(p::APL, v::AbstractVariable)
41+
if iszero(p)
42+
# As `terms(p)` is empty, `differentiate.(terms(p), v)` gives `Any[]`.
43+
T = typeof(differentiate(one(termtype(p)), v))
44+
polynomial!(T[], SortedUniqState())
45+
else
46+
polynomial!(differentiate.(terms(p), v), SortedState())
47+
end
48+
end
4149
differentiate(p::RationalPoly, v::AbstractVariable) = (differentiate(p.num, v) * p.den - p.num * differentiate(p.den, v)) / p.den^2
4250

4351
const ARPL = Union{APL, RationalPoly}

test/differentiation.jl

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
@test differentiate(f, [x, y, z]) == [2x 1 0; 4 0 2z]
3939
@test differentiate(f, (x, y, z)) == [2x 1 0; 4 0 2z]
4040

41+
@testset "Differentiate empty polynomial" begin
42+
p = x^0 - 1
43+
@test iszero(@inferred differentiate(p, x))
44+
@test all(iszero, @inferred differentiate(p, [x, y]))
45+
end
46+
4147
@testset "differentiation with Val{}" begin
4248
@test @inferred(differentiate(x, x, Val{0}())) == x
4349
@test @inferred(differentiate(x, x, Val{1}())) == 1

0 commit comments

Comments
 (0)