Shape

Type Alias Shape 

Source
pub type Shape = ArrayN<usize, { MAX_DIM }>;
Expand description

Type alias for representing tensor shapes.

A Shape stores the size of each dimension in a tensor, using a fixed-capacity array that can hold up to MAX_DIM dimensions. This provides efficient stack-allocated storage for dimension information.

§Examples

use slsl::Shape;

// Create a 2D shape (3x4 matrix)
let shape = Shape::from_slice(&[3, 4]);
assert_eq!(shape.len(), 2);
assert_eq!(shape[0], 3);
assert_eq!(shape[1], 4);
assert_eq!(shape.numel(), 12); // 3 * 4 = 12 elements

Aliased Type§

pub struct Shape {
    pub len: usize,
    pub arr: [usize; 8],
}

Fields§

§len: usize

The current number of elements in use

§arr: [usize; 8]

The underlying fixed-size array storage

Implementations§

Source§

impl Shape

Source

pub fn numel(&self) -> usize

Returns the total number of elements in the tensor.

This method calculates the product of all dimensions in the shape, which gives the total number of elements that would be stored in a tensor with this shape.

§Returns

The total number of elements as a usize. Returns 1 for empty shapes (0-dimensional tensors/scalars).

§Examples
use slsl::Shape;

// 2D shape: 3x4 matrix
let shape = Shape::from_slice(&[3, 4]);
assert_eq!(shape.numel(), 12); // 3 * 4

// 1D shape: vector of 5 elements
let shape_1d = Shape::from_slice(&[5]);
assert_eq!(shape_1d.numel(), 5);

// Empty shape: scalar (0-dimensional)
let shape_scalar = Shape::empty();
assert_eq!(shape_scalar.numel(), 1);