Skip to content

Add Idx value type for positions along an axis - #185

Open
marcelluethi wants to merge 1 commit into
dimwit-dev:mainfrom
marcelluethi:Idx-type
Open

marcelluethi wants to merge 1 commit into
dimwit-dev:mainfrom
marcelluethi:Idx-type

Conversation

@marcelluethi

Copy link
Copy Markdown
Contributor

So far, indices were represented as ordinary Tensor[T, Int32] values . This PR introduces an explicit value type Idx[L], which marks a value as a position along the axis L and records that axis in its type. At runtime it is still an Int32. This adds type safety, as positions can only be used along their own axis, but more importantly it communicates the intent of argmax, argmin, argsort, Random.permutation and Categorical, much better.

Breaking changes:

  • The results to argmax, etc. are no longer Int32 use asInt32 for arithmetic on them.
  • Axis[L].at with a vector of indices requires Tensor1[L2, Idx[L]]

The existing TypedIndex (the per-element index used by fromFunction) meant something else and is renamed to ElementIndex to avoid confusion.

So far, indices were represented as ordinary int32 values. This introduces an explicit value type Idx[L], which marks a value as a position along the axis L and records that axis in its type (at runtime it is still an int32). This adds type safety, as positions can only be used along their own axis, but more importantly it communicates the intent of argmax, argmin, argsort,
Random.permutation and Categorical, which now return Idx.

Breaking changes:
- These results are no longer Int32; use asInt32 for arithmetic on them.
- Axis[L].at with a vector of indices requires Tensor1[L2, Idx[L]]; convert
  plain integers with Idx.fromInt32(Axis[L]).

The existing TypedIndex (the per-element index used by fromFunction) meant
something else and is renamed to ElementIndex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant