www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to sort 2D Slice along 0 axis in mir.ndslice ?

reply p.shkadzko <p.shkadzko gmail.com> writes:
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


----------------------------------

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?
Mar 10 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
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); }
Mar 10 2020
parent reply 9il <ilyayaroshenko gmail.com> writes:
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; }
Mar 10 2020
next sibling parent Pavel Shkadzko <p.shkadzko gmail.com> writes:
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!
Mar 11 2020
prev sibling parent jmh530 <john.michael.hall gmail.com> writes:
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.
Mar 11 2020