2017-08-14 01:53:04 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
use serde;
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use alga::general::ClosedNeg;
|
2018-05-19 23:15:15 +08:00
|
|
|
use num::One;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2018-05-19 23:15:15 +08:00
|
|
|
use allocator::Allocator;
|
|
|
|
use base::{DefaultAllocator, Matrix, Scalar, VectorN};
|
2018-05-27 03:02:24 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
use dimension::Dynamic;
|
|
|
|
use dimension::{Dim, DimName, U1};
|
2017-08-03 01:37:44 +08:00
|
|
|
use storage::StorageMut;
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// A sequence of row or column permutations.
|
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-05-19 23:15:15 +08:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
|
|
|
serde(
|
|
|
|
bound(
|
|
|
|
serialize = "DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
VectorN<(usize, usize), D>: serde::Serialize"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
|
|
|
serde(
|
|
|
|
bound(
|
|
|
|
deserialize = "DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
VectorN<(usize, usize), D>: serde::Deserialize<'de>"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)]
|
2017-08-14 01:53:00 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub struct PermutationSequence<D: Dim>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
{
|
|
|
|
len: usize,
|
|
|
|
ipiv: VectorN<(usize, usize), D>,
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:00 +08:00
|
|
|
impl<D: Dim> Copy for PermutationSequence<D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
VectorN<(usize, usize), D>: Copy,
|
|
|
|
{
|
|
|
|
}
|
2017-08-14 01:53:00 +08:00
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
impl<D: DimName> PermutationSequence<D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
{
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Creates a new statically-allocated sequence of `D` identity permutations.
|
|
|
|
#[inline]
|
|
|
|
pub fn identity() -> Self {
|
|
|
|
Self::identity_generic(D::name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 00:05:56 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2017-08-14 01:53:04 +08:00
|
|
|
impl PermutationSequence<Dynamic>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), Dynamic>,
|
|
|
|
{
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Creates a new dynamically-allocated sequence of `n` identity permutations.
|
|
|
|
#[inline]
|
|
|
|
pub fn identity(n: usize) -> Self {
|
|
|
|
Self::identity_generic(Dynamic::new(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Dim> PermutationSequence<D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<(usize, usize), D>,
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Creates a new sequence of D identity permutations.
|
|
|
|
#[inline]
|
|
|
|
pub fn identity_generic(dim: D) -> Self {
|
|
|
|
unsafe {
|
|
|
|
PermutationSequence {
|
2018-02-02 19:26:35 +08:00
|
|
|
len: 0,
|
|
|
|
ipiv: VectorN::new_uninitialized_generic(dim, U1),
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Adds the interchange of the row (or column) `i` with the row (or column) `i2` to this
|
|
|
|
/// sequence of permutations.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn append_permutation(&mut self, i: usize, i2: usize) {
|
|
|
|
if i != i2 {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
self.len < self.ipiv.len(),
|
|
|
|
"Maximum number of permutations exceeded."
|
|
|
|
);
|
2017-08-03 01:37:44 +08:00
|
|
|
self.ipiv[self.len] = (i, i2);
|
|
|
|
self.len += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Applies this sequence of permutations to the rows of `rhs`.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn permute_rows<N: Scalar, R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
{
|
|
|
|
for i in self.ipiv.rows_range(..self.len).iter() {
|
2017-08-03 01:37:44 +08:00
|
|
|
rhs.swap_rows(i.0, i.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Applies this sequence of permutations in reverse to the rows of `rhs`.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn inv_permute_rows<N: Scalar, R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
{
|
|
|
|
for i in 0..self.len {
|
2017-08-03 01:37:44 +08:00
|
|
|
let (i1, i2) = self.ipiv[self.len - i - 1];
|
|
|
|
rhs.swap_rows(i1, i2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Applies this sequence of permutations to the columns of `rhs`.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn permute_columns<N: Scalar, R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
{
|
|
|
|
for i in self.ipiv.rows_range(..self.len).iter() {
|
2017-08-03 01:37:44 +08:00
|
|
|
rhs.swap_columns(i.0, i.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Applies this sequence of permutations in reverse to the columns of `rhs`.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
pub fn inv_permute_columns<N: Scalar, R2: Dim, C2: Dim, S2>(
|
|
|
|
&self,
|
|
|
|
rhs: &mut Matrix<N, R2, C2, S2>,
|
|
|
|
) where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
{
|
|
|
|
for i in 0..self.len {
|
2017-08-03 01:37:44 +08:00
|
|
|
let (i1, i2) = self.ipiv[self.len - i - 1];
|
|
|
|
rhs.swap_columns(i1, i2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// The number of non-identity permutations applied by this sequence.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The determinant of the matrix corresponding to this permutation.
|
|
|
|
#[inline]
|
|
|
|
pub fn determinant<N: One + ClosedNeg>(&self) -> N {
|
|
|
|
if self.len % 2 == 0 {
|
|
|
|
N::one()
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2017-08-03 01:37:44 +08:00
|
|
|
-N::one()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|