Skip to content

Commit

Permalink
Merge pull request #240 from JuliaDiffEq/myb/const
Browse files Browse the repository at this point in the history
Add diff rule on constants
  • Loading branch information
ChrisRackauckas authored Feb 8, 2020
2 parents 2473af1 + 1423a4a commit 2faa051
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 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.3"
version = "1.2.4"

[deps]
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Expand Down
1 change: 1 addition & 0 deletions src/differentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ sin(x())
```
"""
derivative(O::Operation, idx) = derivative(O.op, (O.args...,), Val(idx))
derivative(O::Constant, ::Any) = Constant(0)

# Pre-defined derivatives
import DiffRules, SpecialFunctions, NaNMath
Expand Down
10 changes: 4 additions & 6 deletions src/direct.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function gradient(O::Operation, vars::AbstractVector{Operation}; simplify = true)
function gradient(O::Expression, vars::AbstractVector{<:Expression}; simplify = true)
out = [expand_derivatives(Differential(v)(O)) for v in vars]
simplify ? simplify_constants.(out) : out
end

function jacobian(ops::AbstractVector{Operation}, vars::AbstractVector{Operation}; simplify = true)
function jacobian(ops::AbstractVector{<:Expression}, vars::AbstractVector{<:Expression}; simplify = true)
out = [expand_derivatives(Differential(v)(O)) for O in ops, v in vars]
simplify ? simplify_constants.(out) : out
end

function hessian(O::Operation, vars::AbstractVector{Operation}; simplify = true)
function hessian(O::Expression, vars::AbstractVector{<:Expression}; simplify = true)
out = [expand_derivatives(Differential(v2)(Differential(v1)(O))) for v1 in vars, v2 in vars]
simplify ? simplify_constants.(out) : out
end
Expand All @@ -25,9 +25,7 @@ function simplified_expr(O::Operation)
return Expr(:call, Symbol(O.op), simplified_expr.(O.args)...)
end

function simplified_expr(c::Constant)
c.value
end
simplified_expr(c::Constant) = c.value

function simplified_expr(eq::Equation)
Expr(:(=), simplified_expr(eq.lhs), simplified_expr(eq.rhs))
Expand Down
8 changes: 8 additions & 0 deletions test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ jac = calculate_jacobian(sys)
# n-ary * and +
isequal(ModelingToolkit.derivative(Operation(*, [x, y, z*ρ]), 1), y*(z*ρ))
isequal(ModelingToolkit.derivative(Operation(+, [x*y, y, z]), 1), 1)

@test iszero(ModelingToolkit.derivative(ModelingToolkit.Constant(42), x))
@test all(iszero, ModelingToolkit.gradient(ModelingToolkit.Constant(42), [t, x, y, z]))
@test all(iszero, ModelingToolkit.hessian(ModelingToolkit.Constant(42), [t, x, y, z]))
@test isequal(ModelingToolkit.jacobian([t, x, ModelingToolkit.Constant(42)], [t, x]),
Expression[ModelingToolkit.Constant(1) ModelingToolkit.Constant(0)
Differential(t)(x) ModelingToolkit.Constant(1)
ModelingToolkit.Constant(0) ModelingToolkit.Constant(0)])

0 comments on commit 2faa051

Please sign in to comment.