2023年2月9日 星期四

Python 量子運算(一八):和差角公式

Python 量子運算(一八):和差角公式

 2023/02/04

-----


Fig. 18.1. Angle sum and difference formulas.

-----

代碼 18.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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Program 18.1:Angle sum and difference formulas
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


# defined by angle
class PointT1:
    def __init__(self, t, r):
        self.t = t  # theta

        self.r = r  # radius
        self.x = r * np.cos(t)
        self.y = r * np.sin(t)


# defined by points
class PointT2:
    def __init__(self, x, y):
        self.x = x
        self.y = y


def Subplot_1():
    ax = plt.subplot(221)

    # circle
    c = np.linspace(0, np.pi*2, 100)
    r = 1
    c_x = r * np.cos(c)
    c_y = r * np.sin(c)
    ax.plot(c_x, c_y, 'g', linestyle='--')

    # points
    t_A = (31/24) * np.pi                # theta A setting
    PA = PointT1(t_A, r)                 # A
    PC = PointT1(t_A-np.pi, r)           # C, determined by A
    ax.plot([PA.x, PC.x], [PA.y, PC.y], 'purple', linewidth=5.0)  # AC
    ax.text(PA.x-0.1, PA.y-0.1, 'A')
    ax.text(PC.x+0.05, PC.y+0.05, 'C')

    PB = PointT2(PC.x, PA.y)             # B, determined by A, C
    ax.plot([PA.x, PB.x], [PA.y, PA.y], 'r', linestyle='--')  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y], 'purple', linestyle='--')  # BC
    ax.text(PB.x+0.05, PB.y-0.1, 'B')

    PG = PointT2(PA.x, PC.y)             # G, determined by A, C
    ax.plot([PA.x, PG.x], [PA.y, PG.y], 'purple', linestyle='--')  # AG
    ax.plot([PC.x, PG.x], [PC.y, PG.y], 'purple', linestyle='--')  # CG
    ax.text(PG.x-0.1, PG.y+0.05, 'G')

    t_E = (47/24) * np.pi                # theta E setting
    PE = PointT1(t_E, r)                 # E
    ax.plot([PA.x, PE.x], [PA.y, PE.y], 'r', linewidth=5.0)  # AE
    ax.plot([PC.x, PE.x], [PC.y, PE.y], 'b', linewidth=5.0)  # CE
    ax.text(PE.x+0.05, PE.y, 'E')

    PD = PointT2(PE.x, PC.y)             # D, determined by C, E
    ax.plot([PC.x, PD.x], [PC.y, PD.y], 'b', linestyle='--')  # CD
    ax.plot([PD.x, PE.x], [PD.y, PE.y], 'b', linestyle='--')  # DE
    ax.text(PD.x+0.05, PD.y+0.05, 'D')

    PF = PointT2(PE.x, PB.y)             # F, determined by B, E
    ax.plot([PB.x, PF.x], [PB.y, PF.y], 'r', linestyle='--')  # BF
    ax.plot([PE.x, PF.x], [PE.y, PF.y], 'r', linestyle='--')  # EF
    ax.text(PF.x+0.05, PF.y-0.1, 'F')

    # angles
    ax.text(PA.x+0.25, PA.y+0.20, r'$\alpha$')
    ax.text(PA.x+0.35, PA.y+0.05, r'$\beta$')
    ax.text(PE.x-0.08, PE.y+0.25, r'$\beta$')
    ax.text(PC.x-0.35, PC.y-0.10, r'$\alpha+\beta$')

    # edges
    ax.text((PA.x+PE.x)/2-0.2, (PA.y+PE.y)/2,
            r'$\cos\alpha$', color='r')
    ax.text((PA.x+PF.x)/2-0.1, (PA.y+PF.y)/2,
            r'$\cos\alpha\cos\beta$', color='r')
    ax.text((PE.x+PF.x)/2, (PE.y+PF.y)/2,
            r'$\cos\alpha\sin\beta$', color='r')

    ax.text((PC.x+PE.x)/2-0.2, (PC.y+PE.y)/2,
            r'$\sin\alpha$', color='b')
    ax.text((PD.x+PE.x)/2, (PD.y+PE.y)/2,
            r'$\sin\alpha\cos\beta$', color='b')
    ax.text((PC.x+PD.x)/2-0.15, (PC.y+PD.y)/2,
            r'$\sin\alpha\sin\beta$', color='b')

    ax.text((PC.x+PG.x)/2-0.2, (PC.y+PG.y)/2,
            r'$\cos(\alpha+\beta)$', color='purple')
    ax.text((PA.x+PG.x)/2-0.4, (PA.y+PG.y)/2,
            r'$\sin(\alpha+\beta)$', color='purple')

    ax.text(0, -1.3, '(a)')
    ax.set_aspect(1)  # height : width
    ax.set_axis_off()

    return


