owned this note
owned this note
Published
Linked with GitHub
# 將線性函數化為矩陣
Linear function as a matrix

This work by Jephian Lin is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).
$\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}}$
```python
from lingeo import random_int_list
```
## Main idea
Let $A$ be an $m\times n$ matrix,
$\mathcal{E}_n = \{ \be_1, \ldots, \be_n \}$ the standard basis of $\mathbb{R}^n$, and
$\bu_1, \ldots, \bu_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 \\
\be_1 & \mapsto & \bu_1 \\
~ & \cdots & ~ \\
\be_n & \mapsto & \bu_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(\bv) = A\bv$ for all $\bv\in\mathbb{R}^n$.
Let $f$ be a linear function from $\mathbb{R}^n$ to $\mathbb{R}^m$ and
$\mathcal{E}_n = \{ \be_1, \ldots, \be_n \}$ the standard basis of $\mathbb{R}^n$.
Calculate $\bu_1 = f(\be_1)$, $\ldots$, $\bu_n = f(\be_n)$ and
construct a matrix $A$ whose columns are $\bu_1, \ldots, \bu_n$.
Thus, $f(\bv) = A\bv$ for all $\bv\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 $\rank(f) + \nul(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. $\nul(f) = 0$.
3. $\rank(f) = n$.
## Side stories
- total derivative
## Experiments
##### Exercise 1
執行以下程式碼。
己知 $f$ 是從 $\mathbb{R}^4$ 到 $\mathbb{R}^3$ 的一個函數。
<!-- eng start -->
Run the code below. Suppose $f$ is a function from $\mathbb{R}^4$ to $\mathbb{R}^3$.
<!-- eng end -->
```python
### 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(\bzero) = \bzero$。
注意這裡兩個零向量分別是定義域和對應域上的零向量。
<!-- eng start -->
Verify if $f(\bzero) = \bzero$. Notice that the two zero vectors in the formula are the zero vectors in the domain and the codomain.
<!-- eng end -->
```python
zero4 = vector([0,0,0,0])
f(zero4)
```
### <font color="red">Ans:</font>
After executing the code above, we can get $(0, 0, 0)$.
So does $f(\bzero) = \bzero$.
___
##### Exercise 1(b)
輸入任意的 $\bv_1, \bv_2\in\mathbb{R}^4$。
驗證明是否 $f(\bv_1 + \bv_2) = f(\bv_1) + f(\bv_2)$。
<!-- eng start -->
Input any $\bv_1, \bv_2\in\mathbb{R}^4$. Then verify if $f(\bv_1 + \bv_2) = f(\bv_1) + f(\bv_2)$.
<!-- eng end -->
```python
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))
```
:::warning
- [x] Missing periods.
:::
### <font color="red">Ans:</font>
Input
$$\bv_1=[1,2,3,4], \bv_2=[1,1,1,1].$$
After executing the code we can get
$$f(\bv_1 + \bv_2)=(-4, -13, -13)$$
$$f(\bv_1) + f(\bv_2)=(-3, -8, -13)+(-1, -5, 0)$$
also equals to $$(-4, -13, -13).
$$
So that we can verify $f(\bv_1 + \bv_2) = f(\bv_1) + f(\bv_2)$.
___
##### Exercise 1(c)
輸入任意的 $k\in\mathbb{R}$ 及 $\bv\in\mathbb{R}^4$。
驗證明是否 $f(k\bv) = kf(\bv)$。
<!-- eng start -->
Input any $k\in\mathbb{R}$ and $\bv\in\mathbb{R}^4$. Then verify if $f(k\bv) = kf(\bv)$.
<!-- eng end -->
```python
k = 3
v = vector([1,1,1,1])
print(f(k * v))
print("%s * %s ="%(k, f(v)), k*f(v))
```
:::warning
- [x] Missing periods.
- [x] For scalar multiplications: $\times$ --> $\cdot$
:::
### <font color="red">Ans:</font>
Input
$$k=3, \bv=[1,1,1,1].$$
After executing the code we can get
$$f(k\cdot\bv)=(-3, -15, 0)$$
$$k \cdot f(\bv)=3\cdot(-1,-5,0)=(-3,-15,0).
$$
So that we can verify $f(k\bv) = kf(\bv)$.
___
##### Exercise 1(d)
找到一個矩陣 $A$ 使得對於所有 $\bv\in\mathbb{R}^4$ 都有 $f(\bv) = A\bv$。
<!-- eng start -->
Find a matrix $A$ such that $f(\bv) = A\bv$ for any $\bv\in\mathbb{R}^4$.
<!-- eng end -->
:::warning
:warning: You have seen in the previous problems that
$$
f(\begin{bmatrix} 1\\ 1\\ 1\\ 1\end{bmatrix}) = \begin{bmatrix} -1 \\ -5 \\ 0 \end{bmatrix}
$$
and many other examples of $\bv$ and $f(\bv)$. Obviously, your $A$ does not meet the requirement.
:warning: This is still wrong. When you run the code with different $\bv$, it must satisfy $f(\bv) = A\bv$, yet your $A$ does not meet the criterion.
:::
### <font color="red">Ans:</font>
Suppose there exists a $3*4$ matrix $A$ which shows that
$$\begin{cases}
A\times\begin{bmatrix} 1\\ 2\\ 3\\ 4\end{bmatrix} = \begin{bmatrix} -3 \\ -8 \\ -13 \end{bmatrix}\\
A\times\begin{bmatrix} 1\\ 1\\ 1\\ 1\end{bmatrix} = \begin{bmatrix} 1 \\ -5 \\ 0 \end{bmatrix}.\\
A\times\begin{bmatrix} 0\\ 0\\ 0\\ 0\end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}\\
\end{cases}
$$
By setting matrix $A$ as
$$A =\begin{bmatrix}
a &a_1 &0 &0\\
b &b_1 &0 &0\\
c &c_1 &0 &0\end{bmatrix},
$$
we can get
$$\begin{cases}
a+2a_1=-3\\
a+a_1=1\\
b+2b_1=-8\\
b+b_1=-51\\
c+2c_1=-3\\
c+c_1=0
\end{cases}.
$$
Eventually, by solving the equations above we can have a matrix $A$
$$A =\begin{bmatrix}
5 &-4 &0 &0\\
-2 &-3 &0 &0\\
13 &-13&0 &0\end{bmatrix}.
$$
___
## Exercises
##### Exercise 2
考慮以下函數 $f$﹐求出矩陣 $A$ 使得 $f = f_A$。
<!-- eng start -->
Consider each of the following function $f$. Find a matrix $A$ such that $f = f_A$.
<!-- eng end -->
##### Exercise 2(a)
$$
f(x,y,z) = (x,y,0).
$$
:::warning
- [x] Use lower case letter in a sentence.
- [x] Missing a verb in the last sentence.
:warning: You answer is logically correct, yet how come you know $A$ is a diagonal matrix?
:::
### <font color="red">Ans:</font>
In order to find a matrix $A$ such that $f = f_A$,
we consider that $A\times \begin{bmatrix}
x\\
y\\
z\end{bmatrix}=
\begin{bmatrix}
x,\
y,\
0\end{bmatrix}.$
Consider that
$$
A=\begin{bmatrix}
a &0 &0\\
0 &b &0\\
0 &0 &c\end{bmatrix},$$
so we can know that$$ax + by + cz= x + y ,$$
and
\begin{aligned}
a &= 1 \\
b &= 1.
\end{aligned}
Therefore matrix $$A =\begin{bmatrix}
1 &0 &0\\
0 &1 &0\\
0 &0 &0\end{bmatrix}.
$$
___
##### Exercise 2(b)
$$
f(x,y,z) = (3x,4y,5z).
$$
:::warning
You did not mention how do you get the matrix $A$.
:::
### <font color="red">Ans:</font>
While $f(x,y,z)=(3x,4y,5z),$
we can know there exists a diagonal $3\times 3$ matrix $A$
$$
A\times \begin{bmatrix}
x\\
y\\
z\end{bmatrix}=
\begin{bmatrix}
3x\\
4y\\
5z\end{bmatrix},
A=\begin{bmatrix}
a &0 &0\\
0 &b &0\\
0 &0 &c\end{bmatrix},$$
from which we can get
$$ax + by + cz= 3x + 4y + 5z.
$$
So that we can have matrix
$$
A=\begin{bmatrix}
3&0&0\\
0&4&0\\
0&0&5\\
\end{bmatrix}.
$$
___
##### Exercise 2(c)
$$
f(x,y,z) = (x+2y+3z,4x+5y+6z,7x+8y+9z).
$$
:::warning
- [x] Put vectors as a column vector.
- [x] Missing a verb in the last sentence.
:::
### <font color="red">Ans:</font>
In order to find a matrix $A$, such that $f = f_A,$
we consider that $A\times \begin{bmatrix}
x\\
y\\
z\end{bmatrix}=
\begin{bmatrix}
x+ 2y+ 3z\\
4x+ 5y +6z\\
0\end{bmatrix}.$
Consider that
$$
A=\begin{bmatrix}
a_1 &b_1 &c_1\\
a_2 &b_2 &c_2\\
a_3 &b_3 &c_3\end{bmatrix},
$$
so we can know that
$$\begin{cases}
a_1x + b_1y + c_1z= x + 2y +3z, \\
a_2x + b_2y + c_2z= 4x + 5y +6z,\\
a_3x + b_3y + c_3z= 0,\\
\end{cases}
$$
and we get
\begin{aligned}
a_1=1,a_2=4,a_3=0, \\
b_1=12,b_2=5,b_3=0, \\
c_1=3,c_2=6,c_3=0.
\end{aligned}
Therefore we can have matrix $$A =\begin{bmatrix}
1 &2 &3\\
4 &5 &6\\
0 &0 &0\end{bmatrix}.$$
___
##### Exercise 2(d)
$$
f(x,y,z) = (y,z,x).
$$
##### Exercise 2(e)
函數 $f$ 把每個 $\mathbb{R}^3$ 中的向量投影到 $(1,1,1)$ 的方向上。
<!-- eng start -->
The function $f: \mathbb{R}^3 \rightarrow \mathbb{R}^3$ that project every vector onto the line of $(1,1,1)$.
<!-- eng end -->
##### Exercise 2(f)
函數 $f$ 把每個 $\mathbb{R}^3$ 中的向量沿著 $z$ 軸逆時鐘旋轉 $45^\circ$。
(這裡的旋轉是以北極往南看的逆時鐘。)
<!-- eng start -->
The function $f: \mathbb{R}^3 \rightarrow \mathbb{R}^3$ that rotates every vector by $45^\circ$ along the $z$-axis. Here the rotation is counterclockwise, from the point of view from the north pole seeing the origin.
<!-- eng end -->
##### 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}.
$$
<!-- eng start -->
Let $F$ be a differentiable function from $\mathbb{R}^n$ to $\mathbb{R}^m$, which is not necessarily linear. Then $f$ can be written as
$$
f(x_1,\ldots, x_n) = (f_1(x_1,\ldots, x_n), \ldots, f_m(x_1,\ldots, x_n)).
$$
Then the **total derivative** of $f$ is
$$
\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}.
$$
<!-- eng end -->
##### Exercise 3(a)
微分的用意是希望函數的區部性質非常接近線性函數。
說明為什麼全微分會被定為一個 $m\times n$ 矩陣而不是一個 $n\times m$ 矩陣。
<!-- eng start -->
The goal of taking the derivative is to approximate a function by a linear function. Give some intuition of why the total derivative of a function from $\mathbb{R}^n$ to $\mathbb{R}^m$ has to be an $m\times n$ matrix, but not an $n\times m$ matrix.
<!-- eng end -->
## <font color="red">Ans:</font>
Since the function approximates a linear function, every linear function $f$ from $\mathbb{R}^n$ to $\mathbb{R}^m$ has an $m\times n$ matrix $A$, such that $f(\bv)=A\bv$ for all $\bv\in\mathbb{R}^n$. So the total derivative of a function from $\mathbb{R}^n$ to $\mathbb{R}^m$ has to be an $m\times n$ matrix.
___
##### Exercise 3(b)
令 $A$ 為一 $m\times n$ 矩陣而 $\bb\in\mathbb{R}^m$。
定義 $f(\bx) = A\bx + \bb$。
求 $f$ 的全微分。
<!-- eng start -->
Let $A$ be an $m\times n$ matrix and $\bb\in\mathbb{R}^m$. Define $f(\bx) = A\bx + \bb$. Find the total derivative of $f$.
<!-- eng end -->
:::info
collaboration: 1.5 (1d not correct)
3 problems: 3
- done: 2a, 2b, 2c
extra: 0.5
- done: 3a
moderator: 1
quality control: 1
:::