Skip to content

Commit 0e41e04

Browse files
committed
Prepare test for array variables (until expand_derivatives bug is fixed)
1 parent ea5a003 commit 0e41e04

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/systems/diffeqs/basic_transformations.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi
121121

122122
# 1) Utility that performs the chain rule on an expression, e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt
123123
function chain_rule(ex)
124-
for var_of_iv1 in vars(ex; op = Nothing) # loop over all variables in expression, e.g. f(t) (op = Nothing so "D(f(t))" yields only "f(t)" and not "D(f(t))"; we want to replace *inside* derivative signs)
125-
if iscall(var_of_iv1) && isequal(only(arguments(var_of_iv1)), iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t))
126-
var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(t) -> f(u(t))
124+
for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable)
125+
is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # is the expression of the form f(t)?
126+
if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # substitute f(t) -> f(u(t)), but not u(t) -> u(u(t))
127+
var_of_iv1 = var # e.g. f(t)
128+
var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t))
127129
ex = substitute(ex, var_of_iv1 => var_of_iv2)
128130
end
129131
end

test/basic_transformations.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ end
102102
end
103103

104104
@testset "Change independent variable (simple)" begin
105-
@variables x(t)
106-
Mt = ODESystem([D(x) ~ 2*x], t; name = :M)
105+
@variables x(t) y1(t) # y(t)[1:1] # TODO: use array variables y(t)[1:2] when fixed: https://github.com/JuliaSymbolics/Symbolics.jl/issues/1383
106+
Mt = ODESystem([D(x) ~ 2*x, D(y1) ~ y1], t; name = :M)
107107
Mx = change_independent_variable(Mt, x; dummies = true)
108-
@test (@variables x xˍt(x) xˍtt(x); Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt]))
108+
@variables x xˍt(x) xˍtt(x) y1(x) # y(x)[1:1] # TODO: array variables
109+
Dx = Differential(x)
110+
@test (Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt, xˍt*Dx(y1) ~ y1]))
109111
end
110112

111113
@testset "Change independent variable (free fall)" begin

0 commit comments

Comments
 (0)