Result<(), ()> to bool (#916)

This commit is contained in:
Violeta Hernández 2021-06-17 02:44:06 -05:00 committed by GitHub
parent 885bbdaf97
commit 7643a24cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,23 +18,22 @@ 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 `Err(())`. Otherwise, it returns `Ok(())` and
/// untouched and returns `false`. Otherwise, it returns `true` and
/// overwrites this matrix with the result.
#[must_use]
pub fn pow_mut<I: PrimInt + DivAssign>(&mut self, mut e: I) -> Result<(), ()> {
pub fn pow_mut<I: PrimInt + DivAssign>(&mut self, mut e: I) -> bool {
let zero = I::zero();
// A matrix raised to the zeroth power is just the identity.
if e == zero {
self.fill_with_identity();
return Ok(());
return true;
}
// 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 Err(());
return false;
}
}
@ -58,7 +57,7 @@ where
multiplier.copy_from(&buf);
if e == zero {
return Ok(());
return true;
}
}
}
@ -77,9 +76,10 @@ where
pub fn pow<I: PrimInt + DivAssign>(&self, e: I) -> Option<OMatrix<T, D, D>> {
let mut clone = self.clone_owned();
match clone.pow_mut(e) {
Ok(()) => Some(clone),
Err(()) => None,
if clone.pow_mut(e) {
Some(clone)
} else {
None
}
}
}