前往
大廳
主題

01. 旋轉2D向量的方法 - 從矩陣、複數到幾何代數

Lumi | 2022-06-20 20:06:49 | 巴幣 0 | 人氣 198

在往下看之前,請先確保你是使用PC觀看,且讓巴哈姆特能顯示數學式子


此文嘗試用常見的應用來簡單介紹幾何代數(Geometric algebra)。相較於矩陣與複數,幾何代數的演算法有更簡單易懂的優點。只是若用筆算,將式子展開後的計算有點繁瑣,或許這是它一百多年來被打入冷宮的原因之一。但現今我們有了程式語言,便可去除缺點而保留優點。



旋轉矩陣

給定二維向量$\textbf{a} = (a_1, a_2)$,將它逆時針旋轉$\theta$度後得到向量$\textbf{a}' = (a'_1, a'_2)$。

旋轉公式如下:
$$
\begin{bmatrix}
a'_1 \\
a'_2\\
\end{bmatrix}
=
\begin{bmatrix}
\cos \theta & -\sin \theta \\
\sin \theta & \cos \theta
\end{bmatrix}
\begin{bmatrix}
a_1 \\
a_2\\
\end{bmatrix} \space
\\
a'_1 = a_1 \cos \theta - a_2 \sin \theta  \\
a'_2 = a_1 \sin \theta + a_2 \cos \theta
$$
維度越高,公式推導與應用就出現越多麻煩。也難怪圖學發展了這麼久,偶爾還是會有人爭論要不要用矩陣來作旋轉。
注意儲存旋轉需要4個元素的矩陣,下面的複數旋轉法只需要兩個元素。



複數

把向量$(a_1, a_2)$寫成複數形式:$a_1 + a_2i$。將向量乘以$e^{i \theta}$便可將向量逆時針旋轉$\theta$度
利用尤拉公式$e^{i \theta} = \cos \theta + i \sin \theta$,可得到:

$$
\begin{aligned}
(a_1 + a_2i) e^{i \theta} &= (a_1 + a_2i)(\cos \theta + i \sin \theta)  \\
&= a_1 \cos \theta + a_1i \sin \theta +  a_2i \cos \theta -  a_2 \sin \theta \\
&= (a_1 \cos \theta -  a_2 \sin \theta) + (a_1 \sin \theta +  a_2 \cos \theta)i
\end{aligned}
$$
可看到使用複數的計算結果與矩陣相同。
複數的作法讓人覺得有點彆扭,為什麼空間中一個軸代表幾何,另一個軸卻是虛數。
接下來介紹的幾何代數有更一致的空間表達方法,比複數表示法更為自然。



幾何代數

應該有不少人對幾何代數很陌生,在我們計算旋轉前,先簡單介紹基本組成要素,還有它們的運算規則和幾何性質。


基底

幾何代數中,所有幾何元素都會寫成基底的線性組合。在2D空間中有兩個軸X與Y,分別對應兩個基底。幾何代數在習慣上不會把基底取名為X、Y,而是$\textbf{e}_1$與$\textbf{e}_2$。因此向量$\textbf{a}$會寫成:
$$\textbf{a} = a_1\textbf{e}_1 + a_2\textbf{e}_2
$$

內積

一般的向量內積和以前學的完全相同。至於$\textbf{e}_1$與$\textbf{e}_2$則可視為單位向量,因此在$\mathbb{R}^2$中,$\textbf{e}_1 \cdot \textbf{e}_1 = \textbf{e}_2 \cdot \textbf{e}_2 = 1$。但$\textbf{e}_1$與$\textbf{e}_2$彼此正交,所以$\textbf{e}_1 \cdot \textbf{e}_2 = 0$。
我們還需要關於角度的性質,給定兩向量$\textbf{u}$與$\textbf{v}$,彼此夾角為$\theta$,則兩向量內積為$\textbf{u} \cdot \textbf{v} = \lVert \textbf{u} \rVert \lVert \textbf{v} \rVert \cos \theta$


外積

