Shape

Struct Shape 

pub struct Shape(/* private fields */);
Expand description

Re-export core types. N-dimensional shape of a tensor.

Implementations§

§

impl Shape

pub fn new(dims: Vec<usize>) -> Shape

Create a new shape from a vector of dimension sizes.

pub fn dims(&self) -> &[usize]

The dimension sizes as a slice.

pub fn rank(&self) -> usize

Number of dimensions (0 for scalar, 1 for vector, 2 for matrix, etc.).

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>

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 dim(&self, d: usize) -> Result<usize, Error>

Size of a specific dimension.

pub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Result<Shape, Error>

Compute the broadcast output shape from two input shapes.

NumPy-style broadcasting rules:

  1. Align shapes from the right (trailing dimensions).
  2. Dimensions are compatible if they are equal or one of them is 1.
  3. 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>

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 Clone for Shape

§

fn clone(&self) -> Shape

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Shape

§

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

Formats the value using the given formatter. Read more
§

impl Display for Shape

§

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

Formats the value using the given formatter. Read more
§

impl From<&[usize]> for Shape

§

fn from(s: &[usize]) -> Shape

Converts to this type from the input type.
§

impl From<()> for Shape

§

fn from(_: ()) -> Shape

Scalar shape (0 dimensions).

§

impl From<(usize,)> for Shape

§

fn from(_: (usize,)) -> Shape

Converts to this type from the input type.
§

impl From<(usize, usize)> for Shape

§

fn from(_: (usize, usize)) -> Shape

Converts to this type from the input type.
§

impl From<(usize, usize, usize)> for Shape

§

fn from(_: (usize, usize, usize)) -> Shape

Converts to this type from the input type.
§

impl From<(usize, usize, usize, usize)> for Shape

§

fn from(_: (usize, usize, usize, usize)) -> Shape

Converts to this type from the input type.
§

impl From<Vec<usize>> for Shape

§

fn from(v: Vec<usize>) -> Shape

Converts to this type from the input type.
§

impl From<usize> for Shape

§

fn from(d: usize) -> Shape

1-D shape.

§

impl Hash for Shape

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl PartialEq for Shape

§

fn eq(&self, other: &Shape) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V