Striding on NumArray objects
![](https://secure.gravatar.com/avatar/5c7407de6b47afcd3b3e2164ff5bcd45.jpg?s=120&d=mm&r=g)
Hi, I'm a bit lost with the next example: In [28]: from numarray import * In [29]: a=arange(10) In [30]: a.iscontiguous() Out[30]: 1 In [31]: b=a[::2] In [32]: b.iscontiguous() Out[32]: 0 That seems to suggest that b shares the same data buffer than a. Indeed: In [36]: a._data Out[36]: <memory at 0x082494d8 with size:0x00000028 held by object 0xb762c260 aliasing object 0x00000000> In [37]: b._data Out[37]: <memory at 0x082494d8 with size:0x00000028 held by object 0xb762c260 aliasing object 0x00000000> At this point, I believe that _bytestride should be different on both arrays, but: In [33]: a._bytestride Out[33]: 4 In [34]: b._bytestride Out[34]: 4 while I expected to find b._bytestride equal to 8. Is that an error or a lack of understanding on my part? Cheers, -- Francesc Altet >qo< http://www.carabos.com/ Cárabos Coop. V. V V Enjoy Data ""
![](https://secure.gravatar.com/avatar/55f7acf47233a7a98f5eb9dfd0b2d763.jpg?s=120&d=mm&r=g)
Francesc Altet wrote:
OK so far.
What you are looking for is _strides. Since, in general, arrays can be multidimensional, the stride(s) need to be specified as a sequence. _bytestride appears to be equivalent to itemsize(); that is, it tells how many bytes are required to represent one item of the array. Here's what strides looks like in your case:
Hope that helps, -tim
Cheers,
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
On Tue, 2004-12-21 at 08:06, Francesc Altet wrote:
Hi Francesc, This is a difficult question for me, but I think the answer is that the use of _bytestride is very limited. _bytestride is used to compute the "natural" strides of an almost contiguous array, e.g. a field of a recarray. That is, given a bytestride and a shape, the strides of a field of a contiguous RecArray are implied. However, once we start slicing (say in more than one dimension), _strides contains more and more information and is no longer implied by just the shape and bytestride but also by the history of slicing. From that perspective, it's not clear what _bytestride can be relied upon for in general or that it needs to be (or can be) kept up to date during slicing. FWIW, looking into this uncovered a related bug in numarray.strings where I tried to use _bytestride to do a simple iteration over all the elements of an array... that doesn't work. Regards, Todd
![](https://secure.gravatar.com/avatar/5c7407de6b47afcd3b3e2164ff5bcd45.jpg?s=120&d=mm&r=g)
Hi Todd, A Dimarts 21 Desembre 2004 16:05, vareu escriure:
Yes, I was trying to use _bytestride mainly in RecArray contexts. It just happens that I've used a NumArray object to check the _bytestride behaviour.
Well, for the time being it seems that RecArray does not support multidimensionalty, so I can just use _bytestride as a shortcut of _strides[0] here. Although now that I think more about this, using itemsize() as Tim suggested, would be the best to compute strides in RecArrays; however this is supposing that fields in RecArrays are contiguous. This would be always the case?
Good! Regards, -- Francesc Altet >qo< http://www.carabos.com/ Cárabos Coop. V. V V Enjoy Data ""
![](https://secure.gravatar.com/avatar/55f7acf47233a7a98f5eb9dfd0b2d763.jpg?s=120&d=mm&r=g)
Francesc Altet wrote:
OK so far.
What you are looking for is _strides. Since, in general, arrays can be multidimensional, the stride(s) need to be specified as a sequence. _bytestride appears to be equivalent to itemsize(); that is, it tells how many bytes are required to represent one item of the array. Here's what strides looks like in your case:
Hope that helps, -tim
Cheers,
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
On Tue, 2004-12-21 at 08:06, Francesc Altet wrote:
Hi Francesc, This is a difficult question for me, but I think the answer is that the use of _bytestride is very limited. _bytestride is used to compute the "natural" strides of an almost contiguous array, e.g. a field of a recarray. That is, given a bytestride and a shape, the strides of a field of a contiguous RecArray are implied. However, once we start slicing (say in more than one dimension), _strides contains more and more information and is no longer implied by just the shape and bytestride but also by the history of slicing. From that perspective, it's not clear what _bytestride can be relied upon for in general or that it needs to be (or can be) kept up to date during slicing. FWIW, looking into this uncovered a related bug in numarray.strings where I tried to use _bytestride to do a simple iteration over all the elements of an array... that doesn't work. Regards, Todd
![](https://secure.gravatar.com/avatar/5c7407de6b47afcd3b3e2164ff5bcd45.jpg?s=120&d=mm&r=g)
Hi Todd, A Dimarts 21 Desembre 2004 16:05, vareu escriure:
Yes, I was trying to use _bytestride mainly in RecArray contexts. It just happens that I've used a NumArray object to check the _bytestride behaviour.
Well, for the time being it seems that RecArray does not support multidimensionalty, so I can just use _bytestride as a shortcut of _strides[0] here. Although now that I think more about this, using itemsize() as Tim suggested, would be the best to compute strides in RecArrays; however this is supposing that fields in RecArrays are contiguous. This would be always the case?
Good! Regards, -- Francesc Altet >qo< http://www.carabos.com/ Cárabos Coop. V. V V Enjoy Data ""
participants (3)
-
Francesc Altet
-
Tim Hochberg
-
Todd Miller