Skip to main content
Log in

Mojo function

crd2idx

crd2idx(crd: IntTuple[origin], shape: IntTuple[origin]) -> 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[origin]): The coordinate tuple to convert.
  • shape (IntTuple[origin]): The shape of the tensor/array.

Returns:

The linear index corresponding to the coordinate.

crd2idx(crd: IntTuple[origin], shape: IntTuple[origin], _stride: IntTuple[origin]) -> 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.
- 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[origin]): The coordinate(s) to convert, can be a single value or a tuple of coordinates.
  • shape (IntTuple[origin]): The shape of the tensor/array, can be a single value or a tuple of dimensions.
  • _stride (IntTuple[origin]): Optional custom strides, defaults to row-major strides if not provided.

Returns:

The linear index corresponding to the coordinate.