Ask Your Question
1

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

asked 2015-12-29 09:42:33 +0200

Peter Luschny gravatar image

updated 2015-12-30 16:59:09 +0200

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.

edit retag flag offensive close merge delete

Comments

I edited my answer to answer the edit in your question.

slelievre gravatar imageslelievre ( 2015-12-30 19:49:52 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-12-29 12:54:49 +0200

slelievre gravatar image

updated 2015-12-30 19:47:40 +0200

Symbolic functions can be multiplied by two, Python functions cannot.

sage: f(n) = n
sage: g = lambda n: n
sage: 2 * f
n |--> 2*n
sage: 2 * g
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Note also that in 2*f(n) is not 2 times f applied to n, but 2 times f(n).

Related: some common issues with functions.

Edit.

The equivalent of Maple's f := n -> n in Sage would be f(n) = n.

Let us define

sage: two(n) = 2
sage: f(n) = n

Then:

sage: [(two*f)(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]
sage: [(2*f)(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]
sage: [2*f(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]
edit flag offensive delete link more

Comments

I thought that the equivalent of Maple's f := n -> n in Sage is f = lambda n: n. Therefore my version did not work. Thanks for pointing this out!

Peter Luschny gravatar imagePeter Luschny ( 2015-12-31 17:23:04 +0200 )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: 2015-12-29 09:42:33 +0200

Seen: 4,856 times

Last updated: Dec 30 '15