From 5fae9206710eeff0fa2883b698b1796cbb4ae0dd Mon Sep 17 00:00:00 2001 From: "Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning" <105787223+Visualize-ML@users.noreply.github.com> Date: Sat, 1 Feb 2025 17:01:02 +0800 Subject: [PATCH] Delete Book4_Ch08_Python_Codes directory --- Book4_Ch08_Python_Codes/Bk4_Ch8_01.py | 73 ---------- Book4_Ch08_Python_Codes/Bk4_Ch8_02.py | 68 --------- .../Streamlit_Bk4_Ch8_02.py | 118 ---------------- .../Streamlit_Bk4_Ch8_03.py | 132 ------------------ 4 files changed, 391 deletions(-) delete mode 100644 Book4_Ch08_Python_Codes/Bk4_Ch8_01.py delete mode 100644 Book4_Ch08_Python_Codes/Bk4_Ch8_02.py delete mode 100644 Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_02.py delete mode 100644 Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_03.py diff --git a/Book4_Ch08_Python_Codes/Bk4_Ch8_01.py b/Book4_Ch08_Python_Codes/Bk4_Ch8_01.py deleted file mode 100644 index be711ae..0000000 --- a/Book4_Ch08_Python_Codes/Bk4_Ch8_01.py +++ /dev/null @@ -1,73 +0,0 @@ - -############### -# Authored by Weisheng Jiang -# Book 4 | From Basic Arithmetic to Machine Learning -# Published and copyrighted by Tsinghua University Press -# Beijing, China, 2022 -############### - -# Bk4_Ch8_01.py - -import matplotlib.pyplot as plt -import numpy as np - -def plot_shape(X,copy = False): - if copy: - fill_color = np.array([255,236,255])/255 - edge_color = np.array([255,0,0])/255 - else: - fill_color = np.array([219,238,243])/255 - edge_color = np.array([0,153,255])/255 - - plt.fill(X[:,0], X[:,1], - color = fill_color, - edgecolor = edge_color) - - plt.plot(X[:,0], X[:,1],marker = 'x', - markeredgecolor = edge_color*0.5, - linestyle = 'None') - -X = np.array([[1,1], - [0,-1], - [-1,-1], - [-1,1]]) - -# visualizations - -fig, ax = plt.subplots() - -plot_shape(X) # plot original - -# translation -t1 = np.array([3,2]); -Z = X + t1 -plot_shape(Z,True) # plot copy - -t2 = np.array([-3,-2]); -Z = X + t2 -plot_shape(Z,True) # plot copy - -t3 = np.array([-2,3]); -Z = X + t3 -plot_shape(Z,True) # plot copy - -t4 = np.array([3,-3]); -Z = X + t4 -plot_shape(Z,True) # plot copy - -# Decorations -ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) -plt.axis('equal') -plt.axis('square') -plt.axhline(y=0, color='k', linewidth = 0.25) -plt.axvline(x=0, color='k', linewidth = 0.25) -plt.xticks(np.arange(-5, 6)) -plt.yticks(np.arange(-5, 6)) -ax.set_xlim(-5,5) -ax.set_ylim(-5,5) -ax.spines['top'].set_visible(False) -ax.spines['right'].set_visible(False) -ax.spines['bottom'].set_visible(False) -ax.spines['left'].set_visible(False) -plt.xlabel('$x_1$') -plt.ylabel('$x_2$') diff --git a/Book4_Ch08_Python_Codes/Bk4_Ch8_02.py b/Book4_Ch08_Python_Codes/Bk4_Ch8_02.py deleted file mode 100644 index 1a83b33..0000000 --- a/Book4_Ch08_Python_Codes/Bk4_Ch8_02.py +++ /dev/null @@ -1,68 +0,0 @@ - -############### -# Authored by Weisheng Jiang -# Book 4 | From Basic Arithmetic to Machine Learning -# Published and copyrighted by Tsinghua University Press -# Beijing, China, 2022 -############### - -# Bk4_Ch8_02.py - -import matplotlib.pyplot as plt -import numpy as np - -def plot_shape(X,copy = False): - if copy: - fill_color = np.array([255,236,255])/255 - edge_color = np.array([255,0,0])/255 - else: - fill_color = np.array([219,238,243])/255 - edge_color = np.array([0,153,255])/255 - - plt.fill(X[:,0], X[:,1], - color = fill_color, - edgecolor = edge_color) - - plt.plot(X[:,0], X[:,1],marker = 'x', - markeredgecolor = edge_color*0.5, - linestyle = 'None') - -X = np.array([[1,1], - [0,-1], - [-1,-1], - [-1,1]]) + np.array([3,3]) - -# visualizations - -thetas = np.linspace(30, 330, num=11) - -for theta in thetas: - - fig, ax = plt.subplots() - - theta = theta/180*np.pi; - # rotation - R = np.array([[np.cos(theta), np.sin(theta)], - [-np.sin(theta), np.cos(theta)]]) - - Z = X@R; - plot_shape(Z,True) # plot copy - - plot_shape(X) # plot original - - # Decorations - ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) - plt.axis('equal') - plt.axis('square') - plt.axhline(y=0, color='k', linewidth = 0.25) - plt.axvline(x=0, color='k', linewidth = 0.25) - plt.xticks(np.arange(-5, 6)) - plt.yticks(np.arange(-5, 6)) - ax.set_xlim(-5,5) - ax.set_ylim(-5,5) - ax.spines['top'].set_visible(False) - ax.spines['right'].set_visible(False) - ax.spines['bottom'].set_visible(False) - ax.spines['left'].set_visible(False) - plt.xlabel('$x_1$') - plt.ylabel('$x_2$') diff --git a/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_02.py b/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_02.py deleted file mode 100644 index a045f0c..0000000 --- a/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_02.py +++ /dev/null @@ -1,118 +0,0 @@ - -############### -# Authored by Weisheng Jiang -# Book 4 | From Basic Arithmetic to Machine Learning -# Published and copyrighted by Tsinghua University Press -# Beijing, China, 2022 -############### - -import plotly.graph_objects as go -import numpy as np -from plotly.subplots import make_subplots -import streamlit as st - - -def bmatrix(a): - """Returns a LaTeX bmatrix - - :a: numpy array - :returns: LaTeX bmatrix as a string - """ - if len(a.shape) > 2: - raise ValueError('bmatrix can at most display two dimensions') - lines = str(a).replace('[', '').replace(']', '').splitlines() - rv = [r'\begin{bmatrix}'] - rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] - rv += [r'\end{bmatrix}'] - return '\n'.join(rv) - -n = m = 20 - -fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.035) - -xv = [] -yv = [] - -for k in range(-n, n+1): - xv.extend([k, k, np.nan]) - yv.extend([-m, m, np.nan]) -lw= 1 #line_width -fig.add_trace(go.Scatter(x=xv, y=yv, mode="lines", line_width=lw, - line_color = 'red'), 1, 1) -#set up the lists of horizontal line x and y-end coordinates - -xh=[] -yh=[] -for k in range(-m, m+1): - xh.extend([-m, m, np.nan]) - yh.extend([k, k, np.nan]) - -fig.add_trace(go.Scatter(x=xh, y=yh, mode="lines", line_width=lw, - line_color = 'blue'), 1, 1) - - -with st.sidebar: - - st.latex(r''' - R = \begin{bmatrix} - \cos(\theta) & -\sin(\theta)\\ - \sin(\theta) & \cos(\theta) - \end{bmatrix}''') - - theta = st.slider('Theta degree: ',-180, 180, step = 5, value = 0) - - theta = theta/180*np.pi - - -R = np.array([[np.cos(theta), -np.sin(theta)], - [np.sin(theta), np.cos(theta)]], dtype=float) - -#get only the coordinates from -3 to 3 -# X = np.array(xv[6:-6]) -# Y = np.array(yv[6:-6]) - -X = np.array(xv) -Y = np.array(yv) - -# transform by T the vector of coordinates [x, y]^T where the vector runs over the columns of np.stack((X, Y)) -Txvyv = R@np.stack((X, Y)) #transform by T the vertical lines - -# X = np.array(xh[6:-6]) -# Y = np.array(yh[6:-6]) - -X = np.array(xh) -Y = np.array(yh) - -Txhyh = R@np.stack((X, Y))# #transform by T the horizontal lines - -st.latex(r'R = ' + bmatrix(R)) - -r1 = R[:,0].reshape((-1, 1)) -r2 = R[:,1].reshape((-1, 1)) - -st.latex(r''' - r_1 = R e_1 = ''' + bmatrix(R) + - 'e_1 = ' + bmatrix(r1) - ) - -st.latex(r''' - r_2 = R e_2 = ''' + bmatrix(R) + - 'e_2 = ' + bmatrix(r2) - ) - -st.latex(r'\begin{vmatrix} R \end{vmatrix} = ' + str(np.linalg.det(R))) - -fig.add_trace(go.Scatter(x=Txvyv[0], y=Txvyv[1], - mode="lines", line_width=lw, - line_color = 'red'), 1, 2) - -fig.add_trace(go.Scatter(x=Txhyh[0], y=Txhyh[1], - mode="lines", line_width=lw, - line_color = 'blue'), 1, 2) - -fig.update_xaxes(range=[-4, 4]) -fig.update_yaxes(range=[-4, 4]) -fig.update_layout(width=800, height=500, showlegend=False, template="none", - plot_bgcolor="white", yaxis2_showgrid=False, xaxis2_showgrid=False) - -st.plotly_chart(fig) \ No newline at end of file diff --git a/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_03.py b/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_03.py deleted file mode 100644 index 0bab197..0000000 --- a/Book4_Ch08_Python_Codes/Streamlit_Bk4_Ch8_03.py +++ /dev/null @@ -1,132 +0,0 @@ - -############### -# Authored by Weisheng Jiang -# Book 4 | From Basic Arithmetic to Machine Learning -# Published and copyrighted by Tsinghua University Press -# Beijing, China, 2022 -############### - -import plotly.graph_objects as go -import numpy as np -from plotly.subplots import make_subplots -import streamlit as st - - -def bmatrix(a): - """Returns a LaTeX bmatrix - - :a: numpy array - :returns: LaTeX bmatrix as a string - """ - if len(a.shape) > 2: - raise ValueError('bmatrix can at most display two dimensions') - lines = str(a).replace('[', '').replace(']', '').splitlines() - rv = [r'\begin{bmatrix}'] - rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] - rv += [r'\end{bmatrix}'] - return '\n'.join(rv) - -n = m = 20 - -fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.035) - -xv = [] -yv = [] - -for k in range(-n, n+1): - xv.extend([k, k, np.nan]) - yv.extend([-m, m, np.nan]) -lw= 1 #line_width -fig.add_trace(go.Scatter(x=xv, y=yv, mode="lines", line_width=lw, - line_color = 'red'), 1, 1) -#set up the lists of horizontal line x and y-end coordinates - -xh=[] -yh=[] -for k in range(-m, m+1): - xh.extend([-m, m, np.nan]) - yh.extend([k, k, np.nan]) - -fig.add_trace(go.Scatter(x=xh, y=yh, mode="lines", line_width=lw, - line_color = 'blue'), 1, 1) - - -with st.sidebar: - - st.latex(r''' - A = \begin{bmatrix} - a & b\\ - c & d - \end{bmatrix}''') - - a = st.slider('a',-2.0, 2.0, step = 0.1, value = 1.0) - b = st.slider('b',-2.0, 2.0, step = 0.1, value = 0.0) - c = st.slider('c',-2.0, 2.0, step = 0.1, value = 0.0) - d = st.slider('d',-2.0, 2.0, step = 0.1, value = 1.0) - -theta = np.pi/6 -A = np.array([[a, b], - [c, d]], dtype=float) - -#get only the coordinates from -3 to 3 -# X = np.array(xv[6:-6]) -# Y = np.array(yv[6:-6]) - -X = np.array(xv) -Y = np.array(yv) - -# transform by T the vector of coordinates [x, y]^T where the vector runs over the columns of np.stack((X, Y)) -Txvyv = A@np.stack((X, Y)) #transform by T the vertical lines - -# X = np.array(xh[6:-6]) -# Y = np.array(yh[6:-6]) - -X = np.array(xh) -Y = np.array(yh) - -Txhyh = A@np.stack((X, Y))# #transform by T the horizontal lines - -st.latex(r'A = ' + bmatrix(A)) - -a1 = A[:,0].reshape((-1, 1)) -a2 = A[:,1].reshape((-1, 1)) - -st.latex(r''' - a_1 = Ae_1 = ''' + bmatrix(A) + - 'e_1 = ' + bmatrix(a1) - ) - -st.latex(r''' - a_2 = Ae_2 = ''' + bmatrix(A) + - 'e_2 = ' + bmatrix(a2) - ) - -st.latex(r'\begin{vmatrix} A \end{vmatrix} = ' + str(np.linalg.det(A))) - -theta_array = np.linspace(0, 2*np.pi, 101) -circle_x = np.cos(theta_array) -circle_y = np.sin(theta_array) -circle_array = np.stack((circle_x, circle_y)) - -fig.add_trace(go.Scatter(x=circle_x, y=circle_y, - fill="toself", line_color='orange'), 1, 1) - -A_times_circle_array = A@circle_array - -fig.add_trace(go.Scatter(x=A_times_circle_array[0,:], - y=A_times_circle_array[1,:], - fill="toself", line_color='orange'), 1, 2) - -fig.add_trace(go.Scatter(x=Txvyv[0], y=Txvyv[1], - mode="lines", line_width=lw, - line_color = 'blue'), 1, 2) - -fig.add_trace(go.Scatter(x=Txhyh[0], y=Txhyh[1], - mode="lines", line_width=lw, - line_color = 'red'), 1, 2) -fig.update_xaxes(range=[-4, 4]) -fig.update_yaxes(range=[-4, 4]) -fig.update_layout(width=800, height=500, showlegend=False, template="none", - plot_bgcolor="white", yaxis2_showgrid=False, xaxis2_showgrid=False) - -st.plotly_chart(fig) \ No newline at end of file