def Subplot_2():
    ax = plt.subplot(222)

    # string setting
    s1 = r'$\overline{AC}=1$'
    s2_1 = r'$\sin(\alpha+\beta)$'
    s2_2 = r'$=\overline{AG}=\overline{DE}+\overline{EF}$'
    s2_3 = r'$=\sin\alpha\cos\beta+\cos\alpha\sin\beta$'
    s3_1 = r'$\cos(\alpha+\beta)$'
    s3_2 = r'$=\overline{CG}=\overline{AF}-\overline{BF}$'
    s3_3 = r'$=\cos\alpha\cos\beta-\sin\alpha\sin\beta$'

    # string output
    ax.text(0.10, 0.95, s1, fontsize=24)

    ax.text(0.20, 0.80, s2_1, color='purple', fontsize=40)
    ax.text(0.10, 0.65, s2_2, fontsize=24)
    ax.text(0.10, 0.50, s2_3, fontsize=40)

    ax.text(0.20, 0.30, s3_1, color='purple', fontsize=40)
    ax.text(0.10, 0.15, s3_2, fontsize=24)
    ax.text(0.10, 0.00, s3_3, fontsize=40)

    ax.text(0.5, -0.09, '(b)')
    ax.set_axis_off()

    return


def Subplot_3():
    ax = plt.subplot(223)

    # circle
    c = np.linspace(0, np.pi*2, 100)
    r = 1
    c_x = r * np.cos(c)
    c_y = r * np.sin(c)
    ax.plot(c_x, c_y, 'g', linestyle='--')

    t_A = (31/24) * np.pi                # theta A setting
    PA = PointT1(t_A, r)                 # A
    PC = PointT1(t_A-np.pi, r)           # C, determined by A
    ax.plot([PA.x, PC.x], [PA.y, PC.y])  # AC
    ax.plot([PA.x, PC.x], [PA.y, PC.y], 'purple', linewidth=5.0)  # AC
    ax.text(PA.x-0.1, PA.y-0.1, 'A')
    ax.text(PC.x+0.05, PC.y+0.05, 'C')

    PB = PointT2(PC.x, PA.y)             # B, determined by A, C
    ax.plot([PA.x, PB.x], [PA.y, PA.y])  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y])  # BC
    ax.plot([PA.x, PB.x], [PA.y, PA.y], 'r', linewidth=5.0)  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y], 'b', linewidth=5.0)  # BC
    ax.text(PB.x+0.05, PB.y-0.1, 'B')

    t_E = (47/24) * np.pi                # theta E setting
    PE = PointT1(t_E, r)                 # E
    ax.plot([PA.x, PE.x], [PA.y, PE.y], 'purple', linestyle='--')  # AE
    ax.plot([PC.x, PE.x], [PC.y, PE.y], 'purple', linestyle='--')  # CE
    ax.text(PE.x+0.05, PE.y, 'E')

    # finding Point G for learning
    slope_AE = (PE.y - PA.y) / (PE.x - PA.x)

    # finding Point D
    slope_CE = (PE.y - PC.y) / (PE.x - PC.x)  # -(1/slope_AE)
    slope_BD = slope_CE
    x1 = (slope_AE * PA.x - slope_BD * PB.x + PB.y - PA.y)
    x2 = (slope_AE - slope_BD)
    PD_x = x1 / x2
    PD_y = slope_AE * (PD_x - PA.x) + PA.y
    PD = PointT2(PD_x, PD_y)
    ax.plot([PB.x, PD.x], [PB.y, PD.y], 'r', linestyle='--')  # BD
    ax.text(PD.x, PD.y+0.05, 'D')

    # finding Point F
    PF_x = PE.x - PD.x + PB.x
    PF_y = PE.y - PD.y + PB.y
    PF = PointT2(PF_x, PF_y)
    ax.plot([PB.x, PF.x], [PB.y, PF.y], 'b', linestyle='--')  # BF
    ax.plot([PE.x, PF.x], [PE.y, PF.y], 'r', linestyle='--')  # EF
    ax.text(PF.x+0.05, PF.y-0.1, 'F')

    # angles
    ax.text(PA.x+0.20, PA.y+0.20, r'$\alpha-\beta$')
    ax.text(PA.x+0.35, PA.y+0.05, r'$\beta$')
    ax.text(PC.x+0.02, PC.y-0.30, r'$\beta$')

    # edges
    ax.text((PA.x+PB.x)/2, (PA.y+PB.y)/2+0.01,
            r'$\cos\alpha$', color='r')
    ax.text((PA.x+PD.x)/2, (PA.y+PD.y)/2+0.10,
            r'$\cos\alpha\cos\beta$', color='r')
    ax.text((PB.x+PD.x)/2-0.20, (PB.y+PD.y)/2,
            r'$\cos\alpha\sin\beta$', color='r')

    ax.text((PB.x+PC.x)/2-0.21, (PB.y+PC.y)/2,
            r'$\sin\alpha$', color='b')
    ax.text((PB.x+PF.x)/2, (PB.y+PF.y)/2-0.05,
            r'$\sin\alpha\sin\beta$', color='b')

    ax.text((PC.x+PE.x)/2, (PC.y+PE.y)/2,
            r'$\sin(\alpha-\beta)$', color='purple')

    ax.text(0, -1.38, '(c)')
    ax.set_aspect(1)  # height : width
    ax.set_axis_off()

    return


