Skip to main content

TensorView

Struct TensorView 

Source
pub struct TensorView<'a, T: 'a, const N: usize, Layout = RowMajor, Ref = &'a [T]> { /* private fields */ }
Expand description

Zero-copy N-dimensional strided view over a borrowed slice.

§Type Parameters

  • 'a — lifetime of the underlying data slice.
  • T — element type.
  • N — tensor rank (number of dimensions). Const generic; resolved at compile time.
  • Layout — layout marker ZST (RowMajor or ColMajor). PhantomData; zero size.
  • Ref — reference type-state (&'a [T] or &'a mut [T]).

§Invariants

  • strides[i] * shape[i] must not overflow usize.
  • data.len() >= ∑ (shape[i]-1) * strides[i] + 1 for any valid element access.
  • new and with_strides both verify product(shape) <= data.len().

Implementations§

Source§

impl<'a, 'b, T, L> TensorView<'a, T, 2, L, &'b [T]>

Source

pub fn row_view( &self, i: usize, ) -> Result<TensorView<'a, T, 1, RowMajor, &'b [T]>, TensorError>

Return a zero-copy 1-D view of row i.

§Errors

Returns TensorError::IndexOutOfBounds if i >= shape[0].

Source

pub fn iter_rows(&self) -> Result<impl Iterator<Item = &'b [T]>, TensorError>

Iterate over row slices of a contiguous 2-D tensor.

§Errors

Returns TensorError::NotContiguous if the view is not row-major contiguous.

Source

pub fn transpose_view(&self) -> TensorView<'a, T, 2, ColMajor, &'b [T]>

Return a zero-copy transposed view (swaps shape and strides).

The result is ColMajor-tagged. No allocation.

Source

pub fn col_iter( &self, j: usize, ) -> Result<impl Iterator<Item = T> + '_, TensorError>
where T: Copy,

Iterate over elements of column j, one element per row.

Zero-copy: index arithmetic over the underlying slice.

§Errors

Returns TensorError::IndexOutOfBounds if j >= shape[1].

Source

pub fn diag_iter(&self) -> impl Iterator<Item = T> + '_
where T: Copy,

Iterate over main diagonal elements (min(rows, cols) elements).

Zero-copy: index arithmetic, no allocation.

Source§

impl<'a, T, L> TensorView<'a, T, 2, L, &mut [T]>

Source

pub fn row_view_mut( &mut self, i: usize, ) -> Result<TensorView<'a, T, 1, RowMajor, &mut [T]>, TensorError>

Return a mutable 1-D view of row i.

§Errors

Returns TensorError::IndexOutOfBounds if i >= shape[0].

Source

pub fn iter_rows_mut( &mut self, ) -> Result<impl Iterator<Item = &mut [T]>, TensorError>

Iterate over mutable row slices of a contiguous 2-D tensor.

§Errors

Returns TensorError::NotContiguous if not row-major contiguous.

Source§

impl<'a, T, L> TensorView<'a, T, 3, L, &'a [T]>

Source

pub fn matrix_at( &self, b: usize, ) -> Result<TensorView<'_, T, 2, RowMajor, &[T]>, TensorError>

Return a 2-D view of the b-th matrix in a batched tensor.

Source§

impl<'a, T, L> TensorView<'a, T, 3, L, &'a mut [T]>

Source

pub fn matrix_at_mut( &mut self, b: usize, ) -> Result<TensorView<'_, T, 2, RowMajor, &mut [T]>, TensorError>

Return a mutable 2-D view of the b-th matrix in a batched tensor.

Source§

impl<'a, T, Ref> TensorView<'a, T, 2, RowMajor, Ref>

Source

pub fn transpose(self) -> TensorView<'a, T, 2, ColMajor, Ref>

Transpose a row-major 2-D tensor view to column-major.

Source§

impl<'a, T, Ref> TensorView<'a, T, 2, ColMajor, Ref>

Source

pub fn transpose(self) -> TensorView<'a, T, 2, RowMajor, Ref>

Transpose a column-major 2-D tensor view to row-major.

Source§

impl<'a, T> TensorView<'a, T, 1, RowMajor, &'a [T]>
where T: Scalar,

Source

pub fn into_simd_view<Arch, Align>( &self, ) -> Option<SimdView<'a, T, Arch, Align, Unmasked, &'a [T]>>
where Arch: SimdArch + SimdKernel<T>, Align: Alignment,

Promote this contiguous rank-1 view into a typed SimdView.

Zero-copy: shares the same underlying slice. Returns None if the slice is empty or if the alignment check fails for Align.

§Example
use hermes_simd_core::tensor::TensorView;
use hermes_simd_intrinsics::Scalar;
use hermes_simd_core::align::Unaligned;

let data = [1.0f32, 2.0, 3.0, 4.0];
let t = TensorView::<f32, 1>::new(&data, [4]).unwrap();
let view = t.into_simd_view::<Scalar, Unaligned>().unwrap();
Source§

impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b [T]>

Source

