changed 3 years ago
Linked with GitHub

將線性函數化為矩陣

Creative Commons License
This work by Jephian Lin is licensed under a Creative Commons Attribution 4.0 International License.

\(\newcommand{\trans}{^\top} \newcommand{\adj}{^{\rm adj}} \newcommand{\cof}{^{\rm cof}} \newcommand{\inp}[2]{\left\langle#1,#2\right\rangle} \newcommand{\dunion}{\mathbin{\dot\cup}} \newcommand{\bzero}{\mathbf{0}} \newcommand{\bone}{\mathbf{1}} \newcommand{\ba}{\mathbf{a}} \newcommand{\bb}{\mathbf{b}} \newcommand{\bc}{\mathbf{c}} \newcommand{\bd}{\mathbf{d}} \newcommand{\be}{\mathbf{e}} \newcommand{\bh}{\mathbf{h}} \newcommand{\bp}{\mathbf{p}} \newcommand{\bq}{\mathbf{q}} \newcommand{\br}{\mathbf{r}} \newcommand{\bx}{\mathbf{x}} \newcommand{\by}{\mathbf{y}} \newcommand{\bz}{\mathbf{z}} \newcommand{\bu}{\mathbf{u}} \newcommand{\bv}{\mathbf{v}} \newcommand{\bw}{\mathbf{w}} \newcommand{\tr}{\operatorname{tr}} \newcommand{\nul}{\operatorname{null}} \newcommand{\rank}{\operatorname{rank}} %\newcommand{\ker}{\operatorname{ker}} \newcommand{\range}{\operatorname{range}} \newcommand{\Col}{\operatorname{Col}} \newcommand{\Row}{\operatorname{Row}} \newcommand{\spec}{\operatorname{spec}} \newcommand{\vspan}{\operatorname{span}} \newcommand{\Vol}{\operatorname{Vol}} \newcommand{\sgn}{\operatorname{sgn}} \newcommand{\idmap}{\operatorname{id}} \newcommand{\am}{\operatorname{am}} \newcommand{\gm}{\operatorname{gm}} \newcommand{\mult}{\operatorname{mult}} \newcommand{\iner}{\operatorname{iner}}\)

from lingeo import random_good_matrix, kernel_matrix

Main idea

Let \(A\) be an \(m\times n\) matrix, \(\mathcal{E}_n = \{ {\bf e}_1, \ldots, {\bf e}_n \}\) the standard basis of \(\mathbb{R}^n\), and \({\bf u}_1, \ldots, {\bf u}_n\) the columns of \(A\).
Recall that \(f_A\) is the unique linear function that satisfies the following conditions.
\[\begin{array}{rcl} f : \mathbb{R}^n & \rightarrow & \mathbb{R}^m \\ {\bf e}_1 & \mapsto & {\bf u}_1 \\ ~ & \cdots & ~ \\ {\bf e}_n & \mapsto & {\bf u}_n \\ \end{array} \]

In fact, every linear function \(f\) from \(\mathbb{R}^n\) to \(\mathbb{R}^m\) has an \(m\times n\) matrix \(A\) such that \(f({\bf v}) = A{\bf v}\) for all \({\bf v}\in\mathbb{R}^n\).
Let \(f\) be a linear function from \(\mathbb{R}^n\) to \(\mathbb{R}^m\) and
\(\mathcal{E}_n = \{ {\bf e}_1, \ldots, {\bf e}_n \}\) the standard basis of \(\mathbb{R}^n\).
Calculate \({\bf u}_1 = f({\bf e}_1)\), \(\ldots\), \({\bf u}_n = f({\bf e}_n)\) and
construct a matrix \(A\) whose columns are \({\bf u}_1, \ldots, {\bf u}_n\).
Thus, \(f({\bf v}) = A{\bf v}\) for all \({\bf v}\in\mathbb{R}^n\), and we call \(A\) the matrix representation of \(f\), denoted as \(A = [f]\).

Dimension theorem (\(\mathbb{R}^n\) to \(\mathbb{R}^m\))

Let \(f\) be a linear function from \(\mathbb{R}^n\) to \(\mathbb{R}^m\).
Then \(\operatorname{rank}(f) + \operatorname{null}(f) = n\).

As a consequence, for a linear function from \(\mathbb{R}^n\) to \(\mathbb{R}^m\), the following are equivalent.

  1. \(f\) is injective.
  2. \(\operatorname{null}(f) = 0\).
  3. \(\operatorname{rank}(f) = n\).

Side stories

  • total derivative

Experiments

Exercise 1

執行以下程式碼。
己知 \(f\) 是從 \(\mathbb{R}^4\)\(\mathbb{R}^3\) 的一個函數。

### code
set_random_seed(0)
print_ans = False
m,n = 3,4
A = matrix(m, random_int_list(m*n))
f = lambda v: A * v

if print_ans:
    print("f(0) = 0?", True)
    print("f(v1 + v2) = f(v1) + f(v2)?", True)
    print("f(k * v) = k * f(v)?", True)
    print("A =")
    show(A)
Exercise 1(a)

驗證是否\(f({\bf 0}) =({\bf 0})\)
注意這裡兩個零向量分別是定義域和對應域上的零向量。

  • 向量用粗體

由驗證得知

\(f({\bf 0})=(0,0,0)\)

得證。

zero4 = vector([0,0,0,0])
f(zero4)
Exercise 1(b)

輸入任意的 \({\bf v}_1, {\bf v}_2\in\mathbb{R}^4\)
驗證明是否 \(f({\bf v}_1 + {\bf v}_2) = f({\bf v}_1) + f({\bf v}_2)\)

由驗證得知

\(f({\bf v}_1)=(-3,-8,-13)\)

\(f({\bf v}_2)=(-1,-5,0)\)

\[f({\bf v}_1+{\bf v}_2)=(-4,-13,-13)=f({\bf v}_1)+f({\bf v}_2) \] 得證。

v1 = vector([1,2,3,4])
v2 = vector([1,1,1,1])
print(f(v1 + v2))
print("%s + %s ="%(f(v1), f(v2)), f(v1) + f(v2))
Exercise 1©

輸入任意的 \(k\in\mathbb{R}\)\({\bf v}\in\mathbb{R}^4\)
驗證明是否 \(f(k{\bf v}) = kf({\bf v})\)

  • 目前空白

由驗證得知

\(f({\bf v}) = (-1,-5,0)\)

\(f(k{\bf v}) = (-3,-15,0) = 3(-1,-5,0) = kf({\bf v})\)

得證。

k = 3
v = vector([1,1,1,1])
print(f(k * v))
print("%s * %s ="%(k, f(v)), k*f(v))
Exercise 1(d)

找到一個矩陣 \(A\) 使得對於所有 \({\bf v}\in\mathbb{R}^4\) 都有 \(f({\bf v}) = A{\bf v}\)

  • 目前空白

假設
\({\bf e}_1 = (1,0,0,0)\)
\({\bf e}_2 = (0,1,0,0)\)
\({\bf e}_3 = (0,0,1,0)\)
\({\bf e}_4 = (0,0,0,1)\)

\({\bf e}_1、{\bf e}_2、{\bf e}_3、{\bf e}_4\) 帶入 \(f(x)\) 函數,得到
\(f({\bf e}_1) = (-4,-5,3)\)
\(f({\bf e}_2) = (3,0,4)\)
\(f({\bf e}_3) = (5,3,-4)\)
\(f({\bf e}_4) = (-5,-3,-3)\)

由上述得知 \[A = \begin{bmatrix} -4 & 3 & 5 & -5\\ -5 & 0 & 3 & -3\\ 3 & 4 & -4 & -3 \end{bmatrix}. \]

e1 = vector([1,0,0,0])
e1 = vector([1,0,0,0])
e1 = vector([1,0,0,0])
e1 = vector([1,0,0,0])
print(f(e1))
print(f(e2))
print(f(e3))
print(f(e4))

Exercises

Exercise 2

考慮以下函數 \(f\)﹐求出矩陣 \(A\) 使得 \(f = f_A\)

Exercise 2(a)

\[f(x,y,z) = (x,y,0). \]
Ans:
\[\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0\\ 0 & 0 & 0 \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} = \begin{bmatrix} x \\ y \\ 0 \end{bmatrix} \] 因此, \(A = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0\\ 0 & 0 & 0 \end{bmatrix}\)

Exercise 2(b)

\[f(x,y,z) = (3x,4y,5z). \]
Ans:
\[\begin{bmatrix} 3 & 0 & 0 \\ 0 & 4 & 0\\ 0 & 0 & 5 \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} = \begin{bmatrix} 3x \\ 4y \\ 5z \end{bmatrix} \] 因此, \(A = \begin{bmatrix} 3 & 0 & 0 \\ 0 & 4 & 0\\ 0 & 0 & 5 \end{bmatrix}\)

