Skip to main content

TensorRank

Trait TensorRank 

Source
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, ValidationError>;
    fn shape_into_vec(shape: Self::Shape) -> SmallVec<[usize; 8]>;
    fn strides_from_vec(
        strides: SmallVec<[isize; 8]>,
    ) -> Result<Self::Strides, ValidationError>;
    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§

Source

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§

Source

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]);
Source

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§

Source

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

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]);
§Errors

Returns ValidationError::RankMismatch when the shape length does not equal the compile-time rank.

Source

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]);
Source

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

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]);
§Errors

Returns ValidationError::RankMismatch when the stride length does not equal the compile-time rank.

Source

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".

Implementors§