On Wednesday, 20 July 2022 at 09:18:29 UTC, anonymouse wrote:

As for task 3, while I understand the concept of transposing a matrix, I'm not sure how to even begin.


By not knowing how to begin, I mean that I don't know how to generalize the algorithm so that it applies to an array of arbitrary dimension/shape. If I already know the dimensions, I can hardcode that information and get it to work just fine. In the example below, since I know that ```a``` has a shape of [3, 5, 7], I use that information to transpose the array:

```d
import std.traits;
auto transpose(A)(A a) if (isArray!A)
{
    auto tmp = new FlatElementType!A[3][5][7];  // [1]

    foreach(n0, i; a)
        foreach(n1, j; i)
            foreach(n2, k; j)
                tmp[n2][n1][n0] = k;

    return tmp;
}

void main()
{
    auto a = [
        [
            [111,112,113,114,115,116,117],
            [121,122,123,124,125,126,127],
            [131,132,133,134,135,136,137],
            [141,142,143,144,145,136,147],
            [151,152,153,154,155,156,137]
        ],
        [
            [211,212,213,214,215,216,217],
            [221,222,223,224,225,226,227],
            [231,232,233,234,235,236,237],
            [241,242,243,244,245,236,247],
            [251,252,253,254,255,256,237]
        ],
        [
            [311,312,313,314,315,316,317],
            [321,322,323,324,325,326,327],
            [331,332,333,334,335,336,337],
            [341,342,343,344,345,336,347],
            [351,352,353,354,355,356,337]
        ]
    ];

    a.transpose.writeln;
}
```

Output reformatted for visual presentation:
```
[
    [
        [111, 211, 311],
        [121, 221, 321],
        [131, 231, 331],
        [141, 241, 341],
        [151, 251, 351]
    ],
    [
        [112, 212, 312],
        [122, 222, 322],
        [132, 232, 332],
        [142, 242, 342],
        [152, 252, 352]
    ],
    [
        [113, 213, 313],
        [123, 223, 323],
        [133, 233, 333],
        [143, 243, 343],
        [153, 253, 353]
    ],
    [
        [114, 214, 314],
        [124, 224, 324],
        [134, 234, 334],
        [144, 244, 344],
        [154, 254, 354]
    ],
    [
        [115, 215, 315],
        [125, 225, 325],
        [135, 235, 335],
        [145, 245, 345],
        [155, 255, 355]
    ],
    [
        [116, 216, 316],
        [126, 226, 326],
        [136, 236, 336],
        [136, 236, 336],
        [156, 256, 356]
    ],
    [
        [117, 217, 317],
        [127, 227, 327],
        [137, 237, 337],
        [147, 247, 347],
        [137, 237, 337]
    ]
]
```

As the example demonstrates, by knowing beforehand that it is a 3D array of shape [3, 5, 7] , I can hardcode that information into the temp array and use the correct amount of nested loops to unwind and reassigned the values. I would like to accomplish this without knowing the shape before hand.

Any pointers would be appreciated.

Thanks,
--anonymouse

[1] Contributed by [ag0aep6g](https://forum.dlang.org/post/tb9pl1$gc1$1...@digitalmars.com)

Reply via email to