2023年2月9日 星期四

Python 量子運算(二0):叉積範例

Python 量子運算(二0):叉積範例

2023/02/08

-----


Fig. 20.1. Cross product examples.

-----

代碼 20.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
88
89
90
91
92
93
94
95
96
97
98
# Program 20.1:Cross product examples
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator


# 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


def Subplot(i, j):
    n = i * 3 + j + 1
    if n == 5:
        return
    ax = fig.add_subplot(3, 3, n, projection='3d')  # 3d subplot

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

    p = [3, 2, 1, 4, 0, 0, 5, 6, 7]
    # title = ['135', '90', '45', '180', '0', '0', '225', '270', '315']
    title = [r"$135^{\circ}$", r"$90^{\circ}$", r"$45^{\circ}$",
             r"$180^{\circ}$", '0', r"$0^{\circ}$",
             r"$135^{\circ}$", r"$90^{\circ}$", r"$45^{\circ}$"]

    # vector red
    vr_r = 2
    vr_p = (p[n - 1] / 8) * 2 * np.pi
    vr = VectorT1(vr_r, vr_p)
    plt.title(title[n - 1], fontsize=40)

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

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

    # arc phi
    if vr.p < np.pi:
        phi = np.linspace(0, vr.p-vb.p, 100)
    elif vr.p > np.pi:
        phi = np.linspace(vr.p-vb.p, 2*np.pi, 100)
    else:
        phi = np.linspace(0, 0, 100)
    x2 = 1 * np.cos(phi)
    y2 = 1 * np.sin(phi)
    ax.plot(x2, y2, 'k')

    ax.set_xlim([-5, 5])
    ax.set_ylim([-5, 5])
    ax.set_zlim([-5, 5])
    ax.xaxis.set_major_locator(MaxNLocator(3))
    ax.yaxis.set_major_locator(MaxNLocator(3))
    ax.zaxis.set_major_locator(MaxNLocator(3)) 

    return


# figure setting
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
mpl.rcParams['font.size'] = 16
fig = plt.figure(figsize=(16, 16))

for i in range(3):
    for j in range(3):
        Subplot(i, j)

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

解說:

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

-----

References


[1] 外積 - 維基百科,自由的百科全書

https://zh.wikipedia.org/zh-tw/%E5%8F%89%E7%A7%AF


[2] 在 Matplotlib 中設定 Ticks 刻度數量 | D棧 - Delft Stack

https://www.delftstack.com/zh-tw/howto/matplotlib/matplotlib-set-number-of-ticks/

-----

Python 量子運算(目錄)

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

-----

沒有留言:

張貼留言

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