use crate::{
allocator::Allocator,
storage::{Storage, StorageMut},
DefaultAllocator, DimMin, Matrix, OMatrix, Scalar,
};
use num::{One, Zero};
use simba::scalar::{ClosedAddAssign, ClosedMulAssign};
impl<T, D, S> Matrix<T, D, D, S>
where
T: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
D: DimMin<D, Output = D>,
S: StorageMut<T, D, D>,
DefaultAllocator: Allocator<D, D> + Allocator<D>,
{
pub fn pow_mut(&mut self, mut exp: u32) {
if exp == 0 {
self.fill_with_identity();
} else if exp > 1 {
let mut x = self.clone_owned();
let mut workspace = self.clone_owned();
if exp % 2 == 0 {
self.fill_with_identity();
} else {
exp -= 1;
}
loop {
if exp % 2 == 1 {
self.mul_to(&x, &mut workspace);
self.copy_from(&workspace);
}
exp /= 2;
if exp == 0 {
break;
}
x.mul_to(&x, &mut workspace);
x.copy_from(&workspace);
}
}
}
}
impl<T, D, S: Storage<T, D, D>> Matrix<T, D, D, S>
where
T: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
D: DimMin<D, Output = D>,
S: StorageMut<T, D, D>,
DefaultAllocator: Allocator<D, D> + Allocator<D>,
{
#[must_use]
pub fn pow(&self, exp: u32) -> OMatrix<T, D, D> {
let mut result = self.clone_owned();
result.pow_mut(exp);
result
}
}