libmir/mir

Overlapping blocks

ljubobratovicrelja opened this issue · 5 comments

Is it possible to achieve this with current mir.ndslice.selection API? For example, I'd like to divide matrix into m-by-n blocks, but I'd like blocks to overlap by k elements, where k < m && k < n.

Something like this:

Slice!(2, float*) matrix = slice!int(6, 3);
matrix[] = [[1,  2,  3,  4,  5,  6  ],             
            [7,  8,  9,  10, 11, 12 ],
            [13, 14, 15, 16, 17, 18 ]];

assert( 
matrix.ovelappedBlocks([2, 2], 1) ==  /*(blocking shape, and overlapping length)*/
[ [ [[1, 2], [7,   8]], [[2, 3], [8,   9]], ...],
  [ [[7, 8], [13, 14]], [[8, 9], [14, 15]] ....]] ); 

Hope example is understandable. :)

I've been trying to achieve this for few hours, and just after posting the issue here I got the solution - matrix.windows(m, n).strided!(0, 1)(k, k) seems to be doing the job.

Sorry for spamming.

Maybe put this in the docs somewhere?

Well, in blocks' documentation it's clearly noted it's non-ovelapping blocking of tensor. My first hint was to look around blocks, and wasn't thinking even remotely windows would solve the problem. And that is most probably the reason for my confusion, and why I was not able to come up with a solution for a period of time.

So, maybe a hint in the blocks description about this would be good. Also an unittest example on this in windows section. @9il @thewilsonator if you both agree, I would like to add this.

9il commented

agree, please open a Phobos PR

agree, please open a Phobos PR

Will do, tnx.