Trait rten_tensor::AsView

source ·
pub trait AsView: Layout {
    type Elem;
    type Layout: for<'a> MutLayout<Index<'a> = Self::Index<'a>>;

Show 43 methods // Required methods fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>; fn layout(&self) -> &Self::Layout; fn data(&self) -> Option<&[Self::Elem]>; fn insert_axis(&mut self, index: usize) where Self::Layout: ResizeLayout; fn remove_axis(&mut self, index: usize) where Self::Layout: ResizeLayout; fn iter(&self) -> Iter<'_, Self::Elem> ; fn merge_axes(&mut self) where Self::Layout: ResizeLayout; fn move_axis(&mut self, from: usize, to: usize); fn permute(&mut self, order: Self::Index<'_>); fn transpose(&mut self); fn to_vec(&self) -> Vec<Self::Elem> where Self::Elem: Clone; fn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem> where Self::Elem: Clone; fn to_shape<S: IntoLayout>( &self, shape: S, ) -> TensorBase<Vec<Self::Elem>, S::Layout> where Self::Elem: Clone; // Provided methods fn as_cow(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where [Self::Elem]: ToOwned { ... } fn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout> { ... } fn axis_chunks( &self, dim: usize, chunk_size: usize, ) -> AxisChunks<'_, Self::Elem, Self::Layout> { ... } fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout> where Self::Layout: RemoveDim { ... } fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout> where Self::Layout: BroadcastLayout<S::Layout> { ... } fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem> { ... } unsafe fn get_unchecked<I: AsIndex<Self::Layout>>( &self, index: I, ) -> &Self::Elem { ... } fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output> where Self::Layout: RemoveDim { ... } fn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, N> { ... } fn inner_iter_dyn( &self, n: usize, ) -> InnerIterDyn<'_, Self::Elem, Self::Layout> { ... } fn item(&self) -> Option<&Self::Elem> { ... } fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> { ... } fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout> where F: Fn(&Self::Elem) -> U { ... } fn map_in<A: Alloc, F, U>( &self, alloc: A, f: F, ) -> TensorBase<Vec<U>, Self::Layout> where F: Fn(&Self::Elem) -> U { ... } fn nd_view<const N: usize>( &self, ) -> TensorBase<ViewData<'_, Self::Elem>, NdLayout<N>> { ... } fn permuted( &self, order: Self::Index<'_>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout> { ... } fn reshaped<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout> { ... } fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout> { ... } fn try_slice_dyn<R: IntoSliceItems>( &self, range: R, ) -> Result<TensorView<'_, Self::Elem>, SliceError> { ... } fn slice<const M: usize, R: IntoSliceItems>( &self, range: R, ) -> NdTensorView<'_, Self::Elem, M> { ... } fn slice_dyn<R: IntoSliceItems>( &self, range: R, ) -> TensorView<'_, Self::Elem> { ... } fn slice_copy<R: Clone + IntoSliceItems>( &self, range: R, ) -> Tensor<Self::Elem> where Self::Elem: Clone { ... } fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems>( &self, pool: A, range: R, ) -> Tensor<Self::Elem> where Self::Elem: Clone { ... } fn squeezed(&self) -> TensorView<'_, Self::Elem> { ... } fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where Self::Elem: Clone { ... } fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where Self::Elem: Clone { ... } fn to_slice(&self) -> Cow<'_, [Self::Elem]> where Self::Elem: Clone { ... } fn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout> where Self::Elem: Clone { ... } fn to_tensor_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<Vec<Self::Elem>, Self::Layout> where Self::Elem: Clone { ... } fn weakly_checked_view( &self, ) -> WeaklyCheckedView<ViewData<'_, Self::Elem>, Self::Layout> { ... }
}
Expand description

Trait implemented by all variants of TensorBase, which provides a view method to get an immutable view of the tensor, plus methods which forward to such a view.

