Thread overview
How to sort 2D Slice along 0 axis in mir.ndslice ?
Mar 10, 2020
p.shkadzko
Mar 11, 2020
jmh530
Mar 11, 2020
9il
Mar 11, 2020
Pavel Shkadzko
Mar 11, 2020
jmh530
March 10, 2020
I need to reproduce numpy sort for 2D array.

----------------------------------
import numpy as np

a = [[1, -1, 3, 2], [0, -2, 3, 1]]
b = np.sort(a)
b
# array([[-1,  1,  2,  3],
#        [-2,  0,  1,  3]])
----------------------------------

Numpy sorted the array by columns, which visually looks like each row of elements was sorted individually.
Going through http://docs.algorithm.dlang.io/latest/mir_ndslice_sorting.html I couldn't find analogous operation.

So, attempting to do the same in mir.ndslice results in the following:

-----------------------------------------------
import mir.ndslice;

auto m = [1, -1, 3, 2, 0, -2, 3, 1].sliced(4, 2);
// [[1, -1], [3, 2], [0, -2], [3, 1]]

m.sort;
// [[-2, -1], [0, 1], [1, 2], [3, 3]]
-----------------------------------------------

It basically flattened the 2D slice, sorted and reshaped it back into 2D with elements being moved.
Trying to do something like m.map!(a => a.sort); won't work because "a" is an int "1" and not a slice of two ints "[1, -1]". You can do it with foreach loop but then you'll have to allocate new elements. How do you do it in-place with mir?
March 11, 2020
On Tuesday, 10 March 2020 at 23:31:55 UTC, p.shkadzko wrote:
> [snip]

Below does the same thing as the numpy version.

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.18"
+/
import mir.ndslice.sorting : sort;
import mir.ndslice.topology : byDim;
import mir.ndslice.slice : sliced;

void main() {
    auto m = [1, -1, 3, 2, 0, -2, 3, 1].sliced(2, 4);
    m.byDim!0.each!(a => a.sort);
}
March 11, 2020
On Wednesday, 11 March 2020 at 00:24:13 UTC, jmh530 wrote:
> On Tuesday, 10 March 2020 at 23:31:55 UTC, p.shkadzko wrote:
>> [snip]
>
> Below does the same thing as the numpy version.
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.18"
> +/
> import mir.ndslice.sorting : sort;
> import mir.ndslice.topology : byDim;
> import mir.ndslice.slice : sliced;
>
> void main() {
>     auto m = [1, -1, 3, 2, 0, -2, 3, 1].sliced(2, 4);
>     m.byDim!0.each!(a => a.sort);
> }

Almost the same, just fixed import for `each` and a bit polished

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.18"
+/
import mir.ndslice;
import mir.ndslice.sorting;
import mir.algorithm.iteration: each;

void main() {
    auto m = [[1, -1, 3, 2],
              [0, -2, 3, 1]].fuse;
    m.byDim!0.each!sort;

    import std.stdio;
    m.byDim!0.each!writeln;
}

March 11, 2020
On Wednesday, 11 March 2020 at 06:12:55 UTC, 9il wrote:
> On Wednesday, 11 March 2020 at 00:24:13 UTC, jmh530 wrote:
>> [...]
>
> Almost the same, just fixed import for `each` and a bit polished
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.18"
> +/
> import mir.ndslice;
> import mir.ndslice.sorting;
> import mir.algorithm.iteration: each;
>
> void main() {
>     auto m = [[1, -1, 3, 2],
>               [0, -2, 3, 1]].fuse;
>     m.byDim!0.each!sort;
>
>     import std.stdio;
>     m.byDim!0.each!writeln;
> }

Great, thanks guys!
March 11, 2020
On Wednesday, 11 March 2020 at 06:12:55 UTC, 9il wrote:
> [snip]
>
> Almost the same, just fixed import for `each` and a bit polished
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.18"
> +/
> import mir.ndslice;
> import mir.ndslice.sorting;
> import mir.algorithm.iteration: each;
>
> void main() {
>     auto m = [[1, -1, 3, 2],
>               [0, -2, 3, 1]].fuse;
>     m.byDim!0.each!sort;
>
>     import std.stdio;
>     m.byDim!0.each!writeln;
> }

Doh on the 'each' import.

Also, I don't think I had used fuse before. That's definitely helpful.