[MultiArray] subscript to index
Hi All, Does MultiArray provide a convenient way to get an raw-index (*i.e. *offset to an element in raw-data) given a collection of indices?
Does MultiArray provide a convenient way to get an raw-index (i.e. offset to an element in raw-data) given a collection of indices?
Not as far as I know. You could accomplish this accumulating indices and strides relative to the origin (similar to the pseudocode for a(index_list) in Table 3 of http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/reference.html). One other option might be &a(index_list) - a.origin(). - Rhys
On Sat, May 1, 2010 at 9:08 AM, Rhys Ulerich
Not as far as I know. You could accomplish this accumulating indices and strides relative to the origin (similar to the pseudocode for a(index_list) in Table 3 of http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/reference.html ).
One other option might be &a(index_list) - a.origin().
... since origin() is arr [0][0]...[0], neither of these is the answer in general. But thanks, good pointers there! Sad there isn't a multi_array::offset( indices ) or something that: assert ( &arr( index_list ) == arr.data() + arr.*offset*( index_list ) ) this kind of stuff is useful when interacting with legacy libs or other languages. On a separate note, since for whatever reason the multi_array::index_bases() doesn't return a const Collection reference or something, the following doesn't work: T element = arr( arr.index_bases() ); Is there an easier alternative to the following (setting the first element of any Multi Array model, note that data() may not be available for all): indices I; // a collection object for ( int i = 0; i < arr.num_dimensions(); ++i ) I[i] = arr.index_bases()[i]; arr( I ) = ... ;
On 05/03/10 22:20, Sami wrote:
On Sat, May 1, 2010 at 9:08 AM, Rhys Ulerich
mailto:rhys.ulerich@gmail.com> wrote: Not as far as I know. You could accomplish this accumulating indices and strides relative to the origin (similar to the pseudocode for a(index_list) in Table 3 of http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/reference.html).
One other option might be &a(index_list) - a.origin().
... since origin() is arr [0][0]...[0], neither of these is the answer in general. But thanks, good pointers there! Sad there isn't a multi_array::offset( indices ) or something that:
assert ( &arr( index_list ) == arr.data() + arr.*offset*( index_list ) )
this kind of stuff is useful when interacting with legacy libs or other languages.
Hmm... I puzzled why you think Sami's method won't work. For example, given the strides, s[ndim] and indices, i[ndim], and a.origin(), then the distance between a.origin() and the element at indices, i[0...ndim-1] is simply the sum of the vector product of s prepended with 1 and i. e.g., For a[2][2][2], the strides would be: s={2,4,8}, and for i={1,1,1}, then offset (of a[1]][1][1]) = 1*i[0]+s[0]*i[1]+s[1]*i[2] Wouldn't that work?
Hi Larry,
On Tue, May 4, 2010 at 1:16 AM, Larry Evans
On 05/03/10 22:20, Sami wrote:
Hmm... I puzzled why you think Sami's method won't work.
For example, given the strides, s[ndim] and indices, i[ndim], and a.origin(), then the distance between a.origin() and the element at indices, i[0...ndim-1] is simply the sum of the vector product of s prepended with 1 and i. e.g., For a[2][2][2], the strides would be: s={2,4,8},
actually that would be {4,2,1} for default C (row-major) storage ordering.
and for i={1,1,1}, then offset (of a[1]][1][1]) = 1*i[0]+s[0]*i[1]+s[1]*i[2] Wouldn't that work?
wouldn't work when any of index_bases() is non-zero. You'd need the following to make it right: // using inner_product instead of transform/accumulate collection index = {i,j,k}; // looking for offset of this from data() size_t base_offset = std::inner_product( a.index_bases(), a.index_bases()+a.num_dimensions(), a.strides(), 0); size_t offset = std::inner_product( index.begin(), index.end(), a.strides(), -base_offset );
participants (3)
-
Larry Evans
-
Rhys Ulerich
-
Sami