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.Wed, 14 Dec 2011 03:14:59 -0600Apply a function to a listhttp://ask.sagemath.org/question/8545/apply-a-function-to-a-list/Is there a simply syntax to apply a function like `f(x) = x*exp(x)` to a list of points `L` for example `L=[1,2,3,4,19]`. I want to be able to write something like `f(L)` which is a new list with the elements `[f(1),f(2),f(3),f(4),f(19)]`Sun, 11 Dec 2011 09:53:07 -0600http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/Answer by Laurent Claessens for <p>Is there a simply syntax to apply a function like <code>f(x) = x*exp(x)</code> to a list of points <code>L</code> for example <code>L=[1,2,3,4,19]</code>. I want to be able to write something like <code>f(L)</code> which is a new list with the elements <code>[f(1),f(2),f(3),f(4),f(19)]</code></p>
http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=11996#post-id-11996Alternatively you can use the comprehension list syntax :
f(x) = x*exp(x)
L = [1,2,3,4,19]
M = [f(x) for x in L]
As far as I know, this is a syntactic sugar for the map function proposed by benjaminfjonesSun, 11 Dec 2011 20:29:10 -0600http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=11996#post-id-11996Comment by DSM for <p>Alternatively you can use the comprehension list syntax :</p>
<pre><code>f(x) = x*exp(x)
L = [1,2,3,4,19]
M = [f(x) for x in L]
</code></pre>
<p>As far as I know, this is a syntactic sugar for the map function proposed by benjaminfjones</p>
http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?comment=20711#post-id-20711It's close. There's one unexpected scoping consequence, though-- after running the above code, the variable x is now equal to 19, not the usual var("x"). list(f(x) for x in L) doesn't have this problem.Wed, 14 Dec 2011 03:14:59 -0600http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?comment=20711#post-id-20711Answer by benjaminfjones for <p>Is there a simply syntax to apply a function like <code>f(x) = x*exp(x)</code> to a list of points <code>L</code> for example <code>L=[1,2,3,4,19]</code>. I want to be able to write something like <code>f(L)</code> which is a new list with the elements <code>[f(1),f(2),f(3),f(4),f(19)]</code></p>
http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=11989#post-id-11989Use python's built-in map function:
f(x) = x*exp(x)
L = [1,2,3,4,19]
M = map(f, L)Sun, 11 Dec 2011 10:11:41 -0600http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=11989#post-id-11989Answer by niles for <p>Is there a simply syntax to apply a function like <code>f(x) = x*exp(x)</code> to a list of points <code>L</code> for example <code>L=[1,2,3,4,19]</code>. I want to be able to write something like <code>f(L)</code> which is a new list with the elements <code>[f(1),f(2),f(3),f(4),f(19)]</code></p>
http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=13008#post-id-13008If you want/need something fancier, you can use the [`@parallel`](http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel) decorator. When you decorate a function definition with this, it automatically becomes a function which can take a list (or other iterable) of inputs. The output in that case is an iterator which computes the values of the original function on each list element, and it does so in parallel. This is probably worse than `map` if your function calls are very very fast, but if the computation takes more time it's quite useful. And it has the advantage of requiring no extra syntax for the function call. The tradeoff is that the output list is not just the list of function values, but also contains the input values too, and thus requires some parsing. This is because the outputs are returned in the order that computations are finished, not the order they are begun.
There are some other answers on this site which expand on the (pretty weak) manual description of `@parallel`. Here's one to start with: [When/How to use parallel](http://ask.sagemath.org/question/702/whenhow-to-use-parallel).Wed, 14 Dec 2011 02:30:57 -0600http://ask.sagemath.org/question/8545/apply-a-function-to-a-list/?answer=13008#post-id-13008