owned this note
owned this note
Published
Linked with GitHub
# 向量、長度、角度

This work by Jephian Lin is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).
{%hackmd 5xqeIJ7VRCGBfLtfMi0_IQ %}
```python
from lingeo import random_int_vec, draw_two_vec
```
## Main idea
A **vector** over $\mathbb{R}$ of dimension $n$ is a sequence ${\bf x} = (x_1, \ldots, x_n)$ of numbers in $\mathbb{R}$.
(In contrast, we often call a number in $\mathbb{R}$ a **scalar**.)
The collection of all vectors over $\mathbb{R}$ of dimension $n$ is denoted by $\mathbb{R}^n$.
We often use ${\bf 0}$ and ${\bf 1}$ to refer to $(0,\ldots,0)$ and $(1,\ldots,1)$ of appropriate dimensions, respectively.
The **length** of a vector ${\bf x} = (x_1,\ldots, x_n)$ is defined as
$$\|{\bf x}\| = \sqrt{x_1^2 + \cdots + x_n^2}.
$$
The **inner product** of two vectors ${\bf x} = (x_1, \ldots, x_n)$ and ${\bf y} = (y_1, \ldots, y_n)$ is defined as
$$\langle{\bf x},{\bf y}\rangle = x_1y_1 + \cdots + x_ny_n.
$$
We have the following properties:
1. $\|{\bf x}\| \geq 0$ and the equality holds if and only if ${\bf x} = {\bf 0}$.
2. $\|k{\bf x}\| = |k|\cdot\|{\bf x}\|$.
3. $\|{\bf x}\| + \|{\bf y}\| \geq \|{\bf x} - {\bf y}\|$ (triangle inequality).
4. $\|{\bf x}\|^2 = \langle{\bf x},{\bf x}\rangle$.
5. $\langle{\bf x}_1 + {\bf x}_2,{\bf y}\rangle = \langle{\bf x}_1,{\bf y}\rangle + \langle{\bf x}_2,{\bf y}\rangle$.
6. $\langle k{\bf x},{\bf y}\rangle = k\langle{\bf x},{\bf y}\rangle$.
7. $\langle {\bf x},{\bf y}\rangle = \langle {\bf y},{\bf x}\rangle$.
8. $|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$ (Cauchy--Schwarz inequality).
Thanks to the Cauchy--Schwarz inequality, we define the **angle** between two vectors ${\bf x}$ and ${\bf y}$ as the angle $\theta$ such that $\cos\theta = \frac{\langle{\bf x},{\bf y}\rangle}{\|{\bf x}\|\|{\bf y}\|}$.
We ${\bf x}$ and ${\bf y}$ are **orthogonal** if $\langle{\bf x},{\bf y}\rangle = 0$.
## Side stories
- cosine law
- orthogonal
- projection
## Experiments
##### Exercise 1
執行下方程式碼。
令 $O$ 為原點、
$A$ 和 $B$ 分別為 `x`, `y` 的向量終點。
(可以選用自己喜好的 `ramdom_seed`)
```python
### code
set_random_seed(0)
print_ans = False
x = random_int_vec(6)
y = random_int_vec(6)
print("x =", x)
print("y =", y)
pic = draw_two_vec(x, y)
pic.axes(False)
show(pic)
if print_ans:
print("OA =", x.norm())
print("OB =", y.norm())
print("AB =", (x-y).norm())
cos_cos = (x.norm()**2 + y.norm()**2 - (x-y).norm()**2) / 2 / x.norm() / y.norm()
cos_inner = x.inner_product(y) / x.norm() / y.norm()
print("cos by cos law =",
cos_cos, "=",
N(cos_cos))
print("cos by inner product =",
cos_inner, "=",
N(cos_inner))
```
##### Exercise 1(a)
計算 $\overline{OA}$、$\overline{OB}$、和 $\overline{AB}$ 的長度,
並利用**餘絃定理**來計算 $\cos\angle AOB$。
:::warning
- [x] 請把所有算式串成句子,大學開始的數學不會只看算式。比如說:令 $\bx = ...$ 及 $\by = ...$。則 ... <-- 這包含下面每一題。
- [x] 交待每一個數字是怎麼出來的,至少 $\cos$ 值要說明一下。
:::
令 $\bx = (-4,3,5,-5,-5,0) = \overrightarrow{OA}$ 及
${\bf y} = (3,-3,3,4,-4,-3) = \overrightarrow{OB}$。
則 $\|{\overrightarrow{OA}}\| = \sqrt{100} = 10$, $\|{\overrightarrow{OB}}\| = \sqrt{68} = 2\sqrt{17}$。
另一方面,$\overrightarrow{AB} = \by - \bx = (7,-6,-2,9,1,-3)$
所以 $\|\overrightarrow{AB}\| = \sqrt{180} = 6\sqrt{5}$。
最後我們得到 $\cos\angle AOB = \frac{\|{\overrightarrow{OA}}\|^2 + \|{\overrightarrow{OB}}\|^2 - \|{\overrightarrow{AB}}\|^2}{2\|{\overrightarrow{OA}}\|\|{\overrightarrow{OB}}\|} = \frac{-3} {10\sqrt{17}}$。
##### Exercise 1(b)
令 $\theta$ 為 `x`, `y` 之間用內積定出來的夾角﹐
求 $\cos\theta$。
沿用 1(a) 之 $\overrightarrow{OA}$、$\overrightarrow{OB}$:
$$
\overrightarrow{OA} = (-4,3,5,-5,-5,0)\\
\overrightarrow{OB} = (3,-3,3,4,-4,-3)
$$
由內積定義我們得到 $\langle{\overrightarrow{OA}}, {\overrightarrow{OB}}\rangle = -6$。
我們由內積的性質得知
$$-6 = \|{\overrightarrow{OA}}\|\|{\overrightarrow{OB}}\|\cos\angle AOB.$$
最後得到
$$\cos\angle AOB = \frac{-3}{10\sqrt{17}}.$$
##### Exercise 1(c)
前兩題算出來總是一樣嗎?
請用代數的方法證明。
:::warning
- [x] 第二行的一式和二式中間可以再加一項
- [x] 等號不用移出數學模式外
:::
Sample:
It is known that $\overline{OA} = \|{\bf x}\|$, $\overline{OB} = \|{\bf y}\|$, and $\overline{AB} = \|{\bf x} - {\bf y}\|$.
Thus,
$$
\frac{\overline{OA}^2 + \overline{OB}^2 - \overline{AB}^2}{2\overline{OA}\cdot \overline{OB}} =
\frac{\|{\bf x}\|^2 + \|{\bf y}\|^2 - \|{\bf x} - {\bf y}\|^2}{2\|{\bf x}\|\|{\bf y}\|}=\frac{2\langle {\bf x},{\bf y}\rangle}{2\|{\bf x}\|\|{\bf y}\|}=\frac{\langle {\bf x},{\bf y}\rangle}{\|{\bf x}\|\|{\bf y}\|}.
$$
<!--
$\frac{\overline{OA}^2 + \overline{OB}^2 - \overline{AB}^2}{2\overline{OA}\cdot \overline{OB}}
\frac{\|{\bf x}\|^2 + \|{\bf y}\|^2 - \|{\bf x} - {\bf y}\|^2}{2\|{\bf x}\|\|{\bf y}\|}=\frac{2\langle {\bf x},{\bf y}\rangle}{2\|{\bf x}\|\|{\bf y}\|}=
\frac{\langle {\bf x},{\bf y}\rangle}{\|{\bf x}\|\|{\bf y}\|}=\cos\theta$
$\|{\bf x} · {\bf y}\|$=${\|{\bf x}\|\|{\bf y}\|}\cos\theta$
$\cos\theta$=$\frac{\langle {\bf x},{\bf y}\rangle}{\|{\bf x}\|\|{\bf y}\|}$
-->
## Exercises
##### Exercise 2
將每個向量都展開
(像是 ${\bf x} = (x_1,\ldots, x_n)$)﹐
把每個長度和內積的定義也都展開。
證明 Main idea 中最後列出來的那些性質
(除了三角不等式和柯西不等式以外)。
:::warning
- [ ] 寫作盡量避免邏輯符號 [B03](https://sagelabtw.github.io/LA-Tea/style.html)
- [ ] as ${x^2} \geq{\bf 0}$ and the equality holds if and only if ${\bf x} = {\bf 0}$ <-- 這句我不知道和前後文怎麼接起來
- [ ] 我看不懂斷句在哪
:::
(1)
Recall the **length** of a vector ${\bf x} = (x_1,\ldots, x_n)$ is defined as
$$\|{\bf x}\| = \sqrt{x_1^2 + \cdots + x_n^2}.
$$
Also, we know $x^2\geq 0$ for any $x\in\mathbb{R}$ and the equality holds if and only if $x = 0$.
Since $x_1^2 , \cdots, x_n^2 \geq{\bf 0}$, we have $\|\bx\| \geq 0$.
Moveover, when $\|{\bf x}\| = \sqrt{x_1^2 + \cdots + x_n^2} = 0$, ${x_1^2 , \cdots, x_n^2}$ must all be $0$, which made ${\bf x} = {\bf 0}$.
:::warning
- [x] 藉由代數計算,可以直接驗證 $\|k\bx\| = ...$ 句點
- [x] 試著用 `aligned` 對齊
:::
(2)
藉由代數計算,可以直接驗證
$$
\begin{aligned}
\|k{\bf x}\| &= \sqrt{{(kx_1)^2} + \cdots + {(kx_n)^2}} \\
&= |k|\sqrt{x_1^2 + \cdots + x_n^2} \\
&= |k|\cdot\|{\bf x}\|,
\end{aligned}
$$
其中 $\bx = (x_1,\ldots, x_n)$。
:::warning
- [x] 加文字,後面幾題都是。
:::
(4)
藉由代數計算,可以直接驗證
$$
\langle{\bf x},{\bf x} \rangle = {x_1^2 + \cdots + x_n^2} = \|{\bf x}\|^2.
$$
(5)
藉由代數計算,可以直接驗證
$$
\begin{aligned}
\langle {\bf x}_1 + {\bf x}_2,{\bf y}\rangle
&=({\bf x}_{11} + {\bf x})_{21}{\bf y} + \cdots + ({\bf x}_{1n} + {\bf x}_{2n})\by_n \\
&=({\bf x}_{11}{\bf y} + \cdots + {\bf x}_{1n}{\bf y}_n) + ({\bf x}_{21}{\bf y} + \cdots + {\bf x}_{2n}{\bf y}_{n})\\
&=\langle \bx_1 , \by \rangle + \langle \bx_2 , \by \rangle.
\end{aligned}
$$
:::warning
- [x] $\langle k{\bf x},{\bf y}\rangle = k\langle{\bf x},{\bf y}\rangle$ --> 我們要驗證 $\langle k{\bf x},{\bf y}\rangle = k\langle{\bf x},{\bf y}\rangle$。
- [x] 假設 --> 令
- [x] , --> ,則
- [x] 最後句點
- [x] 下一題類似
:::
(6)我們要驗證 $\langle k{\bf x},{\bf y}\rangle = k\langle{\bf x},{\bf y}\rangle$。
令 ${\bf x} = (x_1,\ldots, x_n)$,${\bf y} = (y_1,\ldots, y_n)$。
藉由代數運算,可以直接驗證
$$
\begin{aligned}
\langle k{\bf x},{\bf y}\rangle
&= kx_1y_1 + \cdots + kx_ny_n\\
&= k(x_1y_1 + \cdots + x_ny_n)\\
&=k\langle{\bf x},{\bf y}\rangle.
\end{aligned}
$$
(7)我們要驗證 $\langle{\bf x},{\bf y}\rangle = \langle{\bf y},{\bf x}\rangle$。
令 ${\bf x} = (x_1,\ldots, x_n)$,${\bf y} = (y_1,\ldots, y_n)$。
將 $\langle{\bf x},{\bf y}\rangle$ 展開得到 $x_1y_1+\cdots+x_ny_n$,
再利用乘法交換律將式子改寫成 $y_1x_1+\cdots+y_nx_n = \langle{\bf y},{\bf x}\rangle. $
##### Exercise 3
證明 $\|{\bf x} \pm {\bf y}\|^2 = \|{\bf x}\|^2 \pm 2\langle{\bf x},{\bf y}\rangle + \|{\bf y}\|^2$。
:::warning
- [x] 假設 --> 令
- [x] 缺標點 [B01](https://sagelabtw.github.io/LA-Tea/style.html)
- [x] 用 `aligned`
:::
令 ${\bf x} = (x_1,\ldots, x_n)$,${\bf y} = (y_1,\ldots, y_n)$,
則$\|{\bf x} \pm {\bf y}\|^2=\sqrt{(x_1 \pm y_1)^2 + \cdots + (x_n \pm y_n)^2}^2$,
將平方展開後得 $(x_1^2 \pm 2x_1y_1 + y_1^2) + \cdots + (x_n^2 \pm 2x_ny_n + y_n^2)$,
再將式子整理後得到
$$
\sqrt{x_1^2 + \cdots + x_n^2}^2 \pm 2(x_1y_1 + \cdots + x_ny_n) + \sqrt{y_1^2 + \cdots + y_n^2}^2 = \|{\bf x}\|^2 \pm 2\langle{\bf x},{\bf y}\rangle + \|{\bf y}\|^2.
$$
##### Exercise 4
給定兩個向量 ${\bf x},{\bf y}$。
若 ${\bf y}$ 可以寫成兩個分量相加 ${\bf y} = {\bf h} + {\bf w}$﹐
其中 ${\bf h}$ 和 ${\bf x}$ 垂直(所以 $\langle{\bf h},{\bf x}\rangle = 0$)、
而 ${\bf w}$ 和 ${\bf x}$ 平行(所以 ${\bf w} = k{\bf x}$),
求 ${\bf h}$ 和 ${\bf w}$(用 ${\bf x}$ 和 ${\bf y}$ 表示出來)。
:::warning
- [x] 標點
- [x] 第二行不用一直跳出數學模式
- [x] 用 `frac{}{}`
- [x] 那個"則"有點太快了
:::
假設 ${\bf w} = k{\bf x}$
而 ${\bf y} - {\bf w} ={\bf h}$。
已知 $\langle{\bf h},{\bf x}\rangle = 0 = \langle{\bf y} - {\bf w},{\bf x}\rangle = \langle{\bf y} - k{\bf x},{\bf x}\rangle$。
藉由代數運算得 $0 = \langle{\bf y} - k{\bf x},{\bf x}\rangle = \langle{\bf y},{\bf x}\rangle - k\langle{\bf x},{\bf x}\rangle$,所以 $k = \frac{\langle{\bf y},{\bf x}\rangle}{\langle{\bf x},{\bf x}\rangle}$。
將 $k$ 帶入原式得 ${\bf w} = {\bf x}$$\frac{\langle{\bf y},{\bf x}\rangle}{\langle{\bf x},{\bf x}\rangle}$
以及 ${\bf h} = {\bf y} - {\bf x}$$\frac{\langle{\bf y},{\bf x}\rangle}{\langle{\bf x},{\bf x}\rangle}$。
##### Exercise 5
依照以下步驟證明三角不等式和柯西不等式等價。
**三角不等式**
對任意兩個向量 ${\bf x},{\bf y}\in\mathbb{R}^n$
不等式 $\|{\bf x}\| + \|{\bf y}\| \geq \|{\bf x} - {\bf y}\|$ 皆成立。
**柯西不等式**
對任意兩個向量 ${\bf x},{\bf y}\in\mathbb{R}^n$
不等式 $|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$ 皆成立。
##### Exercise 5(a)
證明若三角不等式成立則柯西不等式成立。
:::warning
- [x] $\by$ 是向量怎麼會有正的負的...。開頊可以寫,因為三角不等式對所有 $\bx$ 和 $\by$ 都成立,我們可以把 $\by$ 的位置代入 $-\by$ 得到 $\|\bx\| + \|\by\| \geq \|\bx + \by\|$,因此
$$\|\bx\| + \|\by\| \geq \|\bx \pm \by\|.$$
- [x] 用文字串起來:把三角不等式的兩邊同時平方得到 ...,這等價於 ...,因此 ... ,也就是 ... 。
- [ ] 中英不要雜夾
:::
Sample:
Let ${\bf x}$ and ${\bf y}$ be any given vectors in $\mathbb{R}^n$.
By the triangle inequality, $\|{\bf x}\| + \|{\bf y}\| \geq \|{\bf x} - {\bf y}\|$.
因為三角不等式對所有 $\bx$ 和 $\by$ 都成立,我們可以把 $\by$ 的位置代入 $-\by$ 得到 $\|\bx\| + \|\by\| \geq \|\bx + \by\|$,因此
$$\|\bx\| + \|\by\| \geq \|\bx \pm \by\|.$$
把三角不等式的兩邊同時平方得到
($\|{\bf x}\| + \|{\bf y}\|)^2 \geq (\|{\bf x} \pm {\bf y}\|)^2$,
這等價於
$\|{\bf x}\|^2 + \|{\bf y}\|^2+2\|{\bf x}\|\|{\bf y}\|\geq \|{\bf x}\|^2+ \|{\bf y}\|^2\pm 2\langle {\bf x},{\bf y}\rangle$。
因此 $2\|{\bf x}\|\|{\bf y}\|\geq \pm2\langle {\bf x},{\bf y}\rangle$,
也就是 $\|{\bf x}\|\|{\bf y}\|\geq \pm\langle {\bf x},{\bf y}\rangle$。
Therefore, $|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$.
Since ${\bf x}$ and ${\bf y}$ are arbitrary, the Cauchy--Schwarz inequality holds for any vectors ${\bf x}$ and ${\bf y}$.
##### Exercise 5(b)
證明若柯西不等式成立則三角不等式成立。
:::warning
- [x] 怎麼會開頭就 We know that $\|{\bf x}\| + \|{\bf y}\| \geq \|{\bf x} - {\bf y}\|$ 。如果知道了就不用證了。
- [x] 把因果關係和每一步講清楚。
:::
Sample:
Let ${\bf x}$ and ${\bf y}$ be any given vectors in $\mathbb{R}^n$.
By the Cauchy--Schwarz inequality, $|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$.
Thus,
$\|{\bf x}\|\|{\bf y}\|\geq \pm\langle {\bf x},{\bf y}\rangle$.
By adding $\|{\bf x}\|^2 + \|{\bf y}\|^2$ on both sides, we get
$\|{\bf x}\|^2 + \|{\bf y}\|^2+2\|{\bf x}\|\|{\bf y}\|\geq \|{\bf x}\|^2+ \|{\bf y}\|^2 \pm\ 2\langle {\bf x},{\bf y}\rangle$.
By completing the squares, we have
($\|{\bf x}\| + \|{\bf y}\|)^2 \geq (\|{\bf x} - {\bf y}\|)^2$.
While both $\|{\bf x}\| + \|{\bf y}\|$ and $\|{\bf x} - {\bf y}\|$
are positive, $\|{\bf x}\| + \|{\bf y}\| \geq \|{\bf x} - {\bf y}\|$.
Since ${\bf x}$ and ${\bf y}$ are arbitrary, the triangle inequality holds for any vectors ${\bf x}$ and ${\bf y}$.
##### Exercise 6
依照以下步驟證明柯西不等式。
##### Exercise 6(a)
證明當 $\|{\bf x}\| = \|{\bf y}\| = 1$ 時柯西不等式成立。
:::warning
- [x] 沒標點,句子不完整,前後接不上
- [x] 中英夾雜
:::
Sample:
Let ${\bf x}$ and ${\bf y}$ be two vectors of length $1$.
Since $\|{\bf x} - {\bf y}\|^2 \geq 0$, we have
$\|{\bf x}\|^2+ \|{\bf y}\|^2 - 2\langle {\bf x},{\bf y}\rangle\geq 0$.
Since $\|{\bf x} + {\bf y}\|^2 \geq 0$, then
$\|{\bf x}\|^2+ \|{\bf y}\|^2$+$2\langle {\bf x},{\bf y}\rangle\geq 0$.
Therefore, $\pm2\langle {\bf x},{\bf y}\rangle\geq -2$ and $2 \geq\pm2\langle {\bf x},{\bf y}\rangle$.
Therefore, $|\langle{\bf x},{\bf y}\rangle| \leq 1$ whenever $\|{\bf x}\| = \|{\bf y}\| = 1$.
##### Exercise 6(b)
證明對任何非零向量 ${\bf x},{\bf y}\in\mathbb{R}^n$ 柯西不等式皆成立。
:::warning
- [ ] 中英夾雜
- [x] 句子不完整,因果關係
- [x] 下一題也是
:::
Sample:
Now suppose ${\bf x}$ and ${\bf y}$ be arbitrary nonzero vectors in $\mathbb{R}^n$.
Let ${\bf x}' = {\bf x}/\|{\bf x}\|$ and ${\bf y}' = {\bf y}/\|{\bf y}\|$.
Thus, $\|{\bf x}'\| = \|{\bf y}'\| = 1$.
帶回 (a) 得到 $|\langle {\bf x}',{\bf y}'\rangle| \leq \ 1$,
也就是$\frac{|\langle{\bf x}, {\bf y}\rangle|}{\|{\bf x}\|\|{\bf y}\|} \leq \ 1$。
故$|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$。
Therefore, $|\langle {\bf x},{\bf y}\rangle| \leq \|{\bf x}\|\cdot \|{\bf y}\|$ for any nonzero vectors ${\bf x}$ and ${\bf y}$.
##### Exercise 6(c)
考慮剩餘的可能性並完成柯西不等式的證明。
當 $\|{\bf x}\|$ 或 $\|{\bf y}\|$ 等於 $0$ 時,$\|{\bf x}\|\cdot \|{\bf y}\|=0$ 且 $\inp{\bx}{\by} = 0$。
所以 $|\langle {\bf x},{\bf y}\rangle| =\|{\bf x}\|\cdot \|{\bf y}\|$。
**第七題太長所以被移到別區**
[Exercise 7](https://hackmd.io/VUEic-L2TW6QKlzljHplZw)
:::info
差第七題
目前得分 5/5
:::