Mapping rows and columns
- Dominant language
- Rust
- Stars
- 4.8k
- Forks
- 565
- PR merge metrics
- No merged PRs in 30d
Description
So here's something I wanted as soon as started trying to use the crate.
Strawman signature.
```rust
impl for Matrix
where
N: Scalar,
R: Dim,
C: Dim,
S: Storage,
{
pub fn map_rows(&self, mut f: F) -> MatrixMN
where
F: FnMut(MatrixSlice) -> Matrix,
C2: Dim,
N2: Scalar,
S2: Storage,
DefaultAllocator: Allocator,
{ ... }
pub fn map_columns(&self, mut f: F) -> MatrixMN
where
F: FnMut(MatrixSlice) -> Matrix,
R2: Dim,
N2: Scalar,
S2: Storage,
DefaultAllocator: Allocator,
{ ... }
}
```
My immediate use case was that I needed to compute the norms of a bunch of vectors:
```rust
fn norms_squared(m: &MatrixMN) -> MatrixMN
{ m.map_columns(|col| Matrix1::new(col.norm_squared())) }
```
One also doesn't have to reach too far for a use case for e.g. `zip_map_columns` (a function whose signature would quite possibly be longer than its implementation)
```rust
impl for Matrix
where
N: Scalar,
R: Dim,
C: Dim,
S: Storage,
{
fn zip_map_columns(
&self,
other: &Matrix,
f: F,
) -> MatrixMN
where
F: FnMut(
MatrixSlice,
MatrixSlice,
) -> Matrix,
R2: Dim,
N2: Scalar,
S2: Storage,
DefaultAllocator: Allocator,
R3: Dim,
N3: Scalar,
S3: Storage,
DefaultAllocator: Allocator,
{ ... }
}
// possible use case: Scale each column by a separate scale factor
// (like multiplication on the right by `diag(scales)`)
fn scale_vectors(
vectors: &MatrixMN,
scales: &::na::RowVectorN,
) -> MatrixMN
where
DefaultAllocator: Allocator + Allocator + Allocator,
{
vectors.zip_map_columns(scales, |v, scale| v * scale)
}
```
Let me know if there are other existing, idiomatic ways to achieve this sort of stuff with the library.
([here is a terribly unoptimized implementation of the above as a standalone crate](https://gist.github.com/ExpHP/d453d36ca67fe5c39c96d8291d175b0f))
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.