Skip to content

Commit

Permalink
add straightforward implementation of From trait for SMatrix<->DMatri…
Browse files Browse the repository at this point in the history
…x and SVector<->DVector conversation
  • Loading branch information
Lishen committed Nov 21, 2023
1 parent a91e3b0 commit 3bc3df0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/base/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -428,6 +431,32 @@ where
}
}

impl<'a, T: Scalar, const R: usize, const C: usize> From<&'a DMatrix<T>> for SMatrix<T, R, C> {
fn from(m: &'a DMatrix<T>) -> Self {
let v: DMatrixView<'a, T> = m.as_view();
SMatrixView::<T, R, C>::from(&v).clone_owned()
}
}
impl<'a, T: Scalar, const R: usize, const C: usize> From<&'a SMatrix<T, R, C>> for DMatrix<T> {
fn from(m: &'a SMatrix<T, R, C>) -> Self {
let v: SMatrixView<'a, T, R, C> = m.as_view();
DMatrixView::<T>::from(&v).clone_owned()
}
}
impl<'a, T: Scalar, const R: usize> From<&'a DVector<T>> for SVector<T, R> {
fn from(m: &'a DVector<T>) -> Self {
let v: DVectorView<'a, T> = m.as_view();
SVectorView::<T, R>::from(&v).clone_owned()
}
}

impl<'a, T: Scalar, const R: usize> From<&'a SVector<T, R>> for DVector<T> {
fn from(m: &'a SVector<T, R>) -> Self {
let v: SVectorView<'a, T, R> = m.as_view();
DVectorView::<T>::from(&v).clone_owned()
}
}

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
68 changes: 67 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,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::<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_dvector() {
type T = f64;
let d = DVector::<T>::from_fn(3, |i, j| (i * 3) as T + j as T);
let s = Vector3::<T>::from(&d);
assert_eq!(d, s);
}

#[test]
fn dvector_from_svector3() {
type T = f64;
let s = Vector3::<T>::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);
}

0 comments on commit 3bc3df0

Please sign in to comment.