Skip to content

Commit

Permalink
Make matrix power work for non-owned matrices.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Apr 11, 2021
1 parent 24d546d commit cc4427e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/linalg/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ where
}

/// Computes the determinant of the decomposed matrix.
pub fn determinant(&self) -> N::SimdRealField {
pub fn determinant(&self) -> T::SimdRealField {
let dim = self.chol.nrows();
let mut prod_diag = N::one();
let mut prod_diag = T::one();
for i in 0..dim {
prod_diag *= unsafe { *self.chol.get_unchecked((i, i)) };
}
Expand Down
39 changes: 26 additions & 13 deletions src/linalg/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@
use std::ops::DivAssign;

use crate::{allocator::Allocator, DefaultAllocator, DimMin, MatrixN};
use crate::{
allocator::Allocator,
storage::{Storage, StorageMut},
DefaultAllocator, DimMin, Matrix, OMatrix,
};
use num::PrimInt;
use simba::scalar::ComplexField;

impl<N: ComplexField, D> MatrixN<N, D>
impl<T: ComplexField, D, S> Matrix<T, D, D, S>
where
D: DimMin<D, Output = D>,
DefaultAllocator: Allocator<N, D, D>,
DefaultAllocator: Allocator<N, D>,
S: StorageMut<T, D, D>,
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
{
/// 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
/// overwrites this matrix with the result.
#[must_use]
pub fn pow_mut<T: PrimInt + DivAssign>(&mut self, mut e: T) -> Result<(), ()> {
let zero = T::zero();
pub fn pow_mut<I: PrimInt + DivAssign>(&mut self, mut e: I) -> Result<(), ()> {
let zero = I::zero();

// A matrix raised to the zeroth power is just the identity.
if e == zero {
Expand All @@ -34,18 +38,19 @@ where
}
}

let one = T::one();
let two = T::from(2u8).unwrap();
let one = I::one();
let two = I::from(2u8).unwrap();

// We use the buffer to hold the result of multiplier ^ 2, thus avoiding
// extra allocations.
let mut multiplier = self.clone();
let mut buf = self.clone();
let mut multiplier = self.clone_owned();
let mut buf = self.clone_owned();

// Exponentiation by squares.
loop {
if e % two == one {
*self *= &multiplier;
self.mul_to(&multiplier, &mut buf);
self.copy_from(&buf);
}

e /= two;
Expand All @@ -57,12 +62,20 @@ where
}
}
}
}

impl<T: ComplexField, D, S: Storage<T, D, D>> Matrix<T, D, D, S>
where
D: DimMin<D, Output = D>,
S: StorageMut<T, D, D>,
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
{
/// Attempts to raise this matrix to an integral power `e`. If this matrix
/// is non-invertible and `e` is negative, it returns `None`. Otherwise, it
/// returns the result as a new matrix. Uses exponentiation by squares.
pub fn pow<T: PrimInt + DivAssign>(&self, e: T) -> Option<Self> {
let mut clone = self.clone();
#[must_use]
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),
Expand Down
2 changes: 1 addition & 1 deletion tests/linalg/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ macro_rules! gen_tests(

#[test]
fn cholesky_determinant_static(_n in PROPTEST_MATRIX_DIM) {
let m = RandomSDP::new(U4, || random::<$scalar>().0).unwrap();
let m = RandomSDP::new(Const::<4>, || random::<$scalar>().0).unwrap();
let lu_det = m.clone().lu().determinant();
assert_relative_eq!(lu_det.imaginary(), 0., epsilon = 1.0e-7);
let chol_det = m.cholesky().unwrap().determinant();
Expand Down

0 comments on commit cc4427e

Please sign in to comment.