2023年2月9日 星期四

Python 量子運算(一九):叉積

Python 量子運算(一九):叉積

2023/02/06

-----



Fig. 19.1 Cross product.

-----

代碼 19.1


 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
# Program 19.1:Cross product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


# type 1: defined by angle
class VectorT1:
    def __init__(self, r, p):
        self.r = r  # radius
        self.p = p  # phi

        self.x = 0
        self.y = 0
        self.z = 0

        self.u = r * np.cos(p)
        self.v = r * np.sin(p)
        self.w = 0


# type 2:defined by points
class VectorT2:
    def __init__(self, u, v, w):
        self.x = 0
        self.y = 0
        self.z = 0

        self.u = u
        self.v = v
        self.w = w


# figure setting
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
mpl.rcParams['font.size'] = 40
fig = plt.figure(figsize=(16, 16))
ax = fig.add_subplot(111, projection='3d')  # 3d subplot

# vector blue
vb_r = 2
vb_p = 0
vb = VectorT1(vb_r, vb_p)

# vector red
vr_r = 2
vr_p = (1/3) * np.pi
vr = VectorT1(vr_r, vr_p)

# vector purple
op = vb.r * vr.r * np.sin(vr.p-vb.p)
vp = VectorT2(0, 0, op)

# vector grey
vg = VectorT2(0, 0, 1)

ax.quiver(vb.x, vb.y, vb.z, vb.u, vb.v, vb.w, color='b', linewidth=5)
ax.quiver(vr.x, vr.y, vr.z, vr.u, vr.v, vr.w, color='r', linewidth=5)
ax.quiver(vp.x, vp.y, vp.z, vp.u, vp.v, vp.w, color='purple', linewidth=5)
ax.quiver(vg.x, vg.y, vg.z, vg.u, vg.v, vg.w, color='grey', linewidth=8)

# arc phi
phi = np.linspace(0, vr.p-vb.p, 100)
x2 = 0.5 * np.cos(phi)
y2 = 0.5 * np.sin(phi)
ax.plot(x2, y2, 'k')

ax.set_xlim([0, 4])
ax.set_ylim([0, 4])
ax.set_zlim([0, 4])

s1 = r'$\mathbf{a}=(2,0,0)$'
s2 = r'$\mathbf{b}=(1,\sqrt3,0)$'
s3 = r'$\mathbf{a}\times\mathbf{b}'\
     r'=\|\mathbf{a}\|\|\mathbf{b}\| \sin(\phi)\mathbf{\hat n}=(0,0,2\sqrt3)$'
s4 = r'$\mathbf{\hat n}=(0,0,1)$'
s5 = r'$\phi=60^{\circ}$'

ax.text(vb.u+0.3, vb.v, vb.w, s1, color='b')
ax.text(vr.u+0.3, vr.v, vr.w, s2, color='r')
ax.text(vp.u+0.3, vp.v, vp.w, s3, color='purple')
ax.text(vg.u+0.3, vg.v, vg.w, s4, color='grey')
ax.text(vb.x+0.6, vb.y+0.2, vb.z, s5, color='k')

# plt.savefig('/content/drive/My Drive/pqc/0019_001.png')
plt.show()

解說:

此代碼以繪出向量外積為目的,方便為主,並未嚴格按照外積的公式。

-----

References


[1] Cross product - Wikipedia

https://en.wikipedia.org/wiki/Cross_product


[2] Unit vector - Wikipedia

https://en.wikipedia.org/wiki/Unit_vector


[3] Polar coordinate system - Wikipedia

https://en.wikipedia.org/wiki/Polar_coordinate_system


[4] matplotlib.pyplot.quiver — Matplotlib 3.6.2 documentation

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.quiver.html


[5] 3D quiver plot — Matplotlib 3.6.3 documentation

https://matplotlib.org/stable/gallery/mplot3d/quiver3d.html


[6] Python Matplotlib: How to draw 3D vector - OneLinerHub

https://onelinerhub.com/python-matplotlib/how-to-draw-3d-vector

-----

Python 量子運算(目錄)

https://mandhistory.blogspot.com/2022/01/quantum-computing.html

-----

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。