libmir / mir

Mir (backports): Sparse tensors, Hoffman
http://mir.libmir.org
Boost Software License 1.0
210 stars 20 forks source link

repack by a specific dimension: slice.by!2 (has code) #322

Closed timotheecour closed 7 years ago

timotheecour commented 8 years ago
auto a=sliced!double(2,3,4);

auto by_x=a.by!0;
assert(by_x.shape==[2]);
assert(by_x[0].shape==[3,4]);

auto by_y=a.by!1;
assert(by_y.shape==[3]);
assert(by_y[0].shape==[2,4]);

auto by_z=a.by!2;
assert(by_z.shape==[4]);
assert(by_z[0].shape==[2,3]);

implementation: could by done by combining transposed and pack.

auto by(size_t dim, S)(S s){
  return s.transposed!(dim).pack!1;
}

It's sufficiently common that it deserves it's own function IMO (eg byRows, byColumns etc are common in matrix and higher order libs)

Extension: allow multiple indices:

auto by_xz=a.by!(0,2);
assert(by_xz.shape==[2,4]); //or is it [4,2]?
assert(by_xz[0,0].shape==[3]);

9il commented 8 years ago

Will be added. Thanks!

9il commented 7 years ago

In context of new the new ndslice architecture this addition is less actual.