On Wed, Mar 22, 2023 at 9:34 AM Neal Becker <ndbeck...@gmail.com> wrote:
> I have a function F > def F(a, b): > c = a * b > > Initially, a is a scalar, b[240,3000]. No problem. > Later I want to use F, where a[240] is a vector. I want to allow both the > scalar and vector cases. So I write: > > def F(a,b): > a = np.atleast_1d(a) > c = a[:,None] * b > > This now works for scalar a or vector a. But this solutions seems > inelegant, and somewhat fragile. Suppose later we want to allow > a[240,3000], a 2d array matching b. > > Certainly don't want to write code like: > if a.ndim == 0:... > > Is there a more elegant/robust approach? > I would leave it as `c = a * b` and simply record in the docstring that `a` and `b` should be broadcastable. Yes, that means that the user will have to write `F(a[:, np.newaxis], b)` for that one case, and that looks a little ugly, but overall it's less cognitive load on the user to just reuse the common convention of broadcasting than to record the special case. -- 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