3
3
__all__ = ['logspace' , 'linspace' ]
4
4
5
5
from . import numeric as _nx
6
- from .numeric import array , result_type
6
+ from .numeric import array , result_type , NaN
7
7
8
8
9
9
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):
82
82
83
83
"""
84
84
num = int (num )
85
+ div = (num - 1 ) if endpoint else num
85
86
86
87
# Convert float/complex array scalars to float, gh-3504
87
88
start = start * 1.
@@ -91,38 +92,31 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
91
92
if dtype is None :
92
93
dtype = dt
93
94
94
- if num <= 0 :
95
- return array ([], dtype )
96
- if num == 1 :
97
- return array ([start ], dtype = dtype )
98
95
y = _nx .arange (0 , num , dtype = dt )
99
- if endpoint :
100
- num -= 1
101
- y /= num
102
- y *= stop - start
96
+
97
+ if num > 1 :
98
+ delta = stop - start
99
+ step = delta / div
100
+ if step == 0 :
101
+ # Special handling for denormal numbers, gh-5437
102
+ y /= div
103
+ y *= delta
104
+ else :
105
+ y *= step
106
+ else :
107
+ # 0 and 1 item long sequences have an undefined step
108
+ step = NaN
109
+
103
110
y += start
104
- if endpoint :
111
+
112
+ if endpoint and num > 1 :
105
113
y [- 1 ] = stop
106
114
107
115
if retstep :
108
- return y .astype (dtype , copy = False ), ( stop - start ) / num
116
+ return y .astype (dtype , copy = False ), step
109
117
else :
110
118
return y .astype (dtype , copy = False )
111
119
112
- # if endpoint:
113
- # if num == 1:
114
- # return array([start], dtype=dtype)
115
- # step = (stop-start)/float((num-1))
116
- # y = _nx.arange(0, num, dtype=dtype) * step + start
117
- # y[-1] = stop
118
- # else:
119
- # step = (stop-start)/float(num)
120
- # y = _nx.arange(0, num, dtype=dtype) * step + start
121
- # if retstep:
122
- # return y.astype(dtype), step
123
- # else:
124
- # return y.astype(dtype)
125
-
126
120
127
121
def logspace (start , stop , num = 50 , endpoint = True , base = 10.0 , dtype = None ):
128
122
"""
0 commit comments