diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 783e6d9ed..ace35fe45 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -23,7 +23,10 @@ use crate::base::{ use crate::base::{DVector, RowDVector, VecStorage}; use crate::base::{ViewStorage, ViewStorageMut}; use crate::constraint::DimEq; -use crate::{IsNotStaticOne, RowSVector, SMatrix, SVector, VectorView, VectorViewMut}; +use crate::{ + DMatrix, DMatrixView, IsNotStaticOne, RowSVector, SMatrix, SMatrixView, SVector, SVectorView, + VectorView, VectorViewMut, +}; use std::mem::MaybeUninit; // TODO: too bad this won't work for slice conversions. @@ -428,6 +431,32 @@ where } } +impl<'a, T: Scalar, const R: usize, const C: usize> From<&'a DMatrix> for SMatrix { + fn from(m: &'a DMatrix) -> Self { + let v: DMatrixView<'a, T> = m.as_view(); + SMatrixView::::from(&v).clone_owned() + } +} +impl<'a, T: Scalar, const R: usize, const C: usize> From<&'a SMatrix> for DMatrix { + fn from(m: &'a SMatrix) -> Self { + let v: SMatrixView<'a, T, R, C> = m.as_view(); + DMatrixView::::from(&v).clone_owned() + } +} +impl<'a, T: Scalar, const R: usize> From<&'a DVector> for SVector { + fn from(m: &'a DVector) -> Self { + let v: DVectorView<'a, T> = m.as_view(); + SVectorView::::from(&v).clone_owned() + } +} + +impl<'a, T: Scalar, const R: usize> From<&'a SVector> for DVector { + fn from(m: &'a SVector) -> Self { + let v: SVectorView<'a, T, R> = m.as_view(); + DVectorView::::from(&v).clone_owned() + } +} + impl<'a, T, R, C, RView, CView, RStride, CStride, S> From<&'a mut Matrix> for MatrixView<'a, T, RView, CView, RStride, CStride> where diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 501a05665..18a167ad2 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -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] @@ -1332,3 +1333,68 @@ 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::::from_fn(|i, j| (i * 3) as T + j as T); + let d = DMatrix::::from(&s); + assert_eq!(d, s); +} + +#[test] +fn smatrix_from_dmatrix() { + type T = f64; + let d = DMatrix::::from_fn(3, 3, |i, j| (i * 3) as T + j as T); + let s = SMatrix::::from(&d); + assert_eq!(d, s); +} + +#[test] +fn svector_from_dvector() { + type T = f64; + let d = DVector::::from_fn(3, |i, j| (i * 3) as T + j as T); + let s = SVector::::from(&d); + assert_eq!(d, s); +} + +#[test] +fn dvector_from_svector() { + type T = f64; + let s = SVector::::from_fn(|i, j| (i * 3) as T + j as T); + let d = DVector::::from(&s); + assert_eq!(d, s); +} + +#[test] +fn svector3_from_dvector() { + type T = f64; + let d = DVector::::from_fn(3, |i, j| (i * 3) as T + j as T); + let s = Vector3::::from(&d); + assert_eq!(d, s); +} + +#[test] +fn dvector_from_svector3() { + type T = f64; + let s = Vector3::::from_fn(|i, j| (i * 3) as T + j as T); + let d = DVector::::from(&s); + assert_eq!(d, s); +} + +#[test] +fn svector3_from_dmatrix3x1() { + type T = f64; + let d = DMatrix::::from_fn(3, 1, |i, j| (i * 3) as T + j as T); + let s = Vector3::::from(&d); + assert_eq!(d, s); +} + +#[test] +#[should_panic] +fn svector3_from_dmatrix3x3() { + type T = f64; + let d = DMatrix::::from_fn(3, 3, |i, j| (i * 3) as T + j as T); + let s = Vector3::::from(&d); + assert_eq!(d, s); +}