此處的外積是outer product,記作$\wedge$。並非以往學的cross product(記作$\times$),也不是線性代數的outer product(兩向量相乘產生矩陣)。幾何代數的外積在Grassmann algebra中稱作wedge product或exterior product。兩向量的外積在幾何代數中被視為兩向量所形成的平面,$\textbf{e}_1 \wedge \textbf{e}_2$可視為單位平面。若兩向量方向相同因而無法形成平面,則外積結果為0,因此$\textbf{e}_1 \wedge \textbf{e}_1 = \textbf{e}_2 \wedge \textbf{e}_2 = 0$。外積有結合律但沒有交換律性質,可視為平面的方向性由向量的順序決定,$\textbf{e}_1 \wedge \textbf{e}_2 = -\textbf{e}_2 \wedge \textbf{e}_1$。
兩向量$\textbf{u}$、$\textbf{v}$的外積展開如下:
$$\begin{aligned}
\textbf{u} \wedge \textbf{v} &= (u_1 \textbf{e}_1 + u_2 \textbf{e}_2) \wedge (v_1 \textbf{e}_1 + v_2 \textbf{e}_2) \\
&= u_1v_1\ \textbf{e}_1 \wedge \textbf{e}_1 + u_1v_2 \ \textbf{e}_1 \wedge \textbf{e}_2 + u_2v_1\ \textbf{e}_2 \wedge \textbf{e}_1 + u_2v_2\ \textbf{e}_2 \wedge \textbf{e}_2 \\
&=(u_1v_2-u_2v_1)\textbf{e}_1 \wedge \textbf{e}_2 \\
&= \det ([\textbf{u}\ \textbf{v}]) \textbf{e}_1 \wedge \textbf{e}_2  \\
&= (\lVert \textbf{u} \rVert \lVert \textbf{v} \rVert \sin \theta) \textbf{e}_1 \wedge \textbf{e}_2
\end{aligned}$$
可觀察到$\textbf{e}_1 \wedge \textbf{e}_2$的係數會是$\textbf{u}$與$\textbf{v}$所形成的平行四邊形的面積,也會是以$\textbf{u}$、$\textbf{v}$為行向量的矩陣的行列式(determinant)。

在幾何代數中,cross product運算被視為對兩向量所形成之平面取正交補餘。由於幾何代數已經包含了cross product的概念,且可以用在所有維度上,因此不再需要cross product了。


幾何積(geometric product)

幾何積在幾何代數中非常普遍,只要看到兩個向量相乘但省略乘號,就是用幾何積來乘。兩個向量的幾何積等於兩個向量的內積加上外積。

下面是基底向量的幾何積:
$$
\textbf{e}_1\textbf{e}_1 = \textbf{e}_1 \cdot \textbf{e}_1 + \textbf{e}_1 \wedge \textbf{e}_1 = 1 + 0 = 1\\
\textbf{e}_1\textbf{e}_2 = \textbf{e}_1 \cdot \textbf{e}_2 + \textbf{e}_1 \wedge \textbf{e}_2 = 0 + \textbf{e}_1 \wedge \textbf{e}_2 = \textbf{e}_1 \wedge \textbf{e}_2
$$
有時數學文章中會省去$\textbf{e}_1 \wedge \textbf{e}_2$式子中的外積符號,索性當成幾何積寫成$\textbf{e}_1\textbf{e}_2$。有時會寫得更簡潔,把$\textbf{e}_1\textbf{e}_2$寫成$\textbf{e}_{12}$。內積符號也有可能省略,例如把$\textbf{e}_1 \cdot \textbf{e}_1$寫成$\textbf{e}_1 \textbf{e}_1$。

$n$維空間中,會把$n$個基底的外積(幾何積)記作$\textbf{I}_n = \textbf{e}_1 \textbf{e}_2 \dots \textbf{e}_{n-1}\textbf{e}_n$,以2D為例就是$\textbf{I}_2 = \textbf{e}_1\textbf{e}_2$。這個大寫$\textbf{I}_2$有一個性質和虛數的$i$相同,也就是$(\textbf{I}_2)^2 = \textbf{e}_1\textbf{e}_2\textbf{e}_1\textbf{e}_2 = -\textbf{e}_1\textbf{e}_1\textbf{e}_2\textbf{e}_2 = -(\textbf{e}_1\textbf{e}_1)(\textbf{e}_2\textbf{e}_2) = -1$。

