Skip to content

NDArray type

Array initializing

Array initializing method, i.e., __init__ constructs a pure container by reading in the shape and the stride information. Although the memory is allocated, it is not initialized. Users should always construct arrays using functions in creation routines. For example, zeros, ones, randn, array, etc.

The following ways of array initializing are supported:

  1. NDArrayShape and order.
  2. List and order.
  3. VariadicList and order.

Array indexing and slicing

Array indexing

Array indexing always returns a scalar or an SIMD.

"safe" means whether boundary checks are made. "order" means whether it evaluate the order or directly gets value from the underlying buffer. If yes, the strides information will be considered. If no, the indexing will based on the continuous allocation of the data buffer.

method (overload)argsRetsafe?order?notes
_getitem*IntScalarnoyes
__getitem__IdxScalaryesyes
itemIntScalaryesyes[0, size)
item*IntScalaryesyes[0, shape[i])
loadIntScalaryesno
load[width]IntSIMDyesno
load[width]*IntSIMDyesno

Array slicing

Array slicing always returns an NDArray of the same number of dimensions or less. Currently, the returns are copies of the data. In future, when Mojo's trait are enhanced, slicing will returns a view of the data of the array.

method (overload)argssafe?order?notes
__getitem__Intyesyesi-th row of input
__getitem__List[Int]yesyesRows of input
__getitem__List[Slice]yesyesSame ndim as input
__getitem__*args[Slice]yesyesSame ndim as input
__getitem__*SliceyesyesSame ndim as input
__getitem__*Variant[Slice, Int]yesyesSame or smaller ndim
__getitem__NDArray[DType.bool]nonoItems of buffer
__getitem__NDArray[DType.index]nonoItems of buffer

Mojo Miji