11export Differential, expand_derivatives, @derivatives
22
33
4+ """
5+ $(TYPEDEF)
6+
7+ Represents a differential operator.
8+
9+ # Fields
10+ $(FIELDS)
11+
12+ # Examples
13+
14+ ```jldoctest
15+ julia> using ModelingToolkit
16+
17+ julia> @variables x y;
18+
19+ julia> D = Differential(x)
20+ (D'~x())
21+
22+ julia> D(y) # Differentiate y wrt. x
23+ (D'~x())(y())
24+ ```
25+ """
426struct Differential <: Function
27+ """ The variable or expression to differentiate with respect to."""
528 x:: Expression
629end
730(D:: Differential )(x) = Operation (D, Expression[x])
@@ -11,6 +34,11 @@ Base.convert(::Type{Expr}, D::Differential) = D
1134
1235Base.:(== )(D1:: Differential , D2:: Differential ) = isequal (D1. x, D2. x)
1336
37+ """
38+ $(SIGNATURES)
39+
40+ TODO
41+ """
1442function expand_derivatives (O:: Operation )
1543 @. O. args = expand_derivatives (O. args)
1644
3260expand_derivatives (x) = x
3361
3462# Don't specialize on the function here
63+ """
64+ $(SIGNATURES)
65+
66+ Calculate the derivative of the op `O` with respect to its argument with index
67+ `idx`.
68+
69+ # Examples
70+
71+ ```jldoctest label1
72+ julia> using ModelingToolkit
73+
74+ julia> @variables x y;
75+
76+ julia> ModelingToolkit.derivative(sin(x), 1)
77+ cos(x())
78+ ```
79+
80+ Note that the function does not recurse into the operation's arguments, i.e. the
81+ chain rule is not applied:
82+
83+ ```jldoctest label1
84+ julia> myop = sin(x) * y^2
85+ sin(x()) * y() ^ 2
86+
87+ julia> typeof(myop.op) # Op is multiplication function
88+ typeof(*)
89+
90+ julia> ModelingToolkit.derivative(myop, 1) # wrt. sin(x)
91+ y() ^ 2
92+
93+ julia> ModelingToolkit.derivative(myop, 2) # wrt. y^2
94+ sin(x())
95+ ```
96+ """
3597derivative (O:: Operation , idx) = derivative (O. op, (O. args... ,), Val (idx))
3698
3799# Pre-defined derivatives
@@ -76,10 +138,33 @@ function _differential_macro(x)
76138 ex
77139end
78140
141+ """
142+ $(SIGNATURES)
143+
144+ Define one or more differentials.
145+
146+ # Examples
147+
148+ ```jldoctest
149+ julia> using ModelingToolkit
150+
151+ julia> @variables x y z;
152+
153+ julia> @derivatives Dx'~x Dy'~y # Create differentials wrt. x and y
154+ ((D'~x()), (D'~y()))
155+
156+ julia> Dx(z) # Differentiate z wrt. x
157+ (D'~x())(z())
158+
159+ julia> Dy(z) # Differentiate z wrt. y
160+ (D'~y())(z())
161+ ```
162+ """
79163macro derivatives (x... )
80164 esc (_differential_macro (x))
81165end
82166
167+
83168function calculate_jacobian (eqs, dvs)
84169 Expression[Differential (dv)(eq) for eq ∈ eqs, dv ∈ dvs]
85170end
0 commit comments