www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linear array to matrix

reply Giovanni Di Maria <calimero22 yahoo.it> writes:
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
next sibling parent reply Boris Carvajal <boris2.9 gmail.com> writes:
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.
 Giovanni
If 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
parent Giovanni Di Maria <calimero22 yahoo.it> writes:
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:
 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
If 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);
Ok. Thank you for your help Giovanni
Apr 04 2020
prev sibling next sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
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
parent Giovanni Di Maria <calimero22 yahoo.it> writes:
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:
 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
Thank you very much. Giovanni
Apr 04 2020
prev sibling next sibling parent reply 9il <ilyayaroshenko gmail.com> writes:
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.
 Giovanni
You 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
parent Giovanni Di Maria <calimero22 yahoo.it> writes:
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:
 [...]
You may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy. [...]
Very good. Thank you!!!! G
Apr 04 2020
prev sibling parent reply p.shkadzko <p.shkadzko gmail.com> writes:
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.
 Giovanni
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
parent p.shkadzko <p.shkadzko gmail.com> writes:
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:
 [...]
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
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.
Apr 05 2020