numojo.core.layout.ndshape¶
NDArrayShape (numojo.core.layout.ndshape)
Implements NDArrayShape type representing the shape of an NDArray. The shape is stored as a contiguous buffer of integers, with the number of dimensions (ndim) tracked separately. The NDArrayShape provides methods for element access, shape transformations (e.g., permute, reverse), and properties like size and rank.
Structs¶
NDArrayShape¶
Memory convention: register_passable
Implements: AnyType, Copyable, Equatable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, Representable, Sized, Stringable, Writable
Presents the shape of NDArray type.
The data buffer of the NDArrayShape is a series of Int on memory.
The number of elements in the shape must be positive.
The elements of the shape must be positive.
The number of dimension and values of elements are checked upon
creation of the shape.
Example:
import numojo as nm
var shape1 = nm.Shape(2, 3, 4)
print(shape1) # Shape: (2, 3, 4)
var shape2 = nm.Shape([5, 6, 7])
print(shape2) # Shape: (5, 6, 7)
Fields¶
ndim(Int): Number of dimensions of array. It must be larger than 0.
Aliases¶
element_type¶
Value: DType.int
The data type of the NDArrayShape elements.
__del__is_trivial¶
Value: False
__move_ctor_is_trivial¶
Value: True
__copy_ctor_is_trivial¶
Value: False
Methods¶
__init__¶
Overload 1¶
static
Initializes an empty NDArrayShape.
Returns:
Self
Overload 2¶
static
Initializes the NDArrayShape from an IndexBuffer.
Args:
buf(IndexBuffer)[var]: The IndexBuffer to initialize from.
Returns:
Self
Overload 3¶
static
Initializes the NDArrayShape with variable shape dimensions.
Args:
*shape(Int): Variable number of integers representing the shape dimensions.self(Self)[out]
Returns:
Self
Raises
Error: If any shape dimension is not positive.
Overload 4¶
static
Initializes the NDArrayShape with a list of shape dimensions.
Args:
shape(List): A list of integers representing the shape dimensions.self(Self)[out]
Returns:
Self
Raises
Error: If the number of dimensions is not positive.
Error: If any shape dimension is not positive.
Overload 5¶
static
Initializes the NDArrayShape with a list of shape dimensions.
Args:
shape(VariadicList): A variadic list of integers representing the shape dimensions.self(Self)[out]
Returns:
Self
Raises
Error: If the number of dimensions is not positive.
Error: If any shape dimension is not positive.
Overload 6¶
static
Initializes the NDArrayShape from another NDArrayShape. A deep copy of the data buffer is conducted.
Args:
shape(Self): Another NDArrayShape to initialize from.
Returns:
Self
Overload 7¶
static
Construct NDArrayShape with number of dimensions. This method is useful when you want to create a shape with given ndim without knowing the shape values. ndim == 0 is allowed in this method for 0darray (numojo scalar).
Note: After creating the shape with uninitialized values, you must set the values before using it! Otherwise, it may lead to undefined behavior.
Args:
ndim(Int): Number of dimensions.initialized(Bool): Whether the shape is initialized. If yes, the values will be set to 1. If no, the values will be uninitialized.self(Self)[out]
Returns:
Self
Raises
Error: If the number of dimensions is negative.
Overload 8¶
static
Initializes the NDArrayShape from ancopy NDArrayShape. A deep copy of the data buffer is conducted.
Args:
copy(Self): Ancopy NDArrayShape to initialize from.
Returns:
Self
__getitem__¶
Overload 1¶
Gets shape dimension at specified index.
Args:
self(Self)index(Int): Index to get the shape.
Returns:
Int
Raises
Error: Index out of bound.
Overload 2¶
Gets shape dimension at specified index.
Args:
self(Self)index(Scalar): Index to get the shape.
Returns:
Scalar
Raises
Overload 3¶
Return a sliced view of the dimension tuple as a new NDArrayShape.
Args:
self(Self)slice_index(Slice): Slice object defining the sub-buffer.
Returns:
Self
Raises
__setitem__¶
Overload 1¶
Sets shape at specified index.
Args:
self(Self)[mut]index(Scalar): Index to set the shape.val(Scalar): Value to set at the given index.
Raises
Error: Index out of bound.
Overload 2¶
Sets shape at specified index.
Args:
self(Self)[mut]index(Int): Index to set the shape.val(Int): Value to set at the given index.
Raises
Error: Index out of bound.
__eq__¶
Checks if two shapes have identical dimensions and values.
Args:
self(Self)other(Self): The shape to compare with.
Returns:
Bool
__ne__¶
Checks if two shapes have identical dimensions and values.
Args:
self(Self)other(Self): The shape to compare with.
Returns:
Bool
__contains__¶
Overload 1¶
Checks if the given value is present in the shape dimensions.
Args:
self(Self)val(Int): The value to search for.
Returns:
Bool
Overload 2¶
Check if the NDArrayShape contains the given value.
Args:
self(Self)val(Scalar): Value to check for.
Returns:
Bool
load¶
Load a SIMD vector from the Shape at the specified index.
Parameters:
width(Int): The width of the SIMD vector.
Args:
self(Self)idx(Int): The starting index to load from.
Returns:
SIMD
Raises
Error: If the load exceeds the bounds of the Shape.
store¶
Store a SIMD vector into the Shape at the specified index.
Parameters:
width(Int): The width of the SIMD vector.
Args:
self(Self)idx(Int): The starting index to store to.value(SIMD): The SIMD vector to store.
Raises
Error: If the store exceeds the bounds of the Shape.
unsafe_load¶
Unsafely load a SIMD vector from the Shape at the specified index.
Parameters:
width(Int): The width of the SIMD vector.
Args:
self(Self)idx(Int): The starting index to load from.
Returns:
SIMD
unsafe_store¶
Unsafely store a SIMD vector into the Shape at the specified index.
Parameters:
width(Int): The width of the SIMD vector.
Args:
self(Self)idx(Int): The starting index to store to.value(SIMD): The SIMD vector to store.
row_major¶
Create row-major (C-style) strides from a shape.
Row-major means the last dimension has stride 1 and strides increase going backwards through dimensions.
Args:
self(Self)
Returns:
NDArrayStrides
Raises
col_major¶
Create column-major (Fortran-style) strides from a shape.
Column-major means the first dimension has stride 1 and strides increase going forward through dimensions.
Args:
self(Self)
Returns:
NDArrayStrides
Raises
permute¶
Return a new shape with axes reordered.
Args:
self(Self)axes(List): New axis order. Must contain each axis exactly once.
Returns:
Self
Raises
Error: If axes length doesn't match ndim or contains invalid/duplicate axes.
join¶
Join multiple shapes into a single shape.
Args:
self(Self)*shapes(Self): Variable number of NDArrayShape objects.
Returns:
Self
swapaxes¶
Returns a new shape with the given axes swapped.
Args:
self(Self)axis1(Int): The first axis to swap.axis2(Int): The second axis to swap.
Returns:
Self
Raises
extend¶
Extend the shape by sizes of extended dimensions.
Args:
self(Self)*values(Int): Sizes of extended dimensions.
Returns:
Self
move_axis_to_end¶
Returns a new shape by moving the value of axis to the end.
Args:
self(Self)axis(Int): The axis (index) to move.
Returns:
Self
pop¶
Drops the item at the given axis (index).
Args:
self(Self)axis(Int): The axis (index) to drop.
Returns:
Self
Raises
__len__¶
Gets number of elements in the shape. It equals the number of dimensions of the array.
Args:
self(Self)
Returns:
Int
write_to¶
Writes the shape representation to a writer.
Parameters:
W(Writer)
Args:
self(Self)writer(W)[mut]
normalize_index¶
Normalizes the given index to be within the valid range [0, ndim).
Args:
self(Self)index(Int): The index to normalize.
Returns:
Int
__iter__¶
Iterate over elements of the NDArrayShape, returning copied values.
Args:
self(Self)[ref]
Returns:
_ShapeIter
__reversed__¶
Iterate over elements of the NDArrayShape in reverse order, returning copied values.
Args:
self(Self)[ref]
Returns:
_ShapeIter