Skip to main content

TensorScalar

Trait TensorScalar 

Source
pub trait TensorScalar:
    Copy
    + Clone
    + Send
    + Sync
    + 'static
    + Sealed {
    type Real: TensorScalar;

    // Required methods
    fn dtype() -> DType;
    fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Tensor;
    fn try_as_slice(tensor: &Tensor) -> Option<&[Self]>;
    fn try_into_typed(tensor: Tensor) -> Option<TypedTensor<Self>>;
}
Expand description

Sealed trait for scalar types that can be stored in a Tensor.

This trait is implemented for f64, f32, Complex64, and Complex32.

§Examples

use tenferro_tensor::TensorScalar;

let tensor = <f64 as TensorScalar>::into_tensor(vec![2], vec![1.0, 2.0]);
assert_eq!(tensor.as_slice::<f64>(), Some([1.0, 2.0].as_slice()));

Required Associated Types§

Source

type Real: TensorScalar

Real-valued counterpart of this scalar type.

Required Methods§

Source

fn dtype() -> DType

The DType tag corresponding to this scalar type.

§Examples
use tenferro_tensor::{DType, TensorScalar};

assert_eq!(f64::dtype(), DType::F64);
assert_eq!(f32::dtype(), DType::F32);
Source

fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Tensor

Wrap typed data into a Tensor enum variant.

Source

fn try_as_slice(tensor: &Tensor) -> Option<&[Self]>

Try to borrow the host data from a Tensor.

Source

fn try_into_typed(tensor: Tensor) -> Option<TypedTensor<Self>>

Try to extract a TypedTensor<Self> from a dynamic Tensor.

Returns None if the tensor dtype does not match Self.

§Examples
use tenferro_tensor::{Tensor, TensorScalar};

let tensor = Tensor::from_vec(vec![2], vec![1.0_f64, 2.0]);
let typed = <f64 as TensorScalar>::try_into_typed(tensor).unwrap();

assert_eq!(typed.as_slice(), &[1.0, 2.0]);

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.

Implementations on Foreign Types§

Source§

impl TensorScalar for f32

Source§

impl TensorScalar for f64

Source§

impl TensorScalar for Complex<f32>

Source§

impl TensorScalar for Complex<f64>

Implementors§