@@ -12,20 +12,28 @@ ModelingToolkit IR. Example:
12
12
registers `f` as a possible two-argument function.
13
13
14
14
You may also want to tell ModelingToolkit the derivative of the registered
15
- function. You can achieve this by
15
+ function. Here is an example to do it
16
16
17
17
```julia
18
+ julia> using ModelingToolkit
19
+
18
20
julia> foo(x, y) = sin(x) * cos(y)
19
21
foo (generic function with 1 method)
20
22
21
- julia> ModelingToolkit.derivative(::typeof(foo), x, y, ::Val{1}) = cos(x) * cos(y) # derivative w.r.t. the first argument
23
+ julia> @parameters t; @variables x(t) y(t) z(t); @derivatives D'~t;
22
24
23
- julia> ModelingToolkit.derivative(::typeof(foo), x, y, ::Val{2}) = -sin(x) * sin(y) # derivative w.r.t. the second argument
25
+ julia> @register foo(x, y)
26
+ foo (generic function with 4 methods)
24
27
25
- julia> @parameters t; @variables x(t) y(t) z(t); @derivatives D'~t;
28
+ julia> foo(x, y)
29
+ foo(x(t), y(t))
30
+
31
+ julia> ModelingToolkit.derivative(::typeof(foo), (x, y), ::Val{1}) = cos(x) * cos(y) # derivative w.r.t. the first argument
32
+
33
+ julia> ModelingToolkit.derivative(::typeof(foo), (x, y), ::Val{2}) = -sin(x) * sin(y) # derivative w.r.t. the second argument
26
34
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)
35
+ julia> isequal( expand_derivatives(D(foo(x, y))), expand_derivatives(D(sin(x) * cos(y)) ))
36
+ true
29
37
```
30
38
"""
31
39
macro register (sig)
0 commit comments