綜合上述所有性質,我們可以得到向量$\textbf{u}$與$\textbf{v}$的幾何積:
$$
\begin{aligned}
\textbf{uv} &= (u_1\textbf{e}_1 + u_2\textbf{e}_2)(v_1\textbf{e}_1 + v_2\textbf{e}_2) \\
&= u_1v_1\textbf{e}_1\textbf{e}_1 + u_1v_2\textbf{e}_1\textbf{e}_2 + u_2v_1 \textbf{e}_2\textbf{e}_1 + u_2v_2\textbf{e}_2\textbf{e}_2 \\
&= u_1v_1 + u_1v_2\textbf{e}_1\textbf{e}_2 - u_2v_1 \textbf{e}_1\textbf{e}_2 + u_2v_2\\
&= (u_1v_1 + u_2v_2) + (u_1v_2 - u_2v_1)\textbf{e}_1\textbf{e}_2\\
&= \textbf{u} \cdot \textbf{v} + \textbf{u} \wedge \textbf{v} \\
&= \lVert \textbf{u} \rVert \lVert \textbf{v} \rVert  \cos \theta + \lVert \textbf{u} \rVert \lVert \textbf{v} \rVert \sin \theta \textbf{I}_2 \\
&= \lVert \textbf{u} \rVert \lVert \textbf{v} \rVert (\cos \theta + \sin \theta \textbf{I}_2) \\
&= \lVert \textbf{u} \rVert \lVert \textbf{v} \rVert e^{\textbf{I}_2 \theta}
\end{aligned}
$$
可看到複數的旋轉其實包含在幾何代數內,不只如此,$\textbf{uv}$還能用來伸縮向量。但這裡先把$\textbf{u}$與$\textbf{v}$設定為單位向量,試試用來旋轉$\textbf{a}$向量:
$$
\begin{aligned}
\textbf{auv} &= (a_1\textbf{e}_1+a_2\textbf{e}_2)(\cos \theta + \sin \theta \textbf{e}_1\textbf{e}_2) \\
&= a_1 \cos \theta \textbf{e}_1 + a_1 \sin \theta \textbf{e}_1\textbf{e}_1\textbf{e}_2 + a_2 \cos \theta \textbf{e}_2 + a_2\sin \theta \textbf{e}_2\textbf{e}_1\textbf{e}_2 \\
&= a_1 \cos \theta \textbf{e}_1 + a_1 \sin \theta \textbf{e}_2 + a_2 \cos \theta \textbf{e}_2 - a_2\sin \theta \textbf{e}_1 \\
&= (a_1 \cos \theta - a_2\sin \theta)\textbf{e}_1 + (a_1 \sin \theta + a_2 \cos \theta) \textbf{e}_2 \\
\end{aligned}
$$
可見到即使不把向量改寫成複數形式,計算結果與複數和矩陣都相同。因為複數形式與幾何代數的表示方式差了一個$\textbf{e}_1$基底,若選擇$i$ = -$\textbf{I}_2$,向量除以$\textbf{e}_1$可得到複數,複數乘以$\textbf{e}_1$可還原成向量。雖然旋轉效果一樣,但複數形式少了一堆基底種類,基底可用來辨認幾何元素,缺少基底的複數就無法區分向量和旋轉運算子(operator)。

幾何代數會隨著基底數量與基底性質不同,而有不同數學性質,這裡示範的旋轉計算僅是2D的特例,不可直接套用在3D向量上(除非向量$\textbf{a}$位於$\textbf{u} \wedge \textbf{v}$平面上)。幾何代數有更泛用的旋轉公式,可以用在所有維度上,只是要注意它會旋轉兩倍的夾角。因此若要把向量$\textbf{a}$逆時針旋轉$\theta$度,必須將$\textbf{u}$、$\textbf{v}$之間的夾角設為$\frac{\theta}{2}$。泛用的旋轉公式把$\textbf{a}$夾在中間,寫成:$\textbf{(vu)a(uv)}$

由於幾何積沒有交換律性質,先計算$\textbf{vu}$:
$$
\begin{aligned}
\textbf{vu} &= \textbf{v} \cdot \textbf{u} + \textbf{v} \wedge \textbf{u} \\
&= \textbf{u} \cdot \textbf{v} - \textbf{u} \wedge \textbf{v} \\
&= \cos \frac{\theta}{2} - \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2 \\
\end{aligned}
$$
把上式代入$\textbf{(vu)a(uv)}$後可得:
$$
\begin{aligned}
\textbf{vuauv} &= (\cos \frac{\theta}{2} - \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2)(a_1\textbf{e}_1+a_2\textbf{e}_2)(\cos \frac{\theta}{2} + \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2) \\

