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.
source to share