Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how find the operator type in general for an expression

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 for the first 2 examples, it is not working. Here is what I did in sagemath

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?

how find the operator type in general for an expression

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 for to help with the first 2 examples, it is not working. Here is what I did in sagemath

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?

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
>>> 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?