&= (a_1 \cos \frac{\theta}{2} \textbf{e}_1 - a_1 \sin \frac{\theta}{2} \textbf{e}_1 \textbf{e}_2 \textbf{e}_1 + a_2 \cos \frac{\theta}{2} \textbf{e}_2 - a_2 \sin \frac{\theta}{2} \textbf{e}_1 \textbf{e}_2 \textbf{e}_2)  (\cos \frac{\theta}{2} + \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2) \\

&= (a_1 \cos \frac{\theta}{2} \textbf{e}_1 + a_1 \sin \frac{\theta}{2} \textbf{e}_2 + a_2 \cos \frac{\theta}{2} \textbf{e}_2 - a_2 \sin \frac{\theta}{2} \textbf{e}_1)  (\cos \frac{\theta}{2} + \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2) \\

&= ((a_1 \cos \frac{\theta}{2} - a_2 \sin \frac{\theta}{2}) \textbf{e}_1 + (a_1 \sin \frac{\theta}{2} + a_2 \cos \frac{\theta}{2}) \textbf{e}_2)  (\cos \frac{\theta}{2} + \sin \frac{\theta}{2} \textbf{e}_1\textbf{e}_2) \\

&= (a_1 \cos^2 \frac{\theta}{2} - a_2 \sin \frac{\theta}{2} \cos \frac{\theta}{2}) \textbf{e}_1
+ (a_1 \sin \frac{\theta}{2} \cos \frac{\theta}{2} + a_2 \cos^2 \frac{\theta}{2}) \textbf{e}_2  \\
&+ (a_1 \sin \frac{\theta}{2} \cos \frac{\theta}{2} - a_2 \sin^2 \frac{\theta}{2}) \textbf{e}_1\textbf{e}_1\textbf{e}_2 + (a_1 \sin^2 \frac{\theta}{2} + a_2 \sin \frac{\theta}{2} \cos \frac{\theta}{2})\textbf{e}_2\textbf{e}_1\textbf{e}_2 \\

&= (a_1 \cos^2 \frac{\theta}{2} - a_2 \sin \frac{\theta}{2} \cos \frac{\theta}{2}) \textbf{e}_1
+ (a_1 \sin \frac{\theta}{2} \cos \frac{\theta}{2} + a_2 \cos^2 \frac{\theta}{2}) \textbf{e}_2  \\
&+ (a_1 \sin \frac{\theta}{2} \cos \frac{\theta}{2} - a_2 \sin^2 \frac{\theta}{2}) \textbf{e}_2 - (a_1 \sin^2 \frac{\theta}{2} + a_2 \sin \frac{\theta}{2} \cos \frac{\theta}{2})\textbf{e}_1 \\

&= (a_1 (\cos^2 \frac{\theta}{2} - \sin^2 \frac{\theta}{2}) - a_2 (2\sin \frac{\theta}{2} \cos \frac{\theta}{2})) \textbf{e}_1
+ (a_1 (2\sin \frac{\theta}{2} \cos \frac{\theta}{2}) + a_2(\cos^2 \frac{\theta}{2} - \sin^2 \frac{\theta}{2})) \textbf{e}_2\\

&= (a_1 \cos \theta - a_2\sin \theta)\textbf{e}_1 + (a_1 \sin \theta + a_2 \cos \theta) \textbf{e}_2 \\
\end{aligned}
$$

可以看到結果相同,遺憾的是計算量變多了。幾何代數在組合多個旋轉時,計算量會比矩陣少,但旋轉向量時計算量會輸給矩陣。實作時最好考慮你的應用是哪種運算較多,視情況可兩者都用,讓幾何代數來產生矩陣。

若你曾使用過四元數,應該會發現幾何代數的旋轉法和四元數非常像。但幾何代數在3D中作旋轉,不須把中間的向量改寫成四元數形式,可維持向量進向量出。下一篇介紹幾何代數的旋轉原理,解釋為什麼要把向量夾起來,還有四元數$i,\ j,\ k$等複數奇怪性質的由來。

創作回應

更多創作