[Numpy-discussion] repeat array along new axis without making a copy
Hi I'd like to repeat an array along a new axis (like broadcast): In [8]: a Out[8]: array([[0, 1, 2], [3, 4, 5]]) In [9]: b=repeat(a[None,...], 3, axis=0) In [10]: b Out[10]: array([[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]]) In [18]: id(a); id(b[0,...]); id(b[1,...]); id(b[2,...]) Out[18]: 40129600 Out[18]: 39752080 Out[18]: 40445232 Out[18]: 40510272 Can I do this such that each sub-array b[i,...] is a view and not a copy? Background: I'm working on a container class to store trajectory-like data. The API requires that each array has a time axis (axis=0 here) along which sub-arrays are stored which may be the same in some cases. Then, I don't want to store redundant information if possible. Thanks! best, Steve ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] repeat array along new axis without making a copy
On Wed, Feb 15, 2012 at 2:25 AM, Steve Schmerler elcort...@googlemail.comwrote: Hi I'd like to repeat an array along a new axis (like broadcast): In [8]: a Out[8]: array([[0, 1, 2], [3, 4, 5]]) In [9]: b=repeat(a[None,...], 3, axis=0) In [10]: b Out[10]: array([[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]]) In [18]: id(a); id(b[0,...]); id(b[1,...]); id(b[2,...]) Out[18]: 40129600 Out[18]: 39752080 Out[18]: 40445232 Out[18]: 40510272 Can I do this such that each sub-array b[i,...] is a view and not a copy? Yes, such an array can be created using the as_strided() function from the module numpy.lib.stride_tricks: In [1]: from numpy.lib.stride_tricks import as_strided In [2]: a = array([[1,2,3],[4,5,6]]) In [3]: b = as_strided(a, strides=(0, a.strides[0], a.strides[1]), shape=(3, a.shape[0], a.shape[1])) In [4]: b Out[4]: array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) In [5]: a[0,0] = 99 In [6]: b Out[6]: array([[[99, 2, 3], [ 4, 5, 6]], [[99, 2, 3], [ 4, 5, 6]], [[99, 2, 3], [ 4, 5, 6]]]) Warren ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] repeat array along new axis without making a copy
On Feb 15 06:25 -0600, Warren Weckesser wrote: Yes, such an array can be created using the as_strided() function from the module numpy.lib.stride_tricks: Thank you, I will look into that. best, Steve ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] repeat array along new axis without making a copy
Wow, I wasn't aware of that even if I work with numpy for years now. NumPy is amazing. Samuel ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] repeat array along new axis without making a copy
On Wed, Feb 15, 2012 at 15:16, Samuel John sc...@samueljohn.de wrote: Wow, I wasn't aware of that even if I work with numpy for years now. NumPy is amazing. It's deliberately unpublicized because you can cause segfaults if you get your math wrong. But once you get your math right and can wrap it up into a utility function, it works great. -- 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://mail.scipy.org/mailman/listinfo/numpy-discussion