JuliaSymbolics/SymbolicUtils.jl

@rule for Rewriting Arithmetic Operations on Functions

hiiroo opened this issue · 2 comments

Hello everyone,

I looked for this use case but couldn't find it. I wonder if writing a rule that will compose a new function is possible. Is this a valid use case of SymbolicUtils.jl?

f1(x,y,z)=x^2
f2(x,y,z)=y^2
r1 = @rule a(~x,~y,~z)::Function*b(~x,~y,~z)::Function=>(~x,~y,~z)->a(~x,~y,~z)*b(~x,~y,~z)
f12 = r1(f1(x,y,z)*f2(x,y,z)) # so that it would be possible call it like f12(x,y,z)

Surprisingly this is supported.

using Symbolics
r1 = @rule (~f)(~x, ~y, ~z) * (~g)(~x, ~y, ~z) => (v1, v2, v3) -> (~f)(v1, v2, v3) * (~g)(v1, v2, v3)
f1(x, y, z) = x^2
f2(x, y, z) = y^2
@variables a b c
@register_symbolic f1(a, b, c)
@register_symbolic f2(a, b, c)
expr = f1(a, b, c) * f2(a, b, c)
expr = Symbolics.value(expr)
f12 = r1(expr)
f12(3, 4, 5) == f1(3, 4, 5) * f2(3, 4, 5)

Note that @register_symbolic needs Symbolics.jl and @variables.

That's great!! Thanks a lot. I am also curious if there is a way to do it without actually registering it. I mean if maybe if they are wrapped like this;

symbolic(f1(a,b,c))*symbolic(f2(a,b,c))