[Numpy-discussion] linspace upper bound not met?
Alan G Isaac
aisaac at american.edu
Mon Aug 21 11:31:14 EDT 2006
The definition of linspace is:
def linspace(start, stop, num=50, endpoint=True, retstep=False):
"""Return evenly spaced numbers.
Return 'num' evenly spaced samples from 'start' to 'stop'. If
'endpoint' is True, the last sample is 'stop'. If 'retstep' is
True then return the step value used.
"""
num = int(num)
if num <= 0:
return array([], float)
if endpoint:
if num == 1:
return array([float(start)])
step = (stop-start)/float((num-1))
else:
step = (stop-start)/float(num)
y = _nx.arange(0, num) * step + start
if retstep:
return y, step
else:
return y
The simplest way to achieve this goal is to add right after
the assignment to y two new lines:
if endpoint:
y[-1] = float(stop)
Cheers,
Alan Isaac
PS I'll take this opportunity to state again my opinion that
in the denerate case num=1 that if endpoint=True then
linspace should return stop rather than start. (Otherwise
endpoint is ignored. But I do not expect anyone to agree.)
More information about the NumPy-Discussion
mailing list