Thread overview
Linear array to matrix
Apr 04, 2020
Giovanni Di Maria
Apr 04, 2020
Boris Carvajal
Apr 04, 2020
Giovanni Di Maria
Apr 04, 2020
MoonlightSentinel
Apr 04, 2020
Giovanni Di Maria
Apr 04, 2020
9il
Apr 04, 2020
Giovanni Di Maria
Apr 05, 2020
p.shkadzko
Apr 05, 2020
p.shkadzko
April 04, 2020
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

April 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.
> 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);

April 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
April 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:
>> 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

April 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:
>> 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

April 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.
> 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]);
        	
}

April 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:
>> [...]
>
> You may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy.
>
> [...]




Very good.
Thank you!!!!
G
April 05, 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.
> 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


April 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:
>> [...]
>
> 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.