dimforge / dimforge/nalgebra

Add more matrix concatenation features

Open
#1,146 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

I'm working on implementing Linear Model Predictive Control for a robotics project (https://github.com/qhdwight/mpc-rs). As I do that I am generating some ideas for ease of use functions that numpy/scipy have already. Specifically with regards to concatenating together matrices.

vstack/hstack: https://numpy.org/doc/stable/reference/generated/numpy.vstack.html

block_diag: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.block_diag.html

Here is a sample implementation:

```rust
fn diagonal_block_matrix(matrices: DVector>) -> DMatrix {
let total_rows = matrices.iter().map(DMatrix::nrows).sum();
let total_cols = matrices.iter().map(DMatrix::nrows).sum();
let mut block_diagonal_matrix = DMatrix::zeros(total_rows, total_cols);
// TODO: use ranges instead of two diff variables?
let mut r = 0;
let mut c = 0;
for (i, matrix) in matrices.into_iter().enumerate() {
let nr = r + matrix.nrows();
let nc = c + matrix.ncols();
block_diagonal_matrix.index_mut((r..nr, c..nc)).copy_from(&matrix);
r = nr;
c = nc;
}
block_diagonal_matrix
}
```

Bonus would be implementing the from trait for `Matrix` -> `DMatrix`, e.g. here is what I use right now:

```rust
fn into_dynamic(matrix: Matrix) -> DMatrix where
T: Scalar + Zero,
R: Dim + DimName, C: Dim + DimName,
S: RawStorage {
let mut dynamic_matrix = DMatrix::zeros(matrix.nrows(), matrix.ncols());
dynamic_matrix.copy_from(&matrix);
dynamic_matrix
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.