Skip to content

Commit cf67517

Browse files
shashiYingboMa
andcommitted
IR doc fixes.
Co-authored-by: "Yingbo Ma" <[email protected]>
1 parent cb4a194 commit cf67517

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

docs/src/IR.md

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
# ModelingToolkit IR
22

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.
1310

1411
### Types
1512

1613
```@docs
17-
Expression
18-
Variable
19-
ModelingToolkit.Constant
20-
Operation
14+
Sym
15+
Term
2116
Equation
2217
```
2318

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+
2431
### Function Registration
2532

2633
The ModelingToolkit graph only allowed for registered Julia functions for the
2734
operations. All other functions are automatically traced down to registered
2835
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)
3037
and pre-defines their derivatives. However, the user can utilize the `@register`
3138
macro to add their function to allowed functions of the computation graph.
3239

@@ -36,7 +43,7 @@ macro to add their function to allowed functions of the computation graph.
3643

3744
### Derivatives and Differentials
3845

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`,
4047
which can then be applied to some other operations. For example, `D=Differential(t)`
4148
is what would commonly be referred to as `d/dt`, which can then be applied to
4249
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.
5057
ModelingToolkit.derivative
5158
Differential
5259
expand_derivatives
60+
ModelingToolkit.jacobian
61+
ModelingToolkit.gradient
62+
ModelingToolkit.hessian
5363
```
5464

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.
5867

5968
### Adding Derivatives
6069

6170
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).
6572

6673
```julia
6774
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})
8491

8592
where `i` means that it's the derivative with respect to the `i`th argument. `args` is the
8693
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.
8895

8996
For example, `sin(t)`'s derivative (by `t`) is given by the following:
9097

@@ -94,14 +101,14 @@ ModelingToolkit.derivative(::typeof(sin), args::NTuple{1,Any}, ::Val{1}) = cos(a
94101

95102
### IR Manipulation
96103

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.
105112

106113
Other additional manipulation functions are given below.
107114

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ModelingToolkit has 3 layers:
2323
At the system level, there are *transformations* which take one system to
2424
another, and *targets* which output code for numerical solvers.
2525
3. The IR level, also referred to as the direct level. At this level, one
26-
directly acts on arrays of `Equation`, `Operation`, and `Variable` types to
26+
directly acts on arrays of `Equation`s, and symbolic expressions to
2727
generate functions.
2828

2929
Each level above is built on the level below, giving more context to allow for

0 commit comments

Comments
 (0)