Bug 16537 – [ndslice] cannot use slice as in function parameter, error on opIndex

Status
RESOLVED
Resolution
WONTFIX
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-25T04:14:00Z
Last change time
2016-12-27T11:53:18Z
Assigned to
nobody
Creator
clumsycodemonkey

Comments

Comment #0 by clumsycodemonkey — 2016-09-25T04:14:06Z
The operators of an ndslice object don't handle const very well, which makes it difficult to write functions that take slices as in parameters. See sum slice functions below, the one that takes in parameters requires casts to work! -------------------------------- alias MyMatrix = Slice!(2,double*); MyMatrix sumSlice(MyMatrix a, MyMatrix b) { assert(a.shape == b.shape); auto rows = a.shape[0]; auto cols = a.shape[1]; auto result = (new double[a.elementsCount]).sliced(rows, cols); foreach(r; 0..rows) foreach(c; 0..cols) result[r,c] = a[r,c] + b[r,c]; return result; } MyMatrix sumSlice2(in MyMatrix a, in MyMatrix b) { assert(a.shape == b.shape); auto rows = a.shape[0]; auto cols = a.shape[1]; auto result = (new double[a.elementsCount]).sliced(rows, cols); foreach(r; 0..rows) foreach(c; 0..cols) // Casts required to compile!! result[r,c] = (cast()a)[r,c] + (cast()b)[r,c]; return result; } -------------------------------- I was using ndslice as a matrix and trying to write functions to do matrix factorizations where I did not destroy the original matrix. So it makes sense to be able to use opIndex in this way.
Comment #1 by ilyayaroshenko — 2016-09-26T05:25:53Z
I don't think that the problem with `in` can be solved for D2. This can be used instead: alias MyMatrix = Slice!(2,double*); alias MyConstMatrix = Slice!(2, const(double)*); // should be casted in caller function A DIP [2] can be created to allow implicit converting MyMatrix -> MyConstMatrix. --- BTW, mir.ndslice.algorithm [1] allows to optimise you function few times. Also http://docs.mir.dlang.io/latest/mir_ndslice_slice.html#.uninitializedSlice will help optimization too. mir.ndslice should be used instead of std ndslice (it should be unified soon). Comparing with fortran ndslice indexed loops can not be vectorized, ndslice.algorithm solve this problem. Mir requires recent LDC beta. DMD is not supported. ldmd2 (shell on top of ldc2) should be used instead of ldc2, because dub passes wrong optimization params to ldc2. [1] http://docs.mir.dlang.io/latest/mir_ndslice_algorithm.html [2] https://github.com/dlang/DIPs
Comment #2 by greeenify — 2016-12-27T11:53:18Z
> A DIP [2] can be created to allow implicit converting MyMatrix -> MyConstMatrix. @Ryan: this should be a separate enhancement request for the DMD category. > mir.ndslice should be used instead of std ndslice std.ndslice has been deprecated and all issues should be reported to mir.ndslice -> https://github.com/libmir/mir Hence I am closing this, please reopen at the mir repo if it's still an issue for you.