Skip to main content

TensorRank

Trait TensorRank 

pub trait TensorRank:
    Sealed
    + Clone
    + Copy
    + Debug
    + Eq
    + Send
    + Sync
    + 'static {
    type Shape: Clone + Debug + PartialEq + Eq + AsRef<[usize]>;
    type Strides: Clone + Debug + PartialEq + Eq + AsRef<[isize]>;

    const RANK: Option<usize>;

    // Required methods
    fn shape_from_vec(shape: SmallVec<[usize; 8]>) -> Result<Self::Shape, Error>;
    fn shape_into_vec(shape: Self::Shape) -> SmallVec<[usize; 8]>;
    fn strides_from_vec(
        strides: SmallVec<[isize; 8]>,
    ) -> Result<Self::Strides, Error>;
    fn strides_into_vec(strides: Self::Strides) -> SmallVec<[isize; 8]>;
}
Expand description

Rank contract for tensor metadata shapes and strides.

§Examples

use tenferro_tensor_core::{Rank, TensorRank};

let shape = <Rank<2> as TensorRank>::shape_from_vec(vec![2, 3].into())?;
assert_eq!(shape.as_ref(), &[2, 3]);

Required Associated Constants§

const RANK: Option<usize>

Static rank when known at compile time.

§Examples
use tenferro_tensor_core::{DynRank, Rank, TensorRank};

assert_eq!(DynRank::RANK, None);
assert_eq!(Rank::<2>::RANK, Some(2));

Required Associated Types§

type Shape: Clone + Debug + PartialEq + Eq + AsRef<[usize]>

Shape representation for this rank.

§Examples
use tenferro_tensor_core::{DynRank, TensorRank};

let shape: <DynRank as TensorRank>::Shape = vec![2, 3].into();
assert_eq!(shape.as_ref(), &[2, 3]);

type Strides: Clone + Debug + PartialEq + Eq + AsRef<[isize]>

Stride representation for this rank.

§Examples
use tenferro_tensor_core::{DynRank, TensorRank};

let strides: <DynRank as TensorRank>::Strides = vec![1, 2].into();
assert_eq!(strides.as_ref(), &[1, 2]);

Required Methods§

fn shape_from_vec(shape: SmallVec<[usize; 8]>) -> Result<Self::Shape, Error>

Convert a dynamic shape vector into this rank’s shape representation.

§Examples
use tenferro_tensor_core::{Rank, TensorRank};

let shape = <Rank<1> as TensorRank>::shape_from_vec(vec![4].into())?;
assert_eq!(shape.as_ref(), &[4]);

fn shape_into_vec(shape: Self::Shape) -> SmallVec<[usize; 8]>

Convert this rank’s shape representation into a dynamic shape vector.

§Examples
use tenferro_tensor_core::{Rank, TensorRank};

let shape = <Rank<2> as TensorRank>::shape_from_vec(vec![2, 3].into())?;
assert_eq!(<Rank<2> as TensorRank>::shape_into_vec(shape).as_slice(), &[2, 3]);

fn strides_from_vec( strides: SmallVec<[isize; 8]>, ) -> Result<Self::Strides, Error>

Convert a dynamic stride vector into this rank’s stride representation.

§Examples
use tenferro_tensor_core::{Rank, TensorRank};

let strides = <Rank<2> as TensorRank>::strides_from_vec(vec![1, 2].into())?;
assert_eq!(strides.as_ref(), &[1, 2]);

fn strides_into_vec(strides: Self::Strides) -> SmallVec<[isize; 8]>

Convert this rank’s stride representation into a dynamic stride vector.

§Examples
use tenferro_tensor_core::{Rank, TensorRank};

let strides = <Rank<2> as TensorRank>::strides_from_vec(vec![1, 2].into())?;
assert_eq!(<Rank<2> as TensorRank>::strides_into_vec(strides).as_slice(), &[1, 2]);

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.

Implementors§

§

impl TensorRank for DynRank

§

const RANK: Option<usize> = None

§

type Shape = SmallVec<[usize; 8]>

§

type Strides = SmallVec<[isize; 8]>

§

impl<const N: usize> TensorRank for Rank<N>

§

const RANK: Option<usize>

§

type Shape = [usize; N]

§

type Strides = [isize; N]