pub struct DefaultScalars { /* private fields */ }Expand description
Dynamic host tensor over the crate’s preset scalar set.
The payload is a private inline value: a host tensor is one move and a copy
allocates nothing, and the preset variants are not part of the public
surface. Construct through DefaultScalars::from_vec_col_major and read
through DefaultScalars::as_slice, DefaultScalars::as_mut_slice, or
DefaultScalars::into_vec_col_major.
§Examples
use tenferro_tensor::{DefaultScalars, DType};
use tenferro_tensor::ScalarSet;
let value = DefaultScalars::from_vec_col_major(vec![1], vec![7_i32])?;
assert_eq!(value.tag(), DType::I32);
assert_eq!(value.as_slice::<i32>()?, &[7]);Implementations§
Source§impl DefaultScalars
impl DefaultScalars
Sourcepub fn from_vec_col_major<T: TensorScalar>(
shape: impl Into<Vec<usize>>,
data: Vec<T>,
) -> Result<Self>
pub fn from_vec_col_major<T: TensorScalar>( shape: impl Into<Vec<usize>>, data: Vec<T>, ) -> Result<Self>
Create a dynamic tensor from a column-major host buffer.
§Examples
use tenferro_tensor::{DefaultScalars, DType};
let tensor = DefaultScalars::from_vec_col_major(vec![1], vec![2.0_f32])?;
assert_eq!(tensor.dtype(), DType::F32);§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 validating the shape overflows.
Sourcepub fn dtype(&self) -> DType
pub fn dtype(&self) -> DType
Return the tensor dtype tag.
§Examples
use tenferro_tensor::{DefaultScalars, DType};
let tensor = DefaultScalars::from_vec_col_major(vec![1], vec![false])?;
assert_eq!(tensor.dtype(), DType::Bool);Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Borrow the tensor shape.
§Examples
use tenferro_tensor::DefaultScalars;
let tensor = DefaultScalars::from_vec_col_major(vec![2], vec![1_i32, 2])?;
assert_eq!(tensor.shape(), &[2]);Sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
Return the tensor rank.
§Examples
use tenferro_tensor::DefaultScalars;
let tensor = DefaultScalars::from_vec_col_major(vec![1, 1], vec![1_i64])?;
assert_eq!(tensor.rank(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return whether the tensor has zero elements.
§Examples
use tenferro_tensor::DefaultScalars;
let tensor = DefaultScalars::from_vec_col_major(vec![0], Vec::<f64>::new())?;
assert!(tensor.is_empty());Sourcepub fn as_slice<T: TensorScalar>(&self) -> Result<&[T]>
pub fn as_slice<T: TensorScalar>(&self) -> Result<&[T]>
Borrow the typed host slice when the dtype matches.
§Examples
use tenferro_tensor::DefaultScalars;
let tensor = DefaultScalars::from_vec_col_major(vec![1], vec![3.0_f64])?;
assert_eq!(tensor.as_slice::<f64>()?, &[3.0]);
assert!(tensor.as_slice::<f32>().is_err());§Errors
Returns crate::Error::Validation with a dtype mismatch when T
does not match the tensor’s runtime dtype.
Sourcepub fn as_mut_slice<T: TensorScalar>(&mut self) -> Result<&mut [T]>
pub fn as_mut_slice<T: TensorScalar>(&mut self) -> Result<&mut [T]>
Mutably borrow the typed host slice when the dtype matches.
§Examples
use tenferro_tensor::DefaultScalars;
let mut tensor = DefaultScalars::from_vec_col_major(vec![1], vec![3.0_f64])?;
tensor.as_mut_slice::<f64>()?[0] = 4.0;
assert_eq!(tensor.as_slice::<f64>()?, &[4.0]);§Errors
Returns crate::Error::Validation with a dtype mismatch when T
does not match the tensor’s runtime dtype.
Sourcepub fn as_view(&self) -> DefaultScalarsView<'_>
pub fn as_view(&self) -> DefaultScalarsView<'_>
Borrow this tensor as a dynamic zero-offset view.
§Examples
use tenferro_tensor::{DefaultScalars, DType};
let tensor = DefaultScalars::from_vec_col_major(vec![1], vec![1.0_f32])?;
assert_eq!(tensor.as_view().dtype(), DType::F32);Sourcepub fn into_vec_col_major<T: TensorScalar>(self) -> Result<(Vec<usize>, Vec<T>)>
pub fn into_vec_col_major<T: TensorScalar>(self) -> Result<(Vec<usize>, Vec<T>)>
Consume this tensor and return typed column-major data when the dtype matches.
§Examples
use tenferro_tensor::DefaultScalars;
let tensor = DefaultScalars::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let (shape, data) = tensor.into_vec_col_major::<f64>()?;
assert_eq!(shape, vec![2]);
assert_eq!(data, vec![1.0, 2.0]);§Errors
Returns crate::Error::Validation with a dtype mismatch when T
does not match the tensor’s runtime dtype.
Trait Implementations§
Source§impl Clone for DefaultScalars
impl Clone for DefaultScalars
Source§fn clone(&self) -> DefaultScalars
fn clone(&self) -> DefaultScalars
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more