Skip to content

Commit 2186298

Browse files
feat: support Differential(t) syntax in parse_variable
1 parent afd55cb commit 2186298

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/systems/abstractsystem.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,7 @@ Return the variable in `sys` referred to by its string representation `str`.
32923292
Roughly supports the following CFG:
32933293
32943294
```
3295-
varname = "D(" varname ")" | arrvar | maybe_dummy_var
3295+
varname = "D(" varname ")" | "Differential(" iv ")(" varname ")" | arrvar | maybe_dummy_var
32963296
arrvar = maybe_dummy_var "[idxs...]"
32973297
idxs = int | int "," idxs
32983298
maybe_dummy_var = namespacedvar | namespacedvar "(" iv ")" |
@@ -3310,9 +3310,18 @@ function parse_variable(sys::AbstractSystem, str::AbstractString)
33103310
# I'd write a regex to validate `str`, but https://xkcd.com/1171/
33113311
str = strip(str)
33123312
derivative_level = 0
3313-
while startswith(str, "D(") && endswith(str, ")")
3313+
while ((cond1 = startswith(str, "D(")) || startswith(str, "Differential(")) && endswith(str, ")")
3314+
if cond1
3315+
derivative_level += 1
3316+
str = _string_view_inner(str, 2, 1)
3317+
continue
3318+
end
3319+
_tmpstr = _string_view_inner(str, 13, 1)
3320+
if !startswith(_tmpstr, "$iv)(")
3321+
throw(ArgumentError("Expected differential with respect to independent variable $iv in $str"))
3322+
end
33143323
derivative_level += 1
3315-
str = _string_view_inner(str, 2, 1)
3324+
str = _string_view_inner(_tmpstr, length(iv) + 2, 0)
33163325
end
33173326

33183327
arr_idxs = nothing

test/variable_utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ end
8888
("D(😄.x($iv))", D(sys.😄.x)),
8989
("D(😄₊x)", D(sys.😄.x)),
9090
("D(😄₊x($iv))", D(sys.😄.x)),
91+
("Differential($iv)(😄.x)", D(sys.😄.x)),
92+
("Differential($iv)(😄.x($iv))", D(sys.😄.x)),
93+
("Differential($iv)(😄₊x)", D(sys.😄.x)),
94+
("Differential($iv)(😄₊x($iv))", D(sys.😄.x)),
9195
# other derivative
9296
("😄.xˍ$iv", D(sys.😄.x)),
9397
("😄.x($iv$iv", D(sys.😄.x)),
@@ -113,6 +117,12 @@ end
113117
("D(arr₊x[1])", D(sys.arr.x[1])),
114118
("D(arr.x($iv)[1])", D(sys.arr.x[1])),
115119
("D(arr₊x($iv)[1])", D(sys.arr.x[1])),
120+
("Differential($iv)(arr.x($iv))", D(sys.arr.x)),
121+
("Differential($iv)(arr₊x($iv))", D(sys.arr.x)),
122+
("Differential($iv)(arr.x[1])", D(sys.arr.x[1])),
123+
("Differential($iv)(arr₊x[1])", D(sys.arr.x[1])),
124+
("Differential($iv)(arr.x($iv)[1])", D(sys.arr.x[1])),
125+
("Differential($iv)(arr₊x($iv)[1])", D(sys.arr.x[1])),
116126
# other derivative
117127
("arr.xˍ$iv", D(sys.arr.x)),
118128
("arr₊xˍ$iv", D(sys.arr.x)),

0 commit comments

Comments
 (0)