mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-02 18:21:08 +08:00
Delete Book4_Ch04_Python_Codes directory
This commit is contained in:
committed by
GitHub
parent
85ed9b9746
commit
c8dbd7b9b3
@@ -1,41 +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_Ch4_01.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
# 2d matrix
|
||||
A_matrix = np.matrix([[2,4],
|
||||
[6,8]])
|
||||
print(A_matrix.shape)
|
||||
print(type(A_matrix))
|
||||
|
||||
# 1d array
|
||||
A_1d = np.array([2,4])
|
||||
print(A_1d.shape)
|
||||
print(type(A_1d))
|
||||
|
||||
# 2d array
|
||||
A_2d = np.array([[2,4],
|
||||
[6,8]])
|
||||
print(A_2d.shape)
|
||||
print(type(A_2d))
|
||||
|
||||
# 3d array
|
||||
A1 = [[2,4],
|
||||
[6,8]]
|
||||
|
||||
A2 = [[1,3],
|
||||
[5,7]]
|
||||
|
||||
A3 = [[1,0],
|
||||
[0,1]]
|
||||
A_3d = np.array([A1,A2,A3])
|
||||
print(A_3d.shape)
|
||||
print(type(A_3d))
|
||||
@@ -1,21 +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_Ch4_02.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.matrix([[1,2,3],
|
||||
[4,5,6],
|
||||
[7,8,9]])
|
||||
|
||||
# extract diagonal elements
|
||||
a = np.diag(A)
|
||||
|
||||
# construct a diagonal matrix
|
||||
A_diag = np.diag(a)
|
||||
@@ -1,24 +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_Ch4_03.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
# define matrix
|
||||
A = np.matrix([[1, 2], [3, 4]])
|
||||
B = np.matrix([[2, 6], [4, 8]])
|
||||
|
||||
# matrix addition
|
||||
A_plus_B = np.add(A,B)
|
||||
A_plus_B_2 = A + B
|
||||
|
||||
|
||||
# matrix subtraction
|
||||
A_minus_B = np.subtract(A,B)
|
||||
A_minus_B_2 = A - B
|
||||
@@ -1,19 +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_Ch4_04.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
k = 2
|
||||
X = [[1,2],
|
||||
[3,4]]
|
||||
|
||||
# scalar multiplication
|
||||
k_times_X = np.dot(k,X)
|
||||
k_times_X_2 = k*np.matrix(X)
|
||||
@@ -1,41 +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_Ch4_05.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
# define matrix
|
||||
A = np.matrix([[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]])
|
||||
|
||||
# scaler
|
||||
k = 2;
|
||||
|
||||
# column vector c
|
||||
c = np.array([[3],
|
||||
[2],
|
||||
[1]])
|
||||
|
||||
# row vector r
|
||||
r = np.array([[2,1]])
|
||||
|
||||
# broadcasting principles
|
||||
|
||||
# matrix A plus scalar k
|
||||
A_plus_k = A + k
|
||||
|
||||
# matrix A plus column vector c
|
||||
A_plus_a = A + c
|
||||
|
||||
# matrix A plus row vector r
|
||||
A_plus_r = A + r
|
||||
|
||||
# column vector c plus row vector r
|
||||
c_plus_r = c + r
|
||||
@@ -1,21 +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_Ch4_06.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.array([[1, 2],
|
||||
[3, 4]])
|
||||
|
||||
B = np.array([[2, 4],
|
||||
[1, 3]])
|
||||
|
||||
# matrix multiplication
|
||||
A_times_B = np.matmul(A, B)
|
||||
A_times_B_2 = A@B
|
||||
@@ -1,32 +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_Ch4_07.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.array([[1, 2]])
|
||||
|
||||
B = np.array([[5, 6],
|
||||
[8, 9]])
|
||||
|
||||
print(A*B)
|
||||
|
||||
A = np.array([[1, 2]])
|
||||
|
||||
B = np.matrix([[5, 6],
|
||||
[8, 9]])
|
||||
|
||||
print(A*B)
|
||||
|
||||
A = np.matrix([[1, 2]])
|
||||
|
||||
B = np.matrix([[5, 6],
|
||||
[8, 9]])
|
||||
|
||||
print(A*B)
|
||||
@@ -1,20 +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_Ch4_08.py
|
||||
|
||||
from numpy.linalg import matrix_power as pw
|
||||
A = np.array([[1., 2.],
|
||||
[3., 4.]])
|
||||
|
||||
# matrix inverse
|
||||
A_3 = pw(A,3)
|
||||
A_3_v3 = A@A@A
|
||||
|
||||
# piecewise power
|
||||
A_3_piecewise = A**3
|
||||
@@ -1,21 +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_Ch4_09.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.matrix([[1,3],
|
||||
[2,4]])
|
||||
|
||||
print(A**2)
|
||||
|
||||
B = np.array([[1,3],
|
||||
[2,4]])
|
||||
|
||||
print(B**2)
|
||||
@@ -1,19 +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_Ch4_10.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.array([[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]])
|
||||
|
||||
# matrix transpose
|
||||
A_T = A.transpose()
|
||||
A_T_2 = A.T
|
||||
@@ -1,17 +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_Ch4_11.py
|
||||
|
||||
from numpy.linalg import inv
|
||||
A = np.array([[1., 2.],
|
||||
[3., 4.]])
|
||||
|
||||
# matrix inverse
|
||||
A_inverse = inv(A)
|
||||
A_times_A_inv = A@A_inverse
|
||||
@@ -1,21 +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_Ch4_12.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.matrix([[1, 2],
|
||||
[3, 4]])
|
||||
|
||||
print(A.I)
|
||||
|
||||
B = np.array([[1, 2],
|
||||
[3, 4]])
|
||||
|
||||
print(B.I)
|
||||
@@ -1,17 +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_Ch4_13.py
|
||||
|
||||
import numpy as np
|
||||
A = np.array([[1, -1, 0],
|
||||
[3, 2, 4],
|
||||
[-2, 0, 3]])
|
||||
|
||||
# calculate trace of A
|
||||
tr_A = np.trace(A)
|
||||
@@ -1,21 +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_Ch4_14.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
A = np.array([[1,2],
|
||||
[3,4]])
|
||||
|
||||
B = np.array([[5,6],
|
||||
[7,8]])
|
||||
|
||||
# Hadamard product
|
||||
A_times_B_piecewise = np.multiply(A,B)
|
||||
A_times_B_piecewise_V2 = A*B
|
||||
@@ -1,16 +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_Ch4_15.py
|
||||
|
||||
import numpy as np
|
||||
A = np.array([[4, 2],
|
||||
[1, 3]])
|
||||
|
||||
# calculate determinant of A
|
||||
det_A = np.linalg.det(A)
|
||||
@@ -1,129 +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('c',-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)))
|
||||
square_x = np.array([0, 1, 1, 0])
|
||||
square_y = np.array([0, 0, 1, 1])
|
||||
square_array = np.stack((square_x, square_y))
|
||||
|
||||
fig.add_trace(go.Scatter(x=square_x, y=square_y,
|
||||
fill="toself", line_color='orange'), 1, 1)
|
||||
|
||||
A_times_square_array = A@square_array
|
||||
|
||||
fig.add_trace(go.Scatter(x=A_times_square_array[0,:],
|
||||
y=A_times_square_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 = '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)
|
||||
Reference in New Issue
Block a user