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 nested diff2term edge case #1166

Merged
merged 2 commits into from
Jun 9, 2024
Merged
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
12 changes: 7 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,25 @@ function diff2term(O, O_metadata::Union{Dict, Nothing, Base.ImmutableDict}=nothi
ds = nothing
end
d_separator = 'ˍ'
local opname

if ds === nothing
return maketerm(typeof(O), head(O), map(diff2term, children(O)),
symtype(O), O_metadata isa Nothing ?
metadata(O) : Base.ImmutableDict(metadata(O)..., O_metadata...))
else
oldop = operation(O)
if issym(oldop)
opname = string(nameof(oldop))
opname = if issym(oldop)
string(nameof(oldop))
elseif iscall(oldop) && operation(oldop) === getindex
opname = string(nameof(arguments(oldop)[1]))
args = arguments(O)
string(nameof(arguments(oldop)[1]))
elseif oldop == getindex
args = arguments(O)
opname = string(tosymbol(args[1]), "[", map(tosymbol, args[2:end])..., "]")
return Sym{symtype(O)}(Symbol(opname, d_separator, ds))
elseif oldop isa Function
return nothing
else
error("diff2term case not handled: $oldop")
end
newname = occursin(d_separator, opname) ? Symbol(opname, ds) : Symbol(opname, d_separator, ds)
return setname(maketerm(typeof(O), rename(oldop, newname), children(O), symtype(O), O_metadata isa Nothing ?
Expand Down
6 changes: 6 additions & 0 deletions test/downstream/modeling_toolkit_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ D = Differential(t)
)
)
)

@variables x y t
D = Differential(t)
Symbolics.diff2term(D(x))
Symbolics.diff2term(D(sqrt(sqrt(sqrt(+(x,y))))))
Symbolics.diff2term(D(x+y))
Loading