Struct TensorBase

Source
pub struct TensorBase<S: Storage, L: MutLayout> { /* private fields */ }
Expand description

The base type for multi-dimensional arrays. This consists of storage for elements, plus a layout which maps from a multi-dimensional array index to a storage offset. This base type is not normally used directly but instead through a type alias which selects the storage type and layout.

The storage can be owned (like a Vec<T>), borrowed (like &[T]) or mutably borrowed (like &mut [T]). The layout can have a dimension count that is determined statically (ie. forms part of the tensor’s type), see NdLayout or is only known at runtime, see DynLayout.

Implementations§

Source§

impl<S: Storage, L: MutLayout> TensorBase<S, L>

Source

pub fn from_data<D: IntoStorage<Output = S>>( shape: L::Index<'_>, data: D, ) -> TensorBase<S, L>
where for<'a> L::Index<'a>: Clone,

Construct a new tensor from a given shape and storage.

Panics if the data length does not match the product of shape.

Source

pub fn try_from_data<D: IntoStorage<Output = S>>( shape: L::Index<'_>, data: D, ) -> Result<TensorBase<S, L>, FromDataError>

Construct a new tensor from a given shape and storage.

This will fail if the data length does not match the product of shape.

Source

pub fn from_storage_and_layout(data: S, layout: L) -> TensorBase<S, L>

Create a tensor from a pre-created storage and layout.

Panics if the storage length is too short for the layout, or the storage is mutable and the layout may map multiple indices to the same offset.

Source

pub fn from_data_with_strides<D: IntoStorage<Output = S>>( shape: L::Index<'_>, data: D, strides: L::Index<'_>, ) -> Result<TensorBase<S, L>, FromDataError>

Construct a new tensor from a given shape and storage, and custom strides.

This will fail if the data length is incorrect for the shape and stride combination, or if the strides lead to overlap (see OverlapPolicy). See also TensorBase::from_slice_with_strides which is a similar method for immutable views that does allow overlapping strides.

Source

pub fn into_dyn(self) -> TensorBase<S, DynLayout>
where L: Into<DynLayout>,

Convert the current tensor into a dynamic rank tensor without copying any data.

Source

pub fn data_ptr(&self) -> *const S::Elem

Return a raw pointer to the tensor’s underlying data.

Source§

impl<S: StorageMut, L: MutLayout> TensorBase<S, L>

Source

pub fn axis_iter_mut(&mut self, dim: usize) -> AxisIterMut<'_, S::Elem, L>
where L: RemoveDim,

Return an iterator over mutable slices of this tensor along a given axis. Each view yielded has one dimension fewer than the current layout.

Source

pub fn axis_chunks_mut( &mut self, dim: usize, chunk_size: usize, ) -> AxisChunksMut<'_, S::Elem, L>

Return an iterator over mutable slices of this tensor along a given axis. Each view yielded has the same rank as this tensor, but the dimension dim will only have chunk_size entries.

Source

pub fn apply<F: Fn(&S::Elem) -> S::Elem>(&mut self, f: F)

Replace each element in this tensor with the result of applying f to the element.

Source

pub fn as_dyn_mut(&mut self) -> TensorBase<ViewMutData<'_, S::Elem>, DynLayout>

Return a mutable view of this tensor with a dynamic dimension count.

Source

pub fn copy_from<S2: Storage<Elem = S::Elem>>( &mut self, other: &TensorBase<S2, L>, )
where S::Elem: Clone, L: Clone,

Copy elements from another tensor into this tensor.

This tensor and other must have the same shape.

Source

pub fn data_mut(&mut self) -> Option<&mut [S::Elem]>

Return the data in this tensor as a slice if it is contiguous.

Source

pub fn index_axis_mut( &mut self, axis: usize, index: usize, ) -> TensorBase<ViewMutData<'_, S::Elem>, <L as RemoveDim>::Output>
where L: RemoveDim,

Index the tensor along a given axis.

Returns a mutable view with one dimension removed.

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

Source

pub fn storage_mut(&mut self) -> ViewMutData<'_, S::Elem>

Return a mutable view of the tensor’s underlying storage.

Source

pub fn fill(&mut self, value: S::Elem)
where S::Elem: Clone,

Replace all elements of this tensor with value.

Source

pub fn get_mut<I: AsIndex<L>>(&mut self, index: I) -> Option<&mut S::Elem>

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

Source

pub unsafe fn get_unchecked_mut<I: AsIndex<L>>( &mut self, index: I, ) -> &mut S::Elem

Return the element at a given index, without performing any bounds- checking.

§Safety

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

Source

pub fn inner_iter_mut<const N: usize>( &mut self, ) -> InnerIterMut<'_, S::Elem, NdLayout<N>>

Return a mutable iterator over the N innermost dimensions of this tensor.

Source

pub fn inner_iter_dyn_mut( &mut self, n: usize, ) -> InnerIterMut<'_, S::Elem, DynLayout>

Return a mutable iterator over the n innermost dimensions of this tensor.

Prefer inner_iter_mut if N is known at compile time.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, S::Elem>

Return a mutable iterator over the elements of this tensor, in their logical order.

Source

pub fn lanes_mut(&mut self, dim: usize) -> LanesMut<'_, S::Elem>

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

Source

pub fn nd_view_mut<const N: usize>( &mut self, ) -> TensorBase<ViewMutData<'_, S::Elem>, NdLayout<N>>

Return a view of this tensor with a static dimension count.

Panics if self.ndim() != N.

Source

pub fn permuted_mut( &mut self, order: L::Index<'_>, ) -> TensorBase<ViewMutData<'_, S::Elem>, L>

Permute the order of dimensions according to the given order.

See AsView::permuted.

Source

pub fn reshaped_mut<SH: IntoLayout>( &mut self, shape: SH, ) -> Result<TensorBase<ViewMutData<'_, S::Elem>, SH::Layout>, ReshapeError>

Change the layout of the tensor without moving any data.

This will return an error if the view is not contiguous.

See also AsView::reshaped.

Source

pub fn slice_axis_mut( &mut self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewMutData<'_, S::Elem>, L>

Slice this tensor along a given axis.

Source

pub fn slice_mut<R: IntoSliceItems + IndexCount>( &mut self, range: R, ) -> TensorBase<ViewMutData<'_, S::Elem>, <L as SliceWith<R, R::Count>>::Layout>
where L: SliceWith<R, R::Count>,

Slice this tensor and return a mutable view.

See slice for notes on the layout of the returned view.

Source

pub fn try_slice_mut<R: IntoSliceItems + IndexCount>( &mut self, range: R, ) -> Result<TensorBase<ViewMutData<'_, S::Elem>, <L as SliceWith<R, R::Count>>::Layout>, SliceError>
where L: SliceWith<R, R::Count>,

A variant of slice_mut that returns a result instead of panicking.

Source

pub fn view_mut(&mut self) -> TensorBase<ViewMutData<'_, S::Elem>, L>
where L: Clone,

Return a mutable view of this tensor.

Source

pub fn weakly_checked_view_mut( &mut self, ) -> WeaklyCheckedView<ViewMutData<'_, S::Elem>, L>

Return a mutable view that performs only “weak” checking when indexing, this is faster but can hide bugs. See WeaklyCheckedView.

Source§

impl<T, L: Clone + MutLayout> TensorBase<Vec<T>, L>

Source

pub fn arange(start: T, end: T, step: Option<T>) -> TensorBase<Vec<T>, L>
where T: Copy + PartialOrd + From<bool> + Add<Output = T>, [usize; 1]: AsIndex<L>,

Create a new 1D tensor filled with an arithmetic sequence of values in the range [start, end) separated by step. If step is omitted, it defaults to 1.

Source

pub fn append<S2: Storage<Elem = T>>( &mut self, axis: usize, other: &TensorBase<S2, L>, ) -> Result<(), ExpandError>
where T: Copy,

Append elements from other to this tensor along a given axis.

This will fail if the shapes of self and other do not match along dimensions other than axis, or if the current tensor has insufficient capacity to expand without re-allocating.

Source

pub fn from_vec(vec: Vec<T>) -> TensorBase<Vec<T>, L>
where [usize; 1]: AsIndex<L>,

Create a new 1D tensor from a Vec<T>.

Source

pub fn clip_dim(&mut self, dim: usize, range: Range<usize>)
where T: Copy,

Clip dimension dim to [range.start, range.end). The new size for the dimension must be <= the old size.

This currently requires T: Copy to support efficiently moving data from the new start offset to the beginning of the element buffer.

Source

pub fn has_capacity(&self, axis: usize, new_size: usize) -> bool

Return true if this tensor can be expanded along a given axis to a new size without re-allocating.

Source

pub fn into_cow(self) -> TensorBase<CowData<'static, T>, L>

Convert the storage of this tensor into an owned CowData.

This is useful in contexts where code needs to conditionally copy or create a new tensor. See AsView::as_cow.

Source

pub fn into_data(self) -> Vec<T>
where T: Clone,

Consume self and return the underlying data as a contiguous tensor.

See also TensorBase::to_vec.

Source

pub fn into_non_contiguous_data(self) -> Vec<T>

Consume self and return the underlying data in whatever order the elements are currently stored.

Source

pub fn into_shape<S: IntoLayout>( self, shape: S, ) -> TensorBase<Vec<T>, S::Layout>
where T: Clone,

Consume self and return a new contiguous tensor with the given shape.

This avoids copying the data if it is already contiguous.

Source

pub fn from_fn<F: FnMut(L::Index<'_>) -> T, Idx>( shape: L::Index<'_>, f: F, ) -> TensorBase<Vec<T>, L>
where L::Indices: Iterator<Item = Idx>, Idx: AsIndex<L>,

Create a new tensor with a given shape and values generated by calling f repeatedly.

Each call to f will receive an element index and should return the corresponding value. If the function does not need this index, use from_simple_fn instead, as it is faster.

Source

pub fn from_simple_fn<F: FnMut() -> T>( shape: L::Index<'_>, f: F, ) -> TensorBase<Vec<T>, L>

Create a new tensor with a given shape and values generated by calling f repeatedly.

Source

pub fn from_simple_fn_in<A: Alloc, F: FnMut() -> T>( alloc: A, shape: L::Index<'_>, f: F, ) -> TensorBase<Vec<T>, L>

Variant of from_simple_fn that takes an allocator.

Source

pub fn from_scalar(value: T) -> TensorBase<Vec<T>, L>
where [usize; 0]: AsIndex<L>,

Create a new 0D tensor from a scalar value.

Source

pub fn full(shape: L::Index<'_>, value: T) -> TensorBase<Vec<T>, L>
where T: Clone,

Create a new tensor with a given shape and all elements set to value.

Source

pub fn full_in<A: Alloc>( alloc: A, shape: L::Index<'_>, value: T, ) -> TensorBase<Vec<T>, L>
where T: Clone,

Variant of full which takes an allocator.

Source

pub fn make_contiguous(&mut self)
where T: Clone,

Make the underlying data in this tensor contiguous.

This means that after calling make_contiguous, the elements are guaranteed to be stored in the same order as the logical order in which iter yields elements. This method is cheap if the storage is already contiguous.

Source

pub fn rand<R: RandomSource<T>>( shape: L::Index<'_>, rand_src: &mut R, ) -> TensorBase<Vec<T>, L>

Create a new tensor with a given shape and elements populated using numbers generated by rand_src.

A more general version of this method that generates values using any function is from_simple_fn.

Source

pub fn zeros(shape: L::Index<'_>) -> TensorBase<Vec<T>, L>
where T: Clone + Default,

Create a new tensor with a given shape, with all elements set to their default value (ie. zero for numeric types).

Source

pub fn zeros_in<A: Alloc>( alloc: A, shape: L::Index<'_>, ) -> TensorBase<Vec<T>, L>
where T: Clone + Default,

Variant of zeros which takes an allocator.

Source

pub fn uninit(shape: L::Index<'_>) -> TensorBase<Vec<MaybeUninit<T>>, L>
where MaybeUninit<T>: Clone,

Return a new tensor containing uninitialized elements.

The caller must initialize elements and then call assume_init to convert to an initialized Tensor<T>.

Source

pub fn uninit_in<A: Alloc>( alloc: A, shape: L::Index<'_>, ) -> TensorBase<Vec<MaybeUninit<T>>, L>

Variant of uninit which takes an allocator.

Source

pub fn with_capacity( shape: L::Index<'_>, expand_dim: usize, ) -> TensorBase<Vec<T>, L>
where T: Copy,

Create a tensor which initially has zero elements, but can be expanded along a given dimension without reallocating.

shape specifies the maximum shape that the tensor can be expanded to without reallocating. The initial shape will be the same, except for the dimension specified by expand_dim, which will be zero.

Source

pub fn with_capacity_in<A: Alloc>( alloc: A, shape: L::Index<'_>, expand_dim: usize, ) -> TensorBase<Vec<T>, L>
where T: Copy,

Variant of with_capacity which takes an allocator.

Source§

impl<T, L: MutLayout> TensorBase<CowData<'_, T>, L>

Source

pub fn into_non_contiguous_data(self) -> Option<Vec<T>>

Consume self and return the underlying data in whatever order the elements are currently stored, if the storage is owned, or None if it is borrowed.

Source§

impl<T, S: Storage<Elem = MaybeUninit<T>> + AssumeInit, L: Clone + MutLayout> TensorBase<S, L>
where <S as AssumeInit>::Output: Storage<Elem = T>,

Source

pub unsafe fn assume_init(self) -> TensorBase<<S as AssumeInit>::Output, L>

Convert a tensor of potentially uninitialized elements to one of initialized elements.

See also MaybeUninit::assume_init.

§Safety

The caller must guarantee that all elements in this tensor have been initialized before calling assume_init.

Source

pub fn init_from<S2: Storage<Elem = T>>( self, other: &TensorBase<S2, L>, ) -> TensorBase<<S as AssumeInit>::Output, L>
where T: Copy, S: StorageMut<Elem = MaybeUninit<T>>,

Initialize this tensor with data from another view.

This tensor and other must have the same shape.

Source§

impl<'a, T, L: Clone + MutLayout> TensorBase<ViewData<'a, T>, L>

Source

pub fn axis_iter(&self, dim: usize) -> AxisIter<'a, T, L>
where L: RemoveDim,

Source

pub fn axis_chunks(&self, dim: usize, chunk_size: usize) -> AxisChunks<'a, T, L>

Source

pub fn as_dyn(&self) -> TensorBase<ViewData<'a, T>, DynLayout>

Return a view of this tensor with a dynamic dimension count.

See AsView::as_dyn.

Source

pub fn as_cow(&self) -> TensorBase<CowData<'a, T>, L>

Convert the storage of this view to a borrowed CowData.

See AsView::as_cow.

Source

pub fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'a, T>, S::Layout>
where L: BroadcastLayout<S::Layout>,

Broadcast this view to another shape.

See AsView::broadcast.

Source

pub fn try_broadcast<S: IntoLayout>( &self, shape: S, ) -> Result<TensorBase<ViewData<'a, T>, S::Layout>, ExpandError>
where L: BroadcastLayout<S::Layout>,

Broadcast this view to another shape.

See AsView::broadcast.

Source

pub fn data(&self) -> Option<&'a [T]>

Return the data in this tensor as a slice if it is contiguous, ie. the order of elements in the slice is the same as the logical order yielded by iter, and there are no gaps.

Source

pub fn storage(&self) -> ViewData<'a, T>

Return an immutable view of the tensor’s underlying storage.

Source

pub fn get<I: AsIndex<L>>(&self, index: I) -> Option<&'a T>

Source

pub fn from_slice_with_strides( shape: L::Index<'_>, data: &'a [T], strides: L::Index<'_>, ) -> Result<TensorBase<ViewData<'a, T>, L>, FromDataError>

Create a new view with a given shape and data slice, and custom strides.

If you do not need to specify custom strides, use TensorBase::from_data instead. This method is similar to TensorBase::from_data_with_strides, but allows strides that lead to internal overlap (see OverlapPolicy).

Source

pub unsafe fn get_unchecked<I: AsIndex<L>>(&self, index: I) -> &'a T

Return the element at a given index, without performing any bounds- checking.

§Safety

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

Source

pub fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'a, T>, <L as RemoveDim>::Output>
where L: 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

pub fn inner_iter<const N: usize>(&self) -> InnerIter<'a, T, NdLayout<N>>

Return an iterator over the inner N dimensions of this tensor.

See AsView::inner_iter.

Source

pub fn inner_iter_dyn(&self, n: usize) -> InnerIter<'a, T, DynLayout>

Return an iterator over the inner n dimensions of this tensor.

See AsView::inner_iter_dyn.

Source

pub fn item(&self) -> Option<&'a T>

Return the scalar value in this tensor if it has one element.

Source

pub fn iter(&self) -> Iter<'a, T>

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

See AsView::iter.

Source

pub fn lanes(&self, dim: usize) -> Lanes<'a, T>

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

See AsView::lanes.

Source

pub fn nd_view<const N: usize>( &self, ) -> TensorBase<ViewData<'a, T>, NdLayout<N>>

Return a view of this tensor with a static dimension count.

Panics if self.ndim() != N.

Source

pub fn permuted(&self, order: L::Index<'_>) -> TensorBase<ViewData<'a, T>, L>

Permute the axes of this tensor according to order.

See AsView::permuted.

Source

pub fn reshaped<S: Copy + IntoLayout>( &self, shape: S, ) -> TensorBase<CowData<'a, T>, S::Layout>
where T: Clone,

Return a view or owned tensor that has the given shape.

See AsView::reshaped.

Source

pub fn reshaped_in<A: Alloc, S: Copy + IntoLayout>( &self, alloc: A, shape: S, ) -> TensorBase<CowData<'a, T>, S::Layout>
where T: Clone,

Variant of reshaped that takes an allocator.

Source

pub fn slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<ViewData<'a, T>, <L as SliceWith<R, R::Count>>::Layout>
where L: SliceWith<R, R::Count>,

Slice this tensor and return a view. See AsView::slice.

Source

pub fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewData<'a, T>, L>

Slice this tensor along a given axis.

Source

pub fn try_slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> Result<TensorBase<ViewData<'a, T>, <L as SliceWith<R, R::Count>>::Layout>, SliceError>
where L: SliceWith<R, R::Count>,

A variant of slice that returns a result instead of panicking.

Source

pub fn squeezed(&self) -> TensorView<'a, T>

Remove all size-one dimensions from this tensor.

See AsView::squeezed.

Source

pub fn split_at( &self, axis: usize, mid: usize, ) -> (TensorBase<ViewData<'a, T>, L>, TensorBase<ViewData<'a, T>, L>)

Divide this tensor into two views along a given axis.

Returns a (left, right) tuple of views, where the left view contains the slice from [0, mid) along axis and the right view contains the slice from [mid, end) along axis.

Source

pub fn to_contiguous(&self) -> TensorBase<CowData<'a, T>, L>
where T: Clone,

Return a view of this tensor with elements stored in contiguous order.

If the data is already contiguous, no copy is made, otherwise the elements are copied into a new buffer in contiguous order.

Source

pub fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'a, T>, L>
where T: Clone,

Variant of to_contiguous which takes an allocator.

Source

pub fn to_slice(&self) -> Cow<'a, [T]>
where T: Clone,

Return the underlying data as a flat slice if the tensor is contiguous, or a copy of the data as a flat slice otherwise.

See AsView::to_slice.

Source

pub fn transposed(&self) -> TensorBase<ViewData<'a, T>, L>

Reverse the order of dimensions in this tensor. See AsView::transposed.

Source

pub fn try_slice_dyn<R: IntoSliceItems>( &self, range: R, ) -> Result<TensorView<'a, T>, SliceError>

Source

pub fn view(&self) -> TensorBase<ViewData<'a, T>, L>

Return a read-only view of this tensor. See AsView::view.

Source

pub fn weakly_checked_view(&self) -> WeaklyCheckedView<ViewData<'a, T>, L>

Source§

impl<T, S: Storage<Elem = T>, const N: usize> TensorBase<S, NdLayout<N>>

Source

pub fn get_array<const M: usize>(&self, base: [usize; N], dim: usize) -> [T; M]
where T: Copy + Default,

Load an array of M elements from successive entries of a tensor along the dim axis.

eg. If base is [0, 1, 2], dim=0 and M = 4 this will return an array with values from indices [0, 1, 2], [1, 1, 2][3, 1, 2].

Panics if any of the array indices are out of bounds.

Source§

impl<T> TensorBase<Vec<T>, DynLayout>

Source

pub fn reshape(&mut self, shape: &[usize])
where T: Clone,

Reshape this tensor in place. This is cheap if the tensor is contiguous, as only the layout will be changed, but requires copying data otherwise.

Source

pub fn reshape_in<A: Alloc>(&mut self, alloc: A, shape: &[usize])
where T: Clone,

Variant of reshape which takes an allocator.

Source§

impl<'a, T, L: MutLayout> TensorBase<ViewMutData<'a, T>, L>

Source

pub fn split_at_mut( self, axis: usize, mid: usize, ) -> (TensorBase<ViewMutData<'a, T>, L>, TensorBase<ViewMutData<'a, T>, L>)

Divide this tensor into two mutable views along a given axis.

Returns a (left, right) tuple of views, where the left view contains the slice from [0, mid) along axis and the right view contains the slice from [mid, end) along axis.

Source

pub fn into_slice_mut(self) -> Option<&'a mut [T]>

Consume this view and return a mutable slice, if the tensor is contiguous.

Source§

impl<T, S: StorageMut<Elem = T>, const N: usize> TensorBase<S, NdLayout<N>>

Source

pub fn set_array<const M: usize>( &mut self, base: [usize; N], dim: usize, values: [T; M], )
where T: Copy,

Store an array of M elements into successive entries of a tensor along the dim axis.

See TensorBase::get_array for more details.

Source§

impl<T, S: Storage<Elem = T>> TensorBase<S, NdLayout<1>>

Source

pub fn to_array<const M: usize>(&self) -> [T; M]
where T: Copy + Default,

Convert this vector to a static array of length M.

Panics if the length of this vector is not M.

Source§

impl<T, S: StorageMut<Elem = T>> TensorBase<S, NdLayout<1>>

Source

pub fn assign_array<const M: usize>(&mut self, values: [T; M])
where T: Copy + Default,

Fill this vector with values from a static array of length M.

Panics if the length of this vector is not M.

Trait Implementations§

Source§

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

Source§

type Elem = T

Type of element stored in this tensor.
Source§

type Layout = L

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

fn iter(&self) -> Iter<'_, T>

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

fn copy_into_slice<'a>(&self, dest: &'a mut [MaybeUninit<T>]) -> &'a [T]
where T: Copy,

Copy elements from this tensor into dest in logical order. Read more
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 L: ResizeLayout,

Insert a size-1 axis at the given index.
Source§

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

Remove a size-1 axis at the given index. Read more
Source§

fn merge_axes(&mut self)
where L: ResizeLayout,

Merge consecutive dimensions to the extent possible without copying data or changing the iteration order. Read more
Source§

fn layout(&self) -> &L

Return the layout of this tensor.
Source§

fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, L>
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>, L>
where F: Fn(&Self::Elem) -> U,

Variant of map which takes an allocator.
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. Read more
Source§

fn view(&self) -> TensorBase<ViewData<'_, T>, L>

Return a borrowed view of this tensor.
Source§

fn get<I: AsIndex<L>>(&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<L>>(&self, index: I) -> &T

Return a reference to the element at a given index, without performing bounds checks. Read more
Source§

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

Permute the dimensions of this tensor.
Source§

fn to_vec(&self) -> Vec<T>
where T: 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<T>
where T: Clone,

Variant of to_vec which takes an allocator.
Source§

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

Return a copy of this tensor with a given shape.
Source§

fn transpose(&mut self)

Reverse the order of dimensions in this tensor.
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. Read more
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. Read more
Source§

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

Fallible variant of broadcast.
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. Read more
Source§

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

Return an iterator over the innermost N dimensions.
Source§

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

Return an iterator over the innermost n dimensions. Read more
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 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. Read more
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: Copy + IntoLayout>( &self, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone,

Return either a view or a copy of self with the given shape. Read more
Source§

fn reshaped_in<A: Alloc, S: Copy + IntoLayout>( &self, alloc: A, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone,

A variant of reshaped that allows specifying the allocator to use if a copy is needed.
Source§

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

Return a view with the order of dimensions reversed.
Source§

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>,

Slice this tensor and return a view. Read more
Source§

fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>

Slice this tensor along a given axis.
Source§

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>,

A variant of slice that returns a result instead of panicking.
Source§

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>>>,

Return a slice of this tensor as an owned tensor. Read more
Source§

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>>>,

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). Read more
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. Read more
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.
Source§

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

Source§

fn clone(&self) -> TensorBase<S, L>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Storage, L: MutLayout> Debug for TensorBase<S, L>
where S::Elem: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a, T, L: Clone + MutLayout> From<&'a [T]> for TensorBase<ViewData<'a, T>, L>
where [usize; 1]: AsIndex<L>,

Source§

fn from(slice: &'a [T]) -> Self

Create a 1D view from a slice.

Source§

impl<'a, T, L: Clone + MutLayout, const N: usize> From<&'a [T; N]> for TensorBase<ViewData<'a, T>, L>
where [usize; 1]: AsIndex<L>,

Source§

fn from(slice: &'a [T; N]) -> Self

Create a 1D view from a slice of known length.

Source§

impl<T: Clone + Scalar, L: MutLayout, const D0: usize, const D1: usize, const D2: usize> From<[[[T; D2]; D1]; D0]> for TensorBase<Vec<T>, L>
where [usize; 3]: AsIndex<L>,

Source§

fn from(value: [[[T; D2]; D1]; D0]) -> Self

Construct a 3D tensor from a nested array.

Source§

impl<T: Clone + Scalar, L: MutLayout, const D0: usize, const D1: usize> From<[[T; D1]; D0]> for TensorBase<Vec<T>, L>
where [usize; 2]: AsIndex<L>,

Source§

fn from(value: [[T; D1]; D0]) -> Self

Construct a 2D tensor from a nested array.

Source§

impl<T: Clone + Scalar, L: MutLayout, const D0: usize> From<[T; D0]> for TensorBase<Vec<T>, L>
where [usize; 1]: AsIndex<L>,

Source§

fn from(value: [T; D0]) -> Self

Construct a 1D tensor from a 1D array.

Source§

impl<T: Clone + Scalar, L: MutLayout> From<T> for TensorBase<Vec<T>, L>
where [usize; 0]: AsIndex<L>,

Source§

fn from(value: T) -> Self

Construct a scalar tensor from a scalar value.

Source§

impl<T, S: Storage<Elem = T>, const N: usize> From<TensorBase<S, NdLayout<N>>> for TensorBase<S, DynLayout>

Source§

fn from(tensor: TensorBase<S, NdLayout<N>>) -> Self

Converts to this type from the input type.
Source§

impl<T, L: Clone + MutLayout> From<Vec<T>> for TensorBase<Vec<T>, L>
where [usize; 1]: AsIndex<L>,

Source§

fn from(vec: Vec<T>) -> Self

Create a 1D tensor from a vector.

Source§

impl<T, L: Clone + MutLayout> FromIterator<T> for TensorBase<Vec<T>, L>
where [usize; 1]: AsIndex<L>,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> TensorBase<Vec<T>, L>

Create a new 1D tensor filled with an arithmetic sequence of values in the range [start, end) separated by step. If step is omitted, it defaults to 1.

Source§

impl<T, S: Storage<Elem = T>, L: MutLayout, I: AsIndex<L>> Index<I> for TensorBase<S, L>

Source§

fn index(&self, index: I) -> &Self::Output

Return the element at a given index.

Panics if the index is out of bounds along any dimension.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T, S: StorageMut<Elem = T>, L: MutLayout, I: AsIndex<L>> IndexMut<I> for TensorBase<S, L>

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Return the element at a given index.

Panics if the index is out of bounds along any dimension.

Source§

impl<S: Storage, L: MutLayout> Layout for TensorBase<S, L>

Source§

type Index<'a> = <L as Layout>::Index<'a>

Type used to represent indices. Read more
Source§

type Indices = <L as Layout>::Indices

Iterator over indices in this tensor.
Source§

fn ndim(&self) -> usize

Return the number of dimensions.
Source§

fn len(&self) -> usize

Returns the number of elements in the array.
Source§

fn is_empty(&self) -> bool

Returns true if the array has no elements.
Source§

fn shape(&self) -> Self::Index<'_>

Returns an array of the sizes of each dimension.
Source§

fn size(&self, dim: usize) -> usize

Returns the size of the dimension dim.
Source§

fn strides(&self) -> Self::Index<'_>

Returns an array of the strides of each dimension.
Source§

fn stride(&self, dim: usize) -> usize

Returns the offset between adjacent indices along dimension dim.
Source§

fn indices(&self) -> Self::Indices

Return an iterator over all valid indices in this tensor.
Source§

fn try_offset(&self, index: Self::Index<'_>) -> Option<usize>

Map an index to a storage offset, or return None if the index is out of bounds along any dimension.
Source§

fn offset(&self, index: Self::Index<'_>) -> usize

Map an index to a storage offset. Read more
Source§

fn offset_unchecked(&self, index: Self::Index<'_>) -> usize

Map an index to a storage offset, without checking if it is valid for the tensor’s shape. Read more
Source§

fn is_contiguous(&self) -> bool

Return true if this layout describes a contiguous tensor, where the logical order of elements matches the order in which they are stored.
Source§

fn is_broadcast(&self) -> bool

Return true if iterating over elements in this layout will visit elements multiple times.
Source§

fn can_broadcast_to(&self, target_shape: &[usize]) -> bool

Return true if this layout’s shape can be broadcast to the given shape.
Source§

fn can_broadcast_with(&self, shape: &[usize]) -> bool

Return true if the tensor/view can be broadcast with another tensor or view with a given shape as part of a binary operation. Read more
Source§

fn min_data_len(&self) -> usize

Return the minimum length required for the element data buffer used with this layout.
Source§

impl<S: Storage, L: MutLayout + MatrixLayout> MatrixLayout for TensorBase<S, L>

Source§

fn rows(&self) -> usize

Source§

fn cols(&self) -> usize

Source§

fn row_stride(&self) -> usize

Source§

fn col_stride(&self) -> usize

Source§

impl<T: PartialEq, S: Storage<Elem = T>, L: MutLayout, V: AsView<Elem = T>> PartialEq<V> for TensorBase<S, L>

Source§

fn eq(&self, other: &V) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, S1, S2: Storage<Elem = T>, const N: usize> TryFrom<TensorBase<S1, DynLayout>> for TensorBase<S2, NdLayout<N>>
where S1: Into<S2> + Storage<Elem = T>,

Source§

fn try_from(value: TensorBase<S1, DynLayout>) -> Result<Self, Self::Error>

Convert a tensor or view with dynamic rank into a static rank one.

Fails if value does not have N dimensions.

Source§

type Error = DimensionError

The type returned in the event of a conversion error.
Source§

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

Auto Trait Implementations§

§

impl<S, L> Freeze for TensorBase<S, L>
where S: Freeze, L: Freeze,

§

impl<S, L> RefUnwindSafe for TensorBase<S, L>

§

impl<S, L> Send for TensorBase<S, L>
where S: Send, L: Send,

§

impl<S, L> Sync for TensorBase<S, L>
where S: Sync, L: Sync,

§

impl<S, L> Unpin for TensorBase<S, L>
where S: Unpin, L: Unpin,

§

impl<S, L> UnwindSafe for TensorBase<S, L>
where S: UnwindSafe, L: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.