diff --git a/examples/matrix_construction.rs b/examples/matrix_construction.rs index bb78458f..06f4c30b 100644 --- a/examples/matrix_construction.rs +++ b/examples/matrix_construction.rs @@ -1,6 +1,6 @@ extern crate nalgebra as na; -use na::{DMatrix, Matrix2x3, RowVector3, Vector2}; +use na::{DMatrix, Matrix2x3, Matrix3x2, RowVector3, Vector2}; fn main() { // All the following matrices are equal but constructed in different ways. diff --git a/examples/reshaping.rs b/examples/reshaping.rs new file mode 100644 index 00000000..bf02e769 --- /dev/null +++ b/examples/reshaping.rs @@ -0,0 +1,33 @@ +extern crate nalgebra as na; + +use na::{DMatrix, Dynamic, Matrix2x3, Matrix3x2, U2, U3}; + +fn main() { + // Matrices can be reshaped in-place without moving or copying values. + let m1 = Matrix2x3::new(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + let m2 = Matrix3x2::new(1.1, 2.2, 2.1, 1.3, 1.2, 2.3); + + let m3 = m1.reshape_generic(U3, U2); + assert_eq!(m3, m2); + + // Note that, for statically sized matrices, invalid reshapes will not compile: + //let m4 = m3.reshape_generic(U3, U3); + + // If dynamically sized matrices are used, the reshaping is checked at run-time. + let dm1 = DMatrix::from_vec( + 4, + 3, + vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], + ); + let dm2 = DMatrix::from_vec( + 6, + 2, + vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], + ); + + let dm3 = dm1.reshape_generic(Dynamic::new(6), Dynamic::new(2)); + assert_eq!(dm3, dm2); + + // Invalid reshapings of dynamic matrices will panic at run-time. + //let dm4 = dm3.reshape_generic(Dynamic::new(6), Dynamic::new(6)); +} diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 1743f06b..b86e3a80 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -24,7 +24,9 @@ use typenum::Prod; use crate::base::allocator::Allocator; use crate::base::default_allocator::DefaultAllocator; use crate::base::dimension::{DimName, U1}; -use crate::base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut}; +use crate::base::storage::{ + ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut, +}; use crate::base::Scalar; /* @@ -267,6 +269,25 @@ where { } +impl ReshapableStorage for ArrayStorage +where + N: Scalar, + R1: DimName, + C1: DimName, + R1::Value: Mul, + Prod: ArrayLength, + R2: DimName, + C2: DimName, + R2::Value: Mul>, + Prod: ArrayLength, +{ + type Output = ArrayStorage; + + fn reshape_generic(self, _: R2, _: C2) -> Self::Output { + ArrayStorage { data: self.data } + } +} + /* * * Allocation-less serde impls. diff --git a/src/base/edition.rs b/src/base/edition.rs index b3133648..896e6635 100644 --- a/src/base/edition.rs +++ b/src/base/edition.rs @@ -13,7 +13,7 @@ use crate::base::dimension::Dynamic; use crate::base::dimension::{ Dim, DimAdd, DimDiff, DimMin, DimMinimum, DimName, DimSub, DimSum, U1, }; -use crate::base::storage::{Storage, StorageMut}; +use crate::base::storage::{ReshapableStorage, Storage, StorageMut}; #[cfg(any(feature = "std", feature = "alloc"))] use crate::base::DMatrix; use crate::base::{DefaultAllocator, Matrix, MatrixMN, RowVector, Scalar, Vector}; @@ -745,7 +745,7 @@ impl> Matrix { self.resize_generic(R2::name(), C2::name(), val) } - /// Resizes `self` such that it has dimensions `new_nrows × now_ncols`. + /// Resizes `self` such that it has dimensions `new_nrows × new_ncols`. /// /// The values are copied such that `self[(i, j)] == result[(i, j)]`. If the result has more /// rows and/or columns than `self`, then the extra rows or columns are filled with `val`. @@ -813,6 +813,31 @@ impl> Matrix { } } +impl Matrix +where + N: Scalar, + R: Dim, + C: Dim, +{ + /// Reshapes `self` in-place such that it has dimensions `new_nrows × new_ncols`. + /// + /// The values are not copied or moved. This function will panic if dynamic sizes are provided + /// and not compatible. + pub fn reshape_generic( + self, + new_nrows: R2, + new_ncols: C2, + ) -> Matrix + where + R2: Dim, + C2: Dim, + S: ReshapableStorage, + { + let data = self.data.reshape_generic(new_nrows, new_ncols); + Matrix::from_data(data) + } +} + #[cfg(any(feature = "std", feature = "alloc"))] impl DMatrix { /// Resizes this matrix in-place. diff --git a/src/base/storage.rs b/src/base/storage.rs index 9f039ba2..7b197861 100644 --- a/src/base/storage.rs +++ b/src/base/storage.rs @@ -171,7 +171,7 @@ pub unsafe trait StorageMut: Storage { /// A matrix storage that is stored contiguously in memory. /// -/// The storage requirement means that for any value of `i` in `[0, nrows * ncols[`, the value +/// The storage requirement means that for any value of `i` in `[0, nrows * ncols]`, the value /// `.get_unchecked_linear` returns one of the matrix component. This trait is unsafe because /// failing to comply to this may cause Undefined Behaviors. pub unsafe trait ContiguousStorage: @@ -181,10 +181,26 @@ pub unsafe trait ContiguousStorage: /// A mutable matrix storage that is stored contiguously in memory. /// -/// The storage requirement means that for any value of `i` in `[0, nrows * ncols[`, the value +/// The storage requirement means that for any value of `i` in `[0, nrows * ncols]`, the value /// `.get_unchecked_linear` returns one of the matrix component. This trait is unsafe because /// failing to comply to this may cause Undefined Behaviors. pub unsafe trait ContiguousStorageMut: ContiguousStorage + StorageMut { } + +/// A matrix storage that can be reshaped in-place. +pub trait ReshapableStorage: Storage +where + N: Scalar, + R1: Dim, + C1: Dim, + R2: Dim, + C2: Dim, +{ + /// The reshaped storage type. + type Output: Storage; + + /// Reshapes the storage into the output storage type. + fn reshape_generic(self, nrows: R2, ncols: C2) -> Self::Output; +} diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index 40909c32..37f31213 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -8,7 +8,9 @@ use crate::base::allocator::Allocator; use crate::base::constraint::{SameNumberOfRows, ShapeConstraint}; use crate::base::default_allocator::DefaultAllocator; use crate::base::dimension::{Dim, DimName, Dynamic, U1}; -use crate::base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut}; +use crate::base::storage::{ + ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut, +}; use crate::base::{Scalar, Vector}; #[cfg(feature = "abomonation-serialize")] @@ -225,6 +227,42 @@ unsafe impl ContiguousStorageMut for VecStorag { } +impl ReshapableStorage for VecStorage +where + N: Scalar, + C1: Dim, + C2: Dim, +{ + type Output = VecStorage; + + fn reshape_generic(self, nrows: Dynamic, ncols: C2) -> Self::Output { + assert_eq!(nrows.value() * ncols.value(), self.data.len()); + VecStorage { + data: self.data, + nrows, + ncols, + } + } +} + +impl ReshapableStorage for VecStorage +where + N: Scalar, + C1: Dim, + R2: DimName, +{ + type Output = VecStorage; + + fn reshape_generic(self, nrows: R2, ncols: Dynamic) -> Self::Output { + assert_eq!(nrows.value() * ncols.value(), self.data.len()); + VecStorage { + data: self.data, + nrows, + ncols, + } + } +} + unsafe impl StorageMut for VecStorage where DefaultAllocator: Allocator, @@ -240,6 +278,42 @@ where } } +impl ReshapableStorage for VecStorage +where + N: Scalar, + R1: DimName, + C2: Dim, +{ + type Output = VecStorage; + + fn reshape_generic(self, nrows: Dynamic, ncols: C2) -> Self::Output { + assert_eq!(nrows.value() * ncols.value(), self.data.len()); + VecStorage { + data: self.data, + nrows, + ncols, + } + } +} + +impl ReshapableStorage for VecStorage +where + N: Scalar, + R1: DimName, + R2: DimName, +{ + type Output = VecStorage; + + fn reshape_generic(self, nrows: R2, ncols: Dynamic) -> Self::Output { + assert_eq!(nrows.value() * ncols.value(), self.data.len()); + VecStorage { + data: self.data, + nrows, + ncols, + } + } +} + #[cfg(feature = "abomonation-serialize")] impl Abomonation for VecStorage { unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> {