-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
44 lines (30 loc) · 1.06 KB
/
test.py
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
import numpy as np
N = 100_000_000
x = np.random.rand(N)
y = np.random.rand(N)
r = np.sqrt(x*x + y*y)
r_ = r[r <= 1]
pi = 4 * len(r_) / N
print("Estimated value of pi:", pi)
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use("TkAgg")
hist_, x_ = np.histogram(r, 1000)
plt.axline((1.0, 0), (1.0, 1), color="orange", linestyle='--')
plt.plot(x_[:-1], hist_)
plt.show()
condition = r <= 1
x_frac = x[::int(N / 10000)]
y_frac = y[::int(N / 10000)]
cond_frac = condition[::int(N / 10000)]
plt.scatter(x_frac[cond_frac], y_frac[cond_frac], 1.0, color='orange')
plt.scatter(x_frac[np.logical_not(cond_frac)], y_frac[np.logical_not(cond_frac)], 1.0, color='green')
plt.show()
theta = np.arccos(x/r)
condition = np.logical_and(theta < np.pi/3, theta > np.pi/6)
condition = np.logical_and(condition, r <= 1)
cond_frac = condition[::int(N / 10000)]
plt.scatter(x_frac[cond_frac], y_frac[cond_frac], 1.0, color='orange')
plt.scatter(x_frac[np.logical_not(cond_frac)], y_frac[np.logical_not(cond_frac)], 1.0, color='green')
plt.show()
print(len(x[condition])/N)