Hi Mike and Jason, Thanks a lot for the quick response. My problem becomes a bit more obvious if I have a function of several variables. Then the map function becomes somehow impractical because I can't define which list is used for which variable. List comprehensions also get a lot more difficult. Example:
sage: var('a b c') sage: f = a*x^2+b sage: w = [4,5,6] sage: z = [7,8,9] sage: map(f,v,w,z) [53, 133, 249] sage: map(f,w,v,z) [197, 322, 489] I wouldn't even know how to do this with a list comprehension in one step. Since the functions I work with have to be applied to time series of quite a few variables, I am really desperate for a practical way of doing this. So far, all mathematical programs I worked with were able to do this and I bet that many people are used to applying functions to lists and arrays. Isn't this a lot faster than defining loops? Stan On Nov 19, 4:20 pm, Jason Grout <[EMAIL PROTECTED]> wrote: > Mike Hansen wrote: > > Hi Stan, > > > You should use Python's list comprehensions to do that: > > > sage: f = 2*x^3+1 > > sage: v = [1,2,3] > > sage: [f(x=a) for a in v] > > [3, 17, 55] > > > or you could do > > > sage: map(f, v) > > [3, 17, 55] > > The question was: Is there an easy way of > performing operations on arrays or lists without defining loops? > > The answer is: no, not really. Functions don't automatically thread > themselves over lists. List comprehensions and maps provide a simple > way to loop over a set, though. > > Personally, I think it's generally a good thing that functions don't > automatically thread themselves over lists. > > Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---