Creating arithmetic sequences

Discussion in 'Python' started by mmm, Apr 16, 2008.

  1. mmm

    mmm Guest

    I wrote the code below to create simple arithmetic sequences that are
    iter-able
    I.e., this would basically combine the NUMPY arange(start,end,step)
    to range(start,end), with step not necessarily an integer.

    The code below is in its simplest form and I want to generalize the
    sequence types (multiplicative, cumulative, gauss ...), but first I
    need the simple SEQA( ) function to be more robust. The problem is
    the three test code functions produces different results based on
    step. I understand why steps such as 0.1 have rounding and machine
    math issues, and before I try to solve this I thought it was worth
    asking if this problem has been solved (so I do not re-invent the
    wheel).

    Another way to put my question, is there a PYTHON function that
    emulates SEQA() in APTECH’s GAUSS language and produces iterable
    lists ?

    Note I originally wrote the three versions below see which is fastest,
    but each is fast enough such that I care more about robustness now.


    ## Python arithmetic sequence implimentation
    ## MDB April 16 2008

    from numpy import array, arange, floor
    import time

    # simple arithmetic sequence
    def seqa1(start,end,step=1):
    n= floor( (end-start) / float(step) )
    x= [start,]
    for i in xrange(0,n):
    x.append(x[-1]+step)
    return x

    ##faster seq creation
    def seqa2(start,end,step=1):
    n= floor( (end-start) / float(step) )
    x= [ start+i*step for i in xrange(0,n) ]
    return x

    ##fastest seq creation as array, also allow array --> different type
    def seqa3(start,end,step=1.0,type=array):
    x=arange(start,end,step)
    if type==array: pass
    elif type==list or type==None or type=='' :
    x=list(x)
    elif type==tuple:
    x=tuple(x)
    elif type==dict:
    x= dict(zip(range(1,len(x)),tuple(x)))
    elif type==set:
    x= set(x)
    return x

    if (1):
    start=1
    end=2
    step= 0.10

    t0=time.time()
    x1=seqa1(start,end,step)
    print 'SEQA1 Time= ', time.time()-t0

    t0=time.time()
    x2=seqa2(start,end+step,step)
    print 'SEQA2 Time= ', time.time()-t0

    print 'Check for X1,X2 equivalence-- ', (x1==x2)

    t0=time.time()
    x3=seqa3(start,end+step,step,list)
    print 'SEQA3 Time= ', time.time()-t0

    print 'Check for X2,X3 equivalence-- ', (x2==x3)
     
    mmm, Apr 16, 2008
    #1
    1. Advertising

  2. mmm

    Robert Kern Guest

    mmm wrote:
    > I wrote the code below to create simple arithmetic sequences that are
    > iter-able
    > I.e., this would basically combine the NUMPY arange(start,end,step)
    > to range(start,end), with step not necessarily an integer.
    >
    > The code below is in its simplest form and I want to generalize the
    > sequence types (multiplicative, cumulative, gauss ...), but first I
    > need the simple SEQA( ) function to be more robust. The problem is
    > the three test code functions produces different results based on
    > step. I understand why steps such as 0.1 have rounding and machine
    > math issues, and before I try to solve this I thought it was worth
    > asking if this problem has been solved (so I do not re-invent the
    > wheel).


    Using numpy.arange() with floats is known to be problematic, and it is
    discouraged. Almost all of the use cases are better served with numpy.linspace()
    which accepts a start, end, and the number of points rather than a step.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Apr 17, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Minho Chae

    Creating combination of sequences

    Minho Chae, Nov 13, 2004, in forum: Python
    Replies:
    5
    Views:
    307
    Hung Jung Lu
    Nov 14, 2004
  2. Gregory Petrosyan

    Arithmetic sequences in Python

    Gregory Petrosyan, Jan 16, 2006, in forum: Python
    Replies:
    73
    Views:
    4,034
    Bengt Richter
    Jan 24, 2006
  3. Steven Bethard
    Replies:
    7
    Views:
    408
    Rocco Moretti
    Jan 20, 2006
  4. Jona
    Replies:
    0
    Views:
    275
  5. joshc
    Replies:
    5
    Views:
    583
    Keith Thompson
    Mar 31, 2005
Loading...

Share This Page