Skip to content

Commit 5401f1a

Browse files
committed
Files Added
1 parent ce2da46 commit 5401f1a

File tree

1,209 files changed

+45632
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,209 files changed

+45632
-0
lines changed

.gitattributes

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
7+
# Standard to msysgit
8+
*.doc diff=astextplain
9+
*.DOC diff=astextplain
10+
*.docx diff=astextplain
11+
*.DOCX diff=astextplain
12+
*.dot diff=astextplain
13+
*.DOT diff=astextplain
14+
*.pdf diff=astextplain
15+
*.PDF diff=astextplain
16+
*.rtf diff=astextplain
17+
*.RTF diff=astextplain

.gitignore

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Windows image file caches
2+
Thumbs.db
3+
ehthumbs.db
4+
5+
# Folder config file
6+
Desktop.ini
7+
8+
# Recycle Bin used on file shares
9+
$RECYCLE.BIN/
10+
11+
# Windows Installer files
12+
*.cab
13+
*.msi
14+
*.msm
15+
*.msp
16+
17+
# Windows shortcuts
18+
*.lnk
19+
20+
# =========================
21+
# Operating System Files
22+
# =========================
23+
24+
# OSX
25+
# =========================
26+
27+
.DS_Store
28+
.AppleDouble
29+
.LSOverride
30+
31+
# Thumbnails
32+
._*
33+
34+
# Files that might appear in the root of a volume
35+
.DocumentRevisions-V100
36+
.fseventsd
37+
.Spotlight-V100
38+
.TemporaryItems
39+
.Trashes
40+
.VolumeIcon.icns
41+
42+
# Directories potentially created on remote AFP share
43+
.AppleDB
44+
.AppleDesktop
45+
Network Trash Folder
46+
Temporary Items
47+
.apdisk

