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§
Required Methods§
sourcefn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
Return a borrowed view of this tensor.
sourcefn data(&self) -> Option<&[Self::Elem]>
fn data(&self) -> Option<&[Self::Elem]>
Return the layout of this tensor as a slice, if it is contiguous.
sourcefn insert_axis(&mut self, index: usize)where
Self::Layout: ResizeLayout,
fn insert_axis(&mut self, index: usize)where
Self::Layout: ResizeLayout,
Insert a size-1 axis at the given index.
sourcefn remove_axis(&mut self, index: usize)where
Self::Layout: ResizeLayout,
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.
sourcefn iter(&self) -> Iter<'_, Self::Elem> ⓘ
fn iter(&self) -> Iter<'_, Self::Elem> ⓘ
Return an iterator over elements in this tensor in their logical order.
sourcefn merge_axes(&mut self)where
Self::Layout: ResizeLayout,
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.
sourcefn move_axis(&mut self, from: usize, to: usize)
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().
sourcefn to_vec(&self) -> Vec<Self::Elem>
fn to_vec(&self) -> Vec<Self::Elem>
Return a vector containing the elements of this tensor in their logical order, ie. as if the tensor were flattened into one dimension.
sourcefn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem>
fn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem>
Variant of to_vec which takes an allocator.
sourcefn to_shape<S: IntoLayout>(
&self,
shape: S,
) -> TensorBase<Vec<Self::Elem>, S::Layout>
fn to_shape<S: IntoLayout>( &self, shape: S, ) -> TensorBase<Vec<Self::Elem>, S::Layout>
Return a copy of this tensor with a given shape.
Provided Methods§
sourcefn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout>
fn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout>
Return a view of this tensor with a dynamic rank.
sourcefn axis_chunks(
&self,
dim: usize,
chunk_size: usize,
) -> AxisChunks<'_, Self::Elem, Self::Layout> ⓘ
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.
sourcefn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout> ⓘwhere
Self::Layout: RemoveDim,
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.
sourcefn broadcast<S: IntoLayout>(
&self,
shape: S,
) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>
fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, 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.
sourcefn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>
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.
sourceunsafe fn get_unchecked<I: AsIndex<Self::Layout>>(
&self,
index: I,
) -> &Self::Elem
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.
sourcefn index_axis(
&self,
axis: usize,
index: usize,
) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output>where
Self::Layout: RemoveDim,
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).
sourcefn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, N> ⓘ
fn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, N> ⓘ
Return an iterator over the innermost N dimensions.
sourcefn inner_iter_dyn(&self, n: usize) -> InnerIterDyn<'_, Self::Elem, Self::Layout>
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.
sourcefn item(&self) -> Option<&Self::Elem>
fn item(&self) -> Option<&Self::Elem>
Return the scalar value in this tensor if it has 0 dimensions.
sourcefn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> ⓘ
fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> ⓘ
Return an iterator over 1D slices of this tensor along a given axis.
sourcefn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout>
fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout>
Return a new tensor with the same shape, formed by applying f to each
element in this tensor.
sourcefn map_in<A: Alloc, F, U>(
&self,
alloc: A,
f: F,
) -> TensorBase<Vec<U>, Self::Layout>
fn map_in<A: Alloc, F, U>( &self, alloc: A, f: F, ) -> TensorBase<Vec<U>, Self::Layout>
Variant of map which takes an allocator.
sourcefn nd_view<const N: usize>(
&self,
) -> TensorBase<ViewData<'_, Self::Elem>, NdLayout<N>>
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.
sourcefn permuted(
&self,
order: Self::Index<'_>,
) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
fn permuted( &self, order: Self::Index<'_>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
Return a view with dimensions permuted in the order given by dims.
sourcefn reshaped<S: IntoLayout>(
&self,
shape: S,
) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>
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.
sourcefn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
Return a view with the order of dimensions reversed.
sourcefn try_slice_dyn<R: IntoSliceItems>(
&self,
range: R,
) -> Result<TensorView<'_, Self::Elem>, SliceError>
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.
sourcefn slice<const M: usize, R: IntoSliceItems>(
&self,
range: R,
) -> NdTensorView<'_, Self::Elem, M>
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.
sourcefn slice_dyn<R: IntoSliceItems>(&self, range: R) -> TensorView<'_, Self::Elem>
fn slice_dyn<R: IntoSliceItems>(&self, range: R) -> TensorView<'_, Self::Elem>
Slice this tensor and return a dynamic-rank view.
sourcefn slice_copy<R: Clone + IntoSliceItems>(&self, range: R) -> Tensor<Self::Elem>
fn slice_copy<R: Clone + IntoSliceItems>(&self, range: R) -> Tensor<Self::Elem>
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.
sourcefn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems>(
&self,
pool: A,
range: R,
) -> Tensor<Self::Elem>
fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems>( &self, pool: A, range: R, ) -> Tensor<Self::Elem>
Variant of slice_copy which takes an allocator.
sourcefn squeezed(&self) -> TensorView<'_, Self::Elem>
fn squeezed(&self) -> TensorView<'_, Self::Elem>
Return a view of this tensor with all dimensions of size 1 removed.
sourcefn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
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.
sourcefn to_contiguous_in<A: Alloc>(
&self,
alloc: A,
) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
Variant of to_contiguous which takes an
allocator.
sourcefn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout>
fn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout>
Return a copy of this tensor/view which uniquely owns its elements.
sourcefn to_tensor_in<A: Alloc>(
&self,
alloc: A,
) -> TensorBase<Vec<Self::Elem>, Self::Layout>
fn to_tensor_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<Vec<Self::Elem>, Self::Layout>
Variant of to_tensor which takes an allocator.
sourcefn weakly_checked_view(
&self,
) -> WeaklyCheckedView<ViewData<'_, Self::Elem>, Self::Layout>
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.