|
1 | 1 | //! This module provides the matrix exponential (pow) function to square matrices.
|
2 | 2 |
|
3 |
| -use std::ops::DivAssign; |
4 |
| - |
5 | 3 | use crate::{
|
6 | 4 | allocator::Allocator,
|
7 | 5 | storage::{Storage, StorageMut},
|
8 |
| - DefaultAllocator, DimMin, Matrix, OMatrix, |
| 6 | + DefaultAllocator, DimMin, Matrix, OMatrix, Scalar, |
9 | 7 | };
|
10 |
| -use num::PrimInt; |
11 |
| -use simba::scalar::ComplexField; |
| 8 | +use num::{One, Zero}; |
| 9 | +use simba::scalar::{ClosedAdd, ClosedMul}; |
12 | 10 |
|
13 |
| -impl<T: ComplexField, D, S> Matrix<T, D, D, S> |
| 11 | +impl<T, D, S> Matrix<T, D, D, S> |
14 | 12 | where
|
| 13 | + T: Scalar + Zero + One + ClosedAdd + ClosedMul, |
15 | 14 | D: DimMin<D, Output = D>,
|
16 | 15 | S: StorageMut<T, D, D>,
|
17 | 16 | DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
18 | 17 | {
|
19 |
| - /// Attempts to raise this matrix to an integral power `e` in-place. If this |
20 |
| - /// matrix is non-invertible and `e` is negative, it leaves this matrix |
21 |
| - /// untouched and returns `false`. Otherwise, it returns `true` and |
22 |
| - /// overwrites this matrix with the result. |
23 |
| - pub fn pow_mut<I: PrimInt + DivAssign>(&mut self, mut e: I) -> bool { |
24 |
| - let zero = I::zero(); |
25 |
| - |
| 18 | + /// Raises this matrix to an integral power `exp` in-place. |
| 19 | + pub fn pow_mut(&mut self, mut exp: u32) { |
26 | 20 | // A matrix raised to the zeroth power is just the identity.
|
27 |
| - if e == zero { |
| 21 | + if exp == 0 { |
28 | 22 | self.fill_with_identity();
|
29 |
| - return true; |
30 |
| - } |
31 |
| - |
32 |
| - // If e is negative, we compute the inverse matrix, then raise it to the |
33 |
| - // power of -e. |
34 |
| - if e < zero && !self.try_inverse_mut() { |
35 |
| - return false; |
36 |
| - } |
| 23 | + } else if exp > 1 { |
| 24 | + // We use the buffer to hold the result of multiplier^2, thus avoiding |
| 25 | + // extra allocations. |
| 26 | + let mut x = self.clone_owned(); |
| 27 | + let mut workspace = self.clone_owned(); |
37 | 28 |
|
38 |
| - let one = I::one(); |
39 |
| - let two = I::from(2u8).unwrap(); |
| 29 | + if exp % 2 == 0 { |
| 30 | + self.fill_with_identity(); |
| 31 | + } else { |
| 32 | + // Avoid an useless multiplication by the identity |
| 33 | + // if the exponent is odd. |
| 34 | + exp -= 1; |
| 35 | + } |
40 | 36 |
|
41 |
| - // We use the buffer to hold the result of multiplier ^ 2, thus avoiding |
42 |
| - // extra allocations. |
43 |
| - let mut multiplier = self.clone_owned(); |
44 |
| - let mut buf = self.clone_owned(); |
| 37 | + // Exponentiation by squares. |
| 38 | + loop { |
| 39 | + if exp % 2 == 1 { |
| 40 | + self.mul_to(&x, &mut workspace); |
| 41 | + self.copy_from(&workspace); |
| 42 | + } |
45 | 43 |
|
46 |
| - // Exponentiation by squares. |
47 |
| - loop { |
48 |
| - if e % two == one { |
49 |
| - self.mul_to(&multiplier, &mut buf); |
50 |
| - self.copy_from(&buf); |
51 |
| - } |
| 44 | + exp /= 2; |
52 | 45 |
|
53 |
| - e /= two; |
54 |
| - multiplier.mul_to(&multiplier, &mut buf); |
55 |
| - multiplier.copy_from(&buf); |
| 46 | + if exp == 0 { |
| 47 | + break; |
| 48 | + } |
56 | 49 |
|
57 |
| - if e == zero { |
58 |
| - return true; |
| 50 | + x.mul_to(&x, &mut workspace); |
| 51 | + x.copy_from(&workspace); |
59 | 52 | }
|
60 | 53 | }
|
61 | 54 | }
|
62 | 55 | }
|
63 | 56 |
|
64 |
| -impl<T: ComplexField, D, S: Storage<T, D, D>> Matrix<T, D, D, S> |
| 57 | +impl<T, D, S: Storage<T, D, D>> Matrix<T, D, D, S> |
65 | 58 | where
|
| 59 | + T: Scalar + Zero + One + ClosedAdd + ClosedMul, |
66 | 60 | D: DimMin<D, Output = D>,
|
67 | 61 | S: StorageMut<T, D, D>,
|
68 | 62 | DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
69 | 63 | {
|
70 |
| - /// Attempts to raise this matrix to an integral power `e`. If this matrix |
71 |
| - /// is non-invertible and `e` is negative, it returns `None`. Otherwise, it |
72 |
| - /// returns the result as a new matrix. Uses exponentiation by squares. |
| 64 | + /// Raise this matrix to an integral power `exp`. |
73 | 65 | #[must_use]
|
74 |
| - pub fn pow<I: PrimInt + DivAssign>(&self, e: I) -> Option<OMatrix<T, D, D>> { |
75 |
| - let mut clone = self.clone_owned(); |
76 |
| - |
77 |
| - if clone.pow_mut(e) { |
78 |
| - Some(clone) |
79 |
| - } else { |
80 |
| - None |
81 |
| - } |
| 66 | + pub fn pow(&self, exp: u32) -> OMatrix<T, D, D> { |
| 67 | + let mut result = self.clone_owned(); |
| 68 | + result.pow_mut(exp); |
| 69 | + result |
82 | 70 | }
|
83 | 71 | }
|
0 commit comments