Thread overview
mir - Help on how to transform multidimentional arrays.
Apr 29, 2021
Newbie
Apr 29, 2021
Newbie
Apr 29, 2021
jmh530
Apr 29, 2021
jmh530
Apr 29, 2021
Newbie
April 29, 2021

mir - Help on how to transform multidimentional arrays.


| 0 1 2|
| 3 4 5|
| 6 7 8|
| 9 10 11|
|12 13 14|

|15 16 17|
|18 19 20|
|21 22 23|
|24 25 26|
|27 28 29|

How can i transform it into the following types of arrays.


| 0 10 20|
| 1 11 21|
| 2 12 22|
| 3 13 23|
| 4 14 24|

| 5 15 25|
| 6 16 26|
| 7 17 27|
| 8 18 28|
| 9 19 29|

or


|29 19 9|
|28 18 8|
|27 17 7|
|26 16 6|
|25 15 5|

|24 14 4|
|23 13 3|
|22 12 2|
|21 11 1|
|20 10 0|

Regards,
Newbie.

April 29, 2021
On Thursday, 29 April 2021 at 15:22:55 UTC, Newbie wrote:
> mir - Help on how to transform multidimentional arrays.
>
>
> ----------
> | 0  1  2|
> | 3  4  5|
> | 6  7  8|
> | 9 10 11|
> |12 13 14|
> ----------
> |15 16 17|
> |18 19 20|
> |21 22 23|
> |24 25 26|
> |27 28 29|
> ----------
>
> How can i transform it into the following types of arrays.
>
> ----------
> | 0 10 20|
> | 1 11 21|
> | 2 12 22|
> | 3 13 23|
> | 4 14 24|
> ----------
> | 5 15 25|
> | 6 16 26|
> | 7 17 27|
> | 8 18 28|
> | 9 19 29|
> ----------
>
> or
>
> ----------
> |29 19  9|
> |28 18  8|
> |27 17  7|
> |26 16  6|
> |25 15  5|
> ----------
> |24 14  4|
> |23 13  3|
> |22 12  2|
> |21 11  1|
> |20 10  0|
> ----------
>
> Regards,
> Newbie.

Forgot to add the the first array was created using the following code.
auto base = iota(2, 5, 3);


April 29, 2021

On Thursday, 29 April 2021 at 15:26:15 UTC, Newbie wrote:

>

[snip]

Forgot to add the the first array was created using the following code.
auto base = iota(2, 5, 3);

What you're basically asking for the first one is to convert for row major to column major. There doesn't seem to be a specific function for that, but you can piece it together. The second one is just applying allReversed to the result of that. So we have:

/+dub.sdl:
dependency "mir-algorithm" version="~>3.10.25"
+/

import std.stdio: writeln;
import mir.ndslice.topology: iota, flattened, reshape, universal;
import mir.ndslice.dynamic: transposed, allReversed;

void main() {
    auto x = iota(2, 5, 3);
    int err;
    auto y = x.flattened.reshape([3, 5, 2], err).transposed!(1, 2);
    auto z = y.allReversed;

    writeln(x);
    writeln(y);
    writeln(z);
}
April 29, 2021

On Thursday, 29 April 2021 at 15:56:48 UTC, jmh530 wrote:

>

What you're basically asking for the first one is to convert for row major to column major. There doesn't seem to be a specific function for that, but you can piece it together. The second one is just applying allReversed to the result of that. So we have:

[snip]

Got an extra universal import there.

April 29, 2021
On Thursday, 29 April 2021 at 15:56:48 UTC, jmh530 wrote:
> On Thursday, 29 April 2021 at 15:26:15 UTC, Newbie wrote:
>> [snip]
>>
>> Forgot to add the the first array was created using the following code.
>> auto base = iota(2, 5, 3);
>
> What you're basically asking for the first one is to convert for row major to column major. There doesn't seem to be a specific function for that, but you can piece it together. The second one is just applying allReversed to the result of that. So we have:
>
> ```d
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.10.25"
> +/
>
> import std.stdio: writeln;
> import mir.ndslice.topology: iota, flattened, reshape, universal;
> import mir.ndslice.dynamic: transposed, allReversed;
>
> void main() {
>     auto x = iota(2, 5, 3);
>     int err;
>     auto y = x.flattened.reshape([3, 5, 2], err).transposed!(1, 2);
>     auto z = y.allReversed;
>
>     writeln(x);
>     writeln(y);
>     writeln(z);
> }
> ```

First of all thanks for your guidance and i modified your example to get the results I was looking for.

auto y2 = base.flattened.reshape([3, 5, 2], err).transposed!(1, 2);
produced the following.

---------
|0 10 20|
|1 11 21|
---------
|2 12 22|
|3 13 23|
---------
|4 14 24|
|5 15 25|
---------
|6 16 26|
|7 17 27|
---------
|8 18 28|
|9 19 29|
---------

and auto y2 = base.flattened.reshape([3, 2, 5], err).transposed!(1, 2);
gave me the result i was looking for.

---------
|0 10 20|
|1 11 21|
|2 12 22|
|3 13 23|
|4 14 24|
---------
|5 15 25|
|6 16 26|
|7 17 27|
|8 18 28|
|9 19 29|
---------

Thanks,
Newbie.