dimforge / nalgebra

Linear algebra library for Rust.
https://nalgebra.org
Apache License 2.0
4.02k stars 478 forks source link

Flattening/unflattening vectors/matrices #596

Open Andlon opened 5 years ago

Andlon commented 5 years ago

I find myself repeatedly needing to convert between e.g. Vec<Vector3<N>> and DVector<N>, where the latter is basically a concatenated form of the former.

When working with e.g. Finite Element code that deal with vector-valued functions, such as displacement-based FEM formulations, these kind of operations are quite common. Typically, one might have mesh vertices represented in terms of e.g. Vec<Vector3<N>>, but the linear solver works with a single, long vector.

@sebcrozet kindly pointed out to me on Discord that one solution is something along the lines of

vectors.iter().flat_map(|v| v).cloned().collect::<Vec<_>>()

However, we might discuss if this use case is frequent enough to warrant built-in functionality. And I suppose in that case it should be more general than just flattening a list of vectors into a single vector; we probably want to have a consistent way of handling matrices as well. As an example, consider Vec<Matrix3<N>>, and turning this into either a single long vector or a 3x(3N) matrix or a (3N)x3 matrix. This ambiguity suggests that we would need e.g. multiple functions for flattening, depending on the axis the user wants to flatten along.

At the moment, I don't have a concrete proposal for how best to solve this in a suitably practical, yet generally applicable way, but I wanted to at least explain my specific use case. I would be happy to contribute such a feature, however I would also be happy to get some feedback first, if anyone has some input.

Thefrollickingnerd commented 10 months ago

I would love this feature to exist