Re: [Numpy-discussion] iterate over multiple arrays

2011-10-01 Thread Charles R Harris
On Sat, Oct 1, 2011 at 11:34 AM, Olivier Delalleau wrote: > It'll work, it is equivalent to the suggestion I made in my previous post > with the f_inplace wrapper function (and it has the same drawback that numpy > will allocate temporary memory, which wouldn't be the case if f was working > in-p

Re: [Numpy-discussion] iterate over multiple arrays

2011-10-01 Thread Olivier Delalleau
It'll work, it is equivalent to the suggestion I made in my previous post with the f_inplace wrapper function (and it has the same drawback that numpy will allocate temporary memory, which wouldn't be the case if f was working in-place directly, by implementing it as "arr *= 2"). Note that you don

Re: [Numpy-discussion] iterate over multiple arrays

2011-10-01 Thread David Froger
Thanks everybody for the different solutions proposed, I really appreciate. What about this solution? So simple that I didn't think to it... import numpy as np from numpy import * def f(arr): return arr*2 a = array( [1,1,1] ) b = array( [2,2,2] ) c = array( [3,3,3] ) d = array( [4,4,4] )

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-13 Thread Sturla Molden
Den 12.09.2011 08:52, skrev David Froger: Hy everybody, I'm wondering what is the (best) way to apply the same function to multiple arrays. I tried to experiment a bit with this. Here is from an ipython session: Create some arrays: In [1]: import numpy as np In [2]: a = np.zeros(4) In [3]

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-13 Thread Olivier Delalleau
I agree with Robert, don't use locals(). I should have added a disclaimer "this is very hackish and probably not a good idea", sorry ;) (interesting read: http://stackoverflow.com/questions/1450275/modifying-locals-in-python) >From what you said I think what you really want is f to work in-place.

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-13 Thread Bruce Southey
On 09/13/2011 01:53 AM, David Froger wrote: > Thank you Olivier and Robert for your replies! > > Some remarks about the dictionnary solution: > > from numpy import * > > def f(arr): > return arr + 100. > > arrs = {} > arrs['a'] = array( [1,1,1] ) > arrs['b'] = array( [2,2,2] ) > arrs['c'] = a

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-13 Thread Robert Kern
On Tue, Sep 13, 2011 at 01:53, David Froger wrote: > > Thank you Olivier and Robert for your replies! > > Some remarks about the dictionnary solution: > > from numpy import * > > def f(arr): >     return arr + 100. > > arrs = {} > arrs['a'] = array( [1,1,1] ) > arrs['b'] = array( [2,2,2] ) > arrs[

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-12 Thread David Froger
Thank you Olivier and Robert for your replies! Some remarks about the dictionnary solution: from numpy import * def f(arr): return arr + 100. arrs = {} arrs['a'] = array( [1,1,1] ) arrs['b'] = array( [2,2,2] ) arrs['c'] = array( [3,3,3] ) arrs['d'] = array( [4,4,4] ) for key,value in arr

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-12 Thread Robert Kern
On Mon, Sep 12, 2011 at 01:52, David Froger wrote: > Hy everybody, > > I'm wondering what is the (best) way to apply the same function to multiple > arrays. > > For example, in the following code: > > from numpy import * > > def f(arr): >     return arr*2 > > a = array( [1,1,1] ) > b = array( [2,2

Re: [Numpy-discussion] iterate over multiple arrays

2011-09-12 Thread Olivier Delalleau
If you can make f work in-place then you can just call map(f, [a, b, c, d]): def f(arr): arr *= 2 Otherwise, you can: - Work with a list instead (a_b_c_d = map(f, a_b_c_d), with a_b_c_d = [a, b, c, d], but this won't update the local definitions of a, b, c, d). - Use locals(): for x in ('a', 'b

[Numpy-discussion] iterate over multiple arrays

2011-09-11 Thread David Froger
Hy everybody, I'm wondering what is the (best) way to apply the same function to multiple arrays. For example, in the following code: from numpy import * def f(arr): return arr*2 a = array( [1,1,1] ) b = array( [2,2,2] ) c = array( [3,3,3] ) d = array( [4,4,4] ) a = f(a) b = f(b) c = f(c