1

I'm trying to use numpy-like syntax on an awkward.Array with variable sizes in the second dimension, but it's still confusing..

In numpy i have

normals.shape # (N,3)
idcs.shape    # (m,k)
normals[idcs].shape # (m,k,3)

So the entries of the idcs 2d-array are obviously used as indices for the first dimension of my normals 2d-array and keep that shape.

If I try the same with an awkward.Array, it won't perform that numpy-trick of 'linear-indexing' or 'broadcasting' (whatever you may call it)

# awkward.Arrays here:
normals # N * 3 
idcs    # m * var <- variable sizes in second dimension
normals[idcs] # ERROR
# what I expected instead:
normals[idcs] # m * var * 3

EDIT: what I also thought about, is to reshape idcs to one long index vector, use it to index the normals 2d-array like normals[idcs_vector] and then reshape the result back to m * var * 3 by something like normals[idcs_vector].reshape((m,-1,3)), but since awkward hasn't implemented a reshape function, this is not easy

3
  • You might be running into the fact that broadcasting works differently for variable-length dimensions and fixed-length dimensions. (It depends on variable versus fixed type, not whether the number of items happens to be constant.) See awkward-array.org/doc/main/user-guide/… and awkward-array.org/doc/main/reference/generated/… for "left broadcasting" versus "right broadcasting." With fixed-length dimensions, NumPy could have chosen either method, but with variable dimensions, there's a natural choice (and NumPy's is "wrong"). Commented Jun 17, 2024 at 15:45
  • Reshaping doesn't have a natural definition with variable-length dimensions, either, but what you want to do may be equivalent to ak.flatten followed by ak.unflatten. To get the lengths of lists (second argument of ak.unflatten), there's ak.num. Commented Jun 17, 2024 at 15:47
  • 1
    @JimPivarski I'm aware of left and right broadcasting. I already looked into that, but it didn't really fit my problem, since multiplying entries isn't really what i was looking for. The ak.flatten and ak.unflatten on the other hand worked for me Commented Jun 17, 2024 at 17:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.