Skip to content

Commit 2473af1

Browse files
authored
Merge pull request #238 from JuliaDiffEq/myb/n-ary
Add n-ary *, + rules
2 parents 7591373 + 6ea9552 commit 2473af1

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "1.2.2"
4+
version = "1.2.3"
55

66
[deps]
77
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"

src/differentials.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ derivative(O::Operation, idx) = derivative(O.op, (O.args...,), Val(idx))
9999
# Pre-defined derivatives
100100
import DiffRules, SpecialFunctions, NaNMath
101101
for (modu, fun, arity) DiffRules.diffrules()
102+
fun in [:*, :+] && continue # special
102103
for i 1:arity
103104
@eval function derivative(::typeof($modu.$fun), args::NTuple{$arity,Any}, ::Val{$i})
104105
M, f = $(modu, fun)
@@ -109,9 +110,8 @@ for (modu, fun, arity) ∈ DiffRules.diffrules()
109110
end
110111
end
111112

112-
derivative(::typeof(+), args::NTuple{T,Operation}, ::Val{i}) where {T,i} = args[i]
113-
derivative(::typeof(+), args::Tuple{Operation,Operation}, ::Val{1}) = args[1]
114-
derivative(::typeof(+), args::Tuple{Operation,Operation}, ::Val{2}) = args[2]
113+
derivative(::typeof(+), args::NTuple{N,Any}, ::Val) where {N} = 1
114+
derivative(::typeof(*), args::NTuple{N,Any}, ::Val{i}) where {N,i} = Operation(*, deleteat!(collect(args), i))
115115

116116
function count_order(x)
117117
@assert !(x isa Symbol) "The variable $x must have an order of differentiation that is greater or equal to 1!"

test/derivatives.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dsinsin = D(sin(sin(t)))
3131
d1 = D(sin(t)*t)
3232
d2 = D(sin(t)*cos(t))
3333
@test isequal(expand_derivatives(d1), t*cos(t)+sin(t))
34-
@test isequal(expand_derivatives(d2), simplify_constants(cos(t)*cos(t)+sin(t)*(-1*sin(t))))
34+
@test isequal(expand_derivatives(d2), simplify_constants(cos(t)*cos(t)+(sin(t)*-1)*sin(t)))
3535

3636
eqs = [0 ~ σ*(y-x),
3737
0 ~ x*-z)-y,
@@ -60,3 +60,7 @@ jac = calculate_jacobian(sys)
6060
@test isequal(expand_derivatives(D(2t)), 2)
6161
@test isequal(expand_derivatives(D(2x)), 2D(x))
6262
@test isequal(expand_derivatives(D(x^2)), simplify_constants(2 * x * D(x)))
63+
64+
# n-ary * and +
65+
isequal(ModelingToolkit.derivative(Operation(*, [x, y, z*ρ]), 1), y*(z*ρ))
66+
isequal(ModelingToolkit.derivative(Operation(+, [x*y, y, z]), 1), 1)

0 commit comments

Comments
 (0)