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

How to substitute certain function?

Open zhaoli-IHEP opened this issue 6 years ago • 6 comments

For example

@vars a b x y

@funs f g

ex1 = f(a,b)

ex2 = subs( ex1, f(x_,y_), g(x_+y_) ) # maybe some wildcard for the arguments?

An alternative approach I can imagine is to iterate over the expression tree. So I found there are functions "get_name" and "get_args". However, it is not very clear for "get_name" to show the operation + and *

If neither above works, it seems I have to turn the expression into String and using regex to implement the substitution, but this lose the point to use SymEngine.

zhaoli-IHEP avatar Dec 26 '19 03:12 zhaoli-IHEP

What you can do right now is recursively call get_args on the expression and use get_name if the expression is a FunctionSymbol to create a dictionary for subs. I agree that this is a useful thing to implement in the C++ library.

isuruf avatar Dec 28 '19 07:12 isuruf

What you can do right now is recursively call get_args on the expression and use get_name if the expression is a FunctionSymbol to create a dictionary for subs. I agree that this is a useful thing to implement in the C++ library.

Thank you for your reply. It could be an effective approach. However, how can I know if it is a FunctionSymbol?

zhaoli-IHEP avatar Dec 28 '19 07:12 zhaoli-IHEP

You can use get_symengine_class(x) == :FunctionSymbol

isuruf avatar Dec 29 '19 05:12 isuruf

You can use get_symengine_class(x) == :FunctionSymbol

Thank you!

zhaoli-IHEP avatar Dec 30 '19 12:12 zhaoli-IHEP

Hi zhaoli-IHEP, did you code your recursive function ? I have the same type of problem and if you have an effective function I am very interesting.

Thank's a lot.

Myrddinlefou avatar Sep 26 '20 22:09 Myrddinlefou

Hi zhaoli-IHEP, did you code your recursive function ? I have the same type of problem and if you have an effective function I am very interesting.

Thank's a lot.

Hi, in fact we did not make generic function to handle this problem. What we do is to use function_symbols to find all functions and then filter them and then make replacement subs for each specific instance.

zhaoli-IHEP avatar Sep 27 '20 08:09 zhaoli-IHEP