Ask Your Question
2

Why does @parallel change my outputs?

asked 2011-08-12 12:46:40 +0100

benjaminfjones gravatar image

updated 2013-06-03 11:17:14 +0100

tmonteil gravatar image

Can someone explain this behavior to me:

sage: @parallel
....: def foo(n):
....:         return str(factor(n))
....:     
sage: foo(10)
'2 * 5'
sage: for x in foo([1..10]):
....:         print x[0][0][0]
....: 
1
2
3
4
5
6
7
8
9
10

I expected to get the factorizations of 1 .. 10, not the numbers 1 .. 10. It seems like the output of foo() is getting evaluated somehow when it goes through the @parallel decorator. Is this a bug?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2011-08-12 13:45:37 +0100

niles gravatar image

x[0] holds the arguments to foo, x[1] holds the output :) Try

sage: @parallel
....: def foo(n):
....:         return str(factor(n))
....:     
sage: foo(10)
'2 * 5'
sage: for x in foo([1..10]):
....:         print x[0][0][0],'->', x[1]
....: 
2 -> 2
1 -> 1
3 -> 3
4 -> 2^2
5 -> 5
6 -> 2 * 3
7 -> 7
8 -> 2^3
9 -> 3^2
10 -> 2 * 5
edit flag offensive delete link more

Comments

Of course! Thanks, that was a dumb question.

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-12 13:52:29 +0100 )edit

Ha! No worries :)

niles gravatar imageniles ( 2011-08-12 14:00:49 +0100 )edit

I totally missed this when I was looking at it the other day - good thing Niles is on the job :)

kcrisman gravatar imagekcrisman ( 2011-08-13 21:05:08 +0100 )edit

Sorry, why are you printing x[0][0][0], why doesn't x[0] suffice?

StevenPollack gravatar imageStevenPollack ( 2011-08-13 21:59:33 +0100 )edit

As @niles says, x[0] contains the inputs to the parallel function, but the decorator puts them in a normal form which involves a triple nested tuple. That's what I was getting inside of with x[0][0][0].

benjaminfjones gravatar imagebenjaminfjones ( 2011-08-13 23:00:49 +0100 )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

Stats

Asked: 2011-08-12 12:46:40 +0100

Seen: 538 times

Last updated: Aug 12 '11