[Issue 16537] New: [ndslice] cannot use slice as in function parameter, error on opIndex
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sat Sep 24 21:14:06 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16537
Issue ID: 16537
Summary: [ndslice] cannot use slice as in function parameter,
error on opIndex
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: major
Priority: P1
Component: phobos
Assignee: nobody at puremagic.com
Reporter: clumsycodemonkey at gmail.com
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.
--
More information about the Digitalmars-d-bugs
mailing list