pub fn new(data: &'b [T], shape: [usize; N]) -> Result<Self, TensorError>

Create a row-major tensor view over data with the given shape.

Strides are computed as strides[i] = ∏_{j=i+1..N} shape[j] (C-order).

§Errors

Returns TensorError::ShapeMismatch if ∏ shape > data.len().

§Examples
use hermes_simd_core::TensorView;

let data = [0, 1, 2, 3, 4, 5];
let view = TensorView::<i32, 2>::new(&data, [2, 3]).unwrap();

assert_eq!(view.shape(), [2, 3]);
assert_eq!(view.strides(), [3, 1]);
assert_eq!(view.get([1, 2]).unwrap(), 5);
Source§

impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b mut [T]>

Source

pub fn new_mut( data: &'b mut [T], shape: [usize; N], ) -> Result<Self, TensorError>

Create a mutable row-major tensor view over data with the given shape.

§Errors

Returns TensorError::ShapeMismatch if ∏ shape > data.len().

Source§

impl<'a, 'b, T, const N: usize, L: Layout> TensorView<'a, T, N, L, &'b [T]>

Source

pub fn with_strides( data: &'b [T], shape: [usize; N], strides: [usize; N], ) -> Result<Self, TensorError>

Create a tensor view with explicit strides.

Allows column-major, blocked, or any custom layout.

§Errors

Returns TensorError::ShapeMismatch if ∏ shape > data.len().

Source§

impl<'a, 'b, T, const N: usize, L: Layout> TensorView<'a, T, N, L, &'b mut [T]>

Source

pub fn with_strides_mut( data: &'b mut [T], shape: [usize; N], strides: [usize; N], ) -> Result<Self, TensorError>

Create a mutable tensor view with explicit strides.

§Errors

Returns TensorError::ShapeMismatch if ∏ shape > data.len().

Source

pub fn downgrade(self) -> TensorView<'a, T, N, L, &'b [T]>

Downgrade the exclusive mutable view to a shared read-only view.

Source§

impl<'a, 'b, T> TensorView<'a, T, 2, ColMajor, &'b [T]>

Source

pub fn new_col_major( data: &'b [T], shape: [usize; 2], ) -> Result<Self, TensorError>

Create a column-major (Fortran-order) 2-D tensor view.

Fortran strides: strides[0] = 1, strides[1] = shape[0].

§Errors

Returns TensorError::ShapeMismatch if shape[0] * shape[1] > data.len().

Source§

impl<'a, T, const N: usize, L, Ref> TensorView<'a, T, N, L, Ref>

Source

pub fn shape(&self) -> [usize; N]

The logical shape of this tensor: number of elements per dimension.

Source

pub fn strides(&self) -> [usize; N]

The strides of this tensor in element units.

Source

pub fn num_elements(&self) -> usize

Number of elements in this tensor: ∏ shape[i].

Source

pub fn is_empty(&self) -> bool

Returns true if the tensor is empty (one of its dimensions is 0).

Source

pub fn is_contiguous(&self) -> bool

Whether this view is contiguous in row-major order.

Source

pub fn as_slice(&self) -> &[T]

View the underlying flat slice (in storage order).

Source

pub fn get(&self, idx: [usize; N]) -> Result<T, TensorError>
where T: Copy,

Bounds-checked element access.

Source

pub unsafe fn get_unchecked(&self, idx: [usize; N]) -> T
where T: Copy,

Unchecked element access.

§Safety

idx[i] < shape[i] for all i.

Source

pub fn reshape<const M: usize>( self, new_shape: [usize; M], ) -> Result<TensorView<'a, T, M, RowMajor, Ref>, TensorError>

Reshape this view to a different rank M, reusing the same flat slice.

Source§

impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b mut [T]>

Source

pub fn as_slice_mut(&mut self) -> &mut [T]

Access the underlying flat mutable slice.

Source

pub fn set(&mut self, idx: [usize; N], val: T) -> Result<(), TensorError>

Bounds-checked element write access.

Source

pub unsafe fn set_unchecked(&mut self, idx: [usize; N], val: T)

Unchecked element write access.

§Safety

idx[i] < shape[i] for all i.

Trait Implementations§

Source§

impl<'a, T, const N: usize, Layout> Clone for TensorView<'a, T, N, Layout, &'a [T]>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T, const N: usize, Layout> Copy for TensorView<'a, T, N, Layout, &'a [T]>

Source§

impl<'a, T, const N: usize, Layout, Ref> Send for TensorView<'a, T, N, Layout, Ref>
where Ref: Send,

Source§

impl<'a, T, const N: usize, Layout, Ref> Sync for TensorView<'a, T, N, Layout, Ref>
where Ref: Sync,

Auto Trait Implementations§

§

impl<'a, T, const N: usize, Layout, Ref> Freeze for TensorView<'a, T, N, Layout, Ref>

§

impl<'a, T, const N: usize, Layout, Ref> RefUnwindSafe for TensorView<'a, T, N, Layout, Ref>
where T: RefUnwindSafe, Layout: RefUnwindSafe, Ref: RefUnwindSafe,

§

impl<'a, T, const N: usize, Layout, Ref> Unpin for TensorView<'a, T, N, Layout, Ref>
where Layout: Unpin, Ref: Unpin,

§

impl<'a, T, const N: usize, Layout, Ref> UnsafeUnpin for TensorView<'a, T, N, Layout, Ref>

§

impl<'a, T, const N: usize, Layout, Ref> UnwindSafe for TensorView<'a, T, N, Layout, Ref>
where T: RefUnwindSafe, Layout: UnwindSafe, Ref: 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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> CastTo for T
where T: Copy,

Source§

fn cast_to<U>(self) -> U
where U: CastFrom<Self>,

Cast self to type U.
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<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

Source§

fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
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> Pointee for T

Source§

type Metadata = ()

The type for metadata in pointers and references to Self.
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.