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>) -> Result<Tensor, Error>;
    fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>;
    fn as_slice(tensor: &Tensor) -> Result<&[Self], Error>;
    fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self], Error>;
    fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>, Error>;
}
Expand description

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

This trait is implemented for f64, f32, i32, i64, bool, 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>()?, [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>) -> Result<Tensor, Error>

Wrap typed column-major data into a Tensor enum variant.

Source

fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>

Borrow a typed tensor as a dtype-erased TensorRead view.

This keeps the typed tensor borrowed instead of copying host data into a new dynamic tensor.

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

let tensor = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![1.0, 2.0]).unwrap();
let read = f64::tensor_read(&tensor);
assert_eq!(read.dtype(), DType::F64);
assert_eq!(read.shape(), &[2]);
Source

fn as_slice(tensor: &Tensor) -> Result<&[Self], Error>

Borrow the host data from a Tensor.

Source

fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self], Error>

Mutably borrow the host data from a Tensor.

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

let mut tensor = Tensor::from_vec_col_major(vec![1], vec![2.0_f64])?;
<f64 as TensorScalar>::as_slice_mut(&mut tensor)?[0] = 3.0;

assert_eq!(tensor.as_slice::<f64>()?, &[3.0]);
Source

fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>, Error>

Extract a TypedTensor<Self> from a dynamic Tensor.

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

let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let typed = <f64 as TensorScalar>::into_typed(tensor)?;

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 bool

Source§

impl TensorScalar for f32

Source§

impl TensorScalar for f64

Source§

impl TensorScalar for i32

Source§

impl TensorScalar for i64

Source§

impl TensorScalar for Complex<f32>

Source§

impl TensorScalar for Complex<f64>

Implementors§