IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo function

idx2crd

def idx2crd[idx_t: IntTuple, shape_t: IntTuple, stride_t: IntTuple](idx: RuntimeTuple[idx_t, element_type=idx.element_type], shape: RuntimeTuple[shape_t, element_type=shape.element_type], stride: RuntimeTuple[stride_t, element_type=stride.element_type]) -> RuntimeTuple[idx2crd(idx_t, shape_t, stride_t), element_type=shape.element_type]

Converts a linear index to multi-dimensional coordinates. This function transforms a flat index into coordinate values based on the provided shape and stride information. This is essential for mapping linear memory accesses to multi-dimensional tensor elements.

Constraints:

The index must be a scalar value (not a tuple).

Parameters:

  • ​idx_t (IntTuple): IntTuple type of the index.
  • ​shape_t (IntTuple): IntTuple type of the shape.
  • ​stride_t (IntTuple): IntTuple type of the stride.

Args:

Returns:

RuntimeTuple[idx2crd(idx_t, shape_t, stride_t), element_type=shape.element_type]: A RuntimeTuple containing the multi-dimensional coordinates.

def idx2crd[idx_t: IntTuple, shape_t: IntTuple](idx: RuntimeTuple[idx_t, element_type=idx.element_type], shape: RuntimeTuple[shape_t, element_type=shape.element_type]) -> RuntimeTuple[idx2crd(idx_t, shape_t, prefix_product(shape_t)), element_type=shape.element_type]

Converts a linear index to multi-dimensional coordinates using shape-derived strides. This is a convenience overload of idx2crd that automatically calculates the stride values from the shape using prefix_product. This is the common case for row-major storage order tensors.

Parameters:

  • ​idx_t (IntTuple): IntTuple type of the index.
  • ​shape_t (IntTuple): IntTuple type of the shape.

Args:

Returns:

RuntimeTuple[idx2crd(idx_t, shape_t, prefix_product(shape_t)), element_type=shape.element_type]: A RuntimeTuple containing the multi-dimensional coordinates calculated using automatically derived strides from the shape.

Was this page helpful?