Menu

[r8866]: / trunk / py4science / examples / logistic / maplib.py  Maximize  Restore  History

Download this file

123 lines (93 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import numpy as np
from matplotlib.cbook import iterable
class IteratedMap(object):
"""
Define an interface for a map
"""
def __call__(self, x):
'iterate self one step starting at x. x can be a scalar or array'
raise NotImplementedError('Derived must override')
def plot(self, ax, x, **kwargs):
"""
Plot the first iterate of map over x including the line of identity
kwargs are passed onto mpl plot
"""
y = self(x)
ax.plot(x, y, **kwargs)
ax.plot(x, x, **kwargs)
def plot_timeseries(self, ax, x0, numsteps, **kwargs):
"""
Plot the timeseries in mpl Axes ax by iterating x0 numsteps
If x0 is a vector, plot each of the timeseries
"""
y = self.iterate(x0, numsteps)
if not iterable(x0):
ax.plot(y, **kwargs)
else:
for walker in y:
ax.plot(walker, **kwargs)
def plot_cobweb(self, ax, x0, numsteps, **kwargs):
"""
ax is a matplotlib axes instance.
plot the cobweb map of numsteps iterates of x0
kwargs are passed onto mpl plot
"""
iterates = self.trajectory(x0, numsteps)
vertices = []
lasty = 0
for this, next in zip(iterates[:-1], iterates[1:]):
vertices.append( (this, lasty) )
vertices.append( (this, next) )
vertices.append( (this, next) )
vertices.append( (next, next) )
lasty = next
x, y = zip(*vertices)
ax.plot(x, y, **kwargs)
def iterator_from(self, x0):
while 1:
x0 = self(x0)
yield x0
def iterate_from(self, x0, numsteps):
for i in xrange(numsteps):
x0 = self(x0)
return x0
def trajectory(self, x0, numsteps):
"""iterate self starting at x0 for numsteps, returning whole trajectory
Return value is an array of the time-series. If x0 is a scalar, a
numsteps length 1D vector is returned with x0 as the first value. If x0
is a vector, a 2D array with shape (numsteps, len(x0)) is returned, with
x0 as the first row.
"""
if iterable(x0): # return a 2D array
ret = np.zeros( (numsteps,len(x0)) )
else: # return a 1D array
ret = np.zeros(numsteps)
# assign the initial condtion to the 0-th element
ret[0] = x0
# iterate the map for numsteps
for i in range(1,numsteps):
ret[i] = self(ret[i-1])
return ret
class Logistic(IteratedMap):
def __init__(self, mu):
self.R = 4.0*mu
def __call__(self, x):
'iterate self one step starting at x. x can be a scalar or array'
return self.R*x*(1.0-x)
class Sine(IteratedMap):
def __init__(self, B):
self.B = B
def __call__(self, x):
'iterate self one step starting at x. x can be a scalar or array'
return self.B*np.sin(np.pi*x)
def test():
m = Logistic(0.9)
x0 = np.random.rand(100)
ret = m.iterate(x0, 3)
assert ret.shape == 4,100
x0 = 0.2
ret = m.iterate(x0, 3)
assert ret.shape == 4
print 'all tests passed'
if __name__=='__main__':
test()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.