Skip to content

Commit d3b31f9

Browse files
committed
Define constant kind of symbolic item.
1 parent 9f20b03 commit d3b31f9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/constants.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SymbolicUtils: symtype, term, hasmetadata, issym
2+
struct MTKConstantCtx end
3+
4+
function isconstant(x)
5+
x = unwrap(x)
6+
x isa Symbolic && getmetadata(x, MTKConstantCtx, false)
7+
end
8+
9+
"""
10+
toconst(s::Sym)
11+
12+
Maps the parameter to a constant. The parameter must have a default.
13+
"""
14+
function toconst(s)
15+
if s isa Symbolics.Arr
16+
Symbolics.wrap(toconst(Symbolics.unwrap(s)))
17+
elseif s isa AbstractArray
18+
map(toconst, s)
19+
else
20+
assert(hasmetadata(s,VariableDefaultValue))
21+
setmetadata(s, MTKConstCtx, true)
22+
end
23+
end
24+
toconst(s::Num) = wrap(toconst(value(s)))
25+
26+
"""
27+
$(SIGNATURES)
28+
29+
Define one or more known variables.
30+
"""
31+
macro constants(xs...)
32+
Symbolics._parse_vars(:constants,
33+
Real,
34+
xs,
35+
toconst) |> esc
36+
end

0 commit comments

Comments
 (0)