symengine / SymEngine.jl

Julia wrappers of SymEngine
MIT License
192 stars 43 forks source link

Can't access type of operation #244

Closed PerformanceCoder closed 2 years ago

PerformanceCoder commented 2 years ago

Good morning!

Is there any way in SymEngine.jl to access the type of operation when going through the expression tree? For example, when using Python wrapper I can write something like this:

from symengine import *
x = symbols("x")

expr = 2.123456*x*sin(1.23456*x)

if type(expr) == Mul:
    multipliers = expr.args
    for m in multipliers:
        if type(m) == sin:
            print("Found a sin in expression!")

What can replace type function in Julia? Currently, typeof returns Basic for every expression, so it can't be used.

With best regards!

isuruf commented 2 years ago

I think typeof(BasicType(expr)) should work.

PerformanceCoder commented 2 years ago

Yes, it works:

using SymEngine
x, y = symbols("x, y")
expr = x + y + x*y + x^2 + x ^ 3 + x ^4 + y ^ 4
typeof(SymEngine.BasicType(expr))
#    SymEngine.BasicType{Val{:Add}}

Thank you very much!