Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,24 @@ The `@variables` and `@parameters` macros support this with the following syntax
julia> @variables x[1:3];
julia> x
3-element Array{Operation,1}:
x[1]()
x[2]()
x[3]()
x()
x()
x()

# support for arbitrary ranges and tensors
julia> @variables y[2:3,1:5:6];
julia> y
2×2 Array{Operation,2}:
y[2,1]() y[2,6]()
y[3,1]() y[3,6]()

y₂̒₁() y₂̒₆()
y₃̒₁() y₃̒₆()

# also works for dependent variables
julia> @parameters t; @variables z[1:3](t);
julia> z
3-element Array{Operation,1}:
z[1](t())
z[2](t())
z[3](t())
z(t())
z(t())
z(t())
```

## Core Principles
Expand Down Expand Up @@ -201,7 +200,7 @@ aliased to the given call, allowing implicit use of dependents for convenience.
@parameters t α σ(..) β[1:2]
@variables w(..) x(t) y() z(t, α, x)

expr = β[1] * x + y^α + σ(3) * (z - t) - β[2] * w(t - 1)
expr = β* x + y^α + σ(3) * (z - t) - β * w(t - 1)
```

Note that `@parameters` and `@variables` implicitly add `()` to values that
Expand Down
4 changes: 3 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ $(SIGNATURES)
Generate `ODESystem`, dependent variables, and parameters from an `ODEProblem`.
"""
function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
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)];
t, = @parameters t;
vars = [Variable(:x, i)(t) for i in eachindex(prob.u0)]
params = [Variable(:α,i; known = true)() for i in eachindex(prob.p)]
D, = @derivatives D'~t

rhs = [D(var) for var in vars]
Expand Down
17 changes: 14 additions & 3 deletions src/variables.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
export Variable, @variables, @parameters

const IndexMap = Dict{Int,Char}(
0 => '₀',
1 => '₁',
2 => '₂',
3 => '₃',
4 => '₄',
5 => '₅',
6 => '₆',
7 => '₇',
8 => '₈',
9 => '₉')

"""
$(TYPEDEF)
Expand All @@ -20,7 +31,7 @@ struct Variable <: Function
Variable(name; known = false) = new(name, known)
end
function Variable(name, indices...; known = false)
var_name = Symbol("$name[$(join(indices, ","))]")
var_name = Symbol("$(name)$(join(getindex.((IndexMap,),indices), "̒"))")
Variable(var_name; known=known)
end

Expand Down Expand Up @@ -113,7 +124,7 @@ end
function _construct_var(var_name, known, call_args)
if call_args === nothing
:(Variable($(Meta.quot(var_name)); known = $known)())
elseif call_args[end] == :..
elseif !isempty(call_args) && call_args[end] == :..
:(Variable($(Meta.quot(var_name)); known = $known))
else
:(Variable($(Meta.quot(var_name)); known = $known)($(call_args...)))
Expand All @@ -123,7 +134,7 @@ end
function _construct_var(var_name, known, call_args, ind)
if call_args === nothing
:(Variable($(Meta.quot(var_name)), $ind...; known = $known)())
elseif call_args[end] == :..
elseif !isempty(call_args) && call_args[end] == :..
:(Variable($(Meta.quot(var_name)), $ind...; known = $known))
else
:(Variable($(Meta.quot(var_name)), $ind...; known = $known)($(call_args...)))
Expand Down