@@ -46,7 +46,6 @@ Each operation builds an `Operation` type, and thus `eqs` is an array of
46
46
analyzed by other programs. We can turn this into a ` ODESystem ` via:
47
47
48
48
``` julia
49
- de = ODESystem (eqs,t,[x,y,z],[σ,ρ,β])
50
49
de = ODESystem (eqs)
51
50
```
52
51
@@ -56,7 +55,7 @@ This can then generate the function. For example, we can see the
56
55
generated code via:
57
56
58
57
``` julia
59
- generate_function (de)
58
+ generate_function (de, [x,y,z], [σ,ρ,β] )
60
59
61
60
# # Which returns:
62
61
:((# #363, u, p, t)->begin
@@ -89,7 +88,7 @@ eqs = [0 ~ σ*(y-x),
89
88
0 ~ x* (ρ- z)- y,
90
89
0 ~ x* y - β* z]
91
90
ns = NonlinearSystem (eqs, [x,y,z])
92
- nlsys_func = generate_function (ns)
91
+ nlsys_func = generate_function (ns, [x,y,z], [ρ,σ,β] )
93
92
```
94
93
95
94
which generates:
@@ -130,17 +129,49 @@ In this section we define the core pieces of the IR and what they mean.
130
129
131
130
### Variables
132
131
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
134
140
context-aware single variable of the IR. Its fields are described as follows:
135
141
136
142
- `name`: the name of the `Variable`. Note that this is not necessarily
137
143
the same as the name of the Julia variable. But this symbol itself is considered
138
144
the core identifier of the `Variable` in the sense of equality.
139
145
- `known`: the main denotation of context, storing whether or not the value of
140
146
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
+
144
175
145
176
### Constants
146
177
@@ -243,37 +274,37 @@ is accessible via a function-based interface. This means that all macros are
243
274
syntactic sugar in some form. For example, the variable construction:
244
275
245
276
``` julia
246
- @parameters t σ ρ β
277
+ @parameters t () σ ρ () β ()
247
278
@variables x (t) y (t) z (t)
248
279
@derivatives D' ~ t
249
280
```
250
281
251
282
is syntactic sugar for:
252
283
253
284
``` 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 )()
259
286
σ = 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)
262
293
```
263
294
264
295
### Intermediate Calculations
265
296
266
297
The system building functions can handle intermediate calculations. For example,
267
298
268
299
``` julia
269
- @variables x y z
270
- @parameters σ ρ β
300
+ @variables x () y () z ()
301
+ @parameters σ () ρ () β ()
271
302
a = y - x
272
303
eqs = [0 ~ σ* a,
273
304
0 ~ x* (ρ- z)- y,
274
305
0 ~ x* y - β* z]
275
306
ns = NonlinearSystem (eqs, [x,y,z])
276
- nlsys_func = generate_function (ns)
307
+ nlsys_func = generate_function (ns, [x,y,z], [σ,ρ,β] )
277
308
```
278
309
279
310
expands to:
0 commit comments