def Subplot_4():
    ax = plt.subplot(224)

    # string setting
    s1 = r'$\overline{AC}=1$'
    s2_1 = r'$\sin(\alpha-\beta)$'
    s2_2 = r'$=\overline{CE}=\overline{CF}-\overline{EF}$'
    s2_3 = r'$=\sin\alpha\cos\beta-\cos\alpha\sin\beta$'
    s3_1 = r'$\cos(\alpha-\beta)$'
    s3_2 = r'$=\overline{AE}=\overline{AD}+\overline{DE}$'
    s3_3 = r'$=\cos\alpha\cos\beta+\sin\alpha\sin\beta$'

    # string output
    ax.text(0.10, 0.95, s1, fontsize=24)

    ax.text(0.20, 0.80, s2_1, color='purple', fontsize=40)
    ax.text(0.10, 0.65, s2_2, fontsize=24)
    ax.text(0.10, 0.50, s2_3, fontsize=40)

    ax.text(0.20, 0.30, s3_1, color='purple', fontsize=40)
    ax.text(0.10, 0.15, s3_2, fontsize=24)
    ax.text(0.10, 0.00, s3_3, fontsize=40)

    ax.text(0.5, -0.09, '(d)')
    ax.set_axis_off()

    return


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

Subplot_1()
Subplot_2()
Subplot_3()
Subplot_4()

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

解說:

-----

