Skip to content

Commit

Permalink
Merge pull request #238 from JuliaDiffEq/myb/n-ary
Browse files Browse the repository at this point in the history
Add n-ary *, + rules
  • Loading branch information
YingboMa authored Feb 7, 2020
2 parents 7591373 + 6ea9552 commit 2473af1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ModelingToolkit"
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
authors = ["Chris Rackauckas <[email protected]>"]
version = "1.2.2"
version = "1.2.3"

[deps]
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Expand Down
6 changes: 3 additions & 3 deletions src/differentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ derivative(O::Operation, idx) = derivative(O.op, (O.args...,), Val(idx))
# Pre-defined derivatives
import DiffRules, SpecialFunctions, NaNMath
for (modu, fun, arity) DiffRules.diffrules()
fun in [:*, :+] && continue # special
for i 1:arity
@eval function derivative(::typeof($modu.$fun), args::NTuple{$arity,Any}, ::Val{$i})
M, f = $(modu, fun)
Expand All @@ -109,9 +110,8 @@ for (modu, fun, arity) ∈ DiffRules.diffrules()
end
end

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

function count_order(x)
@assert !(x isa Symbol) "The variable $x must have an order of differentiation that is greater or equal to 1!"
Expand Down
6 changes: 5 additions & 1 deletion test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dsinsin = D(sin(sin(t)))
d1 = D(sin(t)*t)
d2 = D(sin(t)*cos(t))
@test isequal(expand_derivatives(d1), t*cos(t)+sin(t))
@test isequal(expand_derivatives(d2), simplify_constants(cos(t)*cos(t)+sin(t)*(-1*sin(t))))
@test isequal(expand_derivatives(d2), simplify_constants(cos(t)*cos(t)+(sin(t)*-1)*sin(t)))

eqs = [0 ~ σ*(y-x),
0 ~ x*-z)-y,
Expand Down Expand Up @@ -60,3 +60,7 @@ jac = calculate_jacobian(sys)
@test isequal(expand_derivatives(D(2t)), 2)
@test isequal(expand_derivatives(D(2x)), 2D(x))
@test isequal(expand_derivatives(D(x^2)), simplify_constants(2 * x * D(x)))

# n-ary * and +
isequal(ModelingToolkit.derivative(Operation(*, [x, y, z*ρ]), 1), y*(z*ρ))
isequal(ModelingToolkit.derivative(Operation(+, [x*y, y, z]), 1), 1)

2 comments on commit 2473af1

@YingboMa
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/9048

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.3 -m "<description of version>" 2473af1e03a5eeb2ddcf3ade3a7a7dd53c2d542b
git push origin v1.2.3

Please sign in to comment.