pub struct Shape(/* private fields */);Expand description
N-dimensional shape of a tensor.
Implementations§
Source§impl Shape
impl Shape
Sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
Number of dimensions (0 for scalar, 1 for vector, 2 for matrix, etc.).
Sourcepub fn elem_count(&self) -> usize
pub fn elem_count(&self) -> usize
Total number of elements (product of all dimensions). A scalar shape [] has 1 element.
Sourcepub fn stride_contiguous(&self) -> Vec<usize>
pub fn stride_contiguous(&self) -> Vec<usize>
Compute the contiguous (row-major / C-order) strides for this shape.
For shape [2, 3, 4], strides are [12, 4, 1]:
- Moving 1 step in dim 0 jumps 12 elements (3*4)
- Moving 1 step in dim 1 jumps 4 elements
- Moving 1 step in dim 2 jumps 1 element
This is how row-major memory works: the last dimension is contiguous.
Sourcepub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape>
pub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape>
Compute the broadcast output shape from two input shapes.
NumPy-style broadcasting rules:
- Align shapes from the right (trailing dimensions).
- Dimensions are compatible if they are equal or one of them is 1.
- Missing leading dimensions are treated as 1.
Examples: [3, 4] and [4] → [3, 4] (expand [4] to [1, 4] then broadcast dim 0) [2, 1] and [1, 3] → [2, 3] [5, 3, 1] and [3, 4] → [5, 3, 4] [3] and [4] → Error (3 ≠ 4 and neither is 1)
Sourcepub fn broadcast_strides(&self, target: &Shape) -> Vec<usize>
pub fn broadcast_strides(&self, target: &Shape) -> Vec<usize>
Return the broadcast strides for this shape to match a target broadcast shape.
For each dimension where self.dim[i] == 1 and target.dim[i] > 1, the stride is set to 0 (repeating the single element). For missing leading dimensions (self has fewer dims), stride is also 0.