Skip to main content

Tensor

Struct Tensor 

Source
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>

Source

pub fn new(data: Vec<T>, shape: impl Into<Shape>) -> Self

Source

pub fn try_new( data: Vec<T>, shape: impl Into<Shape>, ) -> Result<Self, TensorError>

Source

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);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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());
Source

pub fn as_ptr(&self) -> *const T

— raw pointers —

Source

pub fn as_mut_ptr(&mut self) -> *mut T

Source

pub fn as_raw_parts(&self) -> (*const T, usize)

Source

pub fn as_raw_parts_mut(&mut self) -> (*mut T, usize)

Source

pub fn iter(&self) -> Iter<'_, T>

— iterators —

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Source

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]);
Source§

impl<T: Clone> Tensor<T>

Source

pub fn from_elem(shape: impl Into<Shape>, value: T) -> Self

Source§

impl<T: Default + Clone> Tensor<T>

Source

pub fn zeros(shape: impl Into<Shape>) -> Self

Trait Implementations§

Source§

impl<T: Debug> Debug for Tensor<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Tensor<T>

Source§

fn default() -> Tensor<T>

Returns the “default value” for a type. Read more
Source§

impl<T, const N: usize> Index<[usize; N]> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: [usize; N]) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<(usize, usize)> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<(usize, usize, usize)> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<(usize, usize, usize, usize)> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (usize, usize, usize, usize)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<usize> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Tensor<T>

§

impl<T> RefUnwindSafe for Tensor<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tensor<T>
where T: Send,

§

impl<T> Sync for Tensor<T>
where T: Sync,

§

impl<T> Unpin for Tensor<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Tensor<T>

§

impl<T> UnwindSafe for Tensor<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.