Skip to content

Commit ac993bb

Browse files
committed
Added function to create variables without @variables macro
genvar(::Symbol) now converts a ::Symbol into a Symbolics.jl variable. "genvar(:f)" or "genvar(:f, :t)" (t is the default) is now equivalent to "@variables f(t)". Multiple dependent variables can be used, e.g., "genvar(:f, [:t, :d1, :d2])", as long as the dependent variables have been declared previously as variables or parameters.
1 parent c0fe364 commit ac993bb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/transform/utilities.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,26 @@ zstr(a::Assignment) = string_or_num(a.lhs)
1515

1616
string_or_num(a::Expr) = string(a)
1717
string_or_num(a::Symbol) = string(a)
18-
string_or_num(a::Number) = a
18+
string_or_num(a::Number) = a
19+
20+
# Uses Symbolics functions to generate a variable as a function of the dependent variables of choice (default: t)
21+
function genvar(a::Symbol, b=:t)
22+
vars = Symbol[]
23+
ex = Expr(:block)
24+
var_name, expr = Symbolics.construct_vars(:variables, a, Real, [b], nothing, nothing, identity, false)
25+
push!(vars, var_name)
26+
push!(ex.args, expr)
27+
rhs = Symbolics.build_expr(:vect, vars)
28+
push!(ex.args, rhs)
29+
eval(ex)
30+
end
31+
function genvar(a::Symbol, b::Vector{Symbol})
32+
vars = Symbol[]
33+
ex = Expr(:block)
34+
var_name, expr = Symbolics.construct_vars(:variables, a, Real, b, nothing, nothing, identity, false)
35+
push!(vars, var_name)
36+
push!(ex.args, expr)
37+
rhs = Symbolics.build_expr(:vect, vars)
38+
push!(ex.args, rhs)
39+
eval(ex)
40+
end

0 commit comments

Comments
 (0)