pub trait AsView: Layout {
type Elem;
type Layout: Clone + for<'a> Layout<Index<'a> = Self::Index<'a>>;
Show 46 methods
// Required methods
fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>;
fn layout(&self) -> &Self::Layout;
fn copy_into_slice<'a>(
&self,
dest: &'a mut [MaybeUninit<Self::Elem>],
) -> &'a [Self::Elem]
where Self::Elem: Copy;
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)
where Self::Layout: MutLayout;
fn permute(&mut self, order: Self::Index<'_>)
where Self::Layout: MutLayout;
fn transpose(&mut self)
where Self::Layout: MutLayout;
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,
Self::Layout: MutLayout;
// 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> ⓘ
where Self::Layout: MutLayout { ... }
fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout> ⓘ
where Self::Layout: MutLayout + RemoveDim { ... }
fn broadcast<S: IntoLayout>(
&self,
shape: S,
) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>
where Self::Layout: BroadcastLayout<S::Layout> { ... }
fn try_broadcast<S: IntoLayout>(
&self,
shape: S,
) -> Result<TensorBase<ViewData<'_, Self::Elem>, S::Layout>, ExpandError>
where Self::Layout: BroadcastLayout<S::Layout> { ... }
fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>
where Self::Layout: TrustedLayout { ... }
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: MutLayout + RemoveDim { ... }
fn inner_iter<const N: usize>(
&self,
) -> InnerIter<'_, Self::Elem, NdLayout<N>> ⓘ { ... }
fn inner_iter_dyn(&self, n: usize) -> InnerIter<'_, Self::Elem, DynLayout> ⓘ { ... }
fn item(&self) -> Option<&Self::Elem> { ... }
fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> ⓘ
where Self::Layout: RemoveDim { ... }
fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U,
Self::Layout: MutLayout { ... }
fn map_in<A: Alloc, F, U>(
&self,
alloc: A,
f: F,
) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U,
Self::Layout: MutLayout { ... }
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>
where Self::Layout: MutLayout { ... }
fn reshaped<S: Copy + IntoLayout>(
&self,
shape: S,
) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone,
Self::Layout: MutLayout { ... }
fn reshaped_in<A: Alloc, S: Copy + IntoLayout>(
&self,
alloc: A,
shape: S,
) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone,
Self::Layout: MutLayout { ... }
fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
where Self::Layout: MutLayout { ... }
fn slice<R: IntoSliceItems + IndexCount>(
&self,
range: R,
) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Layout: SliceWith<R, R::Count> { ... }
fn slice_axis(
&self,
axis: usize,
range: Range<usize>,
) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
where Self::Layout: MutLayout { ... }
fn try_slice<R: IntoSliceItems + IndexCount>(
&self,
range: R,
) -> Result<TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>, SliceError>
where Self::Layout: SliceWith<R, R::Count> { ... }
fn slice_copy<R: Clone + IntoSliceItems + IndexCount>(
&self,
range: R,
) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Elem: Clone,
Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>> { ... }
fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems + IndexCount>(
&self,
pool: A,
range: R,
) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Elem: Clone,
Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>> { ... }
fn squeezed(&self) -> TensorView<'_, Self::Elem>
where Self::Layout: MutLayout { ... }
fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone,
Self::Layout: MutLayout { ... }
fn to_contiguous_in<A: Alloc>(
&self,
alloc: A,
) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone,
Self::Layout: MutLayout { ... }
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,
Self::Layout: MutLayout { ... }
fn to_tensor_in<A: Alloc>(
&self,
alloc: A,
) -> TensorBase<Vec<Self::Elem>, Self::Layout>
where Self::Elem: Clone,
Self::Layout: MutLayout { ... }
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 copy_into_slice<'a>(
&self,
dest: &'a mut [MaybeUninit<Self::Elem>],
) -> &'a [Self::Elem]
fn copy_into_slice<'a>( &self, dest: &'a mut [MaybeUninit<Self::Elem>], ) -> &'a [Self::Elem]
Copy elements from this tensor into dest
in logical order.
Returns the initialized slice. Panics if the length of dest
does
not match the number of elements in self
.
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.
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> ⓘ
fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout> ⓘ
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 try_broadcast<S: IntoLayout>(
&self,
shape: S,
) -> Result<TensorBase<ViewData<'_, Self::Elem>, S::Layout>, ExpandError>
fn try_broadcast<S: IntoLayout>( &self, shape: S, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, S::Layout>, ExpandError>
Fallible variant of broadcast
.
Sourcefn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>where
Self::Layout: TrustedLayout,
fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>where
Self::Layout: TrustedLayout,
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>
fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output>
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, NdLayout<N>> ⓘ
fn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, NdLayout<N>> ⓘ
Return an iterator over the innermost N dimensions.
Sourcefn inner_iter_dyn(&self, n: usize) -> InnerIter<'_, Self::Elem, DynLayout> ⓘ
fn inner_iter_dyn(&self, n: usize) -> InnerIter<'_, Self::Elem, DynLayout> ⓘ
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> ⓘwhere
Self::Layout: RemoveDim,
fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> ⓘwhere
Self::Layout: RemoveDim,
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: Copy + IntoLayout>(
&self,
shape: S,
) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
fn reshaped<S: Copy + IntoLayout>( &self, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
Return either a view or a copy of self
with the given shape.
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.
If self
is contiguous this will return a view, as changing the shape
can be done without moving data. Otherwise it will copy elements into
a new tensor.
§Panics
Panics if the number of elements in the new shape does not match the current shape.
Sourcefn reshaped_in<A: Alloc, S: Copy + IntoLayout>(
&self,
alloc: A,
shape: S,
) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
fn reshaped_in<A: Alloc, S: Copy + IntoLayout>( &self, alloc: A, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
A variant of reshaped
that allows specifying the
allocator to use if a copy is needed.
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 slice<R: IntoSliceItems + IndexCount>(
&self,
range: R,
) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
fn slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
Slice this tensor and return a view.
If both this tensor’s layout and the range have a statically-known number of index terms, the result will have a static rank. Otherwise it will have a dynamic rank.
use rten_tensor::prelude::*;
use rten_tensor::NdTensor;
let x = NdTensor::from([[1, 2], [3, 4]]);
let col = x.slice((.., 1)); // `col` is an `NdTensorView<i32, 1>`
assert_eq!(col.shape(), [2usize]);
assert_eq!(col.to_vec(), [2, 4]);
Sourcefn slice_axis(
&self,
axis: usize,
range: Range<usize>,
) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
Slice this tensor along a given axis.
Sourcefn try_slice<R: IntoSliceItems + IndexCount>(
&self,
range: R,
) -> Result<TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>, SliceError>
fn try_slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>, SliceError>
A variant of slice
that returns a result
instead of panicking.
Sourcefn slice_copy<R: Clone + IntoSliceItems + IndexCount>(
&self,
range: R,
) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
fn slice_copy<R: Clone + IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
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 + IndexCount>(
&self,
pool: A,
range: R,
) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems + IndexCount>( &self, pool: A, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.