Skip to content

Commit a1a8a1b

Browse files
authored
Merge pull request #627 from SciML/s/fix-derivatives
Fix a case in expand_derivatives
2 parents 8170b05 + c58f08b commit a1a8a1b

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/differentials.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ function expand_derivatives(O::Term, simplify=true; occurances=nothing)
119119
if isempty(exprs)
120120
return c
121121
elseif length(exprs) == 1
122-
return simplify ? SymbolicUtils.simplify(exprs[1]) : exprs[1]
122+
term = (simplify ? SymbolicUtils.simplify(exprs[1]) : exprs[1])
123+
return _iszero(c) ? term : c + term
123124
else
124-
x = make_operation(+, !iszero(c) ? vcat(c, exprs) : exprs)
125+
x = make_operation(+, !_iszero(c) ? vcat(c, exprs) : exprs)
125126
return simplify ? SymbolicUtils.simplify(x) : x
126127
end
127128
elseif !hasderiv(O)

test/derivatives.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ L = .5 * ∂ₜ(x)^2 - .5 * x^2
110110
@test isequal(expand_derivatives(∂ₓ(L)), -1 * x)
111111
test_equal(expand_derivatives(Differential(x)(L) - ∂ₜ(Differential(∂ₜ(x))(L))), -1 * (∂ₜ(∂ₜ(x)) + x))
112112
@test isequal(expand_derivatives(Differential(x)(L) ~ ∂ₜ(Differential(∂ₜ(x))(L))), (-1 * x) ~ ∂ₜ(∂ₜ(x)))
113+
114+
@variables x2(t)
115+
@test isequal(expand_derivatives(Differential(x)(2 * x + x2 * x)), 2 + x2)

0 commit comments

Comments
 (0)