# SimEngine.py: Python function(s) for iterating one-dimensional # maps
#
# Our fledgling simulation engine for one-dimensional maps
#
# Below we use xrange(), rather than range(), since the latter returns a
# list and so uses up storage. xrange() only returns a single number.
# The function that does the calculation
#
def IterateMap(x,r,nIts):
for i in xrange(nIts):
x = r * x * (1.0 - x)
return x
# LogisticData.py: Generate 10 data files of Logistic Map iterates
#
# Get our iteration function from the module SimEngine.py
#
from SimEngine import *
OutputFileBasename = "data"
nIterations = 50
r = 4.0
x0 = 0.3
delta = 0.00005
for filenumber in xrange(10):
file = open(OutputFileBasename + str(filenumber+1), 'w')
x = x0
for i in xrange(nIterations):
x = IterateMap(x,r,1)
line = str(i) + ' ' + str(x) + '\n'
file.write(line)
file.close()
x0 = x0 + delta
# MatrixIO.py: A module for reading and writing matrices
import string
import numpy
# Read data in from a text file formatted as a matrix of numbers.
# Return data in a numpy array.
#
def readMatrix(filename):
rows = []
for line in file(filename):
columns = []
for number in string.split(line):
columns.append(float(number))
rows.append(columns)
return numpy.array(rows)
# Write out a numpy array to a text file.
#
def writeMatrix(a, filename):
f = file(filename, 'w')
for row in a:
for number in row:
f.write(str(number) + ' ')
f.write('\n')
f.close()
In the first function, the function float() is applied to all elements of a list. This is such a frequent operation that a special shorthand has been introduced: the function map(function, sequence) applies a function to each element of a sequence and returns a list of the results. This makes it possible to replace the lines
columns = []
for number in string.split(line):
columns.append(float(number))
by the single line
columns = map(float, string.split(line))
Now, here's the program MatrixReadWrite.py that uses this module.
# MatrixReadWrite.py: Read in a matrix of data and write it out from MatrixIO import * InputFilename = 'data7' OutputFilename = InputFilename + '.out' m = readMatrix(InputFilename) writeMatrix(m,OutputFilename)
# MaxSeparation.py: Calculate the time-step by time-step size (maximum
# distance between all pairs of points) of a set of Logistic map iterates.
# Plot the size as a function of iteration number.
import MatrixIO
import numpy
# We read data from the files, storing in memory
InputFileBasename = "data"
nFiles = 10
# We first read the data into a list of 2d numpy arrays
a = []
for i in xrange(nFiles):
filename = InputFileBasename + str(i+1)
a.append(MatrixIO.readMatrix(filename))
nIterates = a[i].shape[0]
print "File " + filename + " had " + str(nIterates) + " iterates"
print "Read in " + str(nFiles) + " files, each with " + str(nIterates) + " iterates"
# Now, we analyze the pairwise distance at each time step, storing the
# distance in the MaxSep array.
#
MaxSep = numpy.zeros(nIterates)
# Let's simplify our calculations by putting the data into a simpler structure:
# An array with nIterates rows and nFiles data points in each row.
b = numpy.zeros((nIterates,nFiles),float)
for i in xrange(nIterates):
for j in xrange(nFiles):
b[i][j] = a[j][i][1]
# So, now MaxSep at each iteration is simply the largest pairwise
# distance for the points in each row of b. But these are points
# in one dimension: The largest pairwise distance is just the largest
# value minus the smallest value.
for i in xrange(nIterates):
MaxSep[i] = numpy.max(b[i]) - numpy.min(b[i])
f = open("MaxSep.txt","w")
for i in xrange(nIterates):
f.write(str(i) + " " + str(MaxSep[i]) + '\n')
f.close()