-
Notifications
You must be signed in to change notification settings - Fork 140
/
variable.jl
381 lines (316 loc) · 10.7 KB
/
variable.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using SymbolicUtils: FnType, Sym
using Setfield
const IndexMap = Dict{Char,Char}(
'-' => '₋',
'0' => '₀',
'1' => '₁',
'2' => '₂',
'3' => '₃',
'4' => '₄',
'5' => '₅',
'6' => '₆',
'7' => '₇',
'8' => '₈',
'9' => '₉')
struct VariableDefaultValue end
const _fail = Dict()
function getdefaultval(x, val=_fail)
x = unwrap(x)
if hasmetadata(x, VariableDefaultValue)
return getmetadata(x, VariableDefaultValue)
else
val === _fail && error("$x has no default value")
return val
end
end
function recurse_and_apply(f, x)
if symtype(x) <: AbstractArray
getindex_posthook(x) do r,x,i...
recurse_and_apply(f, r)
end
else
f(x)
end
end
function setdefaultval(x, val)
if symtype(x) <: AbstractArray
if val isa AbstractArray
getindex_posthook(x) do r,x,i...
setdefaultval(r, val[i...])
end
else
getindex_posthook(x) do r,x,i...
setdefaultval(r, val)
end
end
else
setmetadata(x,
VariableDefaultValue,
val)
end
end
struct GetindexParent end
function scalarize_getindex(x, parent=x)
if symtype(x) <: AbstractArray
getindex_posthook(x) do r,x,i...
scalarize_getindex(r, parent)
end
else
setmetadata(scalarize(x), GetindexParent, parent)
end
end
"""
$(TYPEDEF)
A named variable which represents a numerical value. The variable is uniquely
identified by its `name`, and all variables with the same `name` are treated
as equal.
# Fields
$(FIELDS)
For example, the following code defines an independent variable `t`, a parameter
`α`, a function parameter `σ`, a variable `x`, which depends on `t`, a variable
`y` with no dependents, a variable `z`, which depends on `t`, `α`, and `x(t)`
and parameters `β₁` and `β₂`.
```julia
σ = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:σ)) # left uncalled, since it is used as a function
w = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:w)) # unknown, left uncalled
x = Num(Variable{Symbolics.FnType{Tuple{Any},Real}}(:x))(t) # unknown, depends on `t`
y = Num(Variable(:y)) # unknown, no dependents
z = Num(Variable{Symbolics.FnType{NTuple{3,Any},Real}}(:z))(t, α, x) # unknown, multiple arguments
β₁ = Num(Variable(:β, 1)) # with index 1
β₂ = Num(Variable(:β, 2)) # with index 2
expr = β₁ * x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)
```
"""
struct Variable{T} <: Function # backward compat
"""The variable's unique name."""
name::Symbol
Variable(name) = Sym{Real}(name)
Variable{T}(name) where T = Sym{T}(name)
function Variable{T}(name, indices...) where T
var_name = Symbol("$(name)$(join(map_subscripts.(indices), "ˏ"))")
Sym{T}(var_name)
end
end
function Variable(name, indices...)
var_name = Symbol("$(name)$(join(map_subscripts.(indices), "ˏ"))")
Variable(var_name)
end
# TODO: move this to Symutils
function Sym{T}(name, i, indices...) where T
var_name = Symbol("$(name)$(join(map_subscripts.((i, indices...,)), "ˏ"))")
Sym{T}(var_name)
end
function map_subscripts(indices)
str = string(indices)
join(IndexMap[c] for c in str)
end
rename(x::Sym,name) = @set! x.name = name
function rename(x::Symbolic, name)
if operation(x) isa Sym
@assert x isa Term
@set! x.f = rename(operation(x), name)
@set! x.hash = Ref{UInt}(0)
return x
else
error("can't rename $x to $name")
end
end
function unwrap_runtime_var(v)
isruntime = Meta.isexpr(v, :$) && length(v.args) == 1
isruntime && (v = v.args[1])
return isruntime, v
end
# Build variables more easily
function _parse_vars(macroname, type, x, transform=identity)
ex = Expr(:block)
var_names = Symbol[]
# if parsing things in the form of
# begin
# x
# y
# z
# end
x = x isa Tuple && first(x) isa Expr && first(x).head == :tuple ? first(x).args : x # tuple handling
x = flatten_expr!(x)
cursor = 0
isoption(ex) = Meta.isexpr(ex, [:vect, :vcat, :hcat])
while cursor < length(x)
cursor += 1
v = x[cursor]
# We need lookahead to the next `v` to parse
# `@variables x [connect=Flow,unit=u]`
nv = cursor < length(x) ? x[cursor+1] : nothing
val = unit = connect = options = nothing
# x = 1, [connect = flow; unit = u"m^3/s"]
if Meta.isexpr(v, :(=))
v, val = v.args
if Meta.isexpr(val, :tuple) && length(val.args) == 2 && isoption(val.args[2])
options = val.args[2].args
val = val.args[1]
end
end
if Meta.isexpr(v, :(::))
v, type′ = v.args
type = type′ === :Complex ? Complex{type} : type′
end
# x [connect = flow; unit = u"m^3/s"]
if isoption(nv)
options = nv.args
cursor += 1
end
isruntime, v = unwrap_runtime_var(v)
iscall = Meta.isexpr(v, :call)
isarray = Meta.isexpr(v, :ref)
issym = v isa Symbol
@assert iscall || isarray || issym "@$macroname expects a tuple of expressions or an expression of a tuple (`@$macroname x y z(t) v[1:3] w[1:2,1:4]` or `@$macroname x y z(t) v[1:3] w[1:2,1:4] k=1.0`)"
if iscall
isruntime, fname = unwrap_runtime_var(v.args[1])
call_args = map(last∘unwrap_runtime_var, @view v.args[2:end])
var_name, expr = construct_vars(fname, type, call_args, val, options, transform, isruntime)
else
var_name, expr = construct_vars(v, type, nothing, val, options, transform, isruntime)
end
push!(var_names, var_name)
push!(ex.args, expr)
end
rhs = build_expr(:vect, var_names)
push!(ex.args, rhs)
return ex
end
function construct_vars(v, type, call_args, val, prop, transform, isruntime)
issym = v isa Symbol
isarray = isa(v, Expr) && v.head == :ref
if isarray
var_name = v.args[1]
if Meta.isexpr(var_name, :(::))
var_name, type′ = var_name.args
type = type′ === :Complex ? Complex{type} : type′
end
isruntime, var_name = unwrap_runtime_var(var_name)
indices = v.args[2:end]
expr = _construct_array_vars(isruntime ? var_name : Meta.quot(var_name), type, call_args, val, prop, indices...)
else
var_name = v
if Meta.isexpr(v, :(::))
var_name, type′ = v.args
type = type′ === :Complex ? Complex{type} : type′
end
expr = construct_var(isruntime ? var_name : Meta.quot(var_name), type, call_args, val, prop)
end
lhs = isruntime ? gensym(var_name) : var_name
rhs = :($transform($expr))
lhs, :($lhs = $rhs)
end
function option_to_metadata_type(::Val{opt}) where {opt}
throw(Base.Meta.ParseError("unknown property type $opt"))
end
function setprops_expr(expr, props)
isnothing(props) && return expr
for opt in props
if !Meta.isexpr(opt, :(=))
throw(Base.Meta.ParseError(
"Variable properties must be in " *
"the form of `a = b`. Got $opt."))
end
lhs, rhs = opt.args
@assert lhs isa Symbol "the lhs of an option must be a symbol"
expr = :($setmetadata($expr,
$(option_to_metadata_type(Val{lhs}())),
$rhs))
end
expr
end
function construct_var(var_name, type, call_args, val, prop)
expr = if call_args === nothing
:($Sym{$type}($var_name))
elseif !isempty(call_args) && call_args[end] == :..
:($Sym{$FnType{Tuple, $type}}($var_name)) # XXX: using Num as output
else
:($Sym{$FnType{NTuple{$(length(call_args)), Any}, $type}}($var_name)($(map(x->:($value($x)), call_args)...)))
end
if val !== nothing
expr = :($setdefaultval($expr, $val))
end
:($wrap($(setprops_expr(expr, prop))))
end
struct CallWith
args
end
(c::CallWith)(f) = unwrap(f(c.args...))
function _construct_array_vars(var_name, type, call_args, val, prop, indices...)
# TODO: just use Sym here
ndim = length(indices)
expr = if call_args === nothing
ex = :($Sym{Array{$type, $ndim}}($var_name))
:($setmetadata($ex, $ArrayShapeCtx, ($(indices...),)))
elseif !isempty(call_args) && call_args[end] == :..
ex = :($Sym{Array{$FnType{Tuple, $type}, $ndim}}($var_name)) # XXX: using Num as output
:($setmetadata($ex, $ArrayShapeCtx, ($(indices...),)))
else
# [(R -> R)(R) ....]
ex = :($Sym{Array{$FnType{Tuple, $type}, $ndim}}($var_name))
ex = :($setmetadata($ex, $ArrayShapeCtx, ($(indices...),)))
:($scalarize_getindex($map($CallWith(($(call_args...),)), $ex)))
end
if val !== nothing
expr = :($setdefaultval($expr, $val))
end
expr = setprops_expr(expr, prop)
return :($wrap($expr))
end
"""
Define one or more unknown variables.
```julia
@variables t α σ(..) β[1:2]
@variables w(..) x(t) y z(t, α, x)
expr = β[1]* x + y^α + σ(3) * (z - t) - β[2] * w(t - 1)
```
`(..)` signifies that the value should be left uncalled.
Symbolics supports creating variables that denote an array of some size.
```julia
julia> @variables x[1:3]
1-element Vector{Symbolics.Arr{Num, 1}}:
x[1:3]
julia> @variables y[1:3, 1:6] # support for tensors
1-element Vector{Symbolics.Arr{Num, 2}}:
y[1:3,1:6]
julia> @variables t z[1:3](t) # also works for dependent variables
2-element Vector{Any}:
t
(map(#5, z))[1:3]
```
A symbol or expression that represents an array can be turned into an array of
symbols or expressions using the `scalarize` function.
```julia
julia> Symbolics.scalarize(z)
3-element Vector{Num}:
z[1](t)
z[2](t)
z[3](t)
```
Note that `@variables` returns a vector of all the defined variables.
`@variables` can also take runtime symbol values by the `\$` interpolation
operator, and in this case, `@variables` doesn't automatically assign the value,
instead, it only returns a vector of symbolic variables. All the rest of the
syntax also applies here.
```julia
julia> a, b, c = :runtime_symbol_value, :value_b, :value_c
:runtime_symbol_value
julia> vars = @variables t \$a \$b(t) \$c[1:3](t)
4-element Vector{Any}:
t
runtime_symbol_value
value_b(t)
(map(#9, value_c))[1:3]
julia> (t, a, b, c)
(t, :runtime_symbol_value, :value_b, :value_c)
```
"""
macro variables(xs...)
esc(_parse_vars(:variables, Real, xs))
end
TreeViews.hastreeview(x::Sym) = true
function TreeViews.treelabel(io::IO,x::Sym,
mime::MIME"text/plain" = MIME"text/plain"())
show(io,mime,Text(x.name))
end