The purpose of this trait is to allow methods to be specialized for immutable views by preserving the lifetime of the underlying data in return types (eg. iter returns &[T] in the trait, but &'a [T] in the view). This allows for chaining operations on views together (eg. tensor.slice(...).transpose()) without needing to separate each step into separate statements.

This trait is conceptually similar to the way std::ops::Deref in the Rust standard library allows a Vec<T> to have all the methods of an &[T].

If stable Rust gains support for specialization or a Deref trait that can return non-references (see https://github.com/rust-lang/rfcs/issues/997) this will become unnecessary.

Required Associated Types§

source

type Elem

Type of element stored in this tensor.

source

type Layout: for<'a> MutLayout<Index<'a> = Self::Index<'a>>

The underlying layout of this tensor. It must have the same index type (eg. [usize; N] or &[usize]) as this view.

Required Methods§

source

fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>

Return a borrowed view of this tensor.

source

fn layout(&self) -> &Self::Layout

Return the layout of this tensor.

source

fn data(&self) -> Option<&[Self::Elem]>

Return the layout of this tensor as a slice, if it is contiguous.

source

fn insert_axis(&mut self, index: usize)
where Self::Layout: ResizeLayout,

Insert a size-1 axis at the given index.

source

fn remove_axis(&mut self, index: usize)
where Self::Layout: ResizeLayout,

Remove a size-1 axis at the given index.

This will panic if the index is out of bounds or the size of the index is not 1.

source

fn iter(&self) -> Iter<'_, Self::Elem>

Return an iterator over elements in this tensor in their logical order.

source

fn merge_axes(&mut self)
where Self::Layout: ResizeLayout,

Merge consecutive dimensions to the extent possible without copying data or changing the iteration order.

If the tensor is contiguous, this has the effect of flattening the tensor into a vector.

source

fn move_axis(&mut self, from: usize, to: usize)

Re-order the axes of this tensor to move the axis at index from to to.

Panics if from or to is >= self.ndim().

source

fn permute(&mut self, order: Self::Index<'_>)

Permute the dimensions of this tensor.

source

fn transpose(&mut self)

Reverse the order of dimensions in this tensor.

source

fn to_vec(&self) -> Vec<Self::Elem>
where Self::Elem: Clone,

Return a vector containing the elements of this tensor in their logical order, ie. as if the tensor were flattened into one dimension.

source

fn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem>
where Self::Elem: Clone,

Variant of to_vec which takes an allocator.

source

fn to_shape<S: IntoLayout>( &self, shape: S, ) -> TensorBase<Vec<Self::Elem>, S::Layout>
where Self::Elem: Clone,

Return a copy of this tensor with a given shape.

Provided Methods§

source

fn as_cow(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where [Self::Elem]: ToOwned,

Return a view of this tensor using a borrowed CowData for storage.

Together with into_cow, this is useful where code needs to conditionally copy or create a new tensor, and get either the borrowed or owned tensor into the same type.

source

fn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout>

Return a view of this tensor with a dynamic rank.

source

fn axis_chunks( &self, dim: usize, chunk_size: usize, ) -> AxisChunks<'_, Self::Elem, Self::Layout>

Return an iterator over slices of this tensor along a given axis.

source

fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout>
where Self::Layout: RemoveDim,

Return an iterator over slices of this tensor along a given axis.

source

fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>
where Self::Layout: BroadcastLayout<S::Layout>,

Broadcast this view to another shape.

If shape is an array ([usize; N]), the result will have a static-rank layout with N dims. If shape is a slice, the result will have a dynamic-rank layout.

source

fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>

Return a reference to the element at a given index, or None if the index is invalid.

source

unsafe fn get_unchecked<I: AsIndex<Self::Layout>>( &self, index: I, ) -> &Self::Elem

Return a reference to the element at a given index, without performing bounds checks.

§Safety

The caller must ensure that the index is valid for the tensor’s shape.

source

fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output>
where Self::Layout: RemoveDim,

Index the tensor along a given axis.

Returns a view with one dimension removed.

Panics if axis >= self.ndim() or index >= self.size(axis).

source

fn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, N>

Return an iterator over the innermost N dimensions.

source

fn inner_iter_dyn(&self, n: usize) -> InnerIterDyn<'_, Self::Elem, Self::Layout>

Return an iterator over the innermost n dimensions.

Prefer inner_iter if N is known at compile time.

source

fn item(&self) -> Option<&Self::Elem>

Return the scalar value in this tensor if it has 0 dimensions.

source

fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem>

Return an iterator over 1D slices of this tensor along a given axis.

source

fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U,

Return a new tensor with the same shape, formed by applying f to each element in this tensor.

source

fn map_in<A: Alloc, F, U>( &self, alloc: A, f: F, ) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U,

Variant of map which takes an allocator.

source

fn nd_view<const N: usize>( &self, ) -> TensorBase<ViewData<'_, Self::Elem>, NdLayout<N>>

Convert this tensor to one with the same shape but a static dimension count.

Panics if self.ndim() != N.

source

fn permuted( &self, order: Self::Index<'_>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>

Return a view with dimensions permuted in the order given by dims.

source

fn reshaped<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>

Return a view with a given shape, without copying any data. This requires that the tensor is contiguous.

The new shape must have the same number of elments as the current shape. The result will have a static rank if shape is an array or a dynamic rank if it is a slice.

Panics if the tensor is not contiguous.

source

fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>

Return a view with the order of dimensions reversed.

source

fn try_slice_dyn<R: IntoSliceItems>( &self, range: R, ) -> Result<TensorView<'_, Self::Elem>, SliceError>

Slice this tensor and return a dynamic-rank view.

Fails if the range has more dimensions than the view or is out of bounds for any dimension.

source

fn slice<const M: usize, R: IntoSliceItems>( &self, range: R, ) -> NdTensorView<'_, Self::Elem, M>

Slice this tensor and return a static-rank view with M dimensions.

Use AsView::slice_dyn instead if the number of dimensions in the returned view is unknown at compile time.

This method is cheap as it does not copy the data, but does not support ranges with negative steps. For that use slice_copy.

Panics if the dimension count of the result is not M.

source

fn slice_dyn<R: IntoSliceItems>(&self, range: R) -> TensorView<'_, Self::Elem>

Slice this tensor and return a dynamic-rank view.

source

fn slice_copy<R: Clone + IntoSliceItems>(&self, range: R) -> Tensor<Self::Elem>
where Self::Elem: Clone,

Return a slice of this tensor as an owned tensor.

This is more expensive than slice as it copies the data, but is more flexible as it supports ranges with negative steps.

source

fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems>( &self, pool: A, range: R, ) -> Tensor<Self::Elem>
where Self::Elem: Clone,

Variant of slice_copy which takes an allocator.

source

fn squeezed(&self) -> TensorView<'_, Self::Elem>

Return a view of this tensor with all dimensions of size 1 removed.

source

fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone,

Return a tensor with the same shape as this tensor/view but with the data contiguous in memory and arranged in the same order as the logical/iteration order (used by iter).

This will return a view if the data is already contiguous or copy data into a new buffer otherwise.

Certain operations require or are faster with contiguous tensors.

source

fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone,

Variant of to_contiguous which takes an allocator.

source

fn to_slice(&self) -> Cow<'_, [Self::Elem]>
where Self::Elem: Clone,

Return a slice containing the elements of this tensor in their logical order, ie. as if the tensor were flattened into one dimension.

Unlike data this will copy the elements if they are not contiguous. Unlike to_vec this will not copy the elements if the tensor is already contiguous.

source

fn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout>
where Self::Elem: Clone,

Return a copy of this tensor/view which uniquely owns its elements.

source

fn to_tensor_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<Vec<Self::Elem>, Self::Layout>
where Self::Elem: Clone,

Variant of to_tensor which takes an allocator.

source

fn weakly_checked_view( &self, ) -> WeaklyCheckedView<ViewData<'_, Self::Elem>, Self::Layout>

Return a view which performs “weak” checking when indexing via view[<index>]. See WeaklyCheckedView for an explanation.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T, S: Storage<Elem = T>, L: MutLayout + Clone> AsView for TensorBase<S, L>

§

type Elem = T

§

type Layout = L