2020-11-19 23:18:42 +08:00
|
|
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
|
|
use alloc::vec::Vec;
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::{SubsetOf, SupersetOf};
|
2021-07-08 02:26:19 +08:00
|
|
|
use std::borrow::{Borrow, BorrowMut};
|
2018-05-19 23:15:15 +08:00
|
|
|
use std::convert::{AsMut, AsRef, From, Into};
|
2021-07-15 02:24:27 +08:00
|
|
|
use std::mem::MaybeUninit;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-03-23 16:16:01 +08:00
|
|
|
use simba::simd::{PrimitiveSimdValue, SimdValue};
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::{Allocator, SameShapeAllocator};
|
|
|
|
use crate::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
|
2020-03-21 19:16:46 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
use crate::base::dimension::Dynamic;
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::dimension::{
|
2021-01-03 22:20:34 +08:00
|
|
|
Const, Dim, DimName, U1, U10, U11, U12, U13, U14, U15, U16, U2, U3, U4, U5, U6, U7, U8, U9,
|
2018-10-13 16:25:34 +08:00
|
|
|
};
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::iter::{MatrixIter, MatrixIterMut};
|
|
|
|
use crate::base::storage::{ContiguousStorage, ContiguousStorageMut, Storage, StorageMut};
|
2020-03-21 19:16:46 +08:00
|
|
|
use crate::base::{
|
2021-04-11 17:00:38 +08:00
|
|
|
ArrayStorage, DVectorSlice, DVectorSliceMut, DefaultAllocator, Matrix, MatrixSlice,
|
|
|
|
MatrixSliceMut, OMatrix, Scalar,
|
2020-03-21 19:16:46 +08:00
|
|
|
};
|
2020-11-19 23:18:42 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
|
|
use crate::base::{DVector, VecStorage};
|
2019-11-06 00:11:39 +08:00
|
|
|
use crate::base::{SliceStorage, SliceStorageMut};
|
2019-11-05 00:53:21 +08:00
|
|
|
use crate::constraint::DimEq;
|
2021-05-07 00:22:04 +08:00
|
|
|
use crate::{IsNotStaticOne, RowSVector, SMatrix, SVector};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-05-07 00:22:04 +08:00
|
|
|
// TODO: too bad this won't work for slice conversions.
|
2021-07-16 12:56:58 +08:00
|
|
|
impl<T1, T2, R1: Dim, C1: Dim, R2: Dim, C2: Dim> SubsetOf<OMatrix<T2, R2, C2>>
|
|
|
|
for OMatrix<T1, R1, C1>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-07-16 12:56:58 +08:00
|
|
|
T2: SupersetOf<T1>,
|
2018-05-19 23:15:15 +08:00
|
|
|
DefaultAllocator:
|
2021-04-11 17:00:38 +08:00
|
|
|
Allocator<T2, R2, C2> + Allocator<T1, R1, C1> + SameShapeAllocator<T1, R1, C1, R2, C2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn to_superset(&self) -> OMatrix<T2, R2, C2> {
|
2016-12-05 05:44:42 +08:00
|
|
|
let (nrows, ncols) = self.shape();
|
|
|
|
let nrows2 = R2::from_usize(nrows);
|
|
|
|
let ncols2 = C2::from_usize(ncols);
|
|
|
|
|
2021-07-16 12:56:58 +08:00
|
|
|
let mut res = Matrix::new_uninitialized_generic(nrows2, ncols2);
|
2021-07-15 02:24:27 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..nrows {
|
|
|
|
for j in 0..ncols {
|
2020-03-21 19:16:46 +08:00
|
|
|
unsafe {
|
2021-07-15 02:24:27 +08:00
|
|
|
*res.get_unchecked_mut((i, j)) =
|
|
|
|
MaybeUninit::new(T2::from_subset(self.get_unchecked((i, j))));
|
2020-03-21 19:16:46 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 02:24:27 +08:00
|
|
|
// Safety: all entries have been initialized.
|
2021-07-16 12:56:58 +08:00
|
|
|
unsafe { res.assume_init() }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn is_in_subset(m: &OMatrix<T2, R2, C2>) -> bool {
|
2016-12-05 05:44:42 +08:00
|
|
|
m.iter().all(|e| e.is_in_subset())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from_superset_unchecked(m: &OMatrix<T2, R2, C2>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
let (nrows2, ncols2) = m.shape();
|
|
|
|
let nrows = R1::from_usize(nrows2);
|
|
|
|
let ncols = C1::from_usize(ncols2);
|
|
|
|
|
2021-07-15 02:24:27 +08:00
|
|
|
let mut res = OMatrix::new_uninitialized_generic(nrows, ncols);
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..nrows2 {
|
|
|
|
for j in 0..ncols2 {
|
2020-03-21 19:16:46 +08:00
|
|
|
unsafe {
|
2021-07-15 02:24:27 +08:00
|
|
|
*res.get_unchecked_mut((i, j)) =
|
|
|
|
MaybeUninit::new(m.get_unchecked((i, j)).to_subset_unchecked());
|
2020-03-21 19:16:46 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 02:24:27 +08:00
|
|
|
// Safety: all entries have been initialized.
|
|
|
|
unsafe { res.assume_init() }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 20:28:22 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<'a, T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> IntoIterator for &'a Matrix<T, R, C, S> {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = MatrixIter<'a, T, R, C, S>;
|
2017-04-11 06:13:28 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<'a, T: Scalar, R: Dim, C: Dim, S: StorageMut<T, R, C>> IntoIterator
|
|
|
|
for &'a mut Matrix<T, R, C, S>
|
2018-05-19 23:15:15 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
type Item = &'a mut T;
|
|
|
|
type IntoIter = MatrixIterMut<'a, T, R, C, S>;
|
2017-04-11 06:13:28 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, const D: usize> From<[T; D]> for SVector<T, D> {
|
2021-04-27 19:16:01 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(arr: [T; D]) -> Self {
|
|
|
|
unsafe { Self::from_data_statically_unchecked(ArrayStorage([arr; 1])) }
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 04:50:26 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T: Clone, const D: usize> From<SVector<T, D>> for [T; D] {
|
2021-04-27 19:16:01 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(vec: SVector<T, D>) -> Self {
|
2021-04-27 19:16:01 +08:00
|
|
|
// TODO: unfortunately, we must clone because we can move out of an array.
|
2021-06-18 15:45:37 +08:00
|
|
|
vec.data.0[0].clone()
|
2021-04-27 19:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 20:28:22 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T: Clone, const D: usize> From<[T; D]> for RowSVector<T, D>
|
2021-05-07 00:22:04 +08:00
|
|
|
where
|
|
|
|
Const<D>: IsNotStaticOne,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(arr: [T; D]) -> Self {
|
|
|
|
SVector::<T, D>::from(arr).transpose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T: Clone, const D: usize> From<RowSVector<T, D>> for [T; D]
|
2021-05-07 00:22:04 +08:00
|
|
|
where
|
|
|
|
Const<D>: IsNotStaticOne,
|
|
|
|
{
|
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(vec: RowSVector<T, D>) -> [T; D] {
|
|
|
|
vec.transpose().into()
|
2021-05-07 00:22:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:16:01 +08:00
|
|
|
macro_rules! impl_from_into_asref_1D(
|
|
|
|
($(($NRows: ident, $NCols: ident) => $SZ: expr);* $(;)*) => {$(
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T, S> AsRef<[T; $SZ]> for Matrix<T, $NRows, $NCols, S>
|
2021-07-16 14:53:28 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S: ContiguousStorage<T, $NRows, $NCols> {
|
2017-02-18 20:28:22 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn as_ref(&self) -> &[T; $SZ] {
|
2017-02-18 20:28:22 +08:00
|
|
|
unsafe {
|
2021-06-18 15:45:37 +08:00
|
|
|
&*(self.data.ptr() as *const [T; $SZ])
|
2017-02-18 20:28:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T, S> AsMut<[T; $SZ]> for Matrix<T, $NRows, $NCols, S>
|
2021-07-16 14:53:28 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S: ContiguousStorageMut<T, $NRows, $NCols> {
|
2017-02-18 20:28:22 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn as_mut(&mut self) -> &mut [T; $SZ] {
|
2017-02-18 20:28:22 +08:00
|
|
|
unsafe {
|
2021-06-18 15:45:37 +08:00
|
|
|
&mut *(self.data.ptr_mut() as *mut [T; $SZ])
|
2017-02-18 20:28:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
2017-02-27 05:15:11 +08:00
|
|
|
// Implement for vectors of dimension 1 .. 16.
|
2017-02-18 20:28:22 +08:00
|
|
|
impl_from_into_asref_1D!(
|
2017-02-27 05:15:11 +08:00
|
|
|
// Row vectors.
|
|
|
|
(U1, U1 ) => 1; (U1, U2 ) => 2; (U1, U3 ) => 3; (U1, U4 ) => 4;
|
|
|
|
(U1, U5 ) => 5; (U1, U6 ) => 6; (U1, U7 ) => 7; (U1, U8 ) => 8;
|
|
|
|
(U1, U9 ) => 9; (U1, U10) => 10; (U1, U11) => 11; (U1, U12) => 12;
|
|
|
|
(U1, U13) => 13; (U1, U14) => 14; (U1, U15) => 15; (U1, U16) => 16;
|
|
|
|
|
|
|
|
// Column vectors.
|
|
|
|
(U2 , U1) => 2; (U3 , U1) => 3; (U4 , U1) => 4;
|
|
|
|
(U5 , U1) => 5; (U6 , U1) => 6; (U7 , U1) => 7; (U8 , U1) => 8;
|
|
|
|
(U9 , U1) => 9; (U10, U1) => 10; (U11, U1) => 11; (U12, U1) => 12;
|
|
|
|
(U13, U1) => 13; (U14, U1) => 14; (U15, U1) => 15; (U16, U1) => 16;
|
2017-02-18 20:28:22 +08:00
|
|
|
);
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, const R: usize, const C: usize> From<[[T; R]; C]> for SMatrix<T, R, C> {
|
2021-04-27 19:16:01 +08:00
|
|
|
#[inline]
|
|
|
|
fn from(arr: [[T; R]; C]) -> Self {
|
|
|
|
unsafe { Self::from_data_statically_unchecked(ArrayStorage(arr)) }
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 04:49:33 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, const R: usize, const C: usize> From<SMatrix<T, R, C>> for [[T; R]; C] {
|
2021-04-27 19:16:01 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(vec: SMatrix<T, R, C>) -> Self {
|
|
|
|
vec.data.0
|
2021-04-27 19:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 20:28:22 +08:00
|
|
|
|
2021-07-08 02:26:19 +08:00
|
|
|
macro_rules! impl_from_into_asref_borrow_2D(
|
|
|
|
|
|
|
|
//does the impls on one case for either AsRef/AsMut and Borrow/BorrowMut
|
|
|
|
(
|
|
|
|
($NRows: ty, $NCols: ty) => ($SZRows: expr, $SZCols: expr);
|
|
|
|
$Ref:ident.$ref:ident(), $Mut:ident.$mut:ident()
|
|
|
|
) => {
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, S> $Ref<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S>
|
2021-04-11 17:00:38 +08:00
|
|
|
where S: ContiguousStorage<T, $NRows, $NCols> {
|
2017-02-18 20:28:22 +08:00
|
|
|
#[inline]
|
2021-07-08 02:26:19 +08:00
|
|
|
fn $ref(&self) -> &[[T; $SZRows]; $SZCols] {
|
2017-02-18 20:28:22 +08:00
|
|
|
unsafe {
|
2021-06-18 15:45:37 +08:00
|
|
|
&*(self.data.ptr() as *const [[T; $SZRows]; $SZCols])
|
2017-02-18 20:28:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<T, S> $Mut<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S>
|
2021-04-11 17:00:38 +08:00
|
|
|
where S: ContiguousStorageMut<T, $NRows, $NCols> {
|
2017-02-18 20:28:22 +08:00
|
|
|
#[inline]
|
2021-07-08 02:26:19 +08:00
|
|
|
fn $mut(&mut self) -> &mut [[T; $SZRows]; $SZCols] {
|
2017-02-18 20:28:22 +08:00
|
|
|
unsafe {
|
2021-06-18 15:45:37 +08:00
|
|
|
&mut *(self.data.ptr_mut() as *mut [[T; $SZRows]; $SZCols])
|
2017-02-18 20:28:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 02:26:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//collects the mappings from typenum pairs to consts
|
|
|
|
($(($NRows: ty, $NCols: ty) => ($SZRows: expr, $SZCols: expr));* $(;)*) => {$(
|
|
|
|
impl_from_into_asref_borrow_2D!(
|
|
|
|
($NRows, $NCols) => ($SZRows, $SZCols); AsRef.as_ref(), AsMut.as_mut()
|
|
|
|
);
|
|
|
|
impl_from_into_asref_borrow_2D!(
|
|
|
|
($NRows, $NCols) => ($SZRows, $SZCols); Borrow.borrow(), BorrowMut.borrow_mut()
|
|
|
|
);
|
2017-02-18 20:28:22 +08:00
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
// Implement for matrices with shape 2x2 .. 6x6.
|
2021-07-08 02:26:19 +08:00
|
|
|
impl_from_into_asref_borrow_2D!(
|
2017-02-27 05:15:11 +08:00
|
|
|
(U2, U2) => (2, 2); (U2, U3) => (2, 3); (U2, U4) => (2, 4); (U2, U5) => (2, 5); (U2, U6) => (2, 6);
|
|
|
|
(U3, U2) => (3, 2); (U3, U3) => (3, 3); (U3, U4) => (3, 4); (U3, U5) => (3, 5); (U3, U6) => (3, 6);
|
|
|
|
(U4, U2) => (4, 2); (U4, U3) => (4, 3); (U4, U4) => (4, 4); (U4, U5) => (4, 5); (U4, U6) => (4, 6);
|
|
|
|
(U5, U2) => (5, 2); (U5, U3) => (5, 3); (U5, U4) => (5, 4); (U5, U5) => (5, 5); (U5, U6) => (5, 6);
|
|
|
|
(U6, U2) => (6, 2); (U6, U3) => (6, 3); (U6, U4) => (6, 4); (U6, U5) => (6, 5); (U6, U6) => (6, 6);
|
2017-02-18 20:28:22 +08:00
|
|
|
);
|
2017-07-25 23:04:12 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, RStride: Dim, CStride: Dim, const R: usize, const C: usize>
|
2021-04-11 17:00:38 +08:00
|
|
|
From<MatrixSlice<'a, T, Const<R>, Const<C>, RStride, CStride>>
|
|
|
|
for Matrix<T, Const<R>, Const<C>, ArrayStorage<T, R, C>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSlice<'a, T, Const<R>, Const<C>, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:30:28 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, C: Dim, RStride: Dim, CStride: Dim>
|
|
|
|
From<MatrixSlice<'a, T, Dynamic, C, RStride, CStride>>
|
2021-04-11 17:00:38 +08:00
|
|
|
for Matrix<T, Dynamic, C, VecStorage<T, Dynamic, C>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSlice<'a, T, Dynamic, C, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:30:28 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, R: DimName, RStride: Dim, CStride: Dim>
|
|
|
|
From<MatrixSlice<'a, T, R, Dynamic, RStride, CStride>>
|
2021-04-11 17:00:38 +08:00
|
|
|
for Matrix<T, R, Dynamic, VecStorage<T, R, Dynamic>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSlice<'a, T, R, Dynamic, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, RStride: Dim, CStride: Dim, const R: usize, const C: usize>
|
2021-04-11 17:00:38 +08:00
|
|
|
From<MatrixSliceMut<'a, T, Const<R>, Const<C>, RStride, CStride>>
|
|
|
|
for Matrix<T, Const<R>, Const<C>, ArrayStorage<T, R, C>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSliceMut<'a, T, Const<R>, Const<C>, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:30:28 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, C: Dim, RStride: Dim, CStride: Dim>
|
|
|
|
From<MatrixSliceMut<'a, T, Dynamic, C, RStride, CStride>>
|
2021-04-11 17:00:38 +08:00
|
|
|
for Matrix<T, Dynamic, C, VecStorage<T, Dynamic, C>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSliceMut<'a, T, Dynamic, C, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:30:28 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Clone, R: DimName, RStride: Dim, CStride: Dim>
|
|
|
|
From<MatrixSliceMut<'a, T, R, Dynamic, RStride, CStride>>
|
2021-04-11 17:00:38 +08:00
|
|
|
for Matrix<T, R, Dynamic, VecStorage<T, R, Dynamic>>
|
2018-09-29 04:54:41 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(matrix_slice: MatrixSliceMut<'a, T, R, Dynamic, RStride, CStride>) -> Self {
|
2018-09-29 04:54:41 +08:00
|
|
|
matrix_slice.into_owned()
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 00:53:21 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T, R: Dim, C: Dim, RSlice: Dim, CSlice: Dim, RStride: Dim, CStride: Dim, S>
|
|
|
|
From<&'a Matrix<T, R, C, S>> for MatrixSlice<'a, T, RSlice, CSlice, RStride, CStride>
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S: Storage<T, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
ShapeConstraint: DimEq<R, RSlice>
|
|
|
|
+ DimEq<C, CSlice>
|
|
|
|
+ DimEq<RStride, S::RStride>
|
|
|
|
+ DimEq<CStride, S::CStride>,
|
2019-11-05 00:53:21 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(m: &'a Matrix<T, R, C, S>) -> Self {
|
2019-11-05 00:53:21 +08:00
|
|
|
let (row, col) = m.data.shape();
|
|
|
|
let row_slice = RSlice::from_usize(row.value());
|
|
|
|
let col_slice = CSlice::from_usize(col.value());
|
2019-11-06 00:11:39 +08:00
|
|
|
|
|
|
|
let (rstride, cstride) = m.strides();
|
|
|
|
|
|
|
|
let rstride_slice = RStride::from_usize(rstride);
|
|
|
|
let cstride_slice = CStride::from_usize(cstride);
|
|
|
|
|
|
|
|
unsafe {
|
2020-03-21 19:16:46 +08:00
|
|
|
let data = SliceStorage::from_raw_parts(
|
|
|
|
m.data.ptr(),
|
|
|
|
(row_slice, col_slice),
|
|
|
|
(rstride_slice, cstride_slice),
|
|
|
|
);
|
2019-11-06 00:11:39 +08:00
|
|
|
Matrix::from_data_statically_unchecked(data)
|
|
|
|
}
|
2019-11-05 00:53:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T, R: Dim, C: Dim, RSlice: Dim, CSlice: Dim, RStride: Dim, CStride: Dim, S>
|
|
|
|
From<&'a mut Matrix<T, R, C, S>> for MatrixSlice<'a, T, RSlice, CSlice, RStride, CStride>
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S: Storage<T, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
ShapeConstraint: DimEq<R, RSlice>
|
|
|
|
+ DimEq<C, CSlice>
|
|
|
|
+ DimEq<RStride, S::RStride>
|
|
|
|
+ DimEq<CStride, S::CStride>,
|
2019-11-05 00:53:21 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(m: &'a mut Matrix<T, R, C, S>) -> Self {
|
2019-11-05 00:53:21 +08:00
|
|
|
let (row, col) = m.data.shape();
|
|
|
|
let row_slice = RSlice::from_usize(row.value());
|
|
|
|
let col_slice = CSlice::from_usize(col.value());
|
2019-11-06 00:11:39 +08:00
|
|
|
|
|
|
|
let (rstride, cstride) = m.strides();
|
|
|
|
|
|
|
|
let rstride_slice = RStride::from_usize(rstride);
|
|
|
|
let cstride_slice = CStride::from_usize(cstride);
|
|
|
|
|
|
|
|
unsafe {
|
2020-03-21 19:16:46 +08:00
|
|
|
let data = SliceStorage::from_raw_parts(
|
|
|
|
m.data.ptr(),
|
|
|
|
(row_slice, col_slice),
|
|
|
|
(rstride_slice, cstride_slice),
|
|
|
|
);
|
2019-11-06 00:11:39 +08:00
|
|
|
Matrix::from_data_statically_unchecked(data)
|
|
|
|
}
|
2019-11-05 00:53:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T: Dim, R: Dim, C: Dim, RSlice: Dim, CSlice: Dim, RStride: Dim, CStride: Dim, S>
|
|
|
|
From<&'a mut Matrix<T, R, C, S>> for MatrixSliceMut<'a, T, RSlice, CSlice, RStride, CStride>
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S: StorageMut<T, R, C>,
|
2020-03-21 19:16:46 +08:00
|
|
|
ShapeConstraint: DimEq<R, RSlice>
|
|
|
|
+ DimEq<C, CSlice>
|
|
|
|
+ DimEq<RStride, S::RStride>
|
|
|
|
+ DimEq<CStride, S::CStride>,
|
2019-11-05 00:53:21 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(m: &'a mut Matrix<T, R, C, S>) -> Self {
|
2019-11-05 00:53:21 +08:00
|
|
|
let (row, col) = m.data.shape();
|
|
|
|
let row_slice = RSlice::from_usize(row.value());
|
|
|
|
let col_slice = CSlice::from_usize(col.value());
|
2019-11-06 00:11:39 +08:00
|
|
|
|
|
|
|
let (rstride, cstride) = m.strides();
|
|
|
|
|
|
|
|
let rstride_slice = RStride::from_usize(rstride);
|
|
|
|
let cstride_slice = CStride::from_usize(cstride);
|
|
|
|
|
|
|
|
unsafe {
|
2020-03-21 19:16:46 +08:00
|
|
|
let data = SliceStorageMut::from_raw_parts(
|
|
|
|
m.data.ptr_mut(),
|
|
|
|
(row_slice, col_slice),
|
|
|
|
(rstride_slice, cstride_slice),
|
|
|
|
);
|
2019-11-06 00:11:39 +08:00
|
|
|
Matrix::from_data_statically_unchecked(data)
|
|
|
|
}
|
2019-11-05 00:53:21 +08:00
|
|
|
}
|
2019-12-25 00:37:27 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 23:18:42 +08:00
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T> From<Vec<T>> for DVector<T> {
|
2020-11-19 23:18:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(vec: Vec<T>) -> Self {
|
2020-11-19 23:18:42 +08:00
|
|
|
Self::from_vec(vec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T, R: Dim, C: Dim, S: ContiguousStorage<T, R, C>> From<&'a Matrix<T, R, C, S>>
|
|
|
|
for &'a [T]
|
2020-03-21 19:16:46 +08:00
|
|
|
{
|
2019-12-25 00:37:27 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(matrix: &'a Matrix<T, R, C, S>) -> Self {
|
|
|
|
matrix.as_slice()
|
2019-12-25 00:37:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T, R: Dim, C: Dim, S: ContiguousStorageMut<T, R, C>> From<&'a mut Matrix<T, R, C, S>>
|
|
|
|
for &'a mut [T]
|
2020-03-21 19:16:46 +08:00
|
|
|
{
|
2019-12-25 00:37:27 +08:00
|
|
|
#[inline]
|
2021-06-18 15:45:37 +08:00
|
|
|
fn from(matrix: &'a mut Matrix<T, R, C, S>) -> Self {
|
|
|
|
matrix.as_mut_slice()
|
2019-12-25 00:37:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T> From<&'a [T]> for DVectorSlice<'a, T> {
|
2019-12-25 00:37:27 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(slice: &'a [T]) -> Self {
|
2019-12-25 00:37:27 +08:00
|
|
|
Self::from_slice(slice, slice.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T> From<DVectorSlice<'a, T>> for &'a [T] {
|
2021-06-18 08:43:05 +08:00
|
|
|
fn from(vec: DVectorSlice<'a, T>) -> &'a [T] {
|
|
|
|
vec.data.into_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T> From<&'a mut [T]> for DVectorSliceMut<'a, T> {
|
2019-12-25 00:37:27 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(slice: &'a mut [T]) -> Self {
|
2019-12-25 00:37:27 +08:00
|
|
|
Self::from_slice(slice, slice.len())
|
|
|
|
}
|
2020-03-21 19:16:46 +08:00
|
|
|
}
|
2020-03-23 16:16:01 +08:00
|
|
|
|
2021-07-16 14:53:28 +08:00
|
|
|
impl<'a, T> From<DVectorSliceMut<'a, T>> for &'a mut [T] {
|
2021-06-18 08:43:05 +08:00
|
|
|
fn from(vec: DVectorSliceMut<'a, T>) -> &'a mut [T] {
|
|
|
|
vec.data.into_slice_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, R: Dim, C: Dim> From<[OMatrix<T::Element, R, C>; 2]>
|
|
|
|
for OMatrix<T, R, C>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 2]>,
|
|
|
|
T::Element: Scalar + SimdValue,
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [OMatrix<T::Element, R, C>; 2]) -> Self {
|
2020-03-23 16:16:01 +08:00
|
|
|
let (nrows, ncols) = arr[0].data.shape();
|
|
|
|
|
|
|
|
Self::from_fn_generic(nrows, ncols, |i, j| {
|
|
|
|
[
|
|
|
|
arr[0][(i, j)].inlined_clone(),
|
|
|
|
arr[1][(i, j)].inlined_clone(),
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, R: Dim, C: Dim> From<[OMatrix<T::Element, R, C>; 4]>
|
|
|
|
for OMatrix<T, R, C>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 4]>,
|
|
|
|
T::Element: Scalar + SimdValue,
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [OMatrix<T::Element, R, C>; 4]) -> Self {
|
2020-03-23 16:16:01 +08:00
|
|
|
let (nrows, ncols) = arr[0].data.shape();
|
|
|
|
|
|
|
|
Self::from_fn_generic(nrows, ncols, |i, j| {
|
|
|
|
[
|
|
|
|
arr[0][(i, j)].inlined_clone(),
|
|
|
|
arr[1][(i, j)].inlined_clone(),
|
|
|
|
arr[2][(i, j)].inlined_clone(),
|
|
|
|
arr[3][(i, j)].inlined_clone(),
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, R: Dim, C: Dim> From<[OMatrix<T::Element, R, C>; 8]>
|
|
|
|
for OMatrix<T, R, C>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 8]>,
|
|
|
|
T::Element: Scalar + SimdValue,
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [OMatrix<T::Element, R, C>; 8]) -> Self {
|
2020-03-23 16:16:01 +08:00
|
|
|
let (nrows, ncols) = arr[0].data.shape();
|
|
|
|
|
|
|
|
Self::from_fn_generic(nrows, ncols, |i, j| {
|
|
|
|
[
|
|
|
|
arr[0][(i, j)].inlined_clone(),
|
|
|
|
arr[1][(i, j)].inlined_clone(),
|
|
|
|
arr[2][(i, j)].inlined_clone(),
|
|
|
|
arr[3][(i, j)].inlined_clone(),
|
|
|
|
arr[4][(i, j)].inlined_clone(),
|
|
|
|
arr[5][(i, j)].inlined_clone(),
|
|
|
|
arr[6][(i, j)].inlined_clone(),
|
|
|
|
arr[7][(i, j)].inlined_clone(),
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + PrimitiveSimdValue, R: Dim, C: Dim> From<[OMatrix<T::Element, R, C>; 16]>
|
|
|
|
for OMatrix<T, R, C>
|
2020-03-23 16:16:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
T: From<[<T as SimdValue>::Element; 16]>,
|
|
|
|
T::Element: Scalar + SimdValue,
|
|
|
|
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
|
2020-03-23 16:16:01 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(arr: [OMatrix<T::Element, R, C>; 16]) -> Self {
|
2020-03-23 16:16:01 +08:00
|
|
|
let (nrows, ncols) = arr[0].data.shape();
|
|
|
|
|
|
|
|
Self::from_fn_generic(nrows, ncols, |i, j| {
|
|
|
|
[
|
|
|
|
arr[0][(i, j)].inlined_clone(),
|
|
|
|
arr[1][(i, j)].inlined_clone(),
|
|
|
|
arr[2][(i, j)].inlined_clone(),
|
|
|
|
arr[3][(i, j)].inlined_clone(),
|
|
|
|
arr[4][(i, j)].inlined_clone(),
|
|
|
|
arr[5][(i, j)].inlined_clone(),
|
|
|
|
arr[6][(i, j)].inlined_clone(),
|
|
|
|
arr[7][(i, j)].inlined_clone(),
|
|
|
|
arr[8][(i, j)].inlined_clone(),
|
|
|
|
arr[9][(i, j)].inlined_clone(),
|
|
|
|
arr[10][(i, j)].inlined_clone(),
|
|
|
|
arr[11][(i, j)].inlined_clone(),
|
|
|
|
arr[12][(i, j)].inlined_clone(),
|
|
|
|
arr[13][(i, j)].inlined_clone(),
|
|
|
|
arr[14][(i, j)].inlined_clone(),
|
|
|
|
arr[15][(i, j)].inlined_clone(),
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|