Trait concrete_core::math::tensor::AsRefTensor [−][src]
pub trait AsRefTensor {
type Element;
type Container: AsRefSlice<Element = <Self as AsRefTensor>::Element>;
fn as_tensor(&self) -> &Tensor<Self::Container>;
}Expand description
A trait for Tensor-based types, allowing to borrow the enclosed tensor.
This trait is used by the types that build on the Tensor type to implement multi-dimensional
collections of various kind. In essence, this trait allows to extract a tensor properly
qualified to use all the methods of the Tensor type:
use concrete_core::math::tensor::{AsRefSlice, AsRefTensor, Tensor};
pub struct Matrix<Cont> {
tensor: Tensor<Cont>,
row_length: usize,
}
pub struct Row<Cont> {
tensor: Tensor<Cont>,
}
impl<Cont> AsRefTensor for Matrix<Cont>
where
Cont: AsRefSlice,
{
type Element = Cont::Element;
type Container = Cont;
fn as_tensor(&self) -> &Tensor<Cont> {
&self.tensor
}
}
impl<Cont> Matrix<Cont> {
// Returns an iterator over the matrix rows.
pub fn row_iter(&self) -> impl Iterator<Item = Row<&[<Self as AsRefTensor>::Element]>>
where
Self: AsRefTensor,
{
self.as_tensor() // `AsRefTensor` method returning a `&Tensor<Cont>`
.as_slice() // Since `Cont` is `AsView`, so is `Tensor<Cont>`
.chunks(self.row_length) // Split in chunks of the size of the rows.
.map(|sub| Row {
tensor: Tensor::from_container(sub),
}) // Wraps into a row type.
}
}Associated Types
type Container: AsRefSlice<Element = <Self as AsRefTensor>::Element>
type Container: AsRefSlice<Element = <Self as AsRefTensor>::Element>
The container used by the tensor.