Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

TypeError: unsupported operand for '*': integer and function

To multiply an integer and a function seems to work in some cases:

f = lambda n: n
[2*f(n) for n in (0..9)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

On the other hand I often receive this error message but cannot see an error in my code:

/projects/sage/sage-6.9/src/sage/structure/element.pyx in sage.structure.element.RingElement.__mul__ (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/element.c:17265)()
   1852         elif PyInt_CheckExact(left):
   1853             return (<ModuleElement>right)._mul_long(PyInt_AS_LONG(left))
-> 1854         return coercion_model.bin_op(left, right, mul)
   1855 
   1856     cpdef RingElement _mul_(self, RingElement right):

    /projects/sage/sage-6.9/src/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/coerce.c:9739)()
   1068         # We should really include the underlying error.
   1069         # This causes so much headache.
-> 1070         raise TypeError(arith_error_message(x,y,op))
   1071 
   1072     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Can someone explain what is going wrong here?

TypeError: unsupported operand for '*': integer and function

To multiply an integer and a function seems to work in some cases:

f = lambda n: n
[2*f(n) for n in (0..9)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

On the other hand I often receive this error message but cannot see an error in my code:

/projects/sage/sage-6.9/src/sage/structure/element.pyx in sage.structure.element.RingElement.__mul__ (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/element.c:17265)()
   1852         elif PyInt_CheckExact(left):
   1853             return (<ModuleElement>right)._mul_long(PyInt_AS_LONG(left))
-> 1854         return coercion_model.bin_op(left, right, mul)
   1855 
   1856     cpdef RingElement _mul_(self, RingElement right):

    /projects/sage/sage-6.9/src/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/coerce.c:9739)()
   1068         # We should really include the underlying error.
   1069         # This causes so much headache.
-> 1070         raise TypeError(arith_error_message(x,y,op))
   1071 
   1072     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Can someone explain what is going wrong here?

Edit:

Now I see what is going wrong: my expectations lead me astray!

With Maple and f := n->n: two := n->2:

(1) seq(2*f(n), n=0..9);
(2) seq((2*f)(n), n=0..9);
(3) seq((two*f)(n), n=0..9);

All three versions lead to what I expect: 0, 2, 4, ...

Not so with SageMath! Here only (1) works, (2) and (3) give TypeErrors. I'd call Maple's approach more natural and user-friendly.

Perhaps things could be enhanced? The comment of the implementer "1069 # This causes so much headache" looks like this might be seen as a problem also by others.