Re: [Numpy-discussion] What is the pythonic way to write a function that handles arrays and scalars?

2017-12-12 Thread Stephan Hoyer
On Tue, Dec 12, 2017 at 6:20 PM Marten van Kerkwijk < m.h.vankerkw...@gmail.com> wrote: > The real magic happens when you ducktype, and ensure your function > works both for arrays and scalars on its own. This is more often > possible than you might think! Sadly, this still doesn't work in a

Re: [Numpy-discussion] What is the pythonic way to write a function that handles arrays and scalars?

2017-12-12 Thread Mark Campanelli
The different approaches and corresponding "code bloat" here is one of the most annoying things I have found about using numpy+scipy. Furthermore, the flip side to the handling of the inputs, including both type and shape, is getting the output to match the input, including both type and shape.

Re: [Numpy-discussion] What is the pythonic way to write a function that handles arrays and scalars?

2017-12-12 Thread Robert Kern
On Wed, Dec 13, 2017 at 4:50 AM, Joe wrote: > > Hi, > > the best example I found was this one: > > https://stackoverflow.com/a/29319864/7919597 > > def func_for_scalars_or_vectors(x): > x = np.asarray(x) > scalar_input = False > if x.ndim == 0: > x =

[Numpy-discussion] What is the pythonic way to write a function that handles arrays and scalars?

2017-12-12 Thread Joe
Hi, the best example I found was this one: https://stackoverflow.com/a/29319864/7919597 def func_for_scalars_or_vectors(x): x = np.asarray(x) scalar_input = False if x.ndim == 0: x = x[None] # Makes x 1D scalar_input = True # The magic happens here if