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

crd2idx

def crd2idx(crd: IntTuple, shape: IntTuple) -> Int

Map a logical coordinate to a linear index.

This function converts a multi-dimensional coordinate to a linear index based on the shape. It uses default strides computed from the shape.

Args:

  • ​crd (IntTuple): The coordinate tuple to convert.
  • ​shape (IntTuple): The shape of the tensor/array.

Returns:

Int: The linear index corresponding to the coordinate.

def crd2idx(crd: IntTuple, shape: IntTuple, _stride: IntTuple) -> Int

Map a logical coordinate to a linear index with custom strides.

This function converts a multi-dimensional coordinate to a linear index based on the shape and stride information. If no stride is provided, it computes default strides from the shape.

The function handles various input combinations:

  • Tuple coordinates with tuple shapes and strides
  • Single integer coordinate with tuple shapes and strides
  • Single integer coordinate with single integer shape and stride

Aborts:

- If coordinate and shape dimensions don't match.
- If shape and stride dimensions don't match.
- If input type combinations are invalid.

Args:

  • ​crd (IntTuple): The coordinate(s) to convert, can be a single value or a tuple of coordinates.
  • ​shape (IntTuple): The shape of the tensor/array, can be a single value or a tuple of dimensions.
  • ​_stride (IntTuple): Optional custom strides, defaults to row-major strides if not provided.

Returns:

Int: The linear index corresponding to the coordinate.

Was this page helpful?