[Numpy-discussion] Old tickets

Charles R Harris charlesr.harris@gmail....
Thu Mar 31 10:48:43 CDT 2011


On Thu, Mar 31, 2011 at 9:00 AM, Ralf Gommers
<ralf.gommers@googlemail.com>wrote:

> On Wed, Mar 30, 2011 at 11:52 PM, Derek Homeier
> <derek@astro.physik.uni-goettingen.de> wrote:
> >
> > On 30 Mar 2011, at 23:26, Benjamin Root wrote:
> >
> >> Ticket 301: 'Make power and divide return floats from int inputs (like
> >> true_divide)'
> >> http://projects.scipy.org/numpy/ticket/301
> >> Invalid because the output dtype is the same as the input dtype unless
> >> you override using the dtype argument:
> >>  >>> np.power(3, 1, dtype=np.float128).dtype
> >> dtype('float128')
> >> Alternatively return a float and indicate in the docstring that the
> >> output dtype can be changed.
> >>
> >>
> >> FWIW,
> >>
> >> Just thought I'd note (on a python 2.6 system):
> >>
> >> >>> import numpy as np
> >> >>> a = np.array([1, 2, 3, 4])
> >> >>> a.dtype
> >> dtype('int32')
> >> >>> 2 / a
> >> array([2, 1, 0, 0])
> >> >>> from __future__ import division
> >> >>> 2 / a
> >> array([ 2.        ,  1.        ,  0.66666667,  0.5       ])
> >>
> >> So, numpy already does this when true division is imported (and
> >> therefore consistent with whatever the python environment does), and
> >> python currently also returns integers for exponentials when both
> >> inputs are integers.
> >
> > I'd agree, and in my view power(3, -1) is well defined as 1 / 3 -
> > also, in future (or Python3)
> >
> >  >>> a/2
> > array([ 0.5,  1. ,  1.5,  2. ])
> >  >>> a//2
> > array([0, 1, 1, 2], dtype=int32)
>
> The ticket is about the functions np.divide and np.power, not / and
> **. This currently does not work the same, unlike what's said above:
>
> >>> from __future__ import division
> >>> x = np.arange(4) + 1
> >>> 2 / x
> array([ 2.        ,  1.        ,  0.66666667,  0.5       ])
> >>> np.divide(2, x)
> array([2, 1, 0, 0])
>
> The power and divide functions should not check the "from __future__
> import division", just return floats IMHO. This is what's expected by
> most users, and much more useful than integer math


There is true_divide for that.

In [4]: np.true_divide(2, x)
Out[4]: array([ 2.        ,  1.        ,  0.66666667,  0.5       ])

true_divide is what is called by the '/' operator in Python3.

<snip>

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110331/597dbcc1/attachment-0001.html 


More information about the NumPy-Discussion mailing list