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
3 changes: 2 additions & 1 deletion src/differentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ _repeat_apply(f, n) = n == 1 ? f : f ∘ _repeat_apply(f, n-1)
function _differential_macro(x)
ex = Expr(:block)
lhss = Symbol[]
x = x isa Tuple && first(x).head == :tuple ? first(x).args : x # tuple handling
x = flatten_expr!(x)
for di in x
@assert di isa Expr && di.args[1] == :~ "@derivatives expects a form that looks like `@derivatives D''~t E'~t`"
@assert di isa Expr && di.args[1] == :~ "@derivatives expects a form that looks like `@derivatives D''~t E'~t` or `@derivatives (D''~t), (E'~t)`"
lhs = di.args[2]
rhs = di.args[3]
order, lhs = count_order(lhs)
Expand Down
7 changes: 4 additions & 3 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ function _parse_vars(macroname, known, x)
# y
# z
# end
x = x isa Tuple && first(x) isa Expr && first(x).head == :tuple ? first(x).args : x # tuple handling
x = flatten_expr!(x)
for _var in x
iscall = isa(_var, Expr) && _var.head == :call
issym = _var isa Symbol
@assert iscall || issym "@$macroname expects a tuple of expressions (`@$macroname x y z(t)`)"
@assert iscall || issym "@$macroname expects a tuple of expressions or an expression of a tuple (`@$macroname x y z(t)` or `@$macroname x, y, z(t)`)"

if iscall
dependents = :(Variable[$(_var.args[2:end]...)])
Expand All @@ -71,8 +72,8 @@ function _parse_vars(macroname, known, x)
return ex
end
macro variables(xs...)
esc(_parse_vars(:Variable, false, xs))
esc(_parse_vars(:variables, false, xs))
end
macro parameters(xs...)
esc(_parse_vars(:Param, true, xs))
esc(_parse_vars(:parameters, true, xs))
end
2 changes: 2 additions & 0 deletions test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using Test
@variables x(t) y(t) z(t)
@derivatives D'~t D2''~t

@test @macroexpand(@derivatives D'~t D2''~t) == @macroexpand(@derivatives (D'~t), (D2''~t))

@test isequal(expand_derivatives(D(t)), 1)
@test isequal(expand_derivatives(D(D(t))), 0)

Expand Down
8 changes: 5 additions & 3 deletions test/variable_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ using ModelingToolkit
using Test

@parameters t
@variables x(t)
@variables y(t)
@variables z(t)
@variables x(t) y(t) # test multi-arg
@variables z(t) # test single-arg
x1 = Variable(:x, [t])
y1 = Variable(:y, [t])
z1 = Variable(:z, [t])
Expand Down Expand Up @@ -33,3 +32,6 @@ D1 = Differential(t)
@test convert(Expr, D) == D

@test isequal(x ≤ y + 1, (x < y + 1) | (x == y + 1))

@test @macroexpand(@parameters x, y, z(t)) == @macroexpand(@parameters x y z(t))
@test @macroexpand(@variables x, y, z(t)) == @macroexpand(@variables x y z(t))