[−][src]Trait maths_traits::algebra::group_like::additive::MulN
An auto-implemented trait for multiplication by natural numbers with associative types using repeated addition
This is intended as a simple and easy way to compute object multiples in abstract algebraic
algorithms without resorting to explicitly applying addition repeatedly. For this reason, the
trait is automatically implemented for any relevant associative algebraic structure and
the supplied function is generic over the Natural
type.
assert_eq!(2.5f32.mul_n(4u8), 10.0); assert_eq!(2.5f32.mul_n(4u16), 10.0); assert_eq!(2.5f32.mul_n(4u128), 10.0); assert_eq!(2.5f64.mul_n(4u8), 10.0); assert_eq!(2.5f64.mul_n(4u16), 10.0); assert_eq!(2.5f64.mul_n(4u128), 10.0);
Note, however, that while multiplication by natural numbers is very simply defined using repeated addition, in order to add flexibility in implementation and the possibility for proper optimization, the automatic implmentation of this trait will first try to use other traits as a base before defaulting to the general repeated_doubling algorithm
Specifically, for a given Natural type N
, the auto-impl will first attempt to use
Mul<N>
, if implemented. If that fails, it will then try to convert using From<N>
and multiplying if if it implemented and the struct is a Semiring.
Finally, in the general case, it will use the repeated_doubling function.