ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 18 Aug 2013 01:40:32 -0500How to return a list from callable symbolic expressionhttp://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/I was (wrongfully) expecting the return type of all of the following function calls to be the same, namely a list. Why does the callable symbolic expression (1) return a vector, and how to rewrite (1) such that it does return a list?
# (1)
sage: f(x) = [x,x]
# (2)
sage: g = lambda x: [x,x]
# (3)
sage: def h(x):
....: return [x,x]
sage: type(f(x))
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
sage: type(g(x))
<type 'list'>
sage: type(h(x))
<type 'list'>
Sat, 17 Aug 2013 06:51:14 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/Answer by Luca for <p>I was (wrongfully) expecting the return type of all of the following function calls to be the same, namely a list. Why does the callable symbolic expression (1) return a vector, and how to rewrite (1) such that it does return a list?</p>
<pre><code># (1)
sage: f(x) = [x,x]
# (2)
sage: g = lambda x: [x,x]
# (3)
sage: def h(x):
....: return [x,x]
sage: type(f(x))
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
sage: type(g(x))
<type 'list'>
sage: type(h(x))
<type 'list'>
</code></pre>
http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?answer=15356#post-id-15356If you type
sage: f??
You will see that the method `.__call__` (responsible for the behaviour of evaluating `f`) is the one-liner
return vector([e(*args, **kwargs) for e in self])
This has been designed so that symbolic functions can safely be used in symbolic expressions in a meaningful way. For example, you can combine `f` with another symbolic function using the vector product operator `*`:
sage: f(x) = [x,x]
sage: f
x |--> (x, x)
sage: f * f
x |--> 2*x^2
This would not work with ordinary python lists.
There is no way around it. If you really only need to return a list, then an ordinary python function (like (2) or (3)) is probably enough. Or you can convert the output of `f` to a list like so:
sage: list(f(x))
Sat, 17 Aug 2013 07:32:49 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?answer=15356#post-id-15356Comment by Mark for <p>If you type</p>
<pre><code>sage: f??
</code></pre>
<p>You will see that the method <code>.__call__</code> (responsible for the behaviour of evaluating <code>f</code>) is the one-liner</p>
<pre><code>return vector([e(*args, **kwargs) for e in self])
</code></pre>
<p>This has been designed so that symbolic functions can safely be used in symbolic expressions in a meaningful way. For example, you can combine <code>f</code> with another symbolic function using the vector product operator <code>*</code>:</p>
<pre><code>sage: f(x) = [x,x]
sage: f
x |--> (x, x)
sage: f * f
x |--> 2*x^2
</code></pre>
<p>This would not work with ordinary python lists.</p>
<p>There is no way around it. If you really only need to return a list, then an ordinary python function (like (2) or (3)) is probably enough. Or you can convert the output of <code>f</code> to a list like so:</p>
<pre><code>sage: list(f(x))
</code></pre>
http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17149#post-id-17149(Maybe) that helps. Let me ask one for clarification. If I get you right, kind of your point is, that callable symbolic expression should allow for meaningful usage with symbolic operations like multiplication, addition a.s.o. But while a scalar product, as f*f may make sense (even though I wasn't even looking for that) the division f(x)/f(x) is also a symbolic operation, which does not make sense for me in case of vectors - although sage simply return f(x)/f(x) = 1 with my definition (1) Sat, 17 Aug 2013 09:19:34 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17149#post-id-17149Comment by Luca for <p>If you type</p>
<pre><code>sage: f??
</code></pre>
<p>You will see that the method <code>.__call__</code> (responsible for the behaviour of evaluating <code>f</code>) is the one-liner</p>
<pre><code>return vector([e(*args, **kwargs) for e in self])
</code></pre>
<p>This has been designed so that symbolic functions can safely be used in symbolic expressions in a meaningful way. For example, you can combine <code>f</code> with another symbolic function using the vector product operator <code>*</code>:</p>
<pre><code>sage: f(x) = [x,x]
sage: f
x |--> (x, x)
sage: f * f
x |--> 2*x^2
</code></pre>
<p>This would not work with ordinary python lists.</p>
<p>There is no way around it. If you really only need to return a list, then an ordinary python function (like (2) or (3)) is probably enough. Or you can convert the output of <code>f</code> to a list like so:</p>
<pre><code>sage: list(f(x))
</code></pre>
http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17146#post-id-17146This is due to the semantics of symbolic vectors in Sage, as it has been discussed in <http://ask.sagemath.org/question/2917/vector-division>. I agree that this kind of inverse of multiplication by a scalar is somewhat surprising, and that it does not seem to be documented anywhere.Sun, 18 Aug 2013 01:40:32 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17146#post-id-17146Comment by Luca for <p>If you type</p>
<pre><code>sage: f??
</code></pre>
<p>You will see that the method <code>.__call__</code> (responsible for the behaviour of evaluating <code>f</code>) is the one-liner</p>
<pre><code>return vector([e(*args, **kwargs) for e in self])
</code></pre>
<p>This has been designed so that symbolic functions can safely be used in symbolic expressions in a meaningful way. For example, you can combine <code>f</code> with another symbolic function using the vector product operator <code>*</code>:</p>
<pre><code>sage: f(x) = [x,x]
sage: f
x |--> (x, x)
sage: f * f
x |--> 2*x^2
</code></pre>
<p>This would not work with ordinary python lists.</p>
<p>There is no way around it. If you really only need to return a list, then an ordinary python function (like (2) or (3)) is probably enough. Or you can convert the output of <code>f</code> to a list like so:</p>
<pre><code>sage: list(f(x))
</code></pre>
http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17150#post-id-17150Edited my answer to account for your comment.Sat, 17 Aug 2013 08:34:10 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17150#post-id-17150Comment by Mark for <p>If you type</p>
<pre><code>sage: f??
</code></pre>
<p>You will see that the method <code>.__call__</code> (responsible for the behaviour of evaluating <code>f</code>) is the one-liner</p>
<pre><code>return vector([e(*args, **kwargs) for e in self])
</code></pre>
<p>This has been designed so that symbolic functions can safely be used in symbolic expressions in a meaningful way. For example, you can combine <code>f</code> with another symbolic function using the vector product operator <code>*</code>:</p>
<pre><code>sage: f(x) = [x,x]
sage: f
x |--> (x, x)
sage: f * f
x |--> 2*x^2
</code></pre>
<p>This would not work with ordinary python lists.</p>
<p>There is no way around it. If you really only need to return a list, then an ordinary python function (like (2) or (3)) is probably enough. Or you can convert the output of <code>f</code> to a list like so:</p>
<pre><code>sage: list(f(x))
</code></pre>
http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17151#post-id-17151Can you provide me with an example of what you mean by 'unsafe' or 'not meaningful' if the callable symbolic expression would actually return what I have asked for, namely a list?Sat, 17 Aug 2013 07:54:52 -0500http://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/?comment=17151#post-id-17151