Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix fast_substitute folding array of symbolics #1398

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ function fast_substitute(expr, subs; operator = Nothing)
args = let canfold = canfold
map(args) do x
x′ = fast_substitute(x, subs; operator)
canfold[] = canfold[] && !(x′ isa Symbolic)
canfold[] = canfold[] && (symbolic_type(x′) == NotSymbolic() && !is_array_of_symbolics(x′))
x′
end
end
Expand All @@ -633,7 +633,7 @@ function fast_substitute(expr, pair::Pair; operator = Nothing)
args = let canfold = canfold
map(args) do x
x′ = fast_substitute(x, pair; operator)
canfold[] = canfold[] && !(x′ isa Symbolic)
canfold[] = canfold[] && (symbolic_type(x′) == NotSymbolic() && !is_array_of_symbolics(x′))
x′
end
end
Expand All @@ -645,6 +645,13 @@ function fast_substitute(expr, pair::Pair; operator = Nothing)
metadata(expr))
end

function is_array_of_symbolics(x)
symbolic_type(x) == ArraySymbolic() && return true
symbolic_type(x) == ScalarSymbolic() && return false
x isa AbstractArray &&
any(y -> symbolic_type(y) != NotSymbolic() || is_array_of_symbolics(y), x)
end

function getparent(x, val=_fail)
maybe_parent = getmetadata(x, Symbolics.GetindexParent, nothing)
if maybe_parent !== nothing
Expand Down
2 changes: 1 addition & 1 deletion test/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ end
lapu = wrap(lapu)
lapv = wrap(lapv)

f, g = build_function(dtu, u, v, t, expression=Val{false})
f, g = build_function(dtu, u, v, t, expression=Val{false}, nanmath = false)
du = zeros(Num, 8, 8)
f(du, u,v,t)
@test isequal(collect(du), collect(dtu))
Expand Down
10 changes: 9 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,12 @@ end
test_nested_derivative = Dx(Dt(Dt(u)))
result = diff2term(Symbolics.value(test_nested_derivative))
@test typeof(result) === Symbolics.BasicSymbolic{Real}
end
end

@testset "`fast_substitute` inside array symbolics" begin
@variables x y z
@register_symbolic foo(a::AbstractArray, b)
ex = foo([x, y], z)
ex2 = Symbolics.fixpoint_sub(ex, Dict(y => 1.0, z => 2.0))
@test isequal(ex2, foo([x, 1.0], 2.0))
end
Loading