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 typ

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 5:07 PM Mark Campanelli wrote: > I think I saw some other discussion recently about numpy joining forces > with Python 3's gradual type system. Is there any draft formal proposal for > this yet? If numpy+scipy wants to scale to "bigger" projects, I think it > behooves the

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

2017-12-12 Thread Marten van Kerkwijk
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! If you really need, e.g., the shape, you can do `getattr(input, 'shape', ())` and things may well work for scalars, and also for objects

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. Get

[Numpy-discussion] NEP process PR

2017-12-12 Thread Jarrod Millman
Hi all, I've started working on the proposal discussed in this thread: https://mail.python.org/pipermail/numpy-discussion/2017-December/077481.html here: https://github.com/numpy/numpy/pull/10213 You can see how I modified PEP 1 here: https://github.com/numpy/numpy/pull/10213/commits/eaf78

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 5:52 AM, Eric Wieser wrote: > Using np.iscalar is a bad idea, as it fails for 0d arrays. x.ndim is the > better option there. > > I’d maybe suggest not special-casing 0d arrays though, and using: > > def func_for_scalars_or_vectors(x): > x = np.asanyarray(x) # convert

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

2017-12-12 Thread Eric Wieser
Using np.iscalar is a bad idea, as it fails for 0d arrays. x.ndim is the better option there. I’d maybe suggest not special-casing 0d arrays though, and using: def func_for_scalars_or_vectors(x): x = np.asanyarray(x) # convert scalars to 0d arrays # The magic happens here return ret

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

2017-12-12 Thread Kirill Balunov
Oh, sorry for noise... With kind regards, -gdg On Dec 12, 2017 23:05, "Robert Kern" wrote: > On Wed, Dec 13, 2017 at 5:00 AM, Kirill Balunov > wrote: > > > > On minor thing that instead of 'ret' there should be 'x'. > > No, `x` is the input. The code that actually does the computation (elided

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 5:00 AM, Kirill Balunov wrote: > > On minor thing that instead of 'ret' there should be 'x'. No, `x` is the input. The code that actually does the computation (elided here by the `# The magic happens here` comment) would have assigned to `ret`. -- Robert Kern

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

2017-12-12 Thread Kirill Balunov
On minor thing that instead of 'ret' there should be 'x'. With kind regards, -gdg On Dec 12, 2017 22:51, "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

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 = x[None] # Makes x 1D >

[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 scal

Re: [Numpy-discussion] Which rule makes x[np.newaxis, :] and x[np.newaxis] equivalent?

2017-12-12 Thread Sebastian Berg
On Tue, 2017-12-12 at 14:19 +0100, Joe wrote: > Ah, ok, now that I knew what to look for I guess I found it: > > "If the number of objects in the selection tuple is less than N , > then :  > is assumed for any subsequent dimensions." > > https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.in

Re: [Numpy-discussion] Which rule makes x[np.newaxis, :] and x[np.newaxis] equivalent?

2017-12-12 Thread Joe
Ah, ok, now that I knew what to look for I guess I found it: "If the number of objects in the selection tuple is less than N , then : is assumed for any subsequent dimensions." https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html This is the one, right? Am 12.12.2017 09:09

Re: [Numpy-discussion] Which rule makes x[np.newaxis, :] and x[np.newaxis] equivalent?

2017-12-12 Thread Nathaniel Smith
On Tue, Dec 12, 2017 at 12:02 AM, Joe wrote: > Hi, > > question says it all. I looked through the basic and advanced indexing, > but I could not find the rule that is applied to make > x[np.newaxis,:] and x[np.newaxis] the same. I think it's the general rule that all indexing expressions have an

[Numpy-discussion] Which rule makes x[np.newaxis,:] and x[np.newaxis] equivalent?

2017-12-12 Thread Joe
Hi, question says it all. I looked through the basic and advanced indexing, but I could not find the rule that is applied to make x[np.newaxis,:] and x[np.newaxis] the same. Kind regards, Joe ___ NumPy-Discussion mailing list NumPy-Discussion@python.o