Skip to content

Commit 32613f3

Browse files
committed
Fix structural simplification of non-1-indexed variable arrays
1 parent b4f14a4 commit 32613f3

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
@@ -32,6 +32,7 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
3232
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3333
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
3434
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
35+
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3536
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
3637
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
3738
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"

src/structural_transformation/symbolics_tearing.jl

Lines changed: 5 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)
@@ -573,7 +575,9 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
573575
Symbolics.shape(lhs) !== Symbolics.Unknown() || continue
574576
arg1 = arguments(lhs)[1]
575577
haskey(obs_arr_subs, arg1) && continue
576-
obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)]
578+
obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]]
579+
index_first = eachindex(arg1)[1]
580+
obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) # respect non-1-indexed arrays
577581
end
578582
for i in eachindex(neweqs)
579583
neweqs[i] = fast_substitute(neweqs[i], obs_arr_subs; operator = Symbolics.Operator)

test/odesystem.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,3 +1159,13 @@ for sys in [sys1, sys2]
11591159
@test variable_index(sys, x[i]) == variable_index(sys, x)[i]
11601160
end
11611161
end
1162+
1163+
@testset "Non-1-indexed variable array (issue #2670)" begin
1164+
@variables x(t)[0:1] # 0-indexed variable array
1165+
sys = ODESystem([
1166+
x[0] ~ 0.0
1167+
D(x[1]) ~ x[0]
1168+
], t, [x], []; name=:sys)
1169+
@test_nowarn sys = structural_simplify(sys)
1170+
@test equations(sys) == [D(x[1]) ~ 0.0]
1171+
end

0 commit comments

Comments
 (0)