Python 量子運算(目錄)

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

-----

Python 量子運算(一六):和角

Python 量子運算(一六):和角

2023/01/24

-----


Fig. 1. Angle sum.

-----

和角 angle sum

和角公式 angle sum formula

差角 angle difference

差角公式 angle difference formula

「Along with angle difference formulas, we have angle sum formulas as well.」[1]。

-----

代碼 16.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
 99
100
101
102
103
104
105
106
107
108
# Program 16.1:Angle sum
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


# defined by angle
class PointT1:
    def __init__(self, t, r):
        self.t = t  # theta

        self.r = r  # radius
        self.x = r * np.cos(t)
        self.y = r * np.sin(t)


# defined by points
class PointT2:
    def __init__(self, x, y):
        self.x = x
        self.y = y


def Subplot_1():
    ax = plt.subplot(111)

    # circle
    c = np.linspace(0, np.pi*2, 100)
    r = 1
    c_x = r * np.cos(c)
    c_y = r * np.sin(c)
    ax.plot(c_x, c_y, 'g', linestyle='--')

    # points
    t_A = (31/24) * np.pi                # theta A setting
    PA = PointT1(t_A, r)                 # A
    PC = PointT1(t_A-np.pi, r)           # C, determined by A
    ax.plot([PA.x, PC.x], [PA.y, PC.y], 'purple', linewidth=5.0)  # AC
    ax.text(PA.x-0.1, PA.y-0.1, 'A')
    ax.text(PC.x+0.05, PC.y+0.05, 'C')

    PB = PointT2(PC.x, PA.y)             # B, determined by A, C
    ax.plot([PA.x, PB.x], [PA.y, PA.y], 'r', linestyle='--')  # AB
    ax.plot([PB.x, PB.x], [PB.y, PC.y], 'purple', linestyle='--')  # BC
    ax.text(PB.x+0.05, PB.y-0.1, 'B')

    PG = PointT2(PA.x, PC.y)             # G, determined by A, C
    ax.plot([PA.x, PG.x], [PA.y, PG.y], 'purple', linestyle='--')  # AG
    ax.plot([PC.x, PG.x], [PC.y, PG.y], 'purple', linestyle='--')  # CG
    ax.text(PG.x-0.1, PG.y+0.05, 'G')

    t_E = (47/24) * np.pi                # theta E setting
    PE = PointT1(t_E, r)                 # E
    ax.plot([PA.x, PE.x], [PA.y, PE.y], 'r', linewidth=5.0)  # AE
    ax.plot([PC.x, PE.x], [PC.y, PE.y], 'b', linewidth=5.0)  # CE
    ax.text(PE.x+0.05, PE.y, 'E')

    PD = PointT2(PE.x, PC.y)             # D, determined by C, E
    ax.plot([PC.x, PD.x], [PC.y, PD.y], 'b', linestyle='--')  # CD
    ax.plot([PD.x, PE.x], [PD.y, PE.y], 'b', linestyle='--')  # DE
    ax.text(PD.x+0.05, PD.y+0.05, 'D')

    PF = PointT2(PE.x, PB.y)             # F, determined by B, E
    ax.plot([PB.x, PF.x], [PB.y, PF.y], 'r', linestyle='--')  # BF
    ax.plot([PE.x, PF.x], [PE.y, PF.y], 'r', linestyle='--')  # EF
    ax.text(PF.x+0.05, PF.y-0.1, 'F')

    # angles
    ax.text(PA.x+0.25, PA.y+0.20, r'$\alpha$')
    ax.text(PA.x+0.35, PA.y+0.05, r'$\beta$')
    ax.text(PE.x-0.08, PE.y+0.25, r'$\beta$')
    ax.text(PC.x-0.35, PC.y-0.10, r'$\alpha+\beta$')

    # edges
    ax.text((PA.x+PE.x)/2-0.2, (PA.y+PE.y)/2,
            r'$\cos\alpha$', color='r')
    ax.text((PA.x+PF.x)/2-0.1, (PA.y+PF.y)/2,
            r'$\cos\alpha\cos\beta$', color='r')
    ax.text((PE.x+PF.x)/2, (PE.y+PF.y)/2,
            r'$\cos\alpha\sin\beta$', color='r')

    ax.text((PC.x+PE.x)/2-0.2, (PC.y+PE.y)/2,
            r'$\sin\alpha$', color='b')
    ax.text((PD.x+PE.x)/2, (PD.y+PE.y)/2,
            r'$\sin\alpha\cos\beta$', color='b')
    ax.text((PC.x+PD.x)/2-0.15, (PC.y+PD.y)/2,
            r'$\sin\alpha\sin\beta$', color='b')

    ax.text((PC.x+PG.x)/2-0.2, (PC.y+PG.y)/2,
            r'$\cos(\alpha+\beta)$', color='purple')
    ax.text((PA.x+PG.x)/2-0.4, (PA.y+PG.y)/2,
            r'$\sin(\alpha+\beta)$', color='purple')

    ax.set_aspect(1)  # height : width
    ax.set_axis_off()

    return


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

