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
type Real: TensorScalar
Real-valued counterpart of this scalar type.
Required Methods§
fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor, Error>
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
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<'_>
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>
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>
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<'_>
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>
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>
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>
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".