@@ -37,7 +37,8 @@ Base.:(==)(D1::Differential, D2::Differential) = isequal(D1.x, D2.x)
37
37
_isfalse (occ:: Bool ) = occ === false
38
38
_isfalse (occ:: Term ) = _isfalse (operation (occ))
39
39
40
- function occursin_info (x, expr:: Term )
40
+ function occursin_info (x, expr)
41
+ ! istree (expr) && return false
41
42
if isequal (x, expr)
42
43
true
43
44
else
@@ -52,16 +53,15 @@ function occursin_info(x, expr::Sym)
52
53
isequal (x, expr)
53
54
end
54
55
55
- hasderiv (O:: Term ) = operation (O) isa Differential || any (hasderiv, arguments (O))
56
- hasderiv (O) = false
57
-
58
- occursin_info (x, y) = false
56
+ function hasderiv (O)
57
+ istree (O) ? operation (O) isa Differential || any (hasderiv, arguments (O)) : false
58
+ end
59
59
"""
60
60
$(SIGNATURES)
61
61
62
62
TODO
63
63
"""
64
- function expand_derivatives (O:: Symbolic , simplify= true ; occurances= nothing )
64
+ function expand_derivatives (O:: Symbolic , simplify= false ; occurances= nothing )
65
65
if istree (O) && isa (operation (O), Differential)
66
66
@assert length (arguments (O)) == 1
67
67
arg = expand_derivatives (arguments (O)[1 ], false )
@@ -73,45 +73,45 @@ function expand_derivatives(O::Symbolic, simplify=true; occurances=nothing)
73
73
_isfalse (occurances) && return 0
74
74
occurances isa Bool && return 1 # means it's a `true`
75
75
76
- (D, o) = ( operation (O), arg )
76
+ D = operation (O)
77
77
78
- if ! istree (o )
79
- return O # Cannot expand
80
- elseif isa (operation (o ), Sym)
81
- return O # Cannot expand
82
- elseif isa (operation (o ), Differential)
78
+ if ! istree (arg )
79
+ return D (arg) # Cannot expand
80
+ elseif isa (operation (arg ), Sym)
81
+ return D (arg) # Cannot expand
82
+ elseif isa (operation (arg ), Differential)
83
83
# The recursive expand_derivatives was not able to remove
84
84
# a nested Differential. We can attempt to differentiate the
85
85
# inner expression wrt to the outer iv. And leave the
86
86
# unexpandable Differential outside.
87
- if isequal (operation (o ). x, D. x)
88
- return O
87
+ if isequal (operation (arg ). x, D. x)
88
+ return D (arg)
89
89
else
90
- inner = expand_derivatives (D (arguments (o )[1 ]), false )
90
+ inner = expand_derivatives (D (arguments (arg )[1 ]), false )
91
91
# if the inner expression is not expandable either, return
92
92
if istree (inner) && operation (inner) isa Differential
93
- return O
93
+ return D (arg)
94
94
else
95
- return expand_derivatives (operation (o )(inner), simplify)
95
+ return expand_derivatives (operation (arg )(inner), simplify)
96
96
end
97
97
end
98
98
end
99
99
100
- l = length (arguments (o ))
100
+ l = length (arguments (arg ))
101
101
exprs = []
102
102
c = 0
103
103
104
104
for i in 1 : l
105
- t2 = expand_derivatives (D (arguments (o )[i]),false , occurances= arguments (occurances)[i])
105
+ t2 = expand_derivatives (D (arguments (arg )[i]),false , occurances= arguments (occurances)[i])
106
106
107
107
x = if _iszero (t2)
108
108
t2
109
109
elseif _isone (t2)
110
- d = derivative_idx (o , i)
111
- d isa NoDeriv ? D (o ) : d
110
+ d = derivative_idx (arg , i)
111
+ d isa NoDeriv ? D (arg ) : d
112
112
else
113
- t1 = derivative_idx (o , i)
114
- t1 = t1 isa NoDeriv ? D (o ) : t1
113
+ t1 = derivative_idx (arg , i)
114
+ t1 = t1 isa NoDeriv ? D (arg ) : t1
115
115
make_operation (* , [t1, t2])
116
116
end
117
117
0 commit comments