1
1
# ModelingToolkit IR
2
2
3
- ModelingToolkit IR, which falls under the ` Expression ` abstract type, mirrors
4
- the Julia AST but allows for easy mathematical manipulation by itself following
5
- mathematical semantics. The base of the IR is the ` Variable ` type, which defines
6
- a symbolic variable. These variables are combined using ` Operation ` s, which are
7
- registered functions applied to the various variables. These ` Operation ` s then
8
- perform automatic tracing, so normal mathematical functions applied to an ` Operation `
9
- generate a new ` Operation ` . For example, ` op1 = x+y ` is one ` Operation ` and
10
- ` op2 = 2z ` is another, and so ` op1*op2 ` is another ` Operation ` . Then, at the top,
11
- an ` Equation ` , normally written as ` op1 ~ op2 ` , defines the symbolic equality
12
- between two operations.
3
+ ModelingToolkit IR mirrors the Julia AST but allows for easy mathematical
4
+ manipulation by itself following mathematical semantics. The base of the IR is
5
+ the ` Sym ` type, which defines a symbolic variable. Registered (mathematical)
6
+ functions on ` Sym ` s (or ` Term ` s) return ` Term ` s. For example, ` op1 = x+y ` is
7
+ one ` Term ` and ` op2 = 2z ` is another, and so ` op1*op2 ` is another ` Term ` . Then,
8
+ at the top, an ` Equation ` , normally written as ` op1 ~ op2 ` , defines the
9
+ symbolic equality between two operations.
13
10
14
11
### Types
15
12
16
13
``` @docs
17
- Expression
18
- Variable
19
- ModelingToolkit.Constant
20
- Operation
14
+ Sym
15
+ Term
21
16
Equation
22
17
```
23
18
19
+ ### A note about functions restricted to ` Number ` s
20
+
21
+ ` Sym ` and ` Term ` objects are NOT subtypes of ` Number ` . ModelingToolkit provides
22
+ a simple wrapper type called ` Num ` which is a subtype of ` Real ` . ` Num ` wraps
23
+ either a Sym or a Term or any other object, defines the same set of operations
24
+ as symbolic expressions and forwards those to the values it wraps. You can use
25
+ ` ModelingToolkit.value ` function to unwrap a ` Num ` .
26
+
27
+ By default, the ` @variables ` and ` @parameters ` functions return Num-wrapped
28
+ objects so as to allow calling functions which are restricted to ` Number ` or
29
+ ` Real ` .
30
+
24
31
### Function Registration
25
32
26
33
The ModelingToolkit graph only allowed for registered Julia functions for the
27
34
operations. All other functions are automatically traced down to registered
28
35
functions. By default, ModelingToolkit.jl pre-registers the common functions
29
- utilized in the AD package ruleset [ DiffRules .jl] ( https://github.com/JuliaDiff/DiffRules .jl )
36
+ utilized in [ SymbolicUtils .jl] ( https://github.com/JuliaSymbolics/SymbolicUtils .jl )
30
37
and pre-defines their derivatives. However, the user can utilize the ` @register `
31
38
macro to add their function to allowed functions of the computation graph.
32
39
@@ -36,7 +43,7 @@ macro to add their function to allowed functions of the computation graph.
36
43
37
44
### Derivatives and Differentials
38
45
39
- A ` Differential(op) ` is a partial derivative with respect to the operation ` op ` ,
46
+ A ` Differential(op) ` is a partial derivative with respect to ` op ` ,
40
47
which can then be applied to some other operations. For example, ` D=Differential(t) `
41
48
is what would commonly be referred to as ` d/dt ` , which can then be applied to
42
49
other operations using its function call, so ` D(x+y) ` is ` d(x+y)/dt ` .
@@ -50,18 +57,18 @@ of the differentials down to basic one-variable expressions.
50
57
ModelingToolkit.derivative
51
58
Differential
52
59
expand_derivatives
60
+ ModelingToolkit.jacobian
61
+ ModelingToolkit.gradient
62
+ ModelingToolkit.hessian
53
63
```
54
64
55
- Note that the generation of sparse matrices simply follows from the Julia semantics
56
- imbued on the IR, so ` sparse(jac) ` changes a dense Jacobian to a sparse Jacobian
57
- matrix.
65
+ For jacobians which are sparse, use the ` sparsejacobian ` function.
66
+ For hessians which are sparse, use the ` sparsehessian ` function.
58
67
59
68
### Adding Derivatives
60
69
61
70
There is a large amount of derivatives pre-defined by
62
- [ DiffRules.jl] ( https://github.com/JuliaDiff/DiffRules.jl ) . Note that ` Expression `
63
- types are defined as ` <:Real ` , and thus any functions which allow the use of real
64
- numbers can automatically be traced by the derivative mechanism. Thus, for example:
71
+ [ DiffRules.jl] ( https://github.com/JuliaDiff/DiffRules.jl ) .
65
72
66
73
``` julia
67
74
f (x,y,z) = x^ 2 + sin (x+ y) - z
@@ -84,7 +91,7 @@ ModelingToolkit.derivative(::typeof(my_function), args::NTuple{N,Any}, ::Val{i})
84
91
85
92
where ` i ` means that it's the derivative with respect to the ` i ` th argument. ` args ` is the
86
93
array of arguments, so, for example, if your function is ` f(x,t) ` , then ` args = [x,t] ` .
87
- You should return an ` Operation ` for the derivative of your function.
94
+ You should return an ` Term ` for the derivative of your function.
88
95
89
96
For example, ` sin(t) ` 's derivative (by ` t ` ) is given by the following:
90
97
@@ -94,14 +101,14 @@ ModelingToolkit.derivative(::typeof(sin), args::NTuple{1,Any}, ::Val{1}) = cos(a
94
101
95
102
### IR Manipulation
96
103
97
- ModelingToolkit.jl provides functionality for easily manipulating ` Expression `
98
- types. Most of the functionality comes by the ` Expression ` type obeying the
99
- standard mathematical semantics. For example, if one has ` A ` a matrix of
100
- ` Expression ` , then ` A^2 ` calculates the ` Expression ` s for the squared matrix.
101
- In that sense, it is encouraged that one uses standard Julia for performing a
102
- lot of the manipulation on the IR, as, for example, calculating the sparse form
103
- of the matrix via ` sparse(A) ` is valid, legible, and easily understandable
104
- to all Julia programmers.
104
+ ModelingToolkit.jl provides functionality for easily manipulating expressions.
105
+ Most of the functionality comes by the expression objects obeying the standard
106
+ mathematical semantics. For example, if one has ` A ` a matrix of symbolic
107
+ expressions wrapped in ` Num ` , then ` A^2 ` calculates the expressions for the
108
+ squared matrix. In that sense, it is encouraged that one uses standard Julia
109
+ for performing a lot of the manipulation on the IR, as, for example,
110
+ calculating the sparse form of the matrix via ` sparse(A) ` is valid, legible,
111
+ and easily understandable to all Julia programmers.
105
112
106
113
Other additional manipulation functions are given below.
107
114
0 commit comments