use alga::general::{SubsetOf, SupersetOf}; use core::{Scalar, Matrix}; use core::dimension::Dim; use core::constraint::{ShapeConstraint, SameNumberOfRows, SameNumberOfColumns}; use core::storage::OwnedStorage; use core::allocator::{OwnedAllocator, SameShapeAllocator}; // FIXME: too bad this won't work allo slice conversions. impl SubsetOf> for Matrix where R1: Dim, C1: Dim, R2: Dim, C2: Dim, N1: Scalar, N2: Scalar + SupersetOf, SA: OwnedStorage, SB: OwnedStorage, SB::Alloc: OwnedAllocator, SA::Alloc: OwnedAllocator + SameShapeAllocator, ShapeConstraint: SameNumberOfRows + SameNumberOfColumns { #[inline] fn to_superset(&self) -> Matrix { let (nrows, ncols) = self.shape(); let nrows2 = R2::from_usize(nrows); let ncols2 = C2::from_usize(ncols); let mut res = unsafe { Matrix::::new_uninitialized_generic(nrows2, ncols2) }; for i in 0 .. nrows { for j in 0 .. ncols { unsafe { *res.get_unchecked_mut(i, j) = N2::from_subset(self.get_unchecked(i, j)) } } } res } #[inline] fn is_in_subset(m: &Matrix) -> bool { m.iter().all(|e| e.is_in_subset()) } #[inline] unsafe fn from_superset_unchecked(m: &Matrix) -> Self { let (nrows2, ncols2) = m.shape(); let nrows = R1::from_usize(nrows2); let ncols = C1::from_usize(ncols2); let mut res = Self::new_uninitialized_generic(nrows, ncols); for i in 0 .. nrows2 { for j in 0 .. ncols2 { *res.get_unchecked_mut(i, j) = m.get_unchecked(i, j).to_subset_unchecked() } } res } }