Re: [Numpy-discussion] numpy-izing a loop
Stéfan van der Walt ste...@sun.ac.za writes: 2009/2/10 Stéfan van der Walt ste...@sun.ac.za: x = np.arange(dim) y = np.arange(dim)[:, None] z = np.arange(dim)[:, None, None] Do not operate heavy machinery or attempt broadcasting while tired or under the influence. That order was incorrect: z = np.arange(dim) y = np.arange(dim)[:, None] x = np.arange(dim)[:, None, None] Thanks to you both. I can confirm that the two functions below give the same result - as far as my comprehensive testing of one example shows :) def compute_voxels(depth_buffers): assert len(depth_buffers) == 6 dim = depth_buffers[0].shape[0] znear, zfar, ynear, yfar, xnear, xfar = depth_buffers result = numpy.empty((dim, dim, dim), numpy.bool) for x in xrange(dim): for y in xrange(dim): for z in xrange(dim): result[x, y, z] = ((xnear[y, z] xfar[y, z]) and (ynear[x, z] yfar[x, z]) and (znear[x, y] zfar[x, y])) return result def compute_voxels2(depth_buffers): dim = depth_buffers[0].shape[0] znear, zfar, ynear, yfar, xnear, xfar = depth_buffers z = numpy.arange(dim) y = numpy.arange(dim)[:, None] x = numpy.arange(dim)[:, None, None] return ((xnear[y,z] xfar[y, z]) (ynear[x, z] yfar[x, z]) (znear[x,y] zfar[x,y])) All that remains is for me to verify that it's actually the result I want :) (Well - I also need to learn to spot these things for myself, but I guess the intuition comes with practice.) ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] numpy-izing a loop
I've just written this snippet of code: result = numpy.empty((dim, dim, dim), numpy.bool) for x in xrange(dim): for y in xrange(dim): for z in xrange(dim): result[x, y, z] = ((xnear[y, z] xfar[y, z]) and (ynear[x, z] yfar[x, z]) and (znear[x, y] zfar[x, y])) Is there a way to get numpy to do the looping? ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] numpy-izing a loop
Hi Paul 2009/2/10 Paul Rudin p...@rudin.co.uk: I've just written this snippet of code: result = numpy.empty((dim, dim, dim), numpy.bool) for x in xrange(dim): for y in xrange(dim): for z in xrange(dim): result[x, y, z] = ((xnear[y, z] xfar[y, z]) and (ynear[x, z] yfar[x, z]) and (znear[x, y] zfar[x, y])) Is there a way to get numpy to do the looping? You can try the following: x = np.arange(dim) y = np.arange(dim) z = np.arange(dim) result = (xnear[y,z] xfar[y, z]) (ynear[x, z] yfar[x, z]) (znear[x,y] zfar[x,y]) It broadcasts correctly, but you'll have to verify the results to be sure. Cheers Stéfan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] numpy-izing a loop
On Tue, Feb 10, 2009 at 10:19, Stéfan van der Walt ste...@sun.ac.za wrote: Hi Paul 2009/2/10 Paul Rudin p...@rudin.co.uk: I've just written this snippet of code: result = numpy.empty((dim, dim, dim), numpy.bool) for x in xrange(dim): for y in xrange(dim): for z in xrange(dim): result[x, y, z] = ((xnear[y, z] xfar[y, z]) and (ynear[x, z] yfar[x, z]) and (znear[x, y] zfar[x, y])) Is there a way to get numpy to do the looping? You can try the following: x = np.arange(dim) y = np.arange(dim) z = np.arange(dim) result = (xnear[y,z] xfar[y, z]) (ynear[x, z] yfar[x, z]) (znear[x,y] zfar[x,y]) It broadcasts correctly, but you'll have to verify the results to be sure. It doesn't broadcast correctly. You will get result.shape == (dim,) but he wants (dim,dim,dim). You can get this with the following: x = np.arange(dim)[:,np.newaxis,np.newaxis] y = np.arange(dim)[np.newaxis,:,np.newaxis] z = np.arange(dim)[np.newaxis,np.newaxis,:] -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] numpy-izing a loop
2009/2/10 Robert Kern robert.k...@gmail.com: x = np.arange(dim)[:,np.newaxis,np.newaxis] y = np.arange(dim)[np.newaxis,:,np.newaxis] z = np.arange(dim)[np.newaxis,np.newaxis,:] Yes, sorry, I should have copied from my terminal. I think I had x = np.arange(dim) y = np.arange(dim)[:, None] z = np.arange(dim)[:, None, None] which broadcasts the same way. Cheers Stéfan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion