Skip to content

numojo.core.layout.ndstrides

NDArrayStrides (numojo.core.layout.ndstrides)

Implements NDArrayStrides type. NDArrayStrides represents the strides of an NDArray, which is used to calculate the memory offset for each dimension when indexing into the array.

Structs

NDArrayStrides

struct NDArrayStrides

Memory convention: register_passable
Implements: AnyType, Copyable, Equatable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, Representable, Sized, Stringable, Writable

Presents the strides of NDArray type.

The data buffer of the NDArrayStrides is a series of Int on memory. The number of elements in the strides must be positive. The number of dimension is checked upon creation of the strides.

Fields

  • ndim (Int): Number of dimensions of array. It must be larger than 0.

Aliases

element_type
comptime element_type

Value: DType.int

The data type of the NDArrayStrides elements.

__del__is_trivial
comptime __del__is_trivial

Value: False

__move_ctor_is_trivial
comptime __move_ctor_is_trivial

Value: True

__copy_ctor_is_trivial
comptime __copy_ctor_is_trivial

Value: False

Methods

__init__
Overload 1
__init__() -> Self

static

Initializes an empty NDArrayStrides.

Returns:

  • Self
Overload 2
__init__(buf: IndexBuffer) -> Self

static

Initializes the NDArrayStrides from an IndexBuffer.

Args:

  • buf (IndexBuffer): The IndexBuffer to initialize from.

Returns:

  • Self
Overload 3
__init__(out self, *strides: Int)

static

Initializes the NDArrayStrides from strides.

Args:

  • *strides (Int): Strides of the array.
  • self (Self) [out]

Returns:

  • Self

Raises

Error: If the number of dimensions is not positive.

Overload 4
__init__(out self, strides: List[Int])

static

Initializes the NDArrayStrides from a list of strides.

Args:

  • strides (List): Strides of the array.
  • self (Self) [out]

Returns:

  • Self

Raises

Error: If the number of dimensions is not positive.

Overload 5
__init__(out self, strides: VariadicList[Int])

static

Initializes the NDArrayStrides from a variadic list of strides.

Args:

  • strides (VariadicList): Strides of the array.
  • self (Self) [out]

Returns:

  • Self

Raises

Error: If the number of dimensions is not positive.

Overload 6
__init__(strides: Self) -> Self

static

Initializes the NDArrayStrides from another strides. A deep-copy of the elements is conducted.

Args:

  • strides (Self): Strides of the array.

Returns:

  • Self
Overload 7
__init__(out self, shape: NDArrayShape, order: String = "C")

static

Initializes the NDArrayStrides from a shape and an order.

Args:

  • shape (NDArrayShape): Shape of the array.
  • order (String): Order of the memory layout (row-major "C" or column-major "F"). Default is "C".
  • self (Self) [out]

Returns:

  • Self

Raises

ValueError: If the order argument is not C or F.

Overload 8
__init__(out self, *shape: Int, *, order: String)

static

Overloads the function __init__(shape: NDArrayStrides, order: String). Initializes the NDArrayStrides from a given shapes and an order.

Args:

  • *shape (Int): Shape of the array.
  • order (String): Order of the memory layout (row-major "C" or column-major "F").
  • self (Self) [out]

Returns:

  • Self

Raises

ValueError: If the order argument is not C or F.

Overload 9
__init__(out self, shape: List[Int], order: String = "C")

static

Overloads the function __init__(shape: NDArrayStrides, order: String). Initializes the NDArrayStrides from a given shapes and an order.

Args:

  • shape (List): Shape of the array.
  • order (String): Order of the memory layout (row-major "C" or column-major "F").
  • self (Self) [out]

Returns:

  • Self

Raises

ValueError: If the order argument is not C or F.

Overload 10
__init__(out self, shape: VariadicList[Int], order: String = "C")

static

Overloads the function __init__(shape: NDArrayStrides, order: String). Initializes the NDArrayStrides from a given shapes and an order.

Args:

  • shape (VariadicList): Shape of the array.
  • order (String): Order of the memory layout (row-major "C" or column-major "F").
  • self (Self) [out]

Returns:

  • Self

Raises

ValueError: If the order argument is not C or F.

Overload 11
__init__(out self, *, ndim: Int, initialized: Bool)

static

Construct NDArrayStrides with number of dimensions. This method is useful when you want to create a strides with given ndim without knowing the strides values. ndim == 0 is allowed in this method for 0darray (numojo scalar).

Args:

  • ndim (Int): Number of dimensions.
  • initialized (Bool): Whether the strides is initialized. If yes, the values will be set to 0. If no, the values will be uninitialized.
  • self (Self) [out]

Returns:

  • Self

Raises

Error: If the number of dimensions is negative.

Overload 12
__init__(*, copy: Self) -> Self

static

Initializes the NDArrayStrides from ancopy strides. A deep-copy of the elements is conducted.

Args:

  • copy (Self): Strides of the array.

Returns:

  • Self
__getitem__
Overload 1
__getitem__(self, index: Int) -> Int

Gets stride at specified index.

Args:

  • self (Self)
  • index (Int): Index to get the stride.

Returns:

  • Int

Raises

Overload 2
__getitem__(self, index: Scalar[DType.int]) -> Scalar[DType.int]

Gets stride at specified index.

Args:

  • self (Self)
  • index (Scalar): Index to get the shape.

Returns:

  • Scalar

Raises

Overload 3
__getitem__(self, slice_index: Slice) -> Self

Return a sliced view of the strides as a new NDArrayStrides.

Args:

  • self (Self)
  • slice_index (Slice): Slice object defining the sub-buffer.

Returns:

  • Self

Raises

__setitem__
Overload 1
__setitem__(mut self, index: Scalar[DType.int], val: Scalar[DType.int])

Sets stride at specified index.

Args:

  • self (Self) [mut]
  • index (Scalar): Index to set the stride.
  • val (Scalar): Value to set at the given index.

Raises

Error: Index out of bound.

Overload 2
__setitem__(mut self, index: Int, val: Int)

Sets stride 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__
__eq__(self, other: Self) -> Bool

Checks if two strides have identical dimensions and values.

Args:

  • self (Self)
  • other (Self): The strides to compare with.

Returns:

  • Bool
__ne__
__ne__(self, other: Self) -> Bool

Checks if two strides have identical dimensions and values.

Args:

  • self (Self)
  • other (Self): The strides to compare with.

Returns:

  • Bool
__contains__
Overload 1
__contains__(self, val: Int) -> Bool

Checks if the given value is present in the strides.

Args:

  • self (Self)
  • val (Int): The value to search for.

Returns:

  • Bool
Overload 2
__contains__(self, val: Scalar[DType.int]) -> Bool

Check if the NDArrayStrides contains the given value.

Args:

  • self (Self)
  • val (Scalar): Value to check for.

Returns:

  • Bool
load
load[width: Int = 1](self, idx: Int) -> SIMD[DType.int, width]

Load a SIMD vector from the Strides 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 Strides.

store
store[width: Int = 1](self, idx: Int, value: SIMD[DType.int, width])

Store a SIMD vector into the Strides 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 Strides.

unsafe_load
unsafe_load[width: Int = 1](self, idx: Int) -> SIMD[DType.int, width]

Unsafely load a SIMD vector from the Strides 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
unsafe_store[width: Int = 1](self, idx: Int, value: SIMD[DType.int, width])

Unsafely store a SIMD vector into the Strides 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.
permute
permute(self, axes: List[Int]) -> Self

Return new strides 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.

swapaxes
swapaxes(self, axis1: Int, axis2: Int) -> Self

Returns a new strides 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

join
join(self, *strides: Self) -> Self

Join multiple strides into a single strides.

Args:

  • self (Self)
  • *strides (Self): Variable number of NDArrayStrides objects.

Returns:

  • Self
extend
extend(self, *values: Int) -> Self

Extend the shape by sizes of extended dimensions.

Args:

  • self (Self)
  • *values (Int): Sizes of extended dimensions.

Returns:

  • Self
flip
flip(mut self)

Flip the items in-place.

Args:

  • self (Self) [mut]
flipped
flipped(self) -> Self

Returns a new strides by flipping the items.

Args:

  • self (Self)

Returns:

  • Self
move_axis_to_end
move_axis_to_end(self, axis: Int) -> Self

Returns a new strides by moving the value of axis to the end.

Args:

  • self (Self)
  • axis (Int): The axis (index) to move.

Returns:

  • Self
pop
pop(self, axis: Int) -> Self

Drops information of certain axis.

Args:

  • self (Self)
  • axis (Int): The axis (index) to drop.

Returns:

  • Self

Raises

is_contiguous
is_contiguous(self, shape: NDArrayShape) -> Bool

Check if strides represent a contiguous layout for the shape.

Args:

  • self (Self)
  • shape (NDArrayShape): The shape of the array.

Returns:

  • Bool

Raises

__len__
__len__(self) -> Int

Gets number of elements in the strides. It equals to the number of dimensions of the array.

Args:

  • self (Self)

Returns:

  • Int
__repr__
__repr__(self) -> String

Returns a string of the strides of the array.

Args:

  • self (Self)

Returns:

  • String
__str__
__str__(self) -> String

Returns a string of the strides of the array.

Args:

  • self (Self)

Returns:

  • String
write_to
write_to[W: Writer](self, mut writer: W)

Writes the strides representation to a writer.

Parameters:

  • W (Writer)

Args:

  • self (Self)
  • writer (W) [mut]
row_major
row_major(shape: NDArrayShape) -> Self

static

Create row-major (C-style) strides from a shape.

Args:

  • shape (NDArrayShape): The shape of the array.

Returns:

  • Self

Raises

col_major
col_major(shape: NDArrayShape) -> Self

static

Create column-major (Fortran-style) strides from a shape.

Args:

  • shape (NDArrayShape): The shape of the array.

Returns:

  • Self

Raises

default
default(shape: NDArrayShape) -> Self

static

Create default (row-major) strides from a shape.

Args:

  • shape (NDArrayShape): The shape of the array.

Returns:

  • Self

Raises

tolist
tolist(self) -> List[Int]

Convert the strides to a list of integers.

Args:

  • self (Self)

Returns:

  • List
normalize_index
normalize_index(self, index: Int) -> Int

Normalizes the given index to be within the valid range [0, ndim).

Args:

  • self (Self)
  • index (Int): The index to normalize.

Returns:

  • Int
__iter__
__iter__(ref self) -> _StrideIter[origin_of(self)]

Iterate over elements of the NDArrayStrides, returning copied values.

Args:

  • self (Self) [ref]

Returns:

  • _StrideIter
__reversed__
__reversed__(ref self) -> _StrideIter[origin_of(self), False]

Iterate over elements of the NDArrayStrides, returning copied values.

Args:

  • self (Self) [ref]

Returns:

  • _StrideIter