From a6600803d71f240a862458d5d1d0cbd74e579fbc Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 9 Jun 2024 07:13:42 -0400 Subject: [PATCH 1/2] Fix nested diff2term edge case --- src/utils.jl | 11 ++++++----- test/downstream/modeling_toolkit_utils.jl | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 5d56b1998..794768d1f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -111,7 +111,6 @@ 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)), @@ -119,11 +118,13 @@ function diff2term(O, O_metadata::Union{Dict, Nothing, Base.ImmutableDict}=nothi 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 oldop isa Function + @show "here?" + return nothing 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])..., "]") diff --git a/test/downstream/modeling_toolkit_utils.jl b/test/downstream/modeling_toolkit_utils.jl index a48e2f450..e9fbb75db 100644 --- a/test/downstream/modeling_toolkit_utils.jl +++ b/test/downstream/modeling_toolkit_utils.jl @@ -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)) From 448377b97667e1c6b7a3c1455fd4114d520c359b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 9 Jun 2024 08:01:48 -0400 Subject: [PATCH 2/2] only fallback to function --- src/utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 794768d1f..99af0edaa 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -120,15 +120,16 @@ function diff2term(O, O_metadata::Union{Dict, Nothing, Base.ImmutableDict}=nothi oldop = operation(O) opname = if issym(oldop) string(nameof(oldop)) - elseif oldop isa Function - @show "here?" - return nothing elseif iscall(oldop) && operation(oldop) === getindex 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 ?