8000 ENH: Add dtype parameter to linspace and logspace functions. by jjhelmus · Pull Request #3482 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add dtype parameter to linspace and logspace functions. #3482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/1.9.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ functions (ufuncs), ``numpy.core._dotblas.dot``, and
``numpy.core.multiarray.dot`` (the numpy.dot functions). By defining a
``__numpy_ufunc__`` method.

Dtype parameter added to `np.linspace` and `np.logspace`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The returned data type from the `linspace` and `logspace` functions
can now be specificed using the dtype parameter.

Improvements
============
Expand All @@ -39,6 +43,7 @@ Improvements
Changes
=======


C-API
~~~~~

Expand Down
36 changes: 25 additions & 11 deletions numpy/core/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
__all__ = ['logspace', 'linspace']

from . import numeric as _nx
from .numeric import array
from .numeric import array, result_type

def linspace(start, stop, num=50, endpoint=True, retstep=False):

def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
"""
Return evenly spaced numbers over a specified interval.

Expand All @@ -31,6 +32,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False):
retstep : bool, optional
If True, return (`samples`, `step`), where `step` is the spacing
between samples.
dtype : dtype
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.

Returns
-------
Expand Down Expand Up @@ -74,23 +78,28 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False):

"""
num = int(num)

if dtype is None:
dtype = result_type(start, stop, float(num))

if num <= 0:
return array([], float)
return array([], dtype)
if endpoint:
if num == 1:
return array([float(start)])
return array([start], dtype=dtype)
step = (stop-start)/float((num-1))
y = _nx.arange(0, num) * step + start
y = _nx.arange(0, num, dtype=dtype) * step + start
y[-1] = stop
else:
step = (stop-start)/float(num)
y = _nx.arange(0, num) * step + start
y = _nx.arange(0, num, dtype=dtype) * step + start
if retstep:
return y, step
return y.astype(dtype), step
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After double checking, this seems not quite right. dtype(None) == np.float64 in other words, this casts to float no matter what the input is if None is given, which disagrees with the documentation.

else:
return y
return y.astype(dtype)


def logspace(start,stop,num=50,endpoint=True,base=10.0):
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None):
"""
Return numbers spaced evenly on a log scale.

Expand All @@ -116,6 +125,9 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0):
The base of the log space. The step size between the elements in
``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
Default is 10.0.
dtype : dtype
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.

Returns
-------
Expand All @@ -136,7 +148,7 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0):

>>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
... # doctest: +SKIP
>>> power(base, y)
>>> power(base, y).astype(dtype)
... # doctest: +SKIP

Examples
Expand Down Expand Up @@ -165,4 +177,6 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0):

"""
y = linspace(start, stop, num=num, endpoint=endpoint)
return _nx.power(base, y)
if dtype is None:
return _nx.power(base, y)
return _nx.power(base, y).astype(dtype)
30 changes: 25 additions & 5 deletions numpy/core/tests/test_function_base.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
from __future__ import division, absolute_import, print_function

from numpy.testing import *
from numpy import logspace, linspace
from numpy import logspace, linspace, dtype


class TestLogspace(TestCase):

def test_basic(self):
y = logspace(0, 6)
assert_(len(y)==50)
assert_(len(y) == 50)
y = logspace(0, 6, num=100)
assert_(y[-1] == 10**6)
assert_(y[-1] == 10 ** 6)
y = logspace(0, 6, endpoint=0)
assert_(y[-1] < 10**6)
assert_(y[-1] < 10 ** 6)
y = logspace(0, 6, num=7)
assert_array_equal(y, [1, 10, 100, 1e3, 1e4, 1e5, 1e6])

def test_dtype(self):
y = logspace(0, 6, dtype='float32')
assert_equal(y.dtype, dtype('float32'))
y = logspace(0, 6, dtype='float64')
assert_equal(y.dtype, dtype('float64'))
y = logspace(0, 6, dtype='int32')
assert_equal(y.dtype, dtype('int32'))


class TestLinspace(TestCase):

def test_basic(self):
y = linspace(0, 10)
assert_(len(y)==50)
assert_(len(y) == 50)
y = linspace(2, 10, num=100)
assert_(y[-1] == 10)
y = linspace(2, 10, endpoint=0)
Expand All @@ -35,3 +47,11 @@ def test_type(self):
t3 = linspace(0, 1, 2).dtype
assert_equal(t1, t2)
assert_equal(t2, t3)

def test_dtype(self):
y = linspace(0, 6, dtype='float32')
assert_equal(y.dtype, dtype('float32'))
y = linspace(0, 6, dtype='float64')
assert_equal(y.dtype, dtype('float64'))
y = linspace(0, 6, dtype='int32')
assert_equal(y.dtype, dtype('int32'))
0