Exercise 2©

\[f(x,y,z) = (x+2y+3z,4x+5y+6z,7x+8y+9z). \]
Ans:
\[\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} = \begin{bmatrix} x + 2y + 3z \\ 4x + 5y + 6z \\ 7x + 8y + 9z \end{bmatrix} \] 因此, \(A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}\)

Exercise 2(d)

\[f(x,y,z) = (y,z,x). \] Ans:
\[\begin{bmatrix} 0 & 1 & 0\\ 0 & 0 & 1\\ 1 & 0 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} y \\ z \\ x \end{bmatrix} \] 因此, \(A = \begin{bmatrix} 0 & 1 & 0\\ 0 & 0 & 1\\ 1 & 0 & 0 \end{bmatrix}\)

Exercise 2(e)

函數 \(f\) 把每個 \(\mathbb{R}^3\) 中的向量投影到 \((1,1,1)\) 的方向上。

  • 答案錯誤,先算一下 \((x,y,z)\) 投影到 \((1,1,1)\) 上是什麼

Ans:

\[f(x,y,z) = \frac{1}{3} \begin{bmatrix}x+y+z \\ x+y+z \\x+y+z \end{bmatrix} = \begin{bmatrix}\frac{1}{3} & \frac{1}{3} & \frac{1}{3} \\ \frac{1}{3} & \frac{1}{3} & \frac{1}{3}\ \\ \frac{1}{3} & \frac{1}{3} & \frac{1}{3} \end{bmatrix}\begin{bmatrix}x \\ y \\ z \end{bmatrix}\] 因此,\[A = \begin{bmatrix}\frac{1}{3} & \frac{1}{3} & \frac{1}{3} \\ \frac{1}{3} & \frac{1}{3} & \frac{1}{3}\ \\ \frac{1}{3} & \frac{1}{3} & \frac{1}{3} \end{bmatrix}. \]

