Since a few days I’m trying to incorporate nalgebra in my application. But I am unable to figure out how to get the magnitude(modulus) of a vector in a scalar format.
My code: let vector = Vector2::new(0.,0.); let modulus = abs(vector);
The error I’m getting is : the trait bound 'nalgebra::Matrix<f64, nalgebra::U2, nalgebra::U1, nalgebra::MatrixArray<f64, nalgebra::U2, nalgebra::U1>>: num_traits::sign::Signed' is not satisfied.
To compute the magnitude of a vector, you can une the method vector.norm() (this is its euclidean norm).
Note that num::abs computes the absolute value of its argument and cannot be used with types from nalgebra. Instead, the vector.abs()method should be used for componentwise absolute values.
Yes, you can either use .normalize() if you are sure the norm is not zero, or .try_normalize(threshold) which returns None if the norm is too small (i.e. smaller than the provided threshold argument).
Also the method .normalize_mut() will perform the normalization in-place.