Its not completely clear to me which arguments you want substituted
for, but assuming you would like the v-values to be x, w-values for a,
and z-values for b, you could do:

sage: var('a b c')
sage: f = a*x^2+b
sage: v = [1,2,3]
sage: w = [4,5,6]
sage: z = [7,8,9]
sage: [f.subs({x:v1,a:v2,b:v3}) for v1,v2,v3 in
cartesian_product_iterator([v,w,z])]
[11, 12, 13, 12, 13, 14, 13, 14, 15, 23, 24, 25, 27, 28, 29, 31, 32,
33, 43, 44, 45, 52, 53, 54, 61, 62, 63]

There is probably a more elegant way to do it with map but I don't
think the above is awful.

-M. Hampton

On Nov 19, 10:50 am, Stan Schymanski <[EMAIL PROTECTED]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to