Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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