Status of multidimensional slicing

Jared Miller jared at economicmodeling.com
Fri Mar 7 12:55:07 PST 2014


I would like to revisit the topic of operator overloads for 
multidimensional slicing.

Bottom line: opSlice is currently limited to 1 dimension/axis 
only. The cleanest workaround right now is to pass your own 
"slice" structs to opIndex. It works but it's not too pretty.

----
// Suppose we have a user-defined type...
auto mat = Matrix(
     [ [0,1,2],
       [3,4,5],
       [6,7,8] ] );

// This type of indexing can be implemented:
auto cell = mat[1, $-1];

// But multidimensional slicing cannot:
// auto submatrix = mat[0..2, 1..$];

// "Cleanest" workaround with a slice struct S taken by opIndex
//  (no $ capability):
auto submatrix = mat[ S(0,2), S(1,3) ];

// With a bit more hacking, something like this could be done:
auto submatrix = mat[ S[0..2], S[1..$] ];
----

Problem with current state of affairs and rationale for a fix:

* A stated design goal of D is to "Cater to the needs of 
numerical analysis programmers", and presumably HPC / scientific 
computing that's heavy on linear algebra and n-dimensional 
arrays. Well, it seems like the multidimensional slice/stride 
syntax in Matlab, NumPy, and even Fortran has been pretty popular 
with these folks. Syntactic sugar here is a clear win. I don't 
think it's a niche feature.
* The limitation on slicing is inconsistent with the capabilities 
of opIndex and opDollar, and workarounds are ugly.
* The approved DIP#7 briefly mentions multidimensional slicing 
but it was never implemented (despite opDollar getting done).

Recap of discussions so far:

* 2009-10-10: DIP7 
(http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP7)
* 2010-03-08: "Proposal: Multidimensional opSlice solution" 
(http://forum.dlang.org/thread/hn2q9q$263e$1@digitalmars.com)
* 2011-10-09: "Issue #6798: Integrate overloadings for 
multidimensional indexing and slicing"
* 2012-06-01: "[Proposal] Additional operator overloadings for 
multidimentional indexing and slicing" 
(http://forum.dlang.org/thread/mailman.1202.1338515967.24740.digitalmars-d@puremagic.com)
* 2012-11-19: "Multidimensional array operator overloading" 
(http://forum.dlang.org/thread/mailman.2065.1353348152.5162.digitalmars-d@puremagic.com)
* 2012-12-19: "Multidimensional slice" 
(http://forum.dlang.org/thread/lglljlnzoathjxijomrn@forum.dlang.org)
* 2013-04-06: "rationale for opSlice, opSliceAssign, vs a..b 
being syntax suger for a Slice struct?" 
(http://forum.dlang.org/thread/mailman.551.1365290408.4724.digitalmars-d-learn@puremagic.com)
* 2013-05-12: Andrei asks for feedback on Kenji's 2011 pull 
request for #6798
* 2013-10-11: "std.linalg" 
(http://forum.dlang.org/thread/rmyaglfeimzuggoluxvd@forum.dlang.org)

Steps forward:

So I basically want resurrect the topic and gauge support for 
fixing slice overloads. Then, core committers could revisit 
Kenji's 2011 proposal and pull request for issue #6798 as a very 
solid near-term solution. Finally, perhaps a DIP for stride 
syntax/overloads?

Looking forward to discussion.


More information about the Digitalmars-d mailing list