Skip to main content

TensorScalar

Trait TensorScalar 

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 typed_tensor_into_tensor(tensor: TypedTensor<Self>) -> Tensor;
    fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>;
    fn tensor_view<'a>(view: TypedTensorView<'a, Self>) -> TensorView<'a>;
    fn tensor_view_mut<'a>(
        view: TypedTensorViewMut<'a, Self>,
    ) -> TensorViewMut<'a>;
    fn tensor_write(tensor: &mut TypedTensor<Self>) -> TensorWrite<'_>;
    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§

type Real: TensorScalar

Real-valued counterpart of this scalar type.

Required Methods§

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

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

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

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::ShapeDataLengthMismatch] when the shape product differs from data.len(), or [tenferro_tensor_core::ValidationError::IntegerOverflow] when shape arithmetic overflows.

fn typed_tensor_into_tensor(tensor: TypedTensor<Self>) -> Tensor

Wrap a typed tensor into its dynamic Tensor enum variant.

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

let typed = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![3.0])?;
let tensor = <f64 as TensorScalar>::typed_tensor_into_tensor(typed);
assert!(matches!(tensor, Tensor::F64(_)));

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

fn tensor_view<'a>(view: TypedTensorView<'a, Self>) -> TensorView<'a>

Wrap a typed borrowed view as a dtype-erased TensorView.

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

let data = [1.0_f64];
let view = TypedTensorView::from_col_major(&[1], &data)?;
assert_eq!(f64::tensor_view(view).dtype(), DType::F64);

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, Self>) -> TensorViewMut<'a>

Wrap a typed mutable borrowed view as a dtype-erased [TensorViewMut].

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

let mut data = [1.0_f64, 2.0];
let view = TypedTensorViewMut::from_col_major(&[2], &mut data)?;
let erased = f64::tensor_view_mut(view);
assert_eq!(erased.dtype(), DType::F64);
assert_eq!(erased.shape(), &[2]);

fn tensor_write(tensor: &mut TypedTensor<Self>) -> TensorWrite<'_>

Mutably borrow a typed tensor as a dtype-erased [TensorWrite] view.

This keeps the typed output borrowed instead of wrapping it in a temporary dynamic tensor.

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

let mut tensor = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![0.0]).unwrap();
let write = f64::tensor_write(&mut tensor);
assert_eq!(write.dtype(), DType::F64);

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

Borrow the host data from a Tensor.

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::DTypeMismatch] when tensor is not the scalar type represented by this implementation, or [crate::Error::RuntimeState] when the matching tensor uses backend storage that has not been downloaded.

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

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::DTypeMismatch] when tensor is not the scalar type represented by this implementation, or [crate::Error::RuntimeState] when the matching tensor uses backend storage that has not been downloaded.

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

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::DTypeMismatch] when tensor is not the scalar type represented by this implementation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

§

impl TensorScalar for Complex<f32>

§

type Real = f32

§

fn dtype() -> DType

§

fn into_tensor( shape: Vec<usize>, data: Vec<Complex<f32>>, ) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<Complex<f32>>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, Complex<f32>>) -> TensorView<'a>

§

fn tensor_view_mut<'a>( view: TypedTensorViewMut<'a, Complex<f32>>, ) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<Complex<f32>>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for Complex<f64>

§

type Real = f64

§

fn dtype() -> DType

§

fn into_tensor( shape: Vec<usize>, data: Vec<Complex<f64>>, ) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<Complex<f64>>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, Complex<f64>>) -> TensorView<'a>

§

fn tensor_view_mut<'a>( view: TypedTensorViewMut<'a, Complex<f64>>, ) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<Complex<f64>>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for bool

§

type Real = bool

§

fn dtype() -> DType

§

fn into_tensor(shape: Vec<usize>, data: Vec<bool>) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<bool>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, bool>) -> TensorView<'a>

§

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, bool>) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<bool>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for f32

§

type Real = f32

§

fn dtype() -> DType

§

fn into_tensor(shape: Vec<usize>, data: Vec<f32>) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<f32>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, f32>) -> TensorView<'a>

§

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, f32>) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<f32>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for f64

§

type Real = f64

§

fn dtype() -> DType

§

fn into_tensor(shape: Vec<usize>, data: Vec<f64>) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<f64>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, f64>) -> TensorView<'a>

§

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, f64>) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<f64>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for i32

§

type Real = i32

§

fn dtype() -> DType

§

fn into_tensor(shape: Vec<usize>, data: Vec<i32>) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<i32>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, i32>) -> TensorView<'a>

§

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, i32>) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<i32>) -> TensorWrite<'_>

§

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

§

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

§

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

§

impl TensorScalar for i64

§

type Real = i64

§

fn dtype() -> DType

§

fn into_tensor(shape: Vec<usize>, data: Vec<i64>) -> Result<Tensor, Error>

§

fn typed_tensor_into_tensor(tensor: TypedTensor<i64>) -> Tensor

§

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

§

fn tensor_view<'a>(view: TypedTensorView<'a, i64>) -> TensorView<'a>

§

fn tensor_view_mut<'a>(view: TypedTensorViewMut<'a, i64>) -> TensorViewMut<'a>

§

fn tensor_write(tensor: &mut TypedTensor<i64>) -> TensorWrite<'_>

§

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

§

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

§

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

Implementors§