[SciPy-user] linspace

David M. Cooke cookedm at physics.mcmaster.ca
Mon Dec 5 20:51:22 CST 2005


Alan G Isaac <aisaac at american.edu> writes:

>>>> S.linspace(0,1,2.5)
> array([ 0.        ,  0.66666667,  1.33333333])
>>>> S.linspace(0,1,1)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Python24\lib\site-packages\scipy\base\function_base.py", line 91, in
> linspace
>     step = (stop-start)/float((num-1))
> ZeroDivisionError: float division
>
> While this is just an abuse and a corner case, both of these are odd.
> Therefore I suggest a new definition:
>
> def linspace(start,stop,num=50,endpoint=True,retstep=False):
>     """Evenly spaced numbers.
>     
>     Return an array of int(num) evenly spaced numbers from start to stop.
>     If endpoint==True, then last sample is stop.
>     If retstep==True, then also return the step value used.
>     """
>     num = int(num)
>     if num <= 0:
>         return array([])
>     if endpoint:
>         if num == 1:
>             return array([stop])
>         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

Actually, thinking about it: for linspace(0,1,1), do you want to
return [0] or [1]? Every call to linspace returns an array starting
with stop (0 here) regardless of endpoint, except for those that
require 0 points, so this could be thought of an invariant.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca



More information about the SciPy-user mailing list