pow_mut now returns Result.

This commit is contained in:
Violeta Hernández 2021-04-10 00:12:26 -05:00
parent 81f2fc38d7
commit 341091f647

View File

@ -14,22 +14,23 @@ where
{
/// Attempts to raise this matrix to an integral power `e` in-place. If this
/// matrix is non-invertible and `e` is negative, it leaves this matrix
/// untouched and returns `false`. Otherwise, it returns `true` and
/// untouched and returns `Err(())`. Otherwise, it returns `Ok(())` and
/// overwrites this matrix with the result.
pub fn pow_mut<T: PrimInt + DivAssign>(&mut self, mut e: T) -> bool {
#[must_use]
pub fn pow_mut<T: PrimInt + DivAssign>(&mut self, mut e: T) -> Result<(), ()> {
let zero = T::zero();
// A matrix raised to the zeroth power is just the identity.
if e == zero {
self.fill_with_identity();
return true;
return Ok(());
}
// If e is negative, we compute the inverse matrix, then raise it to the
// power of -e.
if e < zero {
if !self.try_inverse_mut() {
return false;
return Err(());
}
}
@ -52,7 +53,7 @@ where
multiplier.copy_from(&buf);
if e == zero {
return true;
return Ok(());
}
}
}
@ -63,10 +64,9 @@ where
pub fn pow<T: PrimInt + DivAssign>(&self, e: T) -> Option<Self> {
let mut clone = self.clone();
if clone.pow_mut(e) {
Some(clone)
} else {
None
match clone.pow_mut(e) {
Ok(()) => Some(clone),
Err(()) => None,
}
}
}