Skip to content

Commit 4043598

Browse files
Make Differential directly callable
1 parent 97c5097 commit 4043598

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/differentials.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct Differential <: Expression
1+
struct Differential <: Function
22
x::Expression
33
order::Int
44
end
@@ -8,31 +8,30 @@ Base.show(io::IO, D::Differential) = print(io,"($(D.x),$(D.order))")
88
Base.Expr(D::Differential) = :($(Symbol("D_$(D.x.name)_$(D.order)")))
99

1010
function Derivative end
11-
(D::Differential)(x::Operation) = Operation(Derivative,Expression[x,D])
11+
(D::Differential)(x::Operation) = Operation(D, Expression[x])
1212
function (D::Differential)(x::Variable)
1313
D.x === x && return Constant(1)
1414
has_dependent(x, D.x) || return Constant(0)
1515
return Variable(x,D)
1616
end
1717
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x
1818

19-
Variable(x::Variable,D::Differential) = Variable(x.name,x.value,x.value_type,
19+
Variable(x::Variable, D::Differential) = Variable(x.name,x.value,x.value_type,
2020
x.subtype,D,x.dependents,x.description,x.flow,x.domain,
2121
x.size,x.context)
2222

2323
function expand_derivatives(O::Operation)
2424
@. O.args = expand_derivatives(O.args)
2525

26-
if O.op == Derivative
27-
D = O.args[2]
26+
if O.op isa Differential
27+
D = O.op
2828
o = O.args[1]
2929
return simplify_constants(sum(i->Derivative(o,i)*expand_derivatives(D(o.args[i])),1:length(o.args)))
3030
end
3131

3232
return O
3333
end
3434
expand_derivatives(x::Variable) = x
35-
expand_derivatives(D::Differential) = D
3635

3736
# Don't specialize on the function here
3837
function Derivative(O::Operation,idx)

src/variables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mutable struct Variable <: Expression
33
value
44
value_type::DataType
55
subtype::Symbol
6-
diff::Union{Expression,Nothing} # FIXME
6+
diff::Union{Function,Nothing} # FIXME
77
dependents::Vector{Variable}
88
description::String
99
flow::Bool

0 commit comments

Comments
 (0)