Mailing List Archive

Stupid Question?
Is there a bulit in function that will give you N evenly spaced numbers
between X1 and X2. For example: 10 numbers between 0 and 1 ([0.1, 0.2, 0.3,
...])
It would be similar to MATLAB's linspace.
How about a function similar to range but allowing a floating point step?
Seems like there would be one out there, so I thought I'd check before I
wrote one.
Thanks,
Jim Moore
Stupid Question? [ In reply to ]
"jim moore" <indiana@ev1.net> writes:
> Is there a bulit in function that will give you N evenly spaced numbers
> between X1 and X2. For example: 10 numbers between 0 and 1 ([0.1, 0.2, 0.3,
> ...])

Uh, sounds pretty trivial to me:

def linspace(v1,v2,n):
return map(lambda x,v1=v1,v2=v2,n=n:\
v1+float(v2-v1)*(1+x)/(n+1), range(n))

I don't see why everything should be builtin :P

> It would be similar to MATLAB's linspace.
> How about a function similar to range but allowing a floating point step?
> Seems like there would be one out there, so I thought I'd check before I
> wrote one.

Same thing; range's quick-and-dirty integer thingy, if there was float
checks or stuff for the step, it'd slow down slightly or great deal, and I
don't see much of a gain in it.

> Thanks,
> Jim Moore

-Markus

--
"Chicks will dig you if you use Debian! Running RedHat is
like going to the beach to scope out the action while wearing
your water wings."
-- slashdot comment
Stupid Question? [ In reply to ]
jim moore wrote:
> =

> Is there a bulit in function that will give you N evenly spaced numbers=

> between X1 and X2. For example: 10 numbers between 0 and 1 ([0.1, 0.2,=
0.3,
> ...])
> It would be similar to MATLAB's linspace.
> How about a function similar to range but allowing a floating point ste=
p?
> Seems like there would be one out there, so I thought I'd check before =
I
> wrote one.

If you want to do numerics in python, you might want to install
the LLNL-Distribution. It provides matrix-oriented math like Matlab and
there's
good documentation.

regards,

Mirko

-- =

M Li=C3=9F, <n89553@hrz.uni-paderborn.de>
Stupid Question? [ In reply to ]
"jim moore" <indiana@ev1.net> writes:

> Is there a bulit in function that will give you N evenly spaced
> numbers between X1 and X2. For example: 10 numbers between 0 and 1
> ([0.1, 0.2, 0.3, ...])

Here's what I use (requires Numeric). To get `10 numbers between 0
and 1' you type `raster(0,1,10)', but note that it returns the more
pythonic [0, 0.1, 0.2, ..., 0.9]. To get your list you would type
`raster(0,1,10,1,11)'. You can also get this as a module, through
http://monsoon.harvard.edu/~mhagger/download/.

Michael

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

import Numeric

def raster(d0, dn, n, lo = 0, hi = None):
"""Return a Numeric array of equally-spaced floating-point numbers.

d0,dn : any numbers (real or integer)
n : nonzero integer
lo, hi : integers

Return a numeric array containing values chosen by dividing the
range from d0 to dn into n equal intervals. By default return n
points, including d0 but excluding dn in the usual python style.
If lo and/or hi are specified, then select the following subset of
(hi-lo) points from the same grid:

[d0 + lo*delta, d0 + (lo+1)*delta, ..., d0 + (hi-1)*delta]

where delta is (dn - d0)/n. The idea is that you can use round
numbers for d0, dn, and n, to get round numbers out, regardless of
your choice of lo and hi.

"""

delta = (float(dn) - float(d0)) / n
if hi is None: hi = n
return d0 + delta*Numeric.arange(lo, hi, typecode=Numeric.Float)


--
Michael Haggerty
mhagger@blizzard.harvard.edu