Understanding `view`

The role of non-scalar getindex is to provide a structured way to repeatedly call the scalar getindex. We don’t think of it as a repeated access (it’s just a slice!), but it’s really just going through the passed arrays of indices and calling the scalar method at each index. That’s what broadcast does at its core, too — it repeatedly calls some scalar function across array(s) of arguments.

The huge advantage to using broadcast to represent nonscalar indexing is that it could fuse with other broadcasted operations. Forget the whole view/copy debate — fusion would be faster than either. For example, we could define the new syntax A.[I...] to mean broadcast((idxs...)->A[idxs...], I...). Then, to extract the first 10 elements from the first column of A, you write A.[1:10, 1]. This means that you could fuse in additional operations without any temporaries or views whatsoever with, e.g., sqrt.(A.[1:10, 1])

The other huge advantage is how this would then generalize these sorts of accesses to all data structures, including dictionaries and more.

There are key differences in how broadcast behaves vs. non-scalar indexing. The biggest of course being how multiple array arguments are combined: indexing takes the cartesian product of its arguments to form our dim-sum rule, whereas broadcasting combines dimensions. For example, A.[1:10, 1:10] would return the first ten elements on the diagonal, whereas A[1:10, 1:10] returns the whole 10x10 rectangle. You can think of nonscalar indexing as doing a broadcast where each successive argument is “lifted” to a dimension orthogonal to all previous arguments. I’ve thought about introducing the operator to represent this. E.g., f.(1:10, ⟂(1:10)) would evaluate f over the cartesian product. It would resolve much of my own abuse of x'.

There are other issues, though, including :s, logical indexing, fusion with index computations, bounds checking, and more. So that’s why it’s just an aside in my above post and not a reality. At least not yet. Broadcast Array Indexing · Issue #19169 · JuliaLang/julia · GitHub

4 Likes