Ask Your Question
1

how find the operator type in general for an expression

asked 2019-12-24 04:13:06 +0100

Nasser gravatar image

updated 2019-12-24 04:20:00 +0100

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
>>> isinstance(expr,Add)
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
sage: expr.operator()==operator.add
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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-12-24 11:18:01 +0100

rburing gravatar image

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
sage: expr_a.operator() == add_vararg
True
sage: expr_m = 3*x
sage: expr_m.operator() == mul_vararg
True
edit flag offensive delete link more

Comments

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.

Nasser gravatar imageNasser ( 2019-12-24 11:29:42 +0100 )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():
    show("add c: ",o.add(c))
for o in expr.operands():
    show("mul x: ",o.mul(x))    
for o in expr.operands():
    show("add a: ",o.add(a))
ortollj gravatar imageortollj ( 2019-12-24 13:06:54 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-12-24 04:13:06 +0100

Seen: 467 times

Last updated: Dec 24 '19