[Numpy-discussion] Old tickets
Derek Homeier
derek@astro.physik.uni-goettingen...
Wed Mar 30 16:52:56 CDT 2011
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)
so I think at least a**n should follow integer math rules; depends on
whether we want
np.power to behave differently from ** (if they are internally handled
separately at all)...
Not sure if I understand the overload suggestion in the ticket, but
maybe a solution
could be using the output argument (if an explicit optional dtype is
not an option):
>>> b = np.zeros(2, dtype=np.int32)
>>> np.power(np.arange(1,3),-2, b)
array([1, 0])
>>> b = np.zeros(2)
>>> np.power(np.arange(1,3),-2, b)
array([ 1., 0.])
^^
this could be changed to array([ 1., 0.25])
Cheers,
Derek
More information about the NumPy-Discussion
mailing list