Implementing array broadcasting
Bradley Lucier 10 Sep 2024 15:44 UTC
Implementing Python array broadcasting is trivial in some sense. All
Python arrays have lower bounds 0.
Assuming that the array indexing function satisfies:
1. It has a representation as a base (integer) and a vector of strides
(integers).
2. Strides for axes of width 1 are 0.
then it's trivial to implement the representation of the indexer for a
broadcast array:
1. For any axes added to the left, add the corresponding number of zero
strides to the stride vector.
2. The base remains the same.
3. Adjust the upper bounds of existing axes with width 1, and add new
upper bounds (if necessary) to the left of the existing axes.
I suppose that having nonzero lower bounds doesn't change much: for
existing axes of width 1, you adjust the lower bound for the new array
domain, and you add new lower bounds on the left as needed. I do wonder
whether you want the one-dimensional interval defined by the i'th lower
and upper bounds of the result array to contain the one-dimensional
interval defined by the i'th lower and upper bounds of the array
argument. (That's automatic for zero-based Python arrays.)
I don't know yet how broadcasting interacts with array reshaping and
other array operations.
But I do think it should be applied only to immutable arrays (more on
that in the next email).
Brad