digitalmars.D.learn - Linear array to matrix
- Giovanni Di Maria (16/16) Apr 04 2020 Hi.
- Boris Carvajal (11/27) Apr 04 2020 If you're really sure about the array and matrix dimensions/types.
- Giovanni Di Maria (3/39) Apr 04 2020 Ok. Thank you for your help
- MoonlightSentinel (15/17) Apr 04 2020 You can combine slide [1] and array [2]:
- Giovanni Di Maria (4/22) Apr 04 2020 Thank you very much.
- 9il (25/41) Apr 04 2020 You may want to look into a mir-algorithm package that supports
- Giovanni Di Maria (4/10) Apr 04 2020 Very good.
- p.shkadzko (14/30) Apr 05 2020 Why not use "chunks" from std.range?
- p.shkadzko (4/19) Apr 05 2020 it should be just one call to chunks --> arr.chunks(3), otherwise
Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. Giovanni
Apr 04 2020
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. GiovanniIf you're really sure about the array and matrix dimensions/types. You can use a cast: int[] a = [10,20,30,40,50,60,70,80,90,100,110,120]; int[3][] m1 = cast(int[3][]) a; writeln(m1); A better way is using the function chunks; import std.range; auto m2 = a.chunks(3); writeln(m2);
Apr 04 2020
On Saturday, 4 April 2020 at 10:52:00 UTC, Boris Carvajal wrote:On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Ok. Thank you for your help GiovanniHi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. GiovanniIf you're really sure about the array and matrix dimensions/types. You can use a cast: int[] a = [10,20,30,40,50,60,70,80,90,100,110,120]; int[3][] m1 = cast(int[3][]) a; writeln(m1); A better way is using the function chunks; import std.range; auto m2 = a.chunks(3); writeln(m2);
Apr 04 2020
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix?You can combine slide [1] and array [2]: import std; void main() { auto input = [10,20,30,40,50,60,70,80,90,100,110,120]; auto output = input.slide(3, 3).array; writeln(input); writeln(); writeln(output); } Note that output is a view of input and not a copy. [1] https://dlang.org/phobos/std_range.html#.slide [2] https://dlang.org/phobos/std_array.html#.array
Apr 04 2020
On Saturday, 4 April 2020 at 10:52:30 UTC, MoonlightSentinel wrote:On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Thank you very much. GiovanniIs there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix?You can combine slide [1] and array [2]: import std; void main() { auto input = [10,20,30,40,50,60,70,80,90,100,110,120]; auto output = input.slide(3, 3).array; writeln(input); writeln(); writeln(output); } Note that output is a view of input and not a copy. [1] https://dlang.org/phobos/std_range.html#.slide [2] https://dlang.org/phobos/std_array.html#.array
Apr 04 2020
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. GiovanniYou may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy. /+dub.sdl: dependency "mir-algorithm" version="~>3.7.27" +/ // http://mir-algorithm.libmir.org/mir_ndslice.html import mir.ndslice; void main() { // auto intArray = [10,20,30,40,50,60,70,80,90,100,110,120]; auto intMatrix = intArray.sliced(4, 3); static assert(is(typeof(intMatrix) == Slice!(int*, 2))); // lazy matrix auto lazyMatrix = iota!int([4, 3]/*shape*/, 10/*start*/, 10/*stride*/); assert(intMatrix == lazyMatrix); //or foreach(i; 0 .. intMatrix.length) foreach(j; 0 .. intMatrix.length!1) assert(intMatrix[i, j] == lazyMatrix[i, j]); }
Apr 04 2020
On Saturday, 4 April 2020 at 14:00:01 UTC, 9il wrote:On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Very good. Thank you!!!! G[...]You may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy. [...]
Apr 04 2020
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. GiovanniWhy not use "chunks" from std.range? import std.range: chunks; void main() { int[] arr = [10,20,30,40,50,60,70,80,90,100,110,120]; auto matrix1 = arr.chunks(3).chunks(4); // no allocation int[][][] matrix2 = arr.chunks(3).array.chunks(4).array; } But, keep in mind using array of arrays is not efficient. For multidimensional arrays use Mir Slices. If you need more information on how to create matrices, see this article: https://tastyminerals.github.io/tasty-blog/random/2020/03/22/multidimensional_arrays_in_d.html
Apr 05 2020
On Sunday, 5 April 2020 at 18:58:17 UTC, p.shkadzko wrote:On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote:it should be just one call to chunks --> arr.chunks(3), otherwise you'll get two nested arrays while you need only one. Sorry for confusion.[...]Why not use "chunks" from std.range? import std.range: chunks; void main() { int[] arr = [10,20,30,40,50,60,70,80,90,100,110,120]; auto matrix1 = arr.chunks(3).chunks(4); // no allocation int[][][] matrix2 = arr.chunks(3).array.chunks(4).array; } But, keep in mind using array of arrays is not efficient. For multidimensional arrays use Mir Slices. If you need more information on how to create matrices, see this article: https://tastyminerals.github.io/tasty-blog/random/2020/03/22/multidimensional_arrays_in_d.html
Apr 05 2020