Struct Shape
pub struct Shape(/* private fields */);Expand description
Re-export core types. N-dimensional shape of a tensor.
Implementations§
§impl Shape
impl Shape
pub 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.
pub 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.
pub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, Error>
pub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, Error>
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)
pub 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.
Trait Implementations§
impl Eq for Shape
impl StructuralPartialEq for Shape
Auto Trait Implementations§
impl Freeze for Shape
impl RefUnwindSafe for Shape
impl Send for Shape
impl Sync for Shape
impl Unpin for Shape
impl UnwindSafe for Shape
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more