On Tue, Mar 25, 2025 at 9:34 AM Shasang Thummar via NumPy-Discussion <
numpy-discussion@python.org> wrote:

> *Why Should This Be Allowed?*
>
> If *a larger dimension is an exact multiple of a smaller one*, then
> logically, the smaller array can be *repeated* along that axis *without
> physically copying data*—just like NumPy does when broadcasting (1,M) to
> (N,M).
>
> In the above example,
>
>    - A has shape (2,3), and B has shape (4,3).
>    - Since 4 is *a multiple of* 2 (4 % 2 == 0), A could be *logically
>    repeated 2 times* to become (4,3).
>    - NumPy *already does* a similar expansion when a dimension is 1, so
>    why not extend the same logic?
>
> It's not a particularly similar expansion. The reason broadcasting a
1-dimension works is that we just set the stride for the broadcasted
dimension to be 0, so the pointer never advances along that dimension and
just keeps pointing at the one value along that axis. You can't do the same
thing with tiling n>1. It would have to advance within the tile and then go
back. So, unlike current broadcasting, I don't think we can just adjust the
strides on each operand separately to match the broadcasted shape, which is
something the current machinery relies upon.

You could reshape `B` to be (2, 2, 3), broadcast as normal, operate, then
reshape the result back. I'm not sure if it's guaranteed in all cases that
the reshaping back could be done without copying, and I have no clear idea
of how this works in the ternary+ operator case. But even so, this is not a
straightforward extension of the broadcasting functionality and would not
just plug in to the rest of the broadcasting machinery.

I could be wrong; haven't put too much effort into it, but I think the
first step would be to walk through what the stride tricks actually would
be to get this operation to work. Then we can talk about whether or not
it's desirable to plug it in.

-- 
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to