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?

Thanks,
Neal
_______________________________________________
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