On Thu, Jan 22, 2009 at 10:46 PM, Robert Bradshaw <[email protected]> wrote: > > On Jan 22, 2009, at 10:25 PM, kcrisman wrote: > >> In 3.3.alpha0, the following works fine: >> >> sage: list= [[i,j] for i in [-3..3] for j in [-3..3]] >> sage: [coords for coords in list if (coords[0])^2+(coords[1])^2-1==0] >> [[-1, 0], [0, -1], [0, 1], [1, 0]] >> >> The following doesn't terminate in a reasonable amount of time: >> >> sage: f(x,y)=x^2+y^2-1 >> sage: list= [[i,j] for i in [-3..3] for j in [-3..3]] >> sage: [coords for coords in list if f(coords[0],coords[1])==0] >> >> In the notebook I can't even interrupt it; I have to restart the >> worksheet, so maybe there's some horrible recursion going on, but ... >> I mean, there are only 49 points to check ... right? What am I doing >> wrong in using a function this way? > > You could try letting f be a multi-variate polynomial rather than a > symbolic maxima expression. > > sage: R.<x,y> = ZZ[] > sage: f = x^2+y^2-1 > sage: list= [[i,j] for i in [-3..3] for j in [-3..3]] > sage: time [coords for coords in list if f(coords[0],coords[1])==0] > CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 s > Wall time: 0.01 s > [[-1, 0], [0, -1], [0, 1], [1, 0]] > > Searching an even larger range: > > sage: list= [[i,j] for i in [-30..30] for j in [-30..30]] > sage: time [coords for coords in list if f(coords[0],coords[1])==0] > CPU times: user 0.69 s, sys: 0.01 s, total: 0.70 s > Wall time: 0.74 s > [[-1, 0], [0, -1], [0, 1], [1, 0]] > > - Robert
You can also use fast float, which is way faster yet: sage: f(x,y)=x^2+y^2-1 sage: list= [[i,j] for i in [-3..3] for j in [-3..3]] sage: g = f._fast_float_() sage: time [coords for coords in list if g(coords[0],coords[1])==0] CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 s [[-1, 0], [0, -1], [0, 1], [1, 0]] and sage: list= [[i,j] for i in [-30..30] for j in [-30..30]] sage: time [coords for coords in list if g(coords[0],coords[1])==0] CPU times: user 0.10 s, sys: 0.03 s, total: 0.13 s Wall time: 0.13 s [[-1, 0], [0, -1], [0, 1], [1, 0]] By the way, Robert -- who answered the previous email -- wrote _fast_float. -- William --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
