0

I tried to implement a generic matrix product function which should work for all primitive types and the BigInt/BigUint types from the num_bigint crate:

extern crate num_bigint;
extern crate num_traits;

use num_traits::identities::Zero;
use std::ops::{AddAssign, Mul};

#[derive(Debug, Clone)]
struct Matrix<T> {
    n: usize,
    m: usize,
    data: Vec<Vec<T>>,
}

fn m_prd<T>(a: &Matrix<T>, b: &Matrix<T>) -> Matrix<T>
where
    T: Clone + AddAssign + Mul<Output = T> + Zero,
{
    let n = a.n;
    let p = b.n;
    let m = b.m;
    let mut c = Matrix {
        n: n,
        m: m,
        data: vec![vec![T::zero(); m]; n],
    };
    for i in 0..n {
        for j in 0..m {
            for k in 0..p {
                c.data[i][j] += &a.data[i][k] * &b.data[k][j];
            }
        }
    }
    c
}

fn main() {}

I got the following error message:

error[E0369]: binary operation `*` cannot be applied to type `&T`
  --> src/main.rs:29:33
   |
29 |                 c.data[i][j] += &a.data[i][k] * &b.data[k][j];
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: an implementation of `std::ops::Mul` might be missing for `&T`

How can I implement the function for &T in this case?

Shepmaster deems this question as duplicated, but it seems due to operator (AddAssign or Mul) applied on reference (&T) require separate statement after where keyword

Gong-Yi Liao
  • 609
  • 4
  • 11
  • 1
    https://play.rust-lang.org/?gist=0adb017cab54fe2094f62ab85edcf888&version=stable&mode=debug ? – Stargateur May 30 '18 at 05:11
  • I believe your question is answered by the answers of [How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function?](https://stackoverflow.com/q/47996700/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 30 '18 at 09:56
  • The above error message (about implementation method) is different from the post you pointed (about lifetime). – Gong-Yi Liao May 30 '18 at 12:56
  • 1
    I didn't ask if the error messages were the same, I asked if *the answers solved your problem*. You can also choose among [How can I require that a reference to a generic type can be compared for equality against the generic type?](https://stackoverflow.com/q/46852367/155423) or [How to write a trait bound for adding two references of a generic type?](https://stackoverflow.com/q/34630695/155423). Note how the answer is the same for all of these, we don't need to state the same answer again. – Shepmaster May 30 '18 at 13:01
  • Possible duplicate of [How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function?](https://stackoverflow.com/questions/47996700/how-do-i-bound-a-generic-type-with-a-trait-that-requires-a-lifetime-parameter-if) – trent May 30 '18 at 13:33
  • This is due to the incorrect use of trait, change the code to `where T: Clone + AddAssign + Zero, for <'a> &'a T: Mul` solves the problem. – Gong-Yi Liao May 30 '18 at 14:14

0 Answers0