Skip to main content

TensorScalar

Trait TensorScalar 

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

Show 14 methods // Required methods fn dtype() -> DType; fn into_default_scalars( shape: Vec<usize>, data: Vec<Self>, ) -> Result<DefaultScalars>; fn default_scalars_slice(set: &DefaultScalars) -> Option<&[Self]>; fn default_scalars_slice_mut( set: &mut DefaultScalars, ) -> Option<&mut [Self]>; fn from_default_scalars( set: DefaultScalars, ) -> Option<TypedTensor<Self, DynRank, Host>>; fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor>; 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]>; fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self]>; fn into_typed( tensor: Tensor, ) -> Result<TypedTensor<Self>, ReinterpretError<Tensor>>;
}
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_default_scalars( shape: Vec<usize>, data: Vec<Self>, ) -> Result<DefaultScalars>

Build the crate’s default scalar set from validated column-major data.

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

let set = <f64 as TensorScalar>::into_default_scalars(vec![2], vec![1.0, 2.0])?;
assert_eq!(set.dtype(), DType::F64);
§Errors

Returns a validation error carrying tenferro_tensor_core::ValidationError::ShapeDataLengthMismatch when the shape product differs from data.len(), or tenferro_tensor_core::ValidationError::IntegerOverflow when shape arithmetic overflows.

Source

fn default_scalars_slice(set: &DefaultScalars) -> Option<&[Self]>

Borrow the default scalar set’s values when it holds this scalar type.

Returns None when the set currently holds another member.

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

let set = DefaultScalars::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(<f64 as TensorScalar>::default_scalars_slice(&set), Some(&[1.0, 2.0][..]));
assert!(<f32 as TensorScalar>::default_scalars_slice(&set).is_none());
Source

fn default_scalars_slice_mut(set: &mut DefaultScalars) -> Option<&mut [Self]>

Exclusively borrow the default scalar set’s values when it holds this scalar type.

Returns None when the set currently holds another member.

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

let mut set = DefaultScalars::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
if let Some(values) = <f64 as TensorScalar>::default_scalars_slice_mut(&mut set) {
    values[0] = 5.0;
}
assert_eq!(set.as_slice::<f64>()?, &[5.0, 2.0]);
Source

fn from_default_scalars( set: DefaultScalars, ) -> Option<TypedTensor<Self, DynRank, Host>>

Move the host tensor out of the default scalar set when it holds this scalar type.

Returns None when the set currently holds another member.

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

let set = DefaultScalars::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let host = <f64 as TensorScalar>::from_default_scalars(set);
assert_eq!(host.as_ref().map(|t| t.shape()), Some(&[2][..]));
assert_eq!(host.as_ref().map(|t| t.as_slice()), Some(&[1.0, 2.0][..]));
Source

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

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

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

let tensor = <f64 as TensorScalar>::into_tensor(vec![2], vec![1.0, 2.0])?;
assert_eq!(tensor.dtype(), DType::F64);
assert_eq!(tensor.shape(), &[2]);
assert!(<f64 as TensorScalar>::into_tensor(vec![3], vec![1.0]).is_err());
§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.

Source

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

Wrap a typed tensor into its dynamic Tensor enum variant.

§Examples
use tenferro_tensor::{DType, 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.dtype(), DType::F64));
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 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);
Source

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

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

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

Borrow the host data from a Tensor.

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

let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(<f64 as TensorScalar>::as_slice(&tensor)?, &[1.0, 2.0]);
assert!(<f32 as TensorScalar>::as_slice(&tensor).is_err());
§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.

Source

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

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.

Source

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

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 Ok(typed) = <f64 as TensorScalar>::into_typed(tensor) else {
    panic!("the dtype matches by construction")
};

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

Returns ReinterpretError carrying the unchanged tensor when it is not the scalar type represented by this implementation, with crate::Error::Validation and tenferro_tensor_core::ValidationError::DTypeMismatch as the cause.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl TensorScalar for Complex32

Source§

impl TensorScalar for Complex64

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

Implementors§