Stupid Question?

Michael Haggerty mhagger at blizzard.harvard.edu
Fri Aug 6 13:41:15 EDT 1999


"jim moore" <indiana at 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 at blizzard.harvard.edu




More information about the Python-list mailing list