diff options
author | jaimefrio <jaime.frio@gmail.com> | 2015-01-13 14:25:00 -0800 |
---|---|---|
committer | jaimefrio <jaime.frio@gmail.com> | 2015-01-13 15:04:06 -0800 |
commit | 2aab65415843c8a8c662ddb5d33536dc95671076 (patch) | |
tree | 07568c4258a3dc81320ad435329a55a4846bbbe9 /numpy/core/function_base.py | |
parent | ad238c1c3d5c136a61e795fc056373e0a1cbd8e1 (diff) | |
download | numpy-2aab65415843c8a8c662ddb5d33536dc95671076.tar.gz |
BUG: linspace should return the same as arange when equivalent
Fixes failures on other projects (scipy.signal) introduced by #5438
Diffstat (limited to 'numpy/core/function_base.py')
-rw-r--r-- | numpy/core/function_base.py | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 4361dc2e1..1e759e0c2 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -3,7 +3,7 @@ from __future__ import division, absolute_import, print_function __all__ = ['logspace', 'linspace'] from . import numeric as _nx -from .numeric import array, result_type +from .numeric import array, result_type, NaN def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): @@ -82,6 +82,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): """ num = int(num) + div = (num - 1) if endpoint else num # Convert float/complex array scalars to float, gh-3504 start = start * 1. @@ -91,38 +92,31 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): if dtype is None: dtype = dt - if num <= 0: - return array([], dtype) - if num == 1: - return array([start], dtype=dtype) y = _nx.arange(0, num, dtype=dt) - if endpoint: - num -= 1 - y /= num - y *= stop - start + + if num > 1: + delta = stop - start + step = delta / div + if step == 0: + # Special handling for denormal numbers, gh-5437 + y /= div + y *= delta + else: + y *= step + else: + # 0 and 1 item long sequences have an undefined step + step = NaN + y += start - if endpoint: + + if endpoint and num > 1: y[-1] = stop if retstep: - return y.astype(dtype, copy=False), (stop - start) / num + return y.astype(dtype, copy=False), step else: return y.astype(dtype, copy=False) - # if endpoint: - # if num == 1: - # return array([start], dtype=dtype) - # step = (stop-start)/float((num-1)) - # y = _nx.arange(0, num, dtype=dtype) * step + start - # y[-1] = stop - # else: - # step = (stop-start)/float(num) - # y = _nx.arange(0, num, dtype=dtype) * step + start - # if retstep: - # return y.astype(dtype), step - # else: - # return y.astype(dtype) - def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None): """ |