Skip to content

Commit d7fa2b9

Browse files
Merge pull request #2671 from hersle/offset_indices
Fix structural simplification of non-1-indexed variable arrays
2 parents 2497d9b + 163be44 commit d7fa2b9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3636
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
3737
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
3838
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
39+
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3940
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
4041
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
4142
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"

src/structural_transformation/symbolics_tearing.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using OffsetArrays: Origin
2+
13
# N.B. assumes `slist` and `dlist` are unique
24
function substitution_graph(graph, slist, dlist, var_eq_matching)
35
ns = length(slist)
@@ -585,7 +587,12 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
585587
Symbolics.shape(lhs) !== Symbolics.Unknown() || continue
586588
arg1 = arguments(lhs)[1]
587589
haskey(obs_arr_subs, arg1) && continue
588-
obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)]
590+
obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]]
591+
index_first = eachindex(arg1)[1]
592+
593+
# respect non-1-indexed arrays
594+
# TODO: get rid of this hack together with the above hack, then remove OffsetArrays dependency
595+
obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1])
589596
end
590597
for i in eachindex(neweqs)
591598
neweqs[i] = fast_substitute(neweqs[i], obs_arr_subs; operator = Symbolics.Operator)

test/odesystem.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,13 @@ for sys in [sys1, sys2]
11641164
end
11651165
end
11661166

1167+
@testset "Non-1-indexed variable array (issue #2670)" begin
1168+
@variables x(t)[0:1] # 0-indexed variable array
1169+
@named sys = ODESystem([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], [])
1170+
@test_nowarn sys = structural_simplify(sys)
1171+
@test equations(sys) == [D(x[1]) ~ 0.0]
1172+
end
1173+
11671174
# Namespacing of array variables
11681175
@variables x(t)[1:2]
11691176
@named sys = ODESystem(Equation[], t)

0 commit comments

Comments
 (0)