Skip to content

Commit 438b982

Browse files
Fix Constant differentiation
1 parent 0c262d7 commit 438b982

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/differentials.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function (D::Differential)(x::Variable)
1313
has_dependent(x, D.x) || return Constant(0)
1414
return Operation(D, Expression[x])
1515
end
16+
(::Differential)(::Constant) = Constant(0)
1617
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x
1718

1819
function expand_derivatives(O::Operation)
@@ -21,7 +22,7 @@ function expand_derivatives(O::Operation)
2122
if O.op isa Differential
2223
D = O.op
2324
o = O.args[1]
24-
isa(o, Variable) && return O
25+
isa(o, Operation) || return O
2526
return simplify_constants(sum(i->Derivative(o,i)*expand_derivatives(D(o.args[i])),1:length(o.args)))
2627
end
2728

test/derivatives.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ jac = ModelingToolkit.calculate_jacobian(sys)
4242

4343
@test expand_derivatives(D(x * y)) == simplify_constants(y*D(x) + x*D(y))
4444
@test_broken expand_derivatives(D(x * y)) == simplify_constants(D(x)*y + x*D(y))
45+
46+
@test expand_derivatives(D(2t)) == 2
47+
@test expand_derivatives(D(2x)) == 2D(x)

0 commit comments

Comments
 (0)