use super::*;
use nalgebra::{dimension::U1, Dim, Dyn, Scalar};
use ndarray::ShapeBuilder;
impl<N: Scalar, R: Dim, S> AsNdarray1 for nalgebra::Vector<N, R, S>
where
S: nalgebra::Storage<N, R, U1>,
{
type Out<'a> = ndarray::ArrayView1<'a, N>
where
S: 'a;
fn as_ndarray1(&self) -> Self::Out<'_> {
unsafe {
ndarray::ArrayView1::from_shape_ptr(
(self.shape().0,).strides((self.strides().0,)),
self.as_ptr(),
)
}
}
}
impl<N: Scalar, R: Dim, S> AsNdarray1Mut for nalgebra::Vector<N, R, S>
where
S: nalgebra::StorageMut<N, R, U1>,
{
type Out<'a> = ndarray::ArrayViewMut1<'a, N>
where
S: 'a;
fn as_ndarray1_mut(&mut self) -> Self::Out<'_> {
unsafe {
ndarray::ArrayViewMut1::from_shape_ptr(
(self.shape().0,).strides((self.strides().0,)),
self.as_ptr() as *mut N,
)
}
}
}
impl<'a, N: Scalar, R: Dim, RStride: Dim, CStride: Dim> IntoNdarray1
for nalgebra::Vector<N, R, nalgebra::ViewStorage<'a, N, R, U1, RStride, CStride>>
{
type Out = ndarray::ArrayView1<'a, N>;
fn into_ndarray1(self) -> Self::Out {
unsafe {
ndarray::ArrayView1::from_shape_ptr(
(self.shape().0,).strides((self.strides().0,)),
self.as_ptr(),
)
}
}
}
impl<'a, N: Scalar, R: Dim, RStride: Dim, CStride: Dim> IntoNdarray1
for nalgebra::Matrix<N, R, U1, nalgebra::ViewStorageMut<'a, N, R, U1, RStride, CStride>>
{
type Out = ndarray::ArrayViewMut1<'a, N>;
fn into_ndarray1(self) -> Self::Out {
unsafe {
ndarray::ArrayViewMut1::from_shape_ptr(
(self.shape().0,).strides((self.strides().0,)),
self.as_ptr() as *mut N,
)
}
}
}
impl<N: Scalar, R: Dim, C: Dim, S> AsNdarray2 for nalgebra::Matrix<N, R, C, S>
where
S: nalgebra::Storage<N, R, C>,
{
type Out<'a> = ndarray::ArrayView2<'a, N>
where
S: 'a;
fn as_ndarray2(&self) -> Self::Out<'_> {
unsafe {
ndarray::ArrayView2::from_shape_ptr(self.shape().strides(self.strides()), self.as_ptr())
}
}
}
impl<N: Scalar, R: Dim, C: Dim, S> AsNdarray2Mut for nalgebra::Matrix<N, R, C, S>
where
S: nalgebra::StorageMut<N, R, C>,
{
type Out<'a> = ndarray::ArrayViewMut2<'a, N>
where
S: 'a;
fn as_ndarray2_mut(&mut self) -> Self::Out<'_> {
unsafe {
ndarray::ArrayViewMut2::from_shape_ptr(
self.shape().strides(self.strides()),
self.as_ptr() as *mut N,
)
}
}
}
impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> IntoNdarray2
for nalgebra::Matrix<N, R, C, nalgebra::ViewStorage<'a, N, R, C, RStride, CStride>>
{
type Out = ndarray::ArrayView2<'a, N>;
fn into_ndarray2(self) -> Self::Out {
unsafe {
ndarray::ArrayView2::from_shape_ptr(self.shape().strides(self.strides()), self.as_ptr())
}
}
}
impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> IntoNdarray2
for nalgebra::Matrix<N, R, C, nalgebra::ViewStorageMut<'a, N, R, C, RStride, CStride>>
{
type Out = ndarray::ArrayViewMut2<'a, N>;
fn into_ndarray2(self) -> Self::Out {
unsafe {
ndarray::ArrayViewMut2::from_shape_ptr(
self.shape().strides(self.strides()),
self.as_ptr() as *mut N,
)
}
}
}
impl<N: Scalar> IntoNdarray1 for nalgebra::DVector<N> {
type Out = ndarray::Array1<N>;
fn into_ndarray1(self) -> Self::Out {
ndarray::Array1::from_shape_vec((self.shape().0,), self.data.into()).unwrap()
}
}
impl<N: Scalar> IntoNdarray2 for nalgebra::Matrix<N, Dyn, Dyn, nalgebra::VecStorage<N, Dyn, Dyn>>
where
nalgebra::DefaultAllocator:
nalgebra::allocator::Allocator<Dyn, Dyn, Buffer<N> = nalgebra::VecStorage<N, Dyn, Dyn>>,
{
type Out = ndarray::Array2<N>;
fn into_ndarray2(self) -> Self::Out {
ndarray::Array2::from_shape_vec(self.shape().strides(self.strides()), self.data.into())
.unwrap()
}
}