Subplot_1()

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

解說:


-----

References


[1] What are Angle Difference Formulas? Examples

https://www.cuemath.com/angle-difference-formula/


[2] 7.2: Sum and Difference Identities - Mathematics LibreTexts

https://math.libretexts.org/Bookshelves/Precalculus/Precalculus_(OpenStax)/07%3A_Trigonometric_Identities_and_Equations/7.02%3A_Sum_and_Difference_Identities


[3] 三角函數的和差角公式

http://www.mathland.idv.tw/fun/triiden.htm


# dashed

[4] How to use linestyles in Matplotlib

https://www.educative.io/answers/how-to-use-linestyles-in-matplotlib


# grey

[5] matplotlib.pyplot.plot — Matplotlib 3.6.3 documentation

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


[6] 如何在 Matplotlib 圖例中設定線的線寬 | D棧 - Delft Stack

https://www.delftstack.com/zh-tw/howto/matplotlib/how-to-change-line-width-of-lines-of-in-matplotlib-legend/


# class、object、__init__()、method、self、del

[7] Python Classes

https://www.w3schools.com/python/python_classes.asp


# inheritance、parent class、child class、super()

[8] Python Inheritance

https://www.w3schools.com/python/python_inheritance.asp

-----

Python 量子運算(目錄)

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

-----

Python 量子運算(一五):內積

 Python 量子運算(一五):內積

2023/01/11

-----


Fig. 15.1. Inner product.

-----

代碼 15.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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Program 15.1:Inner product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


class Vec:
    def __init__(self, t1, t2, v_r, a_r):
        self.t1 = t1    # theta 1
        self.t2 = t2    # theta 2

        self.v_r = v_r  # vector radius
        self.v_x = v_r * np.cos(t2)
        self.v_y = v_r * np.sin(t2)

        self.a = np.linspace(t1, t2, 100)  # arc
        self.a_r = a_r                     # arc radius
        self.a_x = a_r * np.cos(self.a)
        self.a_y = a_r * np.sin(self.a)


