Skip to content

Commit bb8d89f

Browse files
Merge pull request #90 from JuliaDiffEq/fix/export-derivative
Clean up derivatives
2 parents 1fae6e6 + 438b982 commit bb8d89f

File tree

5 files changed

+10
-23
lines changed

5 files changed

+10
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ t = Parameter(:t)
269269
x = Unknown(:x, dependents = [t])
270270
y = Unknown(:y, dependents = [t])
271271
z = Unknown(:z, dependents = [t])
272-
D = Differential(t) # Default of first derivative, Derivative(t,1)
272+
D = Differential(t)
273273
σ = Parameter()
274274
ρ = Parameter()
275275
β = Parameter()

src/differentials.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function (D::Differential)(x::Variable)
1313
has_dependent(x, D.x) || return Constant(0)
1414
return Operation(D, Expression[x])
1515
end
16+
(::Differential)(::Constant) = Constant(0)
1617
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x
1718

1819
function expand_derivatives(O::Operation)
@@ -21,6 +22,7 @@ function expand_derivatives(O::Operation)
2122
if O.op isa Differential
2223
D = O.op
2324
o = O.args[1]
25+
isa(o, Operation) || return O
2426
return simplify_constants(sum(i->Derivative(o,i)*expand_derivatives(D(o.args[i])),1:length(o.args)))
2527
end
2628

@@ -79,4 +81,4 @@ function calculate_jacobian(eqs,vars)
7981
Expression[Differential(vars[j])(eqs[i]) for i in 1:length(eqs), j in 1:length(vars)]
8082
end
8183

82-
export Differential, Derivative, expand_derivatives, @Deriv, calculate_jacobian
84+
export Differential, expand_derivatives, @Deriv, calculate_jacobian

test/basic_variables_and_operations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Test
33

44
@Param t σ ρ β
55
@Unknown x(t) y(t) z(t)
6-
@Deriv D'~t # Default of first derivative, Derivative(t,1)
6+
@Deriv D'~t
77
@Const c=0
88

99
# Default values

test/derivatives.jl

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ dcsch = D(csch(t))
1515
# Chain rule
1616
dsinsin = D(sin(sin(t)))
1717
@test expand_derivatives(dsinsin) == cos(sin(t))*cos(t)
18-
# Binary
19-
dpow1 = Derivative(^, (x, y), Val(1))
20-
dpow2 = Derivative(^, (x, y), Val(2))
21-
@test dpow1 == y*x^(y-1)
22-
@test dpow2 == x^y*log(x)
2318

2419
d1 = D(sin(t)*t)
2520
d2 = D(sin(t)*cos(t))
@@ -45,18 +40,8 @@ jac = ModelingToolkit.calculate_jacobian(sys)
4540
@Unknown a(t) b(a)
4641
@test D(b) Constant(0)
4742

43+
@test expand_derivatives(D(x * y)) == simplify_constants(y*D(x) + x*D(y))
44+
@test_broken expand_derivatives(D(x * y)) == simplify_constants(D(x)*y + x*D(y))
4845

49-
# Regression test for PR #84
50-
let
51-
# Define some variables
52-
@Param t σ ρ β
53-
@Unknown x(t) y(t) z(t)
54-
55-
f(x,y,z) = z*x+y
56-
# This used to seg fault, now raises MethodError
57-
@test_throws MethodError Derivative(f, x, 1)
58-
@Deriv D'~t
59-
op = expand_derivatives(D(f(x,y,z)))
60-
op.args[1].args[2].name == :z
61-
op.args[1].args[2].diff.x.name == :t
62-
end
46+
@test expand_derivatives(D(2t)) == 2
47+
@test expand_derivatives(D(2x)) == 2D(x)

test/system_construction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Test
44
# Define some variables
55
@Param t σ ρ β
66
@Unknown x(t) y(t) z(t)
7-
@Deriv D'~t # Default of first derivative, Derivative(t,1)
7+
@Deriv D'~t
88
@Const c=0
99

1010
# Define a differential equation

0 commit comments

Comments
 (0)