nexus icon indicating copy to clipboard operation
nexus copied to clipboard

numpy-style indexing

Open kitsing opened this issue 7 years ago • 1 comments

Would be nice if it is possible to index tensors with other index-typed tensors.

kitsing avatar Feb 18 '19 02:02 kitsing

Notes here from @kitsing :

NumPy-style advanced indexing has form A[X1, X2, ..., Xk], where

  • the indexee A has k dims, here in Nexus typed with axes-descriptor (A1, A2, ..., Ak)
  • there must be exactly k indexers X1, ..., Xk, each must be of any of the 3 following forms:
    • an Int index: e.g. 2;
    • a range-style object, in the style of NumPy: : / 1:, :4;
    • another tensor of dims (B1, B2, ..., Bm).

@kitsing pointed out that advanced indexing is valid if and only if all indexers X1, ..., Xk can be promoted/broadcasted to the same shape (or in Nexus, same axes-type):

  • An Int index is assumed to be of type ();
  • A range object at the i-th position of the indexee A is assumed to be of type (Ai, );
  • Tensor indexers is typed as they originally are.

Hence we have

def advIdx[T[_], U, Vs]
  (indexee: T[U])(indexers: Vs)(implicit v: BroadcastN.Aux[Vs, V]): T[V]
// Vs is an HList of type descriptors
// e.g.
advIdx(T[(α, β, γ)])(T[(α)], T[(δ)], ?): T[(α, δ, γ)]

ctongfei avatar Feb 18 '19 03:02 ctongfei