Skip to content

Commit 8fce9ca

Browse files
move indices to subscripts
1 parent 0c833cb commit 8fce9ca

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,24 @@ The `@variables` and `@parameters` macros support this with the following syntax
118118
julia> @variables x[1:3];
119119
julia> x
120120
3-element Array{Operation,1}:
121-
x[1]()
122-
x[2]()
123-
x[3]()
121+
x()
122+
x()
123+
x()
124124

125125
# support for arbitrary ranges and tensors
126126
julia> @variables y[2:3,1:5:6];
127127
julia> y
128128
2×2 Array{Operation,2}:
129-
y[2,1]() y[2,6]()
130-
y[3,1]() y[3,6]()
131-
129+
y₂̒₁() y₂̒₆()
130+
y₃̒₁() y₃̒₆()
132131

133132
# also works for dependent variables
134133
julia> @parameters t; @variables z[1:3](t);
135134
julia> z
136135
3-element Array{Operation,1}:
137-
z[1](t())
138-
z[2](t())
139-
z[3](t())
136+
z(t())
137+
z(t())
138+
z(t())
140139
```
141140
142141
## Core Principles
@@ -201,7 +200,7 @@ aliased to the given call, allowing implicit use of dependents for convenience.
201200
@parameters t α σ(..) β[1:2]
202201
@variables w(..) x(t) y() z(t, α, x)
203202

204-
expr = β[1] * x + y^α + σ(3) * (z - t) - β[2] * w(t - 1)
203+
expr = β* x + y^α + σ(3) * (z - t) - β * w(t - 1)
205204
```
206205
207206
Note that `@parameters` and `@variables` implicitly add `()` to values that

src/utils.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ $(SIGNATURES)
9797
Generate `ODESystem`, dependent variables, and parameters from an `ODEProblem`.
9898
"""
9999
function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
100-
t, = @parameters t; vars = [Variable(Symbol(:x, i))(t) for i in eachindex(prob.u0)]; params = [Variable(Symbol(, i); known = true)() for i in eachindex(prob.p)];
100+
t, = @parameters t;
101+
vars = [Variable(:x, i)(t) for i in eachindex(prob.u0)]
102+
params = [Variable(,i; known = true)() for i in eachindex(prob.p)]
101103
D, = @derivatives D'~t
102104

103105
rhs = [D(var) for var in vars]

src/variables.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
export Variable, @variables, @parameters
22

3+
const IndexMap = Dict{Int,Char}(
4+
0 => '',
5+
1 => '',
6+
2 => '',
7+
3 => '',
8+
4 => '',
9+
5 => '',
10+
6 => '',
11+
7 => '',
12+
8 => '',
13+
9 => '')
314

415
"""
516
$(TYPEDEF)
@@ -20,7 +31,7 @@ struct Variable <: Function
2031
Variable(name; known = false) = new(name, known)
2132
end
2233
function Variable(name, indices...; known = false)
23-
var_name = Symbol("$name[$(join(indices, ","))]")
34+
var_name = Symbol("$(name)$(join(getindex.((IndexMap,),indices), "̒"))")
2435
Variable(var_name; known=known)
2536
end
2637

@@ -113,7 +124,7 @@ end
113124
function _construct_var(var_name, known, call_args)
114125
if call_args === nothing
115126
:(Variable($(Meta.quot(var_name)); known = $known)())
116-
elseif call_args[end] == :..
127+
elseif !isempty(call_args) && call_args[end] == :..
117128
:(Variable($(Meta.quot(var_name)); known = $known))
118129
else
119130
:(Variable($(Meta.quot(var_name)); known = $known)($(call_args...)))
@@ -123,7 +134,7 @@ end
123134
function _construct_var(var_name, known, call_args, ind)
124135
if call_args === nothing
125136
:(Variable($(Meta.quot(var_name)), $ind...; known = $known)())
126-
elseif call_args[end] == :..
137+
elseif !isempty(call_args) && call_args[end] == :..
127138
:(Variable($(Meta.quot(var_name)), $ind...; known = $known))
128139
else
129140
:(Variable($(Meta.quot(var_name)), $ind...; known = $known)($(call_args...)))

0 commit comments

Comments
 (0)