Skip to content

Commit 1abd172

Browse files
committed
derivative must always return Constant or Expression! fix #545
1 parent b45615f commit 1abd172

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/differentials.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ for (modu, fun, arity) ∈ DiffRules.diffrules()
192192
end
193193
end
194194

195-
derivative(::typeof(+), args::NTuple{N,Any}, ::Val) where {N} = 1
195+
derivative(::typeof(+), args::NTuple{N,Any}, ::Val) where {N} = Constant(1)
196+
derivative(::typeof(-), args::NTuple{2,Any}, ::Val{1}) = Constant(1)
197+
derivative(::typeof(-), args::NTuple{2,Any}, ::Val{2}) = Constant(-1)
196198
derivative(::typeof(*), args::NTuple{N,Any}, ::Val{i}) where {N,i} = make_operation(*, deleteat!(collect(args), i))
197-
derivative(::typeof(one), args::Tuple{<:Any}, ::Val) = 0
199+
derivative(::typeof(one), args::Tuple{<:Any}, ::Val) = Constant(0)
198200

199201
function count_order(x)
200202
@assert !(x isa Symbol) "The variable $x must have an order of differentiation that is greater or equal to 1!"

test/derivatives.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ test_equal(jac[3,1], y)
5050
test_equal(jac[3,2], x)
5151
test_equal(jac[3,3], -1β)
5252

53+
# issue #545
54+
z = t + t^2
55+
test_equal(expand_derivatives(D(z)), 1 + t * 2)
56+
57+
z = t-2t
58+
test_equal(expand_derivatives(D(z)), -1)
59+
5360
# Variable dependence checking in differentiation
5461
@variables a(t) b(a)
5562
@test !isequal(D(b), 0)

0 commit comments

Comments
 (0)