pub struct Tensor<T> { /* private fields */ }Expand description
Row-major tensor structure. The data is stored in a contiguous vector, and the shape and strides are used to interpret the data.
Implementations§
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
pub fn new(data: Vec<T>, shape: impl Into<Shape>) -> Self
pub fn try_new( data: Vec<T>, shape: impl Into<Shape>, ) -> Result<Self, TensorError>
Sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
The rank (number of dimensions) of the tensor.
For example, a matrix has rank 2, a vector has rank 1, and a scalar has rank 0.
use radiate_utils::Tensor;
let two = Tensor::new(vec![1, 2, 3, 4], (2, 2));
let three = Tensor::new(vec![0; 24], (2, 3, 4));
assert_eq!(two.rank(), 2);
assert_eq!(three.rank(), 3);Sourcepub fn dims(&self) -> &[usize]
pub fn dims(&self) -> &[usize]
The dimensions of the tensor. This is essentially a shortcut
for tensor.shape.as_slice(). Array of length equal to
the tensor’s rank, where each entry is the size of that dimension.
use radiate_utils::Tensor;
let tensor = Tensor::new(vec![1, 2, 3, 4, 5, 6], (2, 3));
assert_eq!(tensor.dims(), &[2, 3]);Sourcepub fn shape(&self) -> &Shape
pub fn shape(&self) -> &Shape
The shape of the tensor. This describes the size of each dimension.
For example, a tensor with shape [2, 3] has 2 rows and 3 columns -
essentially a 2x3 matrix.
use radiate_utils::Tensor;
let tensor = Tensor::new(vec![1, 2, 3, 4, 5, 6], (2, 3));
assert_eq!(tensor.shape().as_slice(), &[2, 3]);Sourcepub fn strides(&self) -> &Strides
pub fn strides(&self) -> &Strides
The strides of the tensor. Strides indicate how many elements to skip in the underlying data vector to move to the next element along each dimension. For a row-major tensor, the last dimension has a stride of 1, the second-to-last dimension has a stride equal to the size of the last dimension, and so on.
use radiate_utils::Tensor;
let tensor = Tensor::new(vec![1, 2, 3, 4, 5, 6], (2, 3));
assert_eq!(tensor.strides().as_slice(), &[3, 1]);Sourcepub fn data(&self) -> &[T]
pub fn data(&self) -> &[T]
The underlying data of the tensor as a flat slice. This data is stored in row-major order.
use radiate_utils::Tensor;
let tensor = Tensor::new(vec![1, 2, 3, 4, 5, 6], (2, 3));
assert_eq!(tensor.data(), &[1, 2, 3, 4, 5, 6]);Sourcepub fn data_mut(&mut self) -> &mut [T]
pub fn data_mut(&mut self) -> &mut [T]
The underlying data of the tensor as a mutable flat slice. This data is stored in row-major order.
use radiate_utils::Tensor;
let mut tensor = Tensor::new(vec![1, 2, 3, 4, 5, 6], (2, 3));
let data_mut = tensor.data_mut();
data_mut[0] = 10;
assert_eq!(tensor.data(), &[10, 2, 3, 4, 5, 6]);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Figure out if the tensor has no elements.
use radiate_utils::Tensor;
let empty = Tensor::<i32>::new(vec![], (0, 3));
let non_empty = Tensor::new(vec![1, 2, 3], (1, 3));
assert!(empty.is_empty());
assert!(!non_empty.is_empty());pub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_raw_parts(&self) -> (*const T, usize)
pub fn as_raw_parts_mut(&mut self) -> (*mut T, usize)
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Sourcepub fn reshape(&mut self, new_shape: impl Into<Shape>)
pub fn reshape(&mut self, new_shape: impl Into<Shape>)
Reshape without changing the underlying data. Panics if the new shape has a different total element count.
use radiate_utils::Tensor;
let mut tensor = Tensor::new(vec![0, 1, 2, 3, 4, 5], (2, 3));
tensor.reshape((3, 2));
assert_eq!(tensor.shape().as_slice(), &[3, 2]);
assert_eq!(tensor.strides().as_slice(), &[2, 1]); // row-major
assert_eq!(tensor.data(), &[0, 1, 2, 3, 4, 5]);