M
mmm
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)
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)