Menu

[r6030]: / trunk / py4science / examples / glass_dots1.py  Maximize  Restore  History

Download this file

89 lines (61 with data), 1.9 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
"""
Moire patterns from random dot fields
http://en.wikipedia.org/wiki/Moir%C3%A9_pattern
See L. Glass. 'Moire effect from random dots' Nature 223, 578580 (1969).
"""
import cmath
from numpy import cos, sin, pi, matrix
import numpy as npy
import numpy.linalg as linalg
from pylab import figure, show
def myeig(M):
"""
compute eigen values and eigenvectors analytically
Solve quadratic:
lamba^2 - tau*lambda + Delta = 0
where tau = trace(M) and Delta = Determinant(M)
"""
a,b = M[0,0], M[0,1]
c,d = M[1,0], M[1,1]
tau = a+d # the trace
delta = a*d-b*c # the determinant
lambda1 = (tau + cmath.sqrt(tau**2 - 4*delta))/2.
lambda2 = (tau - cmath.sqrt(tau**2 - 4*delta))/2.
return lambda1, lambda2
# 2000 random x,y points in the interval[-0.5 ... 0.5]
X1 = matrix(npy.random.rand(2,2000)
)-0.5
name = 'saddle'
sx, sy, angle = 1.05, 0.95, 0.
#name = 'center'
#sx, sy, angle = 1., 1., 2.5
#name= 'stable focus' # spiral
#sx, sy, angle = 0.95, 0.95, 2.5
theta = angle * pi/180.
S = matrix([[sx, 0],
[0, sy]])
R = matrix([[cos(theta), -sin(theta)],
[sin(theta), cos(theta)],])
M = S*R # rotate then stretch
# compute the eigenvalues using numpy linear algebra
vals, vecs = linalg.eig(M)
print 'numpy eigenvalues', vals
# compare with the analytic values from myeig
avals = myeig(M)
print 'analytic eigenvalues', avals
# transform X1 by the matrix
X2 = M*X1
# plot the original x,y as green dots and the transformed x, y as red
# dots
fig = figure()
ax = fig.add_subplot(111)
x1 = X1[0].flat
y1 = X1[1].flat
x2 = X2[0].flat
y2 = X2[1].flat
ax = fig.add_subplot(111)
line1, line2 = ax.plot(x1, y1, 'go', x2, y2, 'ro', markersize=2)
ax.set_title(name)
fig.savefig('glass_dots1.png', dpi=100)
fig.savefig('glass_dots1.eps', dpi=100)
show()
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.