libmir / mir

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

auto a=[[1,2],[3,4]].ndarray; // how to do that? (doens't compile) #380

Closed timotheecour closed 7 years ago

timotheecour commented 7 years ago

eg: (but of course needs to be more general in terms of a.shape.length)

auto makeSlice2(T)(T a){
  alias T2=typeof(a[0][0]);
  auto b=slice!T2(a.length, a[0].length);
  b[]=a;
  return b;
}

unittest{
  auto vals=[[1,2],[3,4],[3,6]];
  auto a=makeSlice2(vals);
  assert(a==vals);
  assert(a.shape==[3,2]);
}

NOTE:

auto makeSlice2(T)(T a){
  return a.sliced.unpack;
}

doesn't do what i would expect

9il commented 7 years ago

ndarray converts slice to a common d array.

You may want to use following steps:

auto sh = a.shape; // computes shape, can throw `SliceException`
auto b = uninitializedSlice(sh);
b[] = a;

I will deprecate shape and reshape functions, so they will be nothrow and @nogc but an addition err reference parameter will be added.

9il commented 7 years ago

Duplicate of https://github.com/libmir/mir/issues/336

9il commented 6 years ago

See also http://docs.algorithm.dlang.io/latest/mir_ndslice_fuse.html#.fuse