pub struct Tensor<C: Memorable, const D: usize> { /* private fields */ }Expand description
A tensor struct that holds data in an aligned memory buffer.
Implementations§
Source§impl<C: Memorable, const D: usize> Tensor<C, D>
impl<C: Memorable, const D: usize> Tensor<C, D>
Sourcepub fn new(data: MemoryBuffer<C>, dims: [usize; D], strides: [usize; D]) -> Self
pub fn new(data: MemoryBuffer<C>, dims: [usize; D], strides: [usize; D]) -> Self
Creates a new tensor from a memory buffer with specified dimensions and strides.
§Arguments
data- The memory buffer containing the tensor datadims- Array specifying the size of each dimensionstrides- Array specifying the stride for each dimension
§Panics
- If any dimension or stride is zero
- If the data buffer is not large enough for the specified dimensions and strides
Sourcepub fn zeros(dims: [usize; D]) -> Self
pub fn zeros(dims: [usize; D]) -> Self
Creates a new tensor with all elements initialized to zero, with specified shape.
§Arguments
dims- A slice ofusizecontaining the size of each dimension.
§Returns
- An new tensor with specified shape, filled with zeros.
§Panics
- If the length of
dimsis not equal to the number of dimensions of the tensor.
§Examples
let test_tensor = Tensor::<f64, 2>::zeros([3, 4]);
for i in 0..3 {
for j in 0..4 {
assert_eq!(test_tensor.get(&[i, j]), &0.0);
}
}Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Returns the total number of elements in the tensor.
Sourcepub fn as_ptr_mut(&mut self) -> *mut C
pub fn as_ptr_mut(&mut self) -> *mut C
Returns a mutable raw pointer to the tensor’s data.
Sourcepub fn get(&self, indices: &[usize; D]) -> &C
pub fn get(&self, indices: &[usize; D]) -> &C
Returns a reference to an element at the given indices.
§Panics
Panics if the indices are out of bounds.
Sourcepub fn get_mut(&mut self, indices: &[usize; D]) -> &mut C
pub fn get_mut(&mut self, indices: &[usize; D]) -> &mut C
Returns a mutable reference to an element at the given indices.
§Panics
Panics if the indices are out of bounds.
Sourcepub unsafe fn get_unchecked(&self, indices: &[usize; D]) -> &C
pub unsafe fn get_unchecked(&self, indices: &[usize; D]) -> &C
Returns an immutable reference to an element at the given indices, without performing bounds checks.
§Safety
Calling this method with out-of-bounds indices is undefined behavior.
Sourcepub unsafe fn get_mut_unchecked(&mut self, indices: &[usize; D]) -> &mut C
pub unsafe fn get_mut_unchecked(&mut self, indices: &[usize; D]) -> &mut C
Returns a mutable reference to an element at the given indices, without performing bounds checks.
§Safety
Calling this method with out-of-bounds indices is undefined behavior.
Sourcepub unsafe fn ptr_at(&self, indices: &[usize; D]) -> *const C
pub unsafe fn ptr_at(&self, indices: &[usize; D]) -> *const C
Returns a raw pointer to an element at the given indices, without performing bounds checks.
§Safety
Calling this method with out-of-bounds indices is undefined behavior.
Sourcepub unsafe fn ptr_at_mut(&mut self, indices: &[usize; D]) -> *mut C
pub unsafe fn ptr_at_mut(&mut self, indices: &[usize; D]) -> *mut C
Returns a mutable raw pointer to an element at the given indices, without performing bounds checks.
§Safety
Calling this method with out-of-bounds indices is undefined behavior.
Sourcepub fn from_slice(slice: &[C], dims: [usize; D]) -> Self
pub fn from_slice(slice: &[C], dims: [usize; D]) -> Self
Creates a new Tensor from a flat Vec and its dimensions.
This is a convenience constructor that automatically converts the Vec
into a MemoryBuffer and then calls the new constructor.
§Panics
Panics if the total number of elements implied by dimensions
(product of all dimension sizes) does not match the length of the data_vec.
§Examples
let tensor_from_slice = Tensor::from_slice(&vec![10, 20, 30, 40], [2, 2]);
assert_eq!(tensor_from_slice.dims(), &[2, 2]);
assert_eq!(tensor_from_slice.strides(), &[2, 1]);Sourcepub fn from_slice_with_strides(
slice: &[C],
dims: [usize; D],
strides: [usize; D],
) -> Self
pub fn from_slice_with_strides( slice: &[C], dims: [usize; D], strides: [usize; D], ) -> Self
Creates a new Tensor from a slice of data, explicit dimensions, and strides.
This constructor allows for creating tensors with custom stride patterns, which can be useful for representing views or sub-tensors of larger data structures without copying the underlying data.
§Panics
Panics if:
- The
dimensionsandstridesarrays do not have the same number of elements asD. - The total number of elements implied by
dimensionsandstrides(i.e., the maximum flat index + 1) exceeds the length of theslice. - Any stride is zero unless its corresponding dimension is also zero.
§Arguments
slice- The underlying data slice.dimensions- An array ofusizedefining the size of each dimension.strides- An array ofusizedefining the stride for each dimension.
§Examples
// Create a 2x3 tensor from a slice with custom strides
let data = vec![1, 2, 3, 4, 5, 6];
let tensor = Tensor::from_slice_with_strides(
&data,
[2, 3], // 2 rows, 3 columns
[3, 1], // Stride for rows is 3 elements, for columns is 1 element
);
assert_eq!(tensor.dims(), &[2, 3]);
assert_eq!(tensor.strides(), &[3, 1]);
assert_eq!(tensor.get(&[0, 0]), &1);
assert_eq!(tensor.get(&[0, 1]), &2);
assert_eq!(tensor.get(&[1, 0]), &4);
// Creating a column vector view from a larger matrix's data
let matrix_data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; // A 3x3 matrix's data
// View the second column (elements 2, 5, 8) as a 3x1 tensor
let column_view = Tensor::from_slice_with_strides(
&matrix_data,
[3, 1], // 3 rows, 1 column
[3, 1], // Stride to next row is 3, stride to next column is 1 (but only 1 column)
);
assert_eq!(column_view.dims(), &[3, 1]);
assert_eq!(column_view.strides(), &[3, 1]);
// Note: This example is slightly misleading as the slice itself doesn't change for the column.
// A more accurate example for strides would involve a sub-view that skips elements.
// This specific case would be more typical for `TensorRef`.Sourcepub fn zeros_with_strides(dims: &[usize; D], strides: &[usize; D]) -> Self
pub fn zeros_with_strides(dims: &[usize; D], strides: &[usize; D]) -> Self
Creates a new tensor with all elements initialized to zero, with specified shape and strides.
§Arguments
dims- A slice ofusizecontaining the size of each dimensionstrides- A slice ofusizecontaining the stride for each dimension.
§Returns
- A new tensor with specified shape and strides, filled with zeros.
§Panics
- If the length of
dimsorstridesis not equal to the number of dimensions of the tensor. - If the size of any dimension is zero but the corresponding stride is non-zero.
- If the size of any dimension is non-zero but the corresponding stride is zero.
§Examples
let test_tensor = Tensor::<f64, 2>::zeros_with_strides(&[3, 4], &[4, 1]);
for i in 0..3 {
for j in 0..4 {
assert_eq!(test_tensor.get(&[i, j]), &0.0);
}
}Source§impl<C: Memorable> Tensor<C, 4>
impl<C: Memorable> Tensor<C, 4>
Sourcepub fn subtensor_ref(&self, m: usize) -> TensorRef<'_, C, 3>
pub fn subtensor_ref(&self, m: usize) -> TensorRef<'_, C, 3>
Returns an immutable view of a 3D subtensor at the given index.
Sourcepub fn subtensor_mut(&mut self, m: usize) -> TensorMut<'_, C, 3>
pub fn subtensor_mut(&mut self, m: usize) -> TensorMut<'_, C, 3>
Returns a mutable view of a 3D subtensor at the given index.
Sourcepub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> TensorRef<'_, C, 3>
pub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> TensorRef<'_, C, 3>
Returns an immutable view of a 3D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Sourcepub unsafe fn subtensor_mut_unchecked(
&mut self,
m: usize,
) -> TensorMut<'_, C, 3>
pub unsafe fn subtensor_mut_unchecked( &mut self, m: usize, ) -> TensorMut<'_, C, 3>
Returns a mutable view of a 3D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Source§impl<C: Memorable> Tensor<C, 3>
impl<C: Memorable> Tensor<C, 3>
Sourcepub fn subtensor_ref(&self, m: usize) -> MatRef<'_, C>
pub fn subtensor_ref(&self, m: usize) -> MatRef<'_, C>
Returns an immutable matrix view of a 2D subtensor at the given index.
Sourcepub fn subtensor_mut(&mut self, m: usize) -> MatMut<'_, C>
pub fn subtensor_mut(&mut self, m: usize) -> MatMut<'_, C>
Returns a mutable matrix view of a 2D subtensor at the given index.
Sourcepub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> MatRef<'_, C>
pub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> MatRef<'_, C>
Returns an immutable matrix view of a 2D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Sourcepub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> MatMut<'_, C>
pub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> MatMut<'_, C>
Returns a mutable matrix view of a 2D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Source§impl<C: Memorable> Tensor<C, 2>
impl<C: Memorable> Tensor<C, 2>
Sourcepub fn subtensor_ref(&self, m: usize) -> RowRef<'_, C>
pub fn subtensor_ref(&self, m: usize) -> RowRef<'_, C>
Returns an immutable row view of a 1D subtensor at the given index.
Sourcepub fn subtensor_mut(&mut self, m: usize) -> RowMut<'_, C>
pub fn subtensor_mut(&mut self, m: usize) -> RowMut<'_, C>
Returns a mutable row view of a 1D subtensor at the given index.
Sourcepub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> RowRef<'_, C>
pub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> RowRef<'_, C>
Returns an immutable row view of a 1D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Sourcepub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> RowMut<'_, C>
pub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> RowMut<'_, C>
Returns a mutable row view of a 1D subtensor at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Source§impl<C: Memorable> Tensor<C, 1>
impl<C: Memorable> Tensor<C, 1>
Sourcepub fn subtensor_ref(&self, m: usize) -> &C
pub fn subtensor_ref(&self, m: usize) -> &C
Returns an immutable reference to an element at the given index.
Sourcepub fn subtensor_mut(&mut self, m: usize) -> &mut C
pub fn subtensor_mut(&mut self, m: usize) -> &mut C
Returns a mutable reference to an element at the given index.
Sourcepub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> &C
pub unsafe fn subtensor_ref_unchecked(&self, m: usize) -> &C
Returns an immutable reference to an element at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.
Sourcepub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> &mut C
pub unsafe fn subtensor_mut_unchecked(&mut self, m: usize) -> &mut C
Returns a mutable reference to an element at the given index without bounds checking.
§Safety
Caller must ensure that m is within bounds.