pub enum Tensor {
F32(HostTensor<f32>),
F64(HostTensor<f64>),
I32(HostTensor<i32>),
I64(HostTensor<i64>),
Bool(HostTensor<bool>),
C32(HostTensor<Complex32>),
C64(HostTensor<Complex64>),
}Expand description
Dynamic owned host tensor over the supported dtype set.
§Examples
use tenferro_tensor_core::{DType, Tensor};
let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(tensor.dtype(), DType::F64);Variants§
F32(HostTensor<f32>)
F64(HostTensor<f64>)
I32(HostTensor<i32>)
I64(HostTensor<i64>)
Bool(HostTensor<bool>)
C32(HostTensor<Complex32>)
C64(HostTensor<Complex64>)
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn from_vec_col_major<T: TensorScalar>(
shape: impl Into<ShapeVec>,
data: Vec<T>,
) -> Result<Self>
pub fn from_vec_col_major<T: TensorScalar>( shape: impl Into<ShapeVec>, data: Vec<T>, ) -> Result<Self>
Create a dynamic tensor from a column-major host buffer.
§Examples
use tenferro_tensor_core::{DType, Tensor};
let tensor = Tensor::from_vec_col_major(vec![1], vec![2.0_f32])?;
assert_eq!(tensor.dtype(), DType::F32);§Errors
Returns ValidationError::ShapeDataLengthMismatch when the shape
product differs from data.len(), or 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_core::{DType, Tensor};
let tensor = Tensor::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_core::Tensor;
let tensor = Tensor::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_core::Tensor;
let tensor = Tensor::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_core::Tensor;
let tensor = Tensor::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_core::Tensor;
let tensor = Tensor::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 ValidationError::DTypeMismatch 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_core::Tensor;
let mut tensor = Tensor::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 ValidationError::DTypeMismatch when T does not match the
tensor’s runtime dtype.
Sourcepub fn as_view(&self) -> TensorView<'_>
pub fn as_view(&self) -> TensorView<'_>
Borrow this tensor as a dynamic zero-offset view.
§Examples
use tenferro_tensor_core::{DType, Tensor};
let tensor = Tensor::from_vec_col_major(vec![1], vec![1_i64])?;
assert_eq!(tensor.as_view().dtype(), DType::I64);Sourcepub fn into_vec_col_major<T: TensorScalar>(self) -> Result<(ShapeVec, Vec<T>)>
pub fn into_vec_col_major<T: TensorScalar>(self) -> Result<(ShapeVec, Vec<T>)>
Consume this tensor and return typed column-major data when the dtype matches.
§Examples
use tenferro_tensor_core::Tensor;
let tensor = Tensor::from_vec_col_major(vec![1], vec![2.0_f32])?;
assert_eq!(tensor.into_vec_col_major::<f32>()?.1, vec![2.0]);§Errors
Returns ValidationError::DTypeMismatch when T does not match the
tensor’s runtime dtype.