[][src]Trait maths_traits::algebra::group_like::multiplicative::PowN

pub trait PowN: MulSemigroup + One {
    fn pow_n<N: Natural>(self, n: N) -> Self { ... }
}

An auto-implemented trait for exponentiation by natural numbers with associative types using repeated multiplication

This is intended as a simple and easy way to compute object powers in abstract algebraic algorithms without resorting to explicitly applying multiplication 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.0f32.pow_n(4u8), 16.0);
 assert_eq!(2.0f32.pow_n(4u16), 16.0);
 assert_eq!(2.0f32.pow_n(4u128), 16.0);
 assert_eq!(2.0f64.pow_n(4u8), 16.0);
 assert_eq!(2.0f64.pow_n(4u16), 16.0);
 assert_eq!(2.0f64.pow_n(4u128), 16.0);

Note, however, that while exponentiation by natural numbers is very simply defined using repeated multiplication, 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_squaring algorithm

Specifically, for a given Natural type N, the auto-impl will first attempt to use Pow<N>, if implemented, then if that fails, it will use the general repeated_squaring algorithm

Provided methods

fn pow_n<N: Natural>(self, n: N) -> Self

Loading content...

Implementors

impl<G: MulSemigroup + One> PowN for G[src]

fn pow_n<N: Natural>(self, n: N) -> Self[src]

Loading content...