2021-04-06 01:17:49 +08:00
|
|
|
//! This module provides the matrix exponential (pow) function to square matrices.
|
|
|
|
|
2021-04-11 20:07:06 +08:00
|
|
|
use crate::{
|
|
|
|
allocator::Allocator,
|
|
|
|
storage::{Storage, StorageMut},
|
2021-12-31 06:03:43 +08:00
|
|
|
DefaultAllocator, DimMin, Matrix, OMatrix, Scalar,
|
2021-04-11 20:07:06 +08:00
|
|
|
};
|
2021-12-31 06:03:43 +08:00
|
|
|
use num::{One, Zero};
|
|
|
|
use simba::scalar::{ClosedAdd, ClosedMul};
|
2021-04-06 01:17:49 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
impl<T, D, S> Matrix<T, D, D, S>
|
2021-04-06 01:17:49 +08:00
|
|
|
where
|
2021-12-31 06:03:43 +08:00
|
|
|
T: Scalar + Zero + One + ClosedAdd + ClosedMul,
|
2021-04-06 01:17:49 +08:00
|
|
|
D: DimMin<D, Output = D>,
|
2021-04-11 20:07:06 +08:00
|
|
|
S: StorageMut<T, D, D>,
|
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
2021-04-06 01:17:49 +08:00
|
|
|
{
|
2021-12-31 06:03:43 +08:00
|
|
|
/// Raises this matrix to an integral power `exp` in-place.
|
|
|
|
pub fn pow_mut(&mut self, mut exp: u32) {
|
2021-04-10 12:43:59 +08:00
|
|
|
// A matrix raised to the zeroth power is just the identity.
|
2021-12-31 06:03:43 +08:00
|
|
|
if exp == 0 {
|
2021-04-06 01:32:12 +08:00
|
|
|
self.fill_with_identity();
|
2021-12-31 06:03:43 +08:00
|
|
|
} else if exp > 1 {
|
|
|
|
// We use the buffer to hold the result of multiplier^2, thus avoiding
|
|
|
|
// extra allocations.
|
|
|
|
let mut x = self.clone_owned();
|
|
|
|
let mut workspace = self.clone_owned();
|
2021-04-06 01:17:49 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
if exp % 2 == 0 {
|
|
|
|
self.fill_with_identity();
|
|
|
|
} else {
|
|
|
|
// Avoid an useless multiplication by the identity
|
|
|
|
// if the exponent is odd.
|
|
|
|
exp -= 1;
|
|
|
|
}
|
2021-04-10 12:43:59 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
// Exponentiation by squares.
|
|
|
|
loop {
|
|
|
|
if exp % 2 == 1 {
|
|
|
|
self.mul_to(&x, &mut workspace);
|
|
|
|
self.copy_from(&workspace);
|
|
|
|
}
|
2021-04-06 01:17:49 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
exp /= 2;
|
2021-04-06 01:17:49 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
if exp == 0 {
|
|
|
|
break;
|
|
|
|
}
|
2021-04-06 01:17:49 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
x.mul_to(&x, &mut workspace);
|
|
|
|
x.copy_from(&workspace);
|
2021-04-10 12:43:59 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 01:32:12 +08:00
|
|
|
}
|
2021-04-11 20:07:06 +08:00
|
|
|
}
|
2021-04-06 01:32:12 +08:00
|
|
|
|
2021-12-31 06:03:43 +08:00
|
|
|
impl<T, D, S: Storage<T, D, D>> Matrix<T, D, D, S>
|
2021-04-11 20:07:06 +08:00
|
|
|
where
|
2021-12-31 06:03:43 +08:00
|
|
|
T: Scalar + Zero + One + ClosedAdd + ClosedMul,
|
2021-04-11 20:07:06 +08:00
|
|
|
D: DimMin<D, Output = D>,
|
|
|
|
S: StorageMut<T, D, D>,
|
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
|
|
|
{
|
2021-12-31 06:03:43 +08:00
|
|
|
/// Raise this matrix to an integral power `exp`.
|
2021-04-11 20:07:06 +08:00
|
|
|
#[must_use]
|
2021-12-31 06:03:43 +08:00
|
|
|
pub fn pow(&self, exp: u32) -> OMatrix<T, D, D> {
|
|
|
|
let mut result = self.clone_owned();
|
|
|
|
result.pow_mut(exp);
|
|
|
|
result
|
2021-04-06 01:17:49 +08:00
|
|
|
}
|
|
|
|
}
|