Why doest a / b work but not a / &b?


#1

I want to divide a matrix’ elements by a number.

let a = DVector::repeat(3, 1.);
let b : f64 = DVector::repeat(3, 1.).dot(&a); // is there no method to sum the elements of a matrix?

Why does these work:

let w =  &a / b;
let w =  a / b;

But these raise an error?

let w =  &a / &b;
let w =  a / &b;

error[E0277]: cannot divide &nalgebra::Matrix<{float}, nalgebra::Dynamic, nalgebra::U1, nalgebra::VecStorage<{float}, nalgebra::Dynamic, nalgebra::U1>> by &{float}


#2

Hi!

A sum()method is already planned for the next release (which will happen on Febuary 3 2019). In the mean time you can do: vector.iter().fold(0.0, |a, b| a + *b).

Multiplication by a reference to a scalar is not implemented because it didn’t seem useful since the scalar types are expected to be small in size and must be Copy anyway. Is there a specific use-case where multiplying by a ref is much more convenient than just deferencing the scalar if it’s a ref?


#3

Thanks! I can’t say I need this, I was just curious to know there was a subtle reason behind this.