Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implementation of From trait for SMatrix<->DMatrix, SVector<->DVector #1324

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/base/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::panic;
use simba::scalar::{SubsetOf, SupersetOf};
use std::borrow::{Borrow, BorrowMut};
use std::convert::{AsMut, AsRef, From, Into};
Expand Down Expand Up @@ -428,6 +430,69 @@ where
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, T: Scalar, const R0: usize, const C0: usize, R1: Dim, C1: Dim>
TryFrom<&VecStorage<T, R1, C1>> for ArrayStorage<T, R0, C0>
{
// TODO: where to put Error enum ?
type Error = ();

fn try_from(vstorage: &VecStorage<T, R1, C1>) -> Result<Self, Self::Error> {
if R0 * C0 != vstorage.len() {
Err(())
} else {
let mut data = MaybeUninit::<[[T; R0]; C0]>::uninit();
unsafe { core::slice::from_raw_parts_mut(data.as_mut_ptr() as *mut T, R0 * C0) }
.clone_from_slice(vstorage.as_slice());
Ok(Self(unsafe { data.assume_init() }))
}
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, T: Scalar, const R0: usize, const C0: usize, R1: Dim, C1: Dim>
From<&ArrayStorage<T, R0, C0>> for VecStorage<T, R1, C1>
{
fn from(astorage: &ArrayStorage<T, R0, C0>) -> Self {
VecStorage::new(
R1::from_usize(R0),
C1::from_usize(C0),
Vec::from(astorage.as_slice()),
)
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, T: Scalar, const R0: usize, const C0: usize, R1: Dim, C1: Dim>
From<&'a Matrix<T, R1, C1, VecStorage<T, R1, C1>>>
for Matrix<T, Const<R0>, Const<C0>, ArrayStorage<T, R0, C0>>
where
VecStorage<T, R1, C1>: RawStorage<T, R1, C1>,
{
fn from(m: &'a Matrix<T, R1, C1, VecStorage<T, R1, C1>>) -> Self {
match ArrayStorage::<T, R0, C0>::try_from(&m.data) {
Ok(s) => {
assert_eq!(R0, m.nrows());
assert_eq!(C0, m.ncols());
Self::from_array_storage(s)
}
Err(_) => panic!("storage dimensions mismatch"),
}
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, T: Scalar, const R0: usize, const C0: usize, R1: Dim, C1: Dim>
From<&'a Matrix<T, Const<R0>, Const<C0>, ArrayStorage<T, R0, C0>>>
for Matrix<T, R1, C1, VecStorage<T, R1, C1>>
where
VecStorage<T, R1, C1>: RawStorage<T, R1, C1>,
{
fn from(m: &'a Matrix<T, Const<R0>, Const<C0>, ArrayStorage<T, R0, C0>>) -> Self {
Self::from_data(VecStorage::from(&m.data))
}
}

impl<'a, T, R, C, RView, CView, RStride, CStride, S> From<&'a mut Matrix<T, R, C, S>>
for MatrixView<'a, T, RView, CView, RStride, CStride>
where
Expand Down
59 changes: 58 additions & 1 deletion tests/core/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use na::dimension::{U15, U8};
use na::{
self, Const, DMatrix, DVector, Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4,
Matrix4, Matrix4x3, Matrix4x5, Matrix5, Matrix6, MatrixView2x3, MatrixViewMut2x3, OMatrix,
RowVector3, RowVector4, RowVector5, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6,
RowVector3, RowVector4, RowVector5, SMatrix, SVector, Vector1, Vector2, Vector3, Vector4,
Vector5, Vector6,
};

#[test]
Expand Down Expand Up @@ -1332,3 +1333,59 @@ fn parallel_column_iteration_mut() {
assert_eq!(first, second);
assert_eq!(second, DMatrix::identity(400, 300));
}

#[test]
fn dmatrix_from_smatrix() {
type T = f64;
let s = SMatrix::<T, 3, 3>::from_fn(|i, j| (i * 3) as T + j as T);
let d = DMatrix::<T>::from(&s);
assert_eq!(d, s);
}

#[test]
fn smatrix_from_dmatrix() {
type T = f64;
let d = DMatrix::<T>::from_fn(3, 3, |i, j| (i * 3) as T + j as T);
let s = SMatrix::<T, 3, 3>::from(&d);
assert_eq!(d, s);
}

#[test]
fn svector_from_dvector() {
type T = f64;
let d = DVector::<T>::from_fn(3, |i, j| (i * 3) as T + j as T);
let s = SVector::<T, 3>::from(&d);
assert_eq!(d, s);
}

#[test]
fn dvector_from_svector() {
type T = f64;
let s = SVector::<T, 3>::from_fn(|i, j| (i * 3) as T + j as T);
let d = DVector::<T>::from(&s);
assert_eq!(d, s);
}

#[test]
fn svector3_from_dmatrix3x1() {
type T = f64;
let d = DMatrix::<T>::from_fn(3, 1, |i, j| (i * 3) as T + j as T);
let s = Vector3::<T>::from(&d);
assert_eq!(d, s);
}

#[test]
#[should_panic]
fn svector3_from_dmatrix3x3() {
type T = f64;
let d = DMatrix::<T>::from_fn(3, 3, |i, j| (i * 3) as T + j as T);
let s = Vector3::<T>::from(&d);
assert_eq!(d, s);
}
#[test]
fn dmatrix_from_svector3() {
type T = f64;
let s = SVector::<T, 3>::from_fn(|i, j| (i * 3) as T + j as T);
let d = DMatrix::<T>::from(&s);
assert_eq!(d, s);
}
Loading