Skip to content

Commit 42a0904

Browse files
Merge pull request #123 from JuliaDiffEq/parameter
simplified interface for declaring parameters
2 parents 254d106 + 0a1b3c2 commit 42a0904

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and parameters. Therefore we label them as follows:
2828
using ModelingToolkit
2929

3030
# Define some variables
31-
@parameters t() σ() ρ() β()
31+
@parameters t σ ρ β
3232
@variables x(t) y(t) z(t)
3333
@derivatives D'~t
3434
```
@@ -80,8 +80,8 @@ state of the previous ODE. This is the nonlinear system defined by where the
8080
derivatives are zero. We use (unknown) variables for our nonlinear system.
8181
8282
```julia
83-
@variables x() y() z()
84-
@parameters σ() ρ() β()
83+
@variables x y z
84+
@parameters σ ρ β
8585

8686
# Define a nonlinear system
8787
eqs = [0 ~ σ*(y-x),
@@ -166,12 +166,16 @@ including and excluding empty parentheses. When in call format, variables are
166166
aliased to the given call, allowing implicit use of dependents for convenience.
167167
168168
```julia
169-
@parameters t() α() σ
170-
@variables w x(t) y() z(t, α, x)
169+
@parameters t α σ(..)
170+
@variables w(..) x(t) y() z(t, α, x)
171171

172172
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
173173
```
174174
175+
Note that `@parameters` and `@variables` implicitly add `()` to values that
176+
are not given a call. The former specifies the values as known, while the
177+
latter specifies it as unknown. `(..)` signifies that the value should be
178+
left uncalled.
175179
176180
### Constants
177181
@@ -274,7 +278,7 @@ is accessible via a function-based interface. This means that all macros are
274278
syntactic sugar in some form. For example, the variable construction:
275279
276280
```julia
277-
@parameters t() σ ρ() β()
281+
@parameters t σ ρ β
278282
@variables x(t) y(t) z(t)
279283
@derivatives D'~t
280284
```
@@ -297,8 +301,8 @@ D = Differential(t)
297301
The system building functions can handle intermediate calculations. For example,
298302
299303
```julia
300-
@variables x() y() z()
301-
@parameters σ() ρ() β()
304+
@variables x y z
305+
@parameters σ ρ β
302306
a = y - x
303307
eqs = [0 ~ σ*a,
304308
0 ~ x*-z)-y,

src/variables.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ function _parse_vars(macroname, known, x)
4747

4848
if iscall
4949
var_name = _var.args[1]
50-
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)($(_var.args[2:end]...)))
50+
if _var.args[end] == :..
51+
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
52+
else
53+
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)($(_var.args[2:end]...)))
54+
end
5155
else
56+
# Implicit 0-args call
5257
var_name = _var
53-
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
58+
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)())
5459
end
5560

5661
push!(var_names, var_name)

test/derivatives.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ModelingToolkit
22
using Test
33

44
# Derivatives
5-
@parameters t() σ() ρ() β()
5+
@parameters t σ ρ β
66
@variables x(t) y(t) z(t)
77
@derivatives D'~t D2''~t Dx'~x
88

test/simplify.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit
22
using Test
33

4-
@parameters t()
4+
@parameters t
55
@variables x(t) y(t) z(t)
66

77
null_op = 0*t

test/system_construction.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ModelingToolkit
22
using Test
33

44
# Define some variables
5-
@parameters t() σ() ρ() β()
5+
@parameters t σ ρ β
66
@variables x(t) y(t) z(t)
77
@derivatives D'~t
88

@@ -60,7 +60,7 @@ fwt(FW, u, p, 0.2, 0.1)
6060
du [11, -3, -7]
6161
end
6262

63-
@parameters σ
63+
@parameters σ(..)
6464
eqs = [D(x) ~ σ(t-1)*(y-x),
6565
D(y) ~ x*-z)-y,
6666
D(z) ~ x*y - β*z]
@@ -127,7 +127,7 @@ test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β))
127127
end
128128

129129
@derivatives D'~t
130-
@parameters A() B() C()
130+
@parameters A B C
131131
_x = y / C
132132
eqs = [D(x) ~ -A*x,
133133
D(y) ~ A*x - B*_x]
@@ -140,8 +140,8 @@ de = ODESystem(eqs)
140140
end
141141

142142
# Now nonlinear system with only variables
143-
@variables x() y() z()
144-
@parameters σ() ρ() β()
143+
@variables x y z
144+
@parameters σ ρ β
145145

146146
# Define a nonlinear system
147147
eqs = [0 ~ σ*(y-x),

test/variable_parsing.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit
22
using Test
33

4-
@parameters t()
4+
@parameters t
55
@variables x(t) y(t) # test multi-arg
66
@variables z(t) # test single-arg
77
x1 = Variable(:x)(t)
@@ -12,10 +12,11 @@ z1 = Variable(:z)(t)
1212
@test isequal(z1, z)
1313

1414
@parameters begin
15-
t()
16-
s()
17-
σ
15+
t
16+
s
1817
end
18+
@parameters σ(..)
19+
1920
t1 = Variable(:t; known = true)()
2021
s1 = Variable(:s; known = true)()
2122
σ1 = Variable(; known = true)

0 commit comments

Comments
 (0)