<jupyter_start><jupyter_text>Lecture 01 The Geometry of Linear EquationsToday's lecture contains:1. 2D Linear Equations2. 3D Linear Equations3. Matrix Multiple VectorFor all equations, we focus on TWO big concepts: **row picture** and **column picture.** 1. 2D Linear EquationsSuppose we have two equations with two unknown:\begin{align}2x-y&=0\\-x+2y&=3\end{align}Such equations can be written to matrix format: \begin{align}\begin{bmatrix}2&-1\\-1&2\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}\end{align}Usually we call the first matrix as **the coefficient matrix** denoted as $A$, the second part as the **unknown** $x$, and the third part as **the right hand side** $b$. Therefore, the system of linear equations can be denoted as $Ax=b$. 1.1 Row Picture of 2D Linear EquationsTo solve above linear system, one of the methods to tackle is called **row picture**. It is simply applied the math from high school:a. Find two points of each equationb. Connect two points to a linec. The joint is the answer.<jupyter_code>%gui qt
from mayavi import mlab

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D
import scipy as sp
import scipy.linalg
import sympy as sy
sy.init_printing() 
np.set_printoptions(precision=3)
np.set_printoptions(suppress=True)
mlab.init_notebook(backend='x3d')<jupyter_output>Notebook initialized with x3d backend.<jupyter_text>For the first equation,\begin{align}2x-y=0\end{align}the solution will be:\begin{align}x=0, y=0\\x=2, y=4\\end{align}For the second equation,\begin{align}-x+2y=3\end{align}the solution will be:\begin{align}x=-3, y=0\\x=1, y=2\\end{align}Hence, the intersection of two line would be:\begin{align}(x, y)^T = (1, 2)^T\end{align}<jupyter_code>x = np.linspace(-5, 5, 100)
y1 = 2*x
y2 = (1/2)*x + 3/2

fig, ax = plt.subplots(figsize = (12, 7))
#plot point
ax.scatter(1, 2, s = 200, zorder=5, color = 'r', alpha = .8) 
ax.scatter(0, 0, s = 100, zorder=5, color = 'k', alpha = .8) 
ax.scatter(2, 4, s = 100, zorder=5, color = 'k', alpha = .8) 
ax.scatter(-3, 0, s = 100, zorder=5, color = 'k', alpha = .8) 
#plot line
ax.plot(x, y1, x, y2, lw = 3)
ax.plot([1, 1], [0, 2], ls = '--', color = 'b', alpha = .5)
ax.plot([0, 1], [2, 2], ls = '--', color = 'b', alpha = .5)
ax.plot([-5, 5], [0, 0], ls = '-', color = 'k', linewidth=5, alpha = .8)
ax.plot([0, 0], [-5, 5], ls = '-', color = 'k', linewidth=5, alpha = .8)
#set plot range
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])

#plot text
s = '$(1,2)$'
pt1 = '$(-3,0)$'
pt2 = '$(0,0)$'
pt3 = '$(2,4)$'
ax.text(1, 2, s, fontsize = 20)
ax.text(-3, .3, pt1, fontsize = 20)
ax.text(0, .3, pt2, fontsize = 20)
ax.text(2, 4, pt3, fontsize = 20)

#
ax.set_title('Row Picture: Solution of $2x-y=0$, $-x+2y=3$', size = 22)
ax.grid()<jupyter_output><empty_output><jupyter_text>1.2 Column Picture of 2D Linear EquationsAnother perspective to see this equation is **column picture**. The above matrix\begin{align}\begin{bmatrix}2&-1\\-1&2\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}\end{align}can be converted to:\begin{align}x\begin{bmatrix}2\\-1\end{bmatrix}+y\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}\end{align}We call the first vector$\begin{bmatrix}2\\-1\end{bmatrix}$ as $col_1$，second vector$\begin{bmatrix}-1\\2\end{bmatrix}$ as $col_2$.That said,to satisfy the output vector as $\begin{bmatrix}3\\0\end{bmatrix}$. We have to  $2×col_1$, $2×col_2$\begin{align}1\begin{bmatrix}2\\-1\end{bmatrix}+2\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}\end{align}Hence, the column picture can be illustrated as followed:<jupyter_code>from functools import partial

fig, ax = plt.subplots(figsize = (12, 7))
plt.axhline(y=0, c='black', linewidth=5)
plt.axvline(x=0, c='black', linewidth=5)
ax = plt.gca()

