import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
sy.init_printing()
There many new terminologies in this chapter, however they are not entirely new to us.
Let $V$ and $W$ be vector spaces. The mapping $T:\ V\rightarrow W$ is called a linear transformation if an only if
$$ T(u+v)=T(u)+T(v)\quad \text{and} \quad T(cu)=cT(u) $$for all $u,v\in V$ and all $c\in R$. If $T:\ V\rightarrow W$, then $T$ is called a linear operator. For each $u\in V$, the vector $w=T(u)$ is called the image of $u$ under $T$.
We need one tool for illustrating the idea of linear transformation.
We want to plot any line in vector space by an equation: $p = p_0+tv$. We need to know vector $p_0$ and $v$ to plot the line.
For instance, $p_0 = (2, 6)$, $v=(5, 3)$ and $p = (x, y)$, subsitute them into our equation $$ \left[ \begin{matrix} x\\y \end{matrix} \right]=\left[ \begin{matrix} 2\\6 \end{matrix} \right]+ t\left[ \begin{matrix} 5\\3 \end{matrix} \right] $$
We will create a plot to illustrate the linear transformation later.
def paraEqPlot(p0, v0, p1, v1):
t = np.linspace(-5, 5)
################### First Line ####################
fig, ax = plt.subplots(figsize = (10, 10))
x = p0[0,:] + v0[0,:]*t
y = p0[1,:] + v0[1,:]*t
ax.plot(x, y, lw = 3, color = 'red')
ax.grid(True)
ax.scatter(p0[0,:], p0[1,:], s = 150, ec = 'red', fc = 'black', zorder = 3)
################### First Line ####################
x = p1[0,:] + v1[0,:]*t
y = p1[1,:] + v1[1,:]*t
ax.plot(x, y, lw = 3, color = 'blue')
ax.grid(True)
ax.scatter(p1[0,:], p1[1,:], s = 150, ec = 'red', fc = 'black', zorder = 3)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
string = '$(%.d, %.d)$' % (p0[0,:], p0[1,:])
ax.text(x= p0[0,:]+.5, y = p0[1,:], s = string, size = 14)
string = '$(%.d, %.d)$' % (p1[0,:], p1[1,:])
ax.text(x= p1[0,:]+.5, y = p1[1,:], s = string, size = 14)
Now we know the parametric functions in $\mathbb{R}^2$, we can show how a linear transformation acturally works on a line.
Let's say, we perform linear transformation on a vector $(x, y)$,
$$ T\left(\left[\matrix{x\cr y}\right]\right)=\pmatrix{3x-2y\cr -2x+3y}\\ $$and substitute the parametric function into the linear operator.
$$ T\left(\left[\matrix{4+t\cr 5+3t}\right]\right)=\pmatrix{3(4+t)-2(5+3t)\cr -2(4+t)+3(5+3t)}=\left[ \begin{matrix} 2-3t\\7+7t \end{matrix} \right] $$The red line is transformed into
p0 = np.array([[4],[5]])
v0 = np.array([[1],[3]])
p1 = np.array([[2],[7]])
v1 = np.array([[-3],[7]])
paraEqPlot(p0,v0,p1, v1)
Change of basis is also a kind of linear transformation. Let's create a grid.
u1, u2 = np.linspace(-5, 5, 10), np.linspace(-5, 5, 10)
U1, U2 = np.meshgrid(u1, u2)
We plot each row of $U2$ again each row of $U1$
fig, ax = plt.subplots(figsize = (10, 10))
ax.plot(U1,U2, color = 'black')
ax.plot(U1.T,U2.T, color = 'black')
plt.show()
Let $A$ and $B$ be two bases in $\mathbb{R}^3$
$$ A=\left\{\left[\matrix{2\cr 1}\right],\ \left[\matrix{1\cr 1}\right]\right\}\\ B=\left\{\left[\matrix{3\cr 2}\right],\ \left[\matrix{0\cr -1}\right]\right\}\\ $$If we want to use basis $A$ to represent $B$, we can construct an augmented matrix like we did before.
$$ [A|B]= \left[ \begin{matrix} 2 & 1 & 3 & 0\\ 1 & 1 & 2 & -1 \end{matrix} \right] $$AB = sy.Matrix([[2,1,3,0],[1,1,2,-1]]); AB.rref()
We find the transition matrix $P_{A\leftarrow B}$ $$ [A|B]=[I|P_{A\leftarrow B}] $$
We can write
$$ \big[x\big]_A = P_{A\leftarrow B}\big[u\big]_B\\ \left[ \begin{matrix} x_1\\x_2 \end{matrix} \right] = \left[ \begin{matrix} 1 & 1\\1 & -2 \end{matrix} \right] \left[ \begin{matrix} u_1\\u_2 \end{matrix} \right]\\ $$Therefore $$ x_1 = u_1+u_2\\ x_2 = u_1 - 2u_2 $$
Let's plot original and transformed coordinates together.
u1, u2 = np.linspace(-10, 10, 21), np.linspace(-10, 10, 21)
U1, U2 = np.meshgrid(u1, u2)
fig, ax = plt.subplots(figsize = (10, 10))
ax.plot(U1,U2, color = 'black', lw = 1)
ax.plot(U1.T,U2.T, color = 'black', lw = 1)
X1 = U1 +U2
X2 = U1 - 2*U2
ax.plot(X1,X2, color = 'red', ls = '--')
ax.plot(X1.T,X2.T, color = 'red', ls = '--')
ax.arrow(0, 0, 1, 1, color = 'blue', width = .07,
length_includes_head = True,
head_width = .2, # default: 3*width
head_length = .3, zorder = 4,
overhang = .4)
ax.arrow(0, 0, 1, -2, color = 'blue', width = .07,
length_includes_head = True,
head_width = .2, # default: 3*width
head_length = .3,zorder = 4,
overhang = .4)
ax.text(0.1,0.1,'$(0, 0)$',size = 14)
ax.scatter(0,0,s = 120, zorder = 5, ec = 'red', fc = 'black')
ax.axis([-4, 4, -5, 5])
plt.show()