[Numpy-discussion] Changes to generalized ufunc core dimension checking
Travis Oliphant
travis at continuum.io
Wed Mar 16 15:48:32 EDT 2016
On Wed, Mar 16, 2016 at 12:55 PM, Nathaniel Smith <njs at pobox.com> wrote:
> Hi Travis,
>
> On Mar 16, 2016 9:52 AM, "Travis Oliphant" <travis at continuum.io> wrote:
> >
> > Hi everyone,
> >
> > Can you help me understand why the stricter changes to generalized ufunc
> argument checking no now longer allows scalars to be interpreted as 1-d
> arrays in the core-dimensions?
> >
> > Is there a way to specify in the core-signature that scalars should be
> allowed and interpreted in those cases as an array with all the elements
> the same? This seems like an important feature.
>
> Can you share some example of when this is useful?
>
Being able to implicitly broadcast scalars to arrays is the core-function
of broadcasting. This is still very useful when you have a core-kernel
an want to pass in a scalar for many of the arguments. It seems that at
least in that case, automatic broadcasting should be allowed --- as it
seems clear what is meant.
While you can use the broadcast* features to get the same effect with the
current code-base, this is not intuitive to a user who is used to having
scalars interpreted as arrays in other NumPy operations.
It used to automatically happen and a few people depended on it in several
companies and so the 1.10 release broke their code.
I can appreciate that in the general case, allowing arbitrary broadcasting
on the internal core dimensions can create confusion. But, scalar
broadcasting still makes sense.
A better workaround would be to use one of the np.broadcast* functions to
> request exactly the broadcasting you want and make an arr2-sized view of
> the scalar. In this case where you presumably (?) want to allow the last
> two arguments to be broadcast against each other arbitrarily:
>
> arr2, arr3 = np.broadcast_arrays(arr2, scalar)
> myufunc(arr1, arr2, arr3)
>
> A little wordier than implicit broadcasting, but not as bad as manually
> creating an array, and like implicit broadcasting the memory overhead is
> O(1) instead of O(size).
>
Thanks for the pointer (after I wrote the email this solution also occured
to me). I think adding back automatic broadcasting for the scalar
case makes a lot of sense as well, however. What do people think of that?
Also adding this example to the documentation as a work-around for people
whose code breaks with the new changes.
Thanks,
-Travis
> -n
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
*Travis Oliphant, PhD*
*Co-founder and CEO*
@teoliphant
512-222-5440
http://www.continuum.io
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160316/b7674f7f/attachment.html>
More information about the NumPy-Discussion
mailing list