[Numpy-discussion] Allowing broadcasting of code dimensions in generalized ufuncs
Marten van Kerkwijk
m.h.vankerkwijk at gmail.com
Wed May 30 14:14:13 EDT 2018
Hi All,
Following on a PR combining the ability to provide fixed and flexible
dimensions [1] (useful for, e.g., 3-vector input with a signature like
`(3),(3)->(3)`, and for `matmul`, resp.; based on earlier PRs by Jaime
[2] and Matt (Picus) [3]), I've now made a PR with a further
enhancement, which allows one can indicate that a core dimension can
be broadcast [4].
A particular use case is `all_equal`, a new function suggested in a
stalled PR by Matt (Harrigan) [5], which compares two arrays
axis-by-axis, but short-circuits if a non-equality is found (unlike
what is the case if one does `(a==b).all(axis)`). One thing that would
be obviously useful for a routine like `all_equal` is to be able to
provide an array as one argument and a constant as another, i.e., if
the core dimensions can be broadcast if needed, just like they are in
`(a==b).all(axis)`. This is currently not possible: with its signature
of `(n),(n)->()`, the two arrays have to have the same trailing size.
My PR provides the ability to indicate in the signature that a core
dimension can be broadcast, by using a suffix of "|1". Thus, the
signature of `all_equal` would become:
```
(n|1),(n|1)->()
```
Comments most welcome (yes, even on the notation - though I think it
is fairly self-explanatory)!
Marten
p.s. There are some similarities to the new "flexible" dimensions
implemented for `matmul` [1], but also differences. In particular, for
a signature of `(n?),(n?)->()`, one could also pass in an array of
trailing size n and a constant, but it would not be possible to pass
in an array with trailing size 1: the dimensions with the same name
have to be either present and the same or absent. In contrast, for
broadcasting, dimensions with the same name can have trailing size n,
size 1, or be absent (in which case they count as 1). For
broadcasting, any output dimensions with the same name are never
affected, while for flexible dimensions those are removed.
[1] https://github.com/numpy/numpy/pull/11175
[2] https://github.com/numpy/numpy/pull/5015
[3] https://github.com/numpy/numpy/pull/11132
[4] https://github.com/numpy/numpy/pull/11179
[5] https://github.com/numpy/numpy/pull/8528
More information about the NumPy-Discussion
mailing list