-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyparameters.jl
59 lines (50 loc) · 1.52 KB
/
myparameters.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
import SymbolicUtils: symtype, term, hasmetadata, issym
@enum VariableType VARIABLE PARAMETER BROWNIAN
struct MTKVariableTypeCtx end
getvariabletype(x, def = VARIABLE) = getmetadata(unwrap(x), MTKVariableTypeCtx, def)
function isparameter(x)
x = unwrap(x)
if x isa Symbolic && (varT = getvariabletype(x, nothing)) !== nothing
return varT === PARAMETER
#TODO: Delete this branch
elseif x isa Symbolic && Symbolics.getparent(x, false) !== false
p = Symbolics.getparent(x)
isparameter(p) ||
(hasmetadata(p, Symbolics.VariableSource) &&
getmetadata(p, Symbolics.VariableSource)[1] == :parameters)
elseif istree(x) && operation(x) isa Symbolic
varT === PARAMETER || isparameter(operation(x))
elseif istree(x) && operation(x) == (getindex)
isparameter(arguments(x)[1])
elseif x isa Symbolic
varT === PARAMETER
else
false
end
end
"""
toparam(s)
Maps the variable to a parameter.
"""
function toparam(s)
if s isa Symbolics.Arr
Symbolics.wrap(toparam(Symbolics.unwrap(s)))
elseif s isa AbstractArray
map(toparam, s)
else
setmetadata(s, MTKVariableTypeCtx, PARAMETER)
end
end
toparam(s::Num) = wrap(toparam(value(s)))
"""
tovar(s)
Maps the variable to a state.
"""
tovar(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, VARIABLE)
tovar(s::Num) = Num(tovar(value(s)))
macro nbparameters(xs...)
Symbolics._parse_vars(:parameters,
Real,
xs,
toparam) |> esc
end