Exercise 2(f)

函數 \(f\) 把每個 \(\mathbb{R}^3\) 中的向量沿著 \(z\) 軸逆時鐘旋轉 \(45^\circ\)
(這裡的旋轉是以北極往南看的逆時鐘。)

  • 答案錯誤,先找一下 \(45^\circ\) 的旋轉矩陣是什麼

Ans:
\[f(x,y,z) = (\frac{\sqrt{2}}{2}x + \frac{\sqrt{2}}{2}y , \frac{\sqrt{2}}{2}x - \frac{\sqrt{2}}{2}y , z).\] \[\begin{bmatrix} \frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2} & 0\\ \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0\\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} \frac{\sqrt{2}}{2}x - \frac{\sqrt{2}}{2}y \\ \frac{\sqrt{2}}{2}x + \frac{\sqrt{2}}{2}y \\ z \end{bmatrix} \] 因此,\[A = \begin{bmatrix} \frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2} & 0\\ \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0\\ 0 & 0 & 1 \end{bmatrix}。\]

Exercise 3

\(f\) 是一個 \(\mathbb{R}^n\)\(\mathbb{R}^m\) 的可微分函數(不一定線性)﹐
\(f\) 可以寫成 \[f(x_1,\ldots, x_n) = (f_1(x_1,\ldots, x_n), \ldots, f_m(x_1,\ldots, x_n)).\] \(f\) 的全微分為
\[\begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \vdots & \cdots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n} \\ \end{bmatrix}. \]

Exercise 3(a)

微分的用意是希望函數的區部性質非常接近線性函數。
說明為什麼全微分會被定為一個 \(m\times n\) 矩陣而不是一個 \(n\times m\) 矩陣。

Ans:
因為一個 \(m\times n\) 矩陣才能表示一個從 \(\mathbb{R}^n\)\(\mathbb{R}^m\) 的函數,
所以, \(f\) 的全微分必為 \(m\times n\) 的矩陣。

Exercise 3(b)

\(A\) 為一 \(m\times n\) 矩陣而 \({\bf b}\in\mathbb{R}^m\)
定義 \(f({\bf x}) = A{\bf x} + {\bf b}\)
\(f\) 的全微分。

  • \(A{\bf x} =\)

\[ \begin{bmatrix} {x_1}a_{11} + \cdots + {x_m}a_{1n} \\ \vdots \\ {x_m}a_{m1} + \cdots + {x_m}a_{mn} \end{bmatrix} \] > \[ A\bx = \begin{bmatrix} {x_1}a_{11} + \cdots + {x_m}a_{1n} \\ \vdots \\ {x_m}a_{m1} + \cdots + {x_m}a_{mn} \end{bmatrix} = \begin{bmatrix} f_1 \\ \vdots \\ f_m \end{bmatrix}. \]

  • 因此, \(f\) 的全微分為 \(A\)>
    由於 \(\frac{\partial f_i}{\partial x_j} = a_{ij}\),因此 \(f\) 的全微分為 \(A\)

Ans:
\(A\) \[\begin{bmatrix} a_{11} & \cdots & a_{1n} \\ \vdots & \cdots & \vdots \\ a_{m1} & \cdots & a_{mn} \end{bmatrix} \] \({\bf x}\) \[\begin{bmatrix} {x_1} \\ \vdots \\ {x_m} \end{bmatrix}. \] 因為 \({\bf b}\) 為常數,所以,微分時無需考慮。
\[ A\bx = \begin{bmatrix} {x_1}a_{11} + \cdots + {x_m}a_{1n} \\ \vdots \\ {x_1}a_{m1} + \cdots + {x_m}a_{mn} \end{bmatrix} = \begin{bmatrix} f_1 \\ \vdots \\ f_m \end{bmatrix}. \] 由於 \(\frac{\partial f_i}{\partial x_j} = a_{ij}\),因此 \(f\) 的全微分為 \(A\)

目前分數 6

Select a repo