ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 31 Dec 2015 17:23:04 +0100TypeError: unsupported operand for '*': integer and functionhttps://ask.sagemath.org/question/31911/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.
Tue, 29 Dec 2015 09:42:33 +0100https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/Comment by slelievre for <p>To multiply an integer and a function seems to work in some cases:</p>
<pre><code>f = lambda n: n
[2*f(n) for n in (0..9)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
</code></pre>
<p>On the other hand I often receive this error message but cannot see an
error in my code:</p>
<pre><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'>'
</code></pre>
<p>Can someone explain what is going wrong here? </p>
<p><strong>Edit:</strong></p>
<p>Now I see what is going wrong: my expectations lead me astray!</p>
<p>With Maple and f := n->n: two := n->2: </p>
<pre><code>(1) seq(2*f(n), n=0..9);
(2) seq((2*f)(n), n=0..9);
(3) seq((two*f)(n), n=0..9);
</code></pre>
<p><em>All</em> three versions lead to what I expect: 0, 2, 4, ... </p>
<p>Not so with SageMath! Here only (1) works, (2) and (3) give TypeErrors. I'd call Maple's approach more natural and user-friendly.</p>
<p>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.</p>
https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?comment=31917#post-id-31917I edited my answer to answer the edit in your question.Wed, 30 Dec 2015 19:49:52 +0100https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?comment=31917#post-id-31917Answer by slelievre for <p>To multiply an integer and a function seems to work in some cases:</p>
<pre><code>f = lambda n: n
[2*f(n) for n in (0..9)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
</code></pre>
<p>On the other hand I often receive this error message but cannot see an
error in my code:</p>
<pre><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'>'
</code></pre>
<p>Can someone explain what is going wrong here? </p>
<p><strong>Edit:</strong></p>
<p>Now I see what is going wrong: my expectations lead me astray!</p>
<p>With Maple and f := n->n: two := n->2: </p>
<pre><code>(1) seq(2*f(n), n=0..9);
(2) seq((2*f)(n), n=0..9);
(3) seq((two*f)(n), n=0..9);
</code></pre>
<p><em>All</em> three versions lead to what I expect: 0, 2, 4, ... </p>
<p>Not so with SageMath! Here only (1) works, (2) and (3) give TypeErrors. I'd call Maple's approach more natural and user-friendly.</p>
<p>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.</p>
https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?answer=31912#post-id-31912Symbolic 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](http://doc.sagemath.org/html/en/tutorial/tour_functions.html).
*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]
Tue, 29 Dec 2015 12:54:49 +0100https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?answer=31912#post-id-31912Comment by Peter Luschny for <p>Symbolic functions can be multiplied by two, Python functions cannot.</p>
<pre><code>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'>'
</code></pre>
<p>Note also that in <code>2*f(n)</code> is not <code>2</code> times <code>f</code> applied to <code>n</code>, but <code>2</code> times <code>f(n)</code>.</p>
<p>Related: <a href="http://doc.sagemath.org/html/en/tutorial/tour_functions.html">some common issues with functions</a>.</p>
<p><em>Edit.</em></p>
<p>The equivalent of Maple's <code>f := n -> n</code> in Sage would be <code>f(n) = n</code>.</p>
<p>Let us define</p>
<pre><code>sage: two(n) = 2
sage: f(n) = n
</code></pre>
<p>Then:</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?comment=31940#post-id-31940I 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!Thu, 31 Dec 2015 17:23:04 +0100https://ask.sagemath.org/question/31911/typeerror-unsupported-operand-for-integer-and-function/?comment=31940#post-id-31940