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.
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.
Am I missing a step when creating the vector?
To compute the magnitude of a vector, you can une the method
vector.norm() (this is its euclidean norm).
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.
Thank you for your response, I did not know about the L2 norm also being called the absolute of a vector.
Am I correct that if I want a unit vector from a vector, I can use the normalize() method?
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
Also the method
.normalize_mut() will perform the normalization in-place.