-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathconstants.jl
50 lines (42 loc) · 1.14 KB
/
constants.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
import SymbolicUtils: symtype, term, hasmetadata, issym
struct MTKConstantCtx end
isconstant(x::Num) = isconstant(unwrap(x))
"""
Test whether `x` is a constant-type Sym.
"""
function isconstant(x)
x = unwrap(x)
x isa Symbolic && getmetadata(x, MTKConstantCtx, false)
end
"""
toconstant(s)
Maps the parameter to a constant. The parameter must have a default.
"""
function toconstant(s)
hasmetadata(s, Symbolics.VariableDefaultValue) ||
throw(ArgumentError("Constant `$(s)` must be assigned a default value."))
setmetadata(s, MTKConstantCtx, true)
end
toconstant(s::Num) = wrap(toconstant(value(s)))
"""
$(SIGNATURES)
Define one or more constants.
See also [`@independent_variables`](@ref), [`@parameters`](@ref) and [`@variables`](@ref).
"""
macro constants(xs...)
Symbolics._parse_vars(:constants,
Real,
xs,
toconstant) |> esc
end
"""
Substitute all `@constants` in the given expression
"""
function subs_constants(eqs)
consts = collect_constants(eqs)
if !isempty(consts)
csubs = Dict(c => getdefault(c) for c in consts)
eqs = substitute(eqs, csubs)
end
return eqs
end