Chapter01/simul.py

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from matplotlib import pyplot as plt
2+
from matplotlib import animation
3+
from random import uniform
4+
import timeit
5+
6+
class Particle:
7+
8+
__slots__ = ('x', 'y', 'ang_speed')
9+
10+
def __init__(self, x, y, ang_speed):
11+
self.x = x
12+
self.y = y
13+
self.ang_speed = ang_speed
14+
15+
16+
class ParticleSimulator:
17+
18+
def __init__(self, particles):
19+
self.particles = particles
20+
21+
def evolve(self, dt):
22+
timestep = 0.00001
23+
nsteps = int(dt/timestep)
24+
25+
for i in range(nsteps):
26+
for p in self.particles:
27+
28+
norm = (p.x**2 + p.y**2)**0.5
29+
v_x = (-p.y)/norm
30+
v_y = p.x/norm
31+
32+
d_x = timestep * p.ang_speed * v_x
33+
d_y = timestep * p.ang_speed * v_y
34+
35+
p.x += d_x
36+
p.y += d_y
37+
38+
# def evolve(self, dt):
39+
# timestep = 0.00001
40+
# nsteps = int(dt/timestep)
41+
42+
# # First, change the loop order
43+
# for p in self.particles:
44+
# t_x_ang = timestep * p.ang_speed
45+
# for i in range(nsteps):
46+
# norm = (p.x**2 + p.y**2)**0.5
47+
# p.x, p.y = p.x - t_x_ang*p.y/norm, p.y + t_x_ang * p.x/norm
48+
49+
def visualize(simulator):
50+
51+
X = [p.x for p in simulator.particles]
52+
Y = [p.y for p in simulator.particles]
53+
54+
fig = plt.figure()
55+
ax = plt.subplot(111, aspect='equal')
56+
line, = ax.plot(X, Y, 'ro')
57+
58+
# Axis limits
59+
plt.xlim(-1, 1)
60+
plt.ylim(-1, 1)
61+
62+
# It will be run when the animation starts
63+
def init():
64+
line.set_data([], [])
65+
return line,
66+
67+
def animate(i):
68+
# We let the particle evolve for 0.1 time units
69+
simulator.evolve(0.01)
70+
X = [p.x for p in simulator.particles]
71+
Y = [p.y for p in simulator.particles]
72+
73+
line.set_data(X, Y)
74+
return line,
75+
76+
# Call the animate function each 10 ms
77+
anim = animation.FuncAnimation(fig,
78+
animate,
79+
init_func=init,
80+
blit=True,
81+
interval=10)
82+
plt.show()
83+
84+
85+
def test_visualize():
86+
particles = [Particle( 0.3, 0.5, +1),
87+
Particle( 0.0, -0.5, -1),
88+
Particle(-0.1, -0.4, +3)]
89+
90+
simulator = ParticleSimulator(particles)
91+
visualize(simulator)
92+
93+
def test_evolve():
94+
particles = [Particle( 0.3, 0.5, +1),
95+
Particle( 0.0, -0.5, -1),
96+
Particle(-0.1, -0.4, +3)]
97+
98+
simulator = ParticleSimulator(particles)
99+
100+
simulator.evolve(0.1)
101+
102+
p0, p1, p2 = particles
103+
104+
def fequal(a, b):
105+
return abs(a - b) < 1e-5
106+
107+
assert fequal(p0.x, 0.2102698450356825)
108+
assert fequal(p0.y, 0.5438635787296997)
109+
110+
assert fequal(p1.x, -0.0993347660567358)
111+
assert fequal(p1.y, -0.4900342888538049)
112+
113+
assert fequal(p2.x, 0.1913585038252641)
114+
assert fequal(p2.y, -0.3652272210744360)
115+
116+
117+
def benchmark():
118+
particles = [Particle(uniform(-1.0, 1.0),
119+
uniform(-1.0, 1.0),
120+
uniform(-1.0, 1.0))
121+
for i in range(100)]
122+
123+
simulator = ParticleSimulator(particles)
124+
simulator.evolve(0.1)
125+
126+
127+
def timing():
128+
result = timeit.timeit('benchmark()',
129+
setup='from __main__ import benchmark',
130+
number=10)
131+
# Result is the time it takes to run the whole loop
132+
print(result)
133+
134+
result = timeit.repeat('benchmark()',
135+
setup='from __main__ import benchmark',
136+
number=10,
137+
repeat=3)
138+
# Result is a list of times
139+
print(result)
140+
141+
142+
def benchmark_memory():
143+
particles = [Particle(uniform(-1.0, 1.0),
144+
uniform(-1.0, 1.0),
145+
uniform(-1.0, 1.0))
146+
for i in range(100000)]
147+
148+
simulator = ParticleSimulator(particles)
149+
simulator.evolve(0.001)
150+
151+
152+
if __name__ == '__main__':
153+
benchmark()

Chapter01/taylor.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def factorial(n):
2+
if n == 0:
3+
return 1.0
4+
else:
5+
return float(n) * factorial(n-1)
6+
7+
def taylor_exp(n):
8+
return [1.0/factorial(i) for i in range(n)]
9+
10+
def taylor_sin(n):
11+
res = []
12+
for i in range(n):
13+
if i % 2 == 1:
14+
res.append((-1)**((i-1)/2)/float(factorial(i)))
15+
else:
16+
res.append(0.0)
17+
return res
18+
19+
def benchmark():
20+
taylor_exp(500)
21+
taylor_sin(500)
22+
23+
if __name__ == '__main__':
24+
benchmark()

Chapter01/test_simul.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from simul import Particle, ParticleSimulator
2+
3+
def test_evolve(benchmark):
4+
particles = [Particle( 0.3, 0.5, +1),
5+
Particle( 0.0, -0.5, -1),
6+
Particle(-0.1, -0.4, +3)]
7+
8+
simulator = ParticleSimulator(particles)
9+
10+
simulator.evolve(0.1)
11+
12+
p0, p1, p2 = particles
13+
14+
def fequal(a, b):
15+
return abs(a - b) < 1e-5
16+
17+
assert fequal(p0.x, 0.2102698450356825)
18+
assert fequal(p0.y, 0.5438635787296997)
19+
20+
assert fequal(p1.x, -0.0993347660567358)
21+
assert fequal(p1.y, -0.4900342888538049)
22+
23+
assert fequal(p2.x, 0.1913585038252641)
24+
assert fequal(p2.y, -0.3652272210744360)
25+
26+
benchmark(simulator.evolve, 0.1)

Chapter02/.cache/v/cache/lastfailed

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)