Skip to content

Commit 3454522

Browse files
Merge pull request #548 from SciML/s/fix-deriv
Fix #545
2 parents b45615f + b01bc64 commit 3454522

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/differentials.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ for (modu, fun, arity) ∈ DiffRules.diffrules()
188188
else
189189
DiffRules.diffrule(modu, fun, ntuple(k->:(args[$k]), arity)...)[i]
190190
end
191-
@eval derivative(::typeof($modu.$fun), args::NTuple{$arity,Any}, ::Val{$i}) = $expr
191+
@eval derivative(::typeof($modu.$fun), args::NTuple{$arity,Any}, ::Val{$i}) = (x = $expr; !(x isa Expression || x isa Constant) ? Constant(x) : x)
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)
196196
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
197+
derivative(::typeof(one), args::Tuple{<:Any}, ::Val) = Constant(0)
198198

199199
function count_order(x)
200200
@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)