Creating arithmetic sequences

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)
 
R

Robert Kern

mmm said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top