CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
weijie-chen

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: weijie-chen/Linear-Algebra-With-Python
Path: blob/master/notebooks/Chapter 11 - Linear Transformation.ipynb
Views: 449
Kernel: Python 3
import matplotlib.pyplot as plt import numpy as np import sympy as sy sy.init_printing()

Linear Transformation

There are many new terminologies in this chapter, however they are not entirely new to us.

Let VV and WW be vector spaces. The mapping T: VWT:\ V\rightarrow W is called a linear transformation if an only if

T(u+v)=T(u)+T(v)andT(cu)=cT(u)T(u+v)=T(u)+T(v)\quad \text{and} \quad T(cu)=cT(u)

for all u,vVu,v\in V and all cRc\in R. If T: VWT:\ V\rightarrow W, then TT is called a linear operator. For each uVu\in V, the vector w=T(u)w=T(u) is called the image of uu under TT.

Parametric Function Plotting

We need one tool for illustrating linear transformation.

We want to plot any line in vector space by an equation: p=p0+tvp = p_0+tv . We need to know vector p0p_0 and vv to plot the line.

For instance, p0=(2,6)p_0 = (2, 6), v=(5,3)v=(5, 3) and p=(x,y)p = (x, y), subsitute them into our equation [xy]=[26]+t[53] \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)) # Plot first line x = p0[0, 0] + v0[0, 0] * t y = p0[1, 0] + v0[1, 0] * t ax.plot(x, y, lw=3, color='red') ax.grid(True) ax.scatter(p0[0, 0], p0[1, 0], s=150, edgecolor='red', facecolor='black', zorder=3) # Plot second line x = p1[0, 0] + v1[0, 0] * t y = p1[1, 0] + v1[1, 0] * t ax.plot(x, y, lw=3, color='blue') ax.grid(True) ax.scatter(p1[0, 0], p1[1, 0], s=150, edgecolor='red', facecolor='black', zorder=3) # Set the position of the spines 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') # Add text annotations string = f'$({int(p0[0, 0])}, {int(p0[1, 0])})$' ax.text(x=p0[0, 0] + 0.5, y=p0[1, 0], s=string, size=14) string = f'$({int(p1[0, 0])}, {int(p1[1, 0])})$' ax.text(x=p1[0, 0] + 0.5, y=p1[1, 0], s=string, size=14) plt.show() # Example usage p0 = np.array([[1], [2]]) v0 = np.array([[1], [0.5]]) p1 = np.array([[2], [3]]) v1 = np.array([[0.5], [1]]) paraEqPlot(p0, v0, p1, v1)
Image in a Jupyter notebook

A Simple Linear Transformation

Now we know the parametric functions in R2\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)(x, y),

T([xy])=(3x2y2x+3y)T\left(\begin{bmatrix} x \\ y \end{bmatrix}\right) = \begin{pmatrix} 3x - 2y \\ -2x + 3y \end{pmatrix}

and substitute the parametric function into the linear operator.

T([4+t5+3t])=(3(4+t)2(5+3t)2(4+t)+3(5+3t))=[23t7+7t]T\left(\begin{bmatrix} 4+t \\ 5+3t \end{bmatrix}\right) = \begin{pmatrix} 3(4+t) - 2(5+3t) \\ -2(4+t) + 3(5+3t) \end{pmatrix} = \begin{bmatrix} 2 - 3t \\ 7 + 7t \end{bmatrix}

The red line is transformed into blue line and point (4,5)(4, 5) transformed into (2,7)(2, 7)

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)
Image in a Jupyter notebook

Visualization of Change of Basis

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 U2U2 again each row of U1U1

fig, ax = plt.subplots(figsize = (10, 10)) ax.plot(U1,U2, color = 'black') ax.plot(U1.T,U2.T, color = 'black') plt.show()
Image in a Jupyter notebook

Let AA and BB be two bases in R3\mathbb{R}^3

A={[21], [11]}B={[32], [01]}A = \left\{ \begin{bmatrix} 2 \\ 1 \end{bmatrix}, \ \begin{bmatrix} 1 \\ 1 \end{bmatrix} \right\} \\ B = \left\{ \begin{bmatrix} 3 \\ 2 \end{bmatrix}, \ \begin{bmatrix} 0 \\ -1 \end{bmatrix} \right\}

If we want to use basis AA to represent BB, we can construct an augmented matrix like we did before.

[AB]=[21301121][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()

([10110112], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & 1 & 1\\0 & 1 & 1 & -2\end{matrix}\right], \ \left( 0, \ 1\right)\right)

We find the transition matrix PABP_{A\leftarrow B} [AB]=[IPAB] [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 x1=u1+u2x2=u12u2 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()
Image in a Jupyter notebook