Skip to content

Commit 59f8e37

Browse files
committed
avoid unnecessary terms in expand_derivatives
1 parent 816722e commit 59f8e37

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/differentials.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,34 @@ function expand_derivatives(O::Operation,simplify=true)
4242
if isa(O.op, Differential)
4343
(D, o) = (O.op, O.args[1])
4444

45+
o isa Constant && return Constant(0)
4546
isequal(o, D.x) && return Constant(1)
4647
occursin(D.x, o) || return Constant(0)
4748
isa(o, Operation) || return O
4849
isa(o.op, Variable) && return O
4950

5051
x = sum(1:length(o.args)) do i
51-
derivative(o, i) * expand_derivatives(D(o.args[i]),false)
52+
t2 = expand_derivatives(D(o.args[i]),false)
53+
54+
if _iszero(t2)
55+
return t2
56+
elseif _isone(t2)
57+
return derivative(o, i)
58+
else
59+
return derivative(o, i) * t2
60+
end
5261
end
5362

5463
return simplify ? ModelingToolkit.simplify(x) : x
5564
end
5665

5766
return simplify ? ModelingToolkit.simplify(O) : O
5867
end
68+
_iszero(x::Constant) = iszero(x.value)
69+
_isone(x::Constant) = isone(x.value)
70+
_iszero(x) = false
71+
_isone(x) = false
72+
5973
expand_derivatives(x,args...) = x
6074

6175
# Don't specialize on the function here

0 commit comments

Comments
 (0)