pub enum Tensor {
Scalar(f64),
Array(Vec<Tensor>),
}Expand description
A multi-dimensional numerical array.
Tensors can be scalars (single values) or arrays of nested tensors. All leaf values are stored as f64.
§Examples
use hedl_core::lex::{parse_tensor, Tensor};
// Scalar
let scalar = Tensor::Scalar(42.0);
assert_eq!(scalar.shape(), vec![]);
assert_eq!(scalar.flatten(), vec![42.0]);
// 1D array
let vec = parse_tensor("[1, 2, 3]").unwrap();
assert_eq!(vec.shape(), vec![3]);
assert!(vec.is_integer());
// 2D matrix
let matrix = parse_tensor("[[1.5, 2.5], [3.5, 4.5]]").unwrap();
assert_eq!(matrix.shape(), vec![2, 2]);
assert!(!matrix.is_integer());Variants§
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if this tensor contains only integers (no decimal points).
A number is considered an integer if its fractional part is zero.
§Examples
use hedl_core::lex::{parse_tensor, Tensor};
let integers = parse_tensor("[1, 2, 3]").unwrap();
assert!(integers.is_integer());
let floats = parse_tensor("[1.5, 2.5]").unwrap();
assert!(!floats.is_integer());Sourcepub fn shape(&self) -> Vec<usize>
pub fn shape(&self) -> Vec<usize>
Returns the shape of the tensor as a vector of dimensions.
§Examples
use hedl_core::lex::{parse_tensor, Tensor};
let scalar = Tensor::Scalar(42.0);
assert_eq!(scalar.shape(), vec![]);
let vec = parse_tensor("[1, 2, 3]").unwrap();
assert_eq!(vec.shape(), vec![3]);
let matrix = parse_tensor("[[1, 2], [3, 4]]").unwrap();
assert_eq!(matrix.shape(), vec![2, 2]);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Tensor
impl<'de> Deserialize<'de> for Tensor
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Tensor
Auto Trait Implementations§
impl Freeze for Tensor
impl RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl UnwindSafe for Tensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more