def Subplot_1():
    ax = plt.subplot(221)

    # coordinates
    ax.plot([-0.1, 1], [0, 0], 'k')
    ax.plot([0, 0], [-0.1, 1], 'k')

    # vector setting
    v1 = Vec(0, np.pi/3, 1, 0.6)
    v2 = Vec(0, np.pi/12, 1, 0.5)
    v3 = Vec(np.pi/12, np.pi/3, 1, 0.4)

    # vector x
    plt.quiver(0, 0, v1.v_x, v1.v_y, scale=1.25, color='r')
    ax.plot([0, v1.v_x], [v1.v_y, v1.v_y], 'r', linestyle=':')
    ax.plot([v1.v_x, v1.v_x], [0, v1.v_y], 'r', linestyle=':')
    ax.text(v1.v_x+0.05, v1.v_y+0.05, r"$\mathbf{x}$", color='r', fontsize=32)
    ax.text(v1.v_x-0.05, -0.1, r"$x1$", color='r', fontsize=32)
    ax.text(-0.15, v1.v_y, r"$x2$", color='r', fontsize=32)

    # vector y
    plt.quiver(0, 0, v2.v_x, v2.v_y, scale=1.2, color='b')
    ax.plot([0, v2.v_x], [v2.v_y, v2.v_y], 'b', linestyle=':')
    ax.plot([v2.v_x, v2.v_x], [0, v2.v_y], 'b', linestyle=':')
    ax.text(v2.v_x+0.05, v2.v_y+0.05, r"$\mathbf{y}$", color='b', fontsize=32)
    ax.text(v2.v_x-0.05, -0.1, r"$y1$", color='b', fontsize=32)
    ax.text(-0.15, v2.v_y, r"$y2$", color='b', fontsize=32)

    # arc
    ax.plot(v1.a_x, v1.a_y, 'r')
    ax.text(0.36, 0.36, r"$\alpha$", color='r', fontsize=32)
    ax.plot(v2.a_x, v2.a_y, 'b')
    ax.text(0.41, 0.03, r"$\beta$", color='b', fontsize=32)
    ax.plot(v3.a_x, v3.a_y, 'k')
    ax.text(0.11, 0.11, r"$\alpha - \beta$", color='k', fontsize=32)

    ax.text(0.5, -0.34, '(a)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_2():
    ax = plt.subplot(222)

    # string setting
    s1_1 = r'$\mathbf{x}=(x_1,\cdots,x_n),$'
    s1_2 = r'$\mathbf{y}=(y_1,\cdots,y_n).$'

    s2_1 = r'$\mathbf{x} \cdot \mathbf{y}$'
    s2_2 = r'$=x_1y_1+ \cdots +x_ny_n$'
    s2_3 = r'$=\sum_{i=1}^n x_iy_i.$'

    s3_1 = r'$\mathbf{x}^T\mathbf{y}$'
    s3_2 = r'$=\begin{bmatrix}x_1 \cdots x_n\end{bmatrix}$'\
           r'$\begin{bmatrix}y_1\\ \vdots \\y_n\end{bmatrix}$'
    s3_3 = r'$=\sum_{i=1}^n x_iy_i$.'

    # string output
    ax.text(0.1, 0.75, s1_1)
    ax.text(0.1, 0.63, s1_2)

    ax.text(0.1, 0.45, s2_1)
    ax.text(0.3, 0.45, s2_2)
    ax.text(0.3, 0.33, s2_3)

    ax.text(0.1, 0.15, s3_1)
    ax.text(0.3, 0.15, s3_2)
    ax.text(0.3, 0.03, s3_3)

    ax.text(0.5, -0.15, '(b)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_3():
    ax = plt.subplot(223)

    # string setting
    s1 = r'$\|\mathbf{x}\|=\sqrt{x_1^2+x_2^2},$'
    s2_1 = r'$\langle \mathbf{x}, \mathbf{y} \rangle$'
    s2_2 = r'$=\|\mathbf{x}\| \cos(\alpha - \beta) \|\mathbf{y}\|$'
    s2_3 = r'$=x_1y_1+x_2y_2.$'

    # string output
    ax.text(0.10, 0.75, s1)

    ax.text(0.20, 0.55, s2_1)
    ax.text(0.10, 0.35, s2_2)
    ax.text(0.10, 0.15, s2_3)

    ax.text(0.5, -0.15, '(c)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_4():
    ax = plt.subplot(224)

    # string setting
    s1_1 = r'$\cos (\alpha - \beta)$'
    s1_2 = r'$=\cos\alpha\cos\beta+\sin\alpha\sin\beta$'
    s1_3 = r'$=\frac{x_1}{\|\mathbf{x}\|}\frac{y_1}{\|\mathbf{y}\|}$'\
           r'$+\frac{x_2}{\|\mathbf{x}\|}\frac{y_2}{\|\mathbf{y}\|}$'
    s1_4 = r'$=\frac{x_1y_1+x_2y_2}{\|\mathbf{x}\|\|\mathbf{y}\|}.$'

    # string output
    ax.text(0.20, 0.75, s1_1)
    ax.text(0.10, 0.55, s1_2)
    ax.text(0.10, 0.35, s1_3)
    ax.text(0.10, 0.15, s1_4)

    ax.text(0.5, -0.15, '(d)', fontsize=20)
    ax.set_axis_off()

    return


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

Subplot_1()
Subplot_2()
Subplot_3()
Subplot_4()

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

解說:

-----

References


[1] Inner Product -- from Wolfram MathWorld

https://mathworld.wolfram.com/InnerProduct.html


[2] 內積的定義 | 線代啟示錄

https://ccjou.wordpress.com/2010/01/27/%E5%85%A7%E7%A9%8D%E7%9A%84%E5%AE%9A%E7%BE%A9/


[3] python 兩點連線matplotlib_yanni0616的博客-CSDN博客_python兩點之間連線

https://blog.csdn.net/yanni0616/article/details/99696020


[4] Python Classes

https://www.w3schools.com/python/python_classes.asp


# norm

# \|

[5] How to write norm symbol in LaTeX like ||a||?

https://www.physicsread.com/latex-norm-symbol/


# 向量

[6] 「LaTeX」LaTeX 中三種向量表示:粗體1,粗體2,箭頭向量- 嗶哩嗶哩

https://www.bilibili.com/read/cv3599113

-----

Python 量子運算(目錄)

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

-----

Python 量子運算(一四):點積

Python 量子運算(一四):點積

2023/01/03

-----


Fig. 14.1. Dot product.

-----

代碼 14.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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Program 14.1:Dot product
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


def Subplot_1():
    ax = plt.subplot(221)

    # basic setting
    theta = np.linspace(0, np.pi/2, 100)
    axis = np.linspace(0, 1, 100)
    zero = axis * 0
    radius = 0.7
    x1 = radius * np.cos(theta)
    y1 = radius * np.sin(theta)

    # basic plotting
    ax.plot(axis, zero, 'k')  # y = 0(x_axis)
    ax.plot(zero, axis, 'k')  # x = 0(y_axis)
    ax.plot(x1, y1, 'k')      # cicle

    # vector setting
    coordinates = [0, 0]      # original point
    splt_radian = np.pi / 4   # subplot

    # vector plotting
    v_p1 = splt_radian
    v_p2 = 0
    vector_1 = [radius*np.cos(v_p1), radius*np.sin(v_p1)]  # direction
    vector_2 = [radius*np.cos(v_p2), radius*np.sin(v_p2)]  # direction
    plt.quiver(coordinates[0], coordinates[1], vector_1[0], vector_1[1],
               scale=1.15, color='r')
    plt.quiver(coordinates[0], coordinates[1], vector_2[0], vector_2[1],
               scale=1.1, color='b')
    plt.quiver(coordinates[0], coordinates[1]-0.05, vector_1[0], vector_2[1],
               scale=1.15, color='k')

    # red line
    r_line_y = np.linspace(0, radius*np.sin(1.05*splt_radian), 100)
    r_line_x = r_line_y * 0 + radius * np.cos(1.05*splt_radian)
    ax.plot(r_line_x, r_line_y, 'r', linestyle=':')

    # red arc
    phi = np.linspace(0, splt_radian, 100)
    x2 = 0.2 * np.cos(phi)
    y2 = 0.2 * np.sin(phi)
    ax.plot(x2, y2, 'r')

    ax.text(0.25, 0.1, r"$\theta=45^{\circ}$", fontsize='20')
    ax.text(radius+0.1, 0.05, r"$\vec u=(1,0)$", color='b')
    ax.text(radius*np.cos(v_p1)+0.1, radius*np.sin(v_p1),
            r"$\vec v=(\frac{\sqrt 2}{2},\frac{\sqrt 2}{2})$", color='r')
    ax.text(radius*np.cos(v_p1)+0.1, -0.15,
            r"$\vec w=(\frac{\sqrt 2}{2},0)$", color='k')
    ax.text(0.5, -0.28, '(a)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_2():
    ax = plt.subplot(222)

    # string setting
    s1 = r'${\rm cos}\ \theta=\frac{\vert \vec w\vert}{\vert \vec v\vert}$'
    s2 = r'${\vert \vec w\vert}={\vert \vec v\vert}{\rm cos}\ \theta$'
    s3 = r'$\vec u \cdot \vec v={\vert \vec u\vert}{\vert \vec w\vert}=$'\
         r'${\vert \vec u\vert}{\vert \vec v\vert}{\rm cos}\ \theta$'

    # string output
    ax.text(0.10, 0.75, s1)
    ax.text(0.10, 0.45, s2)
    ax.text(0.10, 0.15, s3)
    ax.text(0.5, -0.15, '(b)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_3():
    ax = plt.subplot(223)

    # string setting
    s1_1 = r'$\vec v\cdot \vec u$'
    s1_2 = r'$=\begin{bmatrix}\sqrt2/2\quad \sqrt2/2\end{bmatrix}$'\
           r'$\begin{bmatrix}1\\0\end{bmatrix}$'
    s2 = r'$=\frac{\sqrt 2}{2}*1+\frac{\sqrt 2}{2}*0$'
    s3 = r'$=\frac{\sqrt 2}{2}$'

    # string output
    ax.text(0.10, 0.75, s1_1)
    ax.text(0.30, 0.75, s1_2)
    ax.text(0.30, 0.45, s2)
    ax.text(0.30, 0.15, s3)
    ax.text(0.5, -0.15, '(c)', fontsize=20)
    ax.set_axis_off()

    return


def Subplot_4():
    ax = plt.subplot(224)

    # string setting
    s1_1 = r'$\vec u\cdot \vec v$'
    s1_2 = r'$=\begin{bmatrix}1\quad 0\end{bmatrix}$'\
           r'$\begin{bmatrix}\sqrt2/2\\\sqrt2/2\end{bmatrix}$'
    s2 = r'$=1*\frac{\sqrt 2}{2}+0*\frac{\sqrt 2}{2}$'
    s3 = r'$=\frac{\sqrt 2}{2}$'

    # string output
    ax.text(0.10, 0.75, s1_1)
    ax.text(0.30, 0.75, s1_2)
    ax.text(0.30, 0.45, s2)
    ax.text(0.30, 0.15, s3)
    ax.text(0.5, -0.15, '(d)', fontsize=20)
    ax.set_axis_off()

    return


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

Subplot_1()
Subplot_2()
Subplot_3()
Subplot_4()

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

解說:

-----

References


[1] Dot Product -- from Wolfram MathWorld

https://mathworld.wolfram.com/DotProduct.html


[2] Engineering Math | ShareTechnote

https://www.sharetechnote.com/html/Handbook_EngMath_Matrix_InnerProduct.html


[3] Dot Product of Two Vectors – GeoGebra

https://www.geogebra.org/m/e95pnpwk


[4] Dot Product vs Cross Product - Difference & Similarities in Tabular Form - Physics In My View

https://physicsinmyview.com/2020/10/dot-product-vs-cross-product.html

-----

Python 量子運算(目錄)

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

-----

2023年2月8日 星期三

內惟藝術中心

 內惟藝術中心

2023/02/08

-----



-----