Multidimensional arrays as types

I can write

type MyMatrix4 = [[f32; 4]; 4];

but I can't do much with it. No "impl", no operators, etc.

I can write

pub struct YourMatrix4 {
    pub m: [[f32; 4]; 4]
}

   impl YourMatrix4 {
}

but I can't just subscript a variable of type YourMatrix4.

Am I missing something, or is type that limited?

type is just that limited. Using a struct here is the right solution (if you want to add your own impls). Note: You can add an impl for std::ops::Index and std::ops::IndexMut to regain the indexing operator.

2 Likes

Oh well. One can hope.

This always bugs me. We don't seem to get first class support for matrices in general purpose languages. C, C++, Java, Python, Go, Rust - none take multidimensional arrays seriously enough. There's Matlab, but there, everything is an array.

you can use nalgebra for vectors and matricies, or ndarray if you need something with more than two dimensions.

1 Like

There isn't a one-size fits-all solution to handling matrices. The algorithms for large, sparse matrices aren't good on small ones and vice versa. Then you've got SIMD and compute shaders to contend with. Do you need to reference submatrices? Do you need to efficient operations on column-order or row-order matrices? What would you expect a general purpose language to do to take this seriously? There are so many situational tradeoffs.

3 Likes

What is it that you are wishing for, arithmetic operators? If so, which ones, and what do you think they should do? I don't know of a single arithmetic operator that does the same thing on all languages that support arithmetic on arrays, so it's hard to see that there is some behavior that ought to be supported by default.

In case this isn't clear, the reason type is so limited is because it doesn't actually create a new type, only an alias. For the coherence checks on whether to allow impl, MyMatrix4 is still a foreign type, because it's completely identical to writing [[f32; 4]; 4] everywhere.

4 Likes