Symbolics.jl icon indicating copy to clipboard operation
Symbolics.jl copied to clipboard

Complicatiosn when attempting to "deregister"(/expand) function in expression

Open TorkelE opened this issue 2 years ago • 0 comments

Per discussion with Shasi, this process is kind of messy (deregister function wroten by Shashi, I claim not credit for it!)

using Symbolics
import Symbolics: wrap, unwrap, issym, symtype

my_func(x,y) = x*y + 1
@register_symbolic my_func(x, y)

@variables X, Y
eq = 2my_func(X,Y)
deregister(my_func, eq)

deregister(f, expr) = wrap(Rewriters.Postwalk(Rewriters.PassThrough(deregisterer(f)))(unwrap(expr)))
function deregisterer(f)
    (expr) ->
    if istree(expr) && operation(expr) == f
        args = arguments(expr)
        invoke_with = map(args) do a
            t = typeof(a)
            issym(a) || istree(a) ? wrap(a) => symtype(a) : a => typeof(a)
        end 
        invoke(f, Tuple{last.(invoke_with)...}, first.(invoke_with)...)
    end
end

Here, I want to turn 2my_func(X,Y) into 2(1 + X*Y). While it succeeds, it is messy.

TorkelE avatar Sep 20 '23 00:09 TorkelE