Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Allow the multiplication of matrices of matrices #1392

Open
teruyamato0731 opened this issue May 15, 2024 · 0 comments
Open

Request: Allow the multiplication of matrices of matrices #1392

teruyamato0731 opened this issue May 15, 2024 · 0 comments

Comments

@teruyamato0731
Copy link

Currently, matrix multiplication only works when the matrices have the same type T. However, matrices of matrices are common in control engineering and should be supported.

Current behavior:

use nalgebra::matrix;

let m1 = matrix![1, 2; 3, 4];
let m2 = matrix![m1, m1; m1, m1];

// Multiplication works for matrices of the same type `T`.
let _ = m2 * m2;

// T: Matrix<i32, U2, U2>
// m2: Matrix<Matrix<i32, U2, U2>, U2, U2> = Matrix<T, U2, U2>
// So Output: Matrix<Matrix<i32, U2, U2>, U2, U2> = Matrix<T, U2, U2>

Requested behavior:

// Multiplication fails because the types `T` are different.
// However, the inner matrices (2x2 and 2x1) can be multiplied, so this should be allowed.
let v = vector![5, 6];
let m3 = matrix![v, v; v, v];
let _ = m2 * m3;

// T1: Matrix<i32, U2, U2>
// T2: Matrix<i32, U2, U1>
// m1 * v: Matrix<i32, U2, U1> 
// So Output should be: Matrix<Matrix<i32, U2, U1>, U2, U2>

The compile-time error is as follows:

error[E0277]: cannot multiply `Matrix<Matrix<{integer}, Const<2>, Const<2>, ArrayStorage<{integer}, 2, 2>>, Const<2>, Const<2>, ArrayStorage<Matrix<{integer}, Const<2>, Const<2>, ArrayStorage<{integer}, 2, 2>>, 2, 2>>` by `Matrix<{integer}, Const<2>, Const<1>, ArrayStorage<{integer}, 2, 1>>`
  --> src/main.rs:26:16
   |
26 |     let _ = m2 * v;
   |                ^ no implementation for `Matrix<Matrix<{integer}, Const<2>, Const<2>, ArrayStorage<{integer}, 2, 2>>, Const<2>, Const<2>, ArrayStorage<Matrix<{integer}, Const<2>, Const<2>, ArrayStorage<{integer}, 2, 2>>, 2, 2>> * Matrix<{integer}, Const<2>, Const<1>, ArrayStorage<{integer}, 2, 1>>`
   |
   = help: the trait `Mul<Matrix<{integer}, Const<2>, Const<1>, ArrayStorage<{integer}, 2, 1>>>` is not implemented for `Matrix<Matrix<{integer}, Const<2>, Const<2>, ArrayStorage<{integer}, 2, 2>>, Const<2>, Const<2>, ArrayStorage<Matrix<..., ..., ..., ...>, 2, 2>>`
   = help: the following other types implement trait `Mul<Rhs>`:
             <Matrix<T, Const<R1>, Const<C1>, SA> as Mul<OPoint<T, Const<D2>>>>
             <Matrix<T, R, C, S> as Mul<T>>
             <Matrix<T, R1, C1, SA> as Mul<Rotation<T, D2>>>
             <Matrix<T, R1, C1, SA> as Mul<Matrix<T, R2, C2, SB>>>
             <Matrix<T, Const<R1>, Const<C1>, SA> as Mul<&'b OPoint<T, Const<D2>>>>
             <Matrix<T, R1, C1, SA> as Mul<&'b Rotation<T, D2>>>
             <Matrix<T, R1, C1, SA> as Mul<&'b Matrix<T, R2, C2, SB>>>
             <&'a Matrix<T, R1, C1, SA> as Mul<&'b Matrix<T, R2, C2, SB>>>
           and 6 others

For more information about this error, try `rustc --explain E0277`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant