Skip to content

Commit 97c5097

Browse files
Fix dependence checking in differentiation
1 parent 07b15e8 commit 97c5097

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/differentials.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Base.Expr(D::Differential) = :($(Symbol("D_$(D.x.name)_$(D.order)")))
1010
function Derivative end
1111
(D::Differential)(x::Operation) = Operation(Derivative,Expression[x,D])
1212
function (D::Differential)(x::Variable)
13-
D.x === x && return Constant(1)
14-
D.x x.dependents && return Constant(0) # FIXME
13+
D.x === x && return Constant(1)
14+
has_dependent(x, D.x) || return Constant(0)
1515
return Variable(x,D)
1616
end
1717
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x

src/utils.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ function flatten_expr!(x)
1818
end
1919

2020
toexpr(ex) = MacroTools.postwalk(x->x isa Union{Expression,Operation} ? Expr(x) : x, ex)
21+
22+
has_dependent(t::Variable) = Base.Fix2(has_dependent, t)
23+
has_dependent(x::Variable, t::Variable) =
24+
t x.dependents || any(has_dependent(t), x.dependents)

test/derivatives.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ jac = ModelingToolkit.calculate_jacobian(sys)
4141
@test jac[3,1] == y
4242
@test jac[3,2] == x
4343
@test jac[3,3] == -1*β
44+
45+
# Variable dependence checking in differentiation
46+
@Var a(t) b(a)
47+
@test D(b) Constant(0)

0 commit comments

Comments
 (0)