Skip to content

Commit af476bd

Browse files
Update README
1 parent 2ba4c8d commit af476bd

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

README.md

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Each operation builds an `Operation` type, and thus `eqs` is an array of
4646
analyzed by other programs. We can turn this into a `ODESystem` via:
4747

4848
```julia
49-
de = ODESystem(eqs,t,[x,y,z],[σ,ρ,β])
5049
de = ODESystem(eqs)
5150
```
5251

@@ -56,7 +55,7 @@ This can then generate the function. For example, we can see the
5655
generated code via:
5756

5857
```julia
59-
generate_function(de)
58+
generate_function(de, [x,y,z], [σ,ρ,β])
6059

6160
## Which returns:
6261
:((##363, u, p, t)->begin
@@ -89,7 +88,7 @@ eqs = [0 ~ σ*(y-x),
8988
0 ~ x*-z)-y,
9089
0 ~ x*y - β*z]
9190
ns = NonlinearSystem(eqs, [x,y,z])
92-
nlsys_func = generate_function(ns)
91+
nlsys_func = generate_function(ns, [x,y,z], [ρ,σ,β])
9392
```
9493
9594
which generates:
@@ -130,17 +129,49 @@ In this section we define the core pieces of the IR and what they mean.
130129
131130
### Variables
132131
133-
The most fundamental part of the IR is the `Variable`. The `Variable` is the
132+
The most fundamental part of the IR is the `Variable`. In order to mirror the
133+
intention of solving for variables and representing function-like parameters,
134+
we treat each instance of `Variable` as a function which is called on its
135+
arguments using the natural syntax. Rather than having additional mechanisms
136+
for handling constant variables and parameters, we simply represent them as
137+
constant functions.
138+
139+
The `Variable` is the
134140
context-aware single variable of the IR. Its fields are described as follows:
135141
136142
- `name`: the name of the `Variable`. Note that this is not necessarily
137143
the same as the name of the Julia variable. But this symbol itself is considered
138144
the core identifier of the `Variable` in the sense of equality.
139145
- `known`: the main denotation of context, storing whether or not the value of
140146
the variable is known.
141-
- `dependents`: the vector of variables on which the current variable
142-
is dependent. For example, `u(t,x)` has dependents `[t,x]`. Derivatives thus
143-
require this information in order to simplify down.
147+
148+
For example, the following code defines an independent variable `t`, a parameter
149+
`α`, a function parameter `σ`, a variable `x` which depends on `t`, a variable
150+
`y` with no dependents, and a variable `z` which depends on `t`, `α`, and `x(t)`.
151+
152+
```julia
153+
t = Variable(:t; known = true)() # independent variables are treated as known
154+
α = Variable(; known = true)() # parameters are known
155+
σ = Variable(; known = true) # left uncalled, since it is used as a function
156+
w = Variable(:w; known = false) # unknown, left uncalled
157+
x = Variable(:x; known = false)(t) # unknown, depends on `t`
158+
y = Variable(:y; known = false)() # unknown, no dependents
159+
z = Variable(:z; known = false)(t, α, x) # unknown, multiple arguments
160+
161+
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
162+
```
163+
164+
We can rewrite this more concisely using macros. Note the difference between
165+
including and excluding empty parentheses. When in call format, variables are
166+
aliased to the given call, allowing implicit use of dependents for convenience.
167+
168+
```julia
169+
@parameters t() α() σ
170+
@variables w x(t) y() z(t, α, x)
171+
172+
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
173+
```
174+
144175
145176
### Constants
146177
@@ -243,37 +274,37 @@ is accessible via a function-based interface. This means that all macros are
243274
syntactic sugar in some form. For example, the variable construction:
244275
245276
```julia
246-
@parameters t σ ρ β
277+
@parameters t() σ ρ() β()
247278
@variables x(t) y(t) z(t)
248279
@derivatives D'~t
249280
```
250281
251282
is syntactic sugar for:
252283
253284
```julia
254-
t = Variable(:t; known = true)
255-
x = Variable(:x, [t])
256-
y = Variable(:y, [t])
257-
z = Variable(:z, [t])
258-
D = Differential(t)
285+
t = Variable(:t; known = true)()
259286
σ = Variable(; known = true)
260-
ρ = Variable(; known = true)
261-
β = Variable(; known = true)
287+
ρ = Variable(; known = true)()
288+
β = Variable(; known = true)()
289+
x = Variable(:x)(t)
290+
y = Variable(:y)(t)
291+
z = Variable(:z)(t)
292+
D = Differential(t)
262293
```
263294
264295
### Intermediate Calculations
265296
266297
The system building functions can handle intermediate calculations. For example,
267298
268299
```julia
269-
@variables x y z
270-
@parameters σ ρ β
300+
@variables x() y() z()
301+
@parameters σ() ρ() β()
271302
a = y - x
272303
eqs = [0 ~ σ*a,
273304
0 ~ x*-z)-y,
274305
0 ~ x*y - β*z]
275306
ns = NonlinearSystem(eqs, [x,y,z])
276-
nlsys_func = generate_function(ns)
307+
nlsys_func = generate_function(ns, [x,y,z], [σ,ρ,β])
277308
```
278309
279310
expands to:

0 commit comments

Comments
 (0)