# how find the operator type in general for an expression

Using SageMath version 8.9, Release Date: 2019-09-29

This is a follow up new question related to my question what-is-sage-equivalent-to-pow-in-sympy

I tried to use the method shown in the answer above, but it is not working and after some search, I can't figure how to do it. I am translating function in sympy to sagemath. In sympy, I can find if the operator in an expression is multiplication as in 3*x or addition, as in 3+x using isinstance(expr,Mul) and using isinstance(expr,Add) and so on. Here is an example in sympy

>>> expr=3+x
True

>>> expr=3*x
>>> isinstance(expr,Mul)
True

>>> expr=x**3
>>> isinstance(expr,Pow)
True


The answer in the link above showed how to do it for the third example above. When I tried to use that answer to help with the first 2 examples, it is not working. Here is what I did in sagemath

sage: var('x')
sage: expr=x^3
sage: expr.operator()==operator.pow
True

sage: expr=3+x
False

sage: expr=3*x
sage: expr.operator()==operator.mul
False


I thought may be I am using wrong name for the operator. I looked at attributes of operator using the command

sage: print(dir(operator))
['__abs__', '__add__', '__and__', '__concat__', '__contains__', '__delitem__', '__delslice__', '__div__', '__doc__', '__eq__', '__file__', '__floordiv__', '__ge__', '__getitem__', '__getslice__', '__gt__', '__iadd__', '__iand__', '__iconcat__', '__idiv__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__index__', '__inv__', '__invert__', '__ior__', '__ipow__', '__irepeat__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__', '__pos__', '__pow__', '__repeat__', '__rshift__', '__setitem__', '__setslice__', '__sub__', '__truediv__', '__xor__', '_compare_digest', 'abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem', 'delslice', 'div', 'eq', 'floordiv', 'ge', 'getitem', 'getslice', 'gt', 'iadd', 'iand', 'iconcat', 'idiv', 'ifloordiv', 'ilshift', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irepeat', 'irshift', 'isCallable', 'isMappingType', 'isNumberType', 'isSequenceType', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'lshift', 'lt', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'repeat', 'rshift', 'sequenceIncludes', 'setitem', 'setslice', 'sub', 'truediv', 'truth', 'xor']


And

sage: expr=3*x
sage: expr.operator()
<function mul_vararg at 0x7f82483953d0>


Which does not display the same thing as the case where it worked with pow:

sage: expr=x^3
sage: expr.operator()
<built-in function pow>


Here is says built-in function pow, and I guess this is why expr.operator()==operator.pow worked above but not with + and *

Question is: How to translate isinstance(expr,Mul) and isinstance(expr,Add) to sagemath?

edit retag close merge delete

Sort by ยป oldest newest most voted

Based on your research of calling .operator() on an expression, you have to find where mul_vararg (and similarly add_vararg) are defined, so you can compare with them. Here it is:

sage: from sage.symbolic.operators import add_vararg, mul_vararg
sage: expr_a = 3 + x
True
sage: expr_m = 3*x
sage: expr_m.operator() == mul_vararg
True

more

Thanks. I actually did do expr_a.operator() == add_vararg but got an error. This is because I did not know I needed to add import add_vararg, mul_vararg.

( 2019-12-24 11:29:42 +0200 )edit
2

it appears we need to give the name of the variable concerned for operands functions add and mul

var('x a b c')
expr= x^3*sqrt(c)+(a+b)

for o in expr.operands():
show("mul c: ",o.mul(c))
for o in expr.operands():
for o in expr.operands():
show("mul x: ",o.mul(x))
for o in expr.operands():

( 2019-12-24 13:06:54 +0200 )edit

## Stats

Seen: 87 times

Last updated: Dec 24 '19