Rust doesn't see my f64 overloaded multiplication operator

I am implementing a simple matrix library in rust and I am currently writing an operator for scalar multiplication. Working with direct multiplication:

impl<T: Num + Zero + Clone + Float> Mul<T, Mat<T>> for Mat<T> {
    fn mul(&self, rhs: &T) -> Mat<T> {
        self.componentwise(|v| v.clone() * *rhs)
    }
}

      

But I cannot do left multiplication, I assumed the following code would do what I want:

impl<T: Num + Zero + Clone + Float> Mul<Mat<T>, Mat<T>> for T {
    fn mul(&self, rhs: &Mat<T>) -> Mat<T> {
        rhs.componentwise(|v| *self * v.clone())
    }
}

      

But the following doesn't work:

let A = mat![[1f64, 2., 3.], [4., 5., 6.]];
let P = A * 4f64; // works!
let Q = 4f64 * A; // error...

      

Error error: mismatched types: expected `f64`, found `linalg::Mat<f64>` (expected f64, found struct linalg::Mat)

. Can I only multiply f64 * f64 or am I not wrong in the second case? I tried to implement it for f64 specifically with impl Mul<Mat<f64>, Mat<f64>> for f64

, but that still doesn't work.

I found I can get it to work with help 4f64.mul(&A)

, but that is not ideal.

+3


source to share


1 answer


Maybe it has to do with the ad: your seconde impl ad is for Mult<Mat<T>, Mat<T>>

, so I'm guessing it allows two matrices to be multiplied, which is not what you want.



(I wanted to post a comment, but I cannot do that :()

0


source







All Articles