File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -42,20 +42,34 @@ function expand_derivatives(O::Operation,simplify=true)
42
42
if isa (O. op, Differential)
43
43
(D, o) = (O. op, O. args[1 ])
44
44
45
+ o isa Constant && return Constant (0 )
45
46
isequal (o, D. x) && return Constant (1 )
46
47
occursin (D. x, o) || return Constant (0 )
47
48
isa (o, Operation) || return O
48
49
isa (o. op, Variable) && return O
49
50
50
51
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
52
61
end
53
62
54
63
return simplify ? ModelingToolkit. simplify (x) : x
55
64
end
56
65
57
66
return simplify ? ModelingToolkit. simplify (O) : O
58
67
end
68
+ _iszero (x:: Constant ) = iszero (x. value)
69
+ _isone (x:: Constant ) = isone (x. value)
70
+ _iszero (x) = false
71
+ _isone (x) = false
72
+
59
73
expand_derivatives (x,args... ) = x
60
74
61
75
# Don't specialize on the function here
You can’t perform that action at this time.
0 commit comments