Note
Go to the end to download the full example code.
Shamrock 3D unit vector generator#
This example shows how to use the unit vector generator
10 import matplotlib.pyplot as plt # plots
11 import numpy as np # sqrt & arctan2
12
13 import shamrock
Use shamrock documentation style for matplotlib
17 shamrock.matplotlib.set_shamrock_mpl_style()
Pseudo random number generator seed
23 eng = shamrock.algs.gen_seed(111)
Generate positions
28 list_pos = []
29 for i in range(1000000):
30 list_pos.append(shamrock.algs.mock_unit_vector_f64_3(eng))
Compute r and theta
35 r_val = []
36 for x, y, z in list_pos:
37 r = np.sqrt(x**2 + y**2 + z**2)
38 r_val.append(r)
39
40 for r in r_val:
41 if abs(r - 1.0) > 1e-15:
42 raise ValueError(f"abs({r} - 1.0) > 1e-15 | delta = {abs(r - 1.0)}")
43
44
45 theta_val = []
46 for x, y, z in list_pos:
47 theta = np.arctan2(y, x)
48 theta_val.append(theta)
Radial distribution
54 hist_r, bins_r = np.histogram(r_val, bins=101, density=True, range=(0, 2))
55 r = np.linspace(bins_r[0], bins_r[-1], 101)
56
57 plt.figure()
58 plt.bar(bins_r[:-1], hist_r, np.diff(bins_r), alpha=0.5)
59 plt.xlabel("$r$")
60 plt.ylabel("$f(r)$")
61 plt.show()

Angular distribution
66 hist_theta, bins_theta = np.histogram(theta_val, bins=1000, density=True)
67 theta = np.linspace(bins_theta[0], bins_theta[-1], 1000)
68
69 plt.figure()
70 plt.plot(theta, [1 / (2 * np.pi) for _ in theta], "r--", lw=2)
71 plt.bar(bins_theta[:-1], hist_theta, np.diff(bins_theta), alpha=0.5)
72 plt.xlabel(r"$\theta$")
73 plt.ylabel(r"$f(\theta)$")
74
75 plt.show()

Total running time of the script: (0 minutes 4.439 seconds)
Estimated memory usage: 354 MB