Skip to content

Commit 2224d14

Browse files
committed
Document registering the derivative of a function
1 parent 8b2a3c8 commit 2224d14

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/function_registration.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ ModelingToolkit IR. Example:
1010
```
1111
1212
registers `f` as a possible two-argument function.
13+
14+
You may also want to tell ModelingToolkit the derivative of the registered
15+
function. You can achieve this by
16+
17+
```julia
18+
julia> foo(x, y) = sin(x) * cos(y)
19+
foo (generic function with 1 method)
20+
21+
julia> ModelingToolkit.derivative(::typeof(foo), x, y, ::Val{1}) = cos(x) * cos(y) # derivative w.r.t. the first argument
22+
23+
julia> ModelingToolkit.derivative(::typeof(foo), x, y, ::Val{2}) = -sin(x) * sin(y) # derivative w.r.t. the second argument
24+
25+
julia> @parameters t; @variables x(t) y(t) z(t); @derivatives D'~t;
26+
27+
julia> expand_derivatives(D(foo(x, y) * z))
28+
z(t) * (derivative(x(t), t) * cos(x(t)) * cos(y(t)) + -1 * sin(x(t)) * derivative(y(t), t) * sin(y(t))) + sin(x(t)) * cos(y(t)) * derivative(z(t), t)
29+
```
1330
"""
1431
macro register(sig)
1532
splitsig = splitdef(:($sig = nothing))
@@ -116,4 +133,4 @@ function inject_registered_module_functions(expr)
116133

117134
return x
118135
end
119-
end
136+
end

0 commit comments

Comments
 (0)