Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Neil Martinsen-Burrell
On 2009-08-27 16:09 , Jonathan T wrote: > Hi, > > I want to define a 3-D array as the sum of two 2-D arrays as follows: > > C[x,y,z] := A[x,y] + B[x,z] > > My linear algebra is a bit rusty; is there a good way to do this that does not > require me to loop over x,y,z? Thanks! Numpy's broadcasti

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Jonathan T
Perfect, that is exactly what I was looking for. Thanks to all who responded. There is one more problem which currently has me stumped. Same idea but slightly different effect: V[p,x,r] := C[p, E[p,x,r], r] This multidimensional array stuff is confusing but the time savings seem to be worth i

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Charles R Harris
On Thu, Aug 27, 2009 at 3:32 PM, Citi, Luca wrote: > Or > a[:,:,None] + b[:,None,:] I think that is the way to go. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Damian Eads
Hi Jonathan, This isn't quite your typical linear algebra. NumPy has a nice feature called array broadcasting, which enables you to perform element-wise operations on arrays of different shapes. The number of dimensions of the arrays must be the same, in your case, all the arrays must have three d

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Citi, Luca
Or a[:,:,None] + b[:,None,:] ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Citi, Luca
One solution I can think of still requires one loop (instead of three): import numpy as np a = np.arange(12).reshape(3,4) b = np.arange(15).reshape(3,5) z = np.empty(a.shape + (b.shape[-1],)) for i in range(len(z)): z[i] = np.add.outer(a[i], b[i]) _

Re: [Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Christopher Barker
Jonathan T wrote: > I want to define a 3-D array as the sum of two 2-D arrays as follows: > >C[x,y,z] := A[x,y] + B[x,z] Is this what you mean? In [14]: A = np.arange(6).reshape((2,3,1)) In [15]: B = np.arange(12).reshape((1,3,4)) In [18]: A Out[18]: array([[[0], [1], [2

[Numpy-discussion] Efficiently defining a multidimensional array

2009-08-27 Thread Jonathan T
Hi, I want to define a 3-D array as the sum of two 2-D arrays as follows: C[x,y,z] := A[x,y] + B[x,z] My linear algebra is a bit rusty; is there a good way to do this that does not require me to loop over x,y,z? Thanks! Jonathan ___ NumPy-Discussi