#plot vector
arrow_vector = partial(plt.arrow, width=0.01, head_width=0.1, head_length=0.2, length_includes_head=True)
arrow_vector(0, 0, 2, -1, color='r')
arrow_vector(0, 0, -1, 2, color='g')
arrow_vector(2, -1, -2, 4, color='k')
arrow_vector(0, 0, 0, 3, width=0.05, color='b')
#plot point
ax.scatter(2, -1, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(0, 0, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(0, 3, s = 200, zorder=5, color = 'k', alpha = .8) 
ax.scatter(-1, 2, s = 200, zorder=5, color = 'k', alpha = .8) 

#set plot range
ax.set_xlim([-2, 3])
ax.set_ylim([-2, 4])

#plot text
col_1 = '$col_1$'
col_2 = '$col_2$'
col_3 = '1·$col_1$+2·$col_2$'
ax.text(1, -.5, col_1, fontsize = 20)
ax.text(-.5, 1, col_2, fontsize = 20)
ax.text(1, 1.5, col_3, fontsize = 20)

#
ax.set_title('Column Picture: Solution of $2x-y=0$, $-x+2y=3$', size = 22)
ax.grid()<jupyter_output><empty_output><jupyter_text>So we have this kind of linear combination: \begin{align}x\begin{bmatrix}2\\-1\end{bmatrix}+y\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}0\\3\end{bmatrix}\end{align}That said, the linear combination of $col_1,col_2$ is vector $b$. Then what would be all the linear combination of $col_1,col_2$**The whole plane!**<jupyter_code>x, y = np.arange(-10, 11, 5), np.arange(-10, 11, 5)
X, Y = np.meshgrid(x, y)

fig, ax = plt.subplots(figsize = (8, 8))
plt.axhline(y=0, c='black', linewidth=5)
plt.axvline(x=0, c='black', linewidth=5)
ax.scatter(X, Y, s = 200, color = 'red', zorder = 3)
ax.axis([-15, 15, -15, 15])
ax.grid()<jupyter_output><empty_output><jupyter_text>2. 3D Linear EquationsThen we move from 2D to 3D. Suppose we have 3 equations as followed:\begin{align}\begin{cases}2x&-y&&=0\\-x&+2y&-z&=-1\\&-3y&+4z&=4\end{cases}\end{align}Such equations can be written to matrix format: \begin{align}A=\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-3&4\end{bmatrix},\ b=\begin{bmatrix}0\\-1\\4\end{bmatrix}\end{align}  2.1 Row Picture of 2D Linear EquationsThe above linear system can be written as followed in row picture:\begin{align}\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-3&4\end{bmatrix}\begin{bmatrix}x\\y\\z\end{bmatrix}=\begin{bmatrix}0\\-1\\4\end{bmatrix}\end{align}First, we try to figure out this linear system from row picture.In 2D space, the unknown can be illustrated as **2 lines** in a plane. In 3D space, the unknown can be illustrated as **3 planes** in a space.In the end, 3 plane will be intersected in a point **if** the three equations are independent.---TO-DO: Visualize 3D plane intersection. (matplotlib is fake 3d)  2.2 Column Picture of 2D Linear EquationsColumn picture is Prof. Strang's favorite which is much easier to be understood. The above linear system can be written into column picture as followed:\begin{align}x\begin{bmatrix}2\\-1\\0\end{bmatrix}+y\begin{bmatrix}-1\\2\\-3\end{bmatrix}+z\begin{bmatrix}0\\-1\\4\end{bmatrix}=\begin{bmatrix}0\\-1\\4\end{bmatrix}\end{align}<jupyter_code>from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import


fig = plt.figure(figsize=(5,5))
ax = fig.gca(projection='3d')

# Make the grid
x, y, z = np.zeros((3,3))

# Make the direction data for the arrows
u=np.array([2,-1,0])
w=np.array([-1,2,-3])
v=np.array([0,-1,4])

ax.quiver(x, y, z, u, v, w)
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
ax.set_zlim([-5,5])

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1901340334392704, 'fraction_numerical': 0.05582423656211137, 'mean_word_length': 4.855987055016182, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '163338', 'license': 'MIT', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2854, 'n_words': 753, 'path': 'Lecture 01 The Geometry of Linear Equations.ipynb', 'repo_name': 'XingxinHE/Linear_Algebra', 'size': '7237'}