digitalmars.D.learn - How to sort 2D Slice along 0 axis in mir.ndslice ?
- p.shkadzko (29/29) Mar 10 2020 I need to reproduce numpy sort for 2D array.
- jmh530 (12/13) Mar 10 2020 Below does the same thing as the numpy version.
- 9il (15/28) Mar 10 2020 Almost the same, just fixed import for `each` and a bit polished
- Pavel Shkadzko (2/18) Mar 11 2020 Great, thanks guys!
- jmh530 (4/19) Mar 11 2020 Doh on the 'each' import.
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
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
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: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; }[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
On Wednesday, 11 March 2020 at 06:12:55 UTC, 9il wrote:On Wednesday, 11 March 2020 at 00:24:13 UTC, jmh530 wrote:Great, thanks guys![...]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 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.
Mar 11 2020