diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py new file mode 100644 index 0000000..9813ec3 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py @@ -0,0 +1,41 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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)) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py new file mode 100644 index 0000000..94e4574 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_03.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_03.py new file mode 100644 index 0000000..f7fa989 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_03.py @@ -0,0 +1,24 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_04.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_04.py new file mode 100644 index 0000000..769531d --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_04.py @@ -0,0 +1,19 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_05.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_05.py new file mode 100644 index 0000000..1bc5f51 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_05.py @@ -0,0 +1,41 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_06.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_06.py new file mode 100644 index 0000000..32563c9 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_06.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_07.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_07.py new file mode 100644 index 0000000..1aa25ac --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_07.py @@ -0,0 +1,32 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_08.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_08.py new file mode 100644 index 0000000..efe16e3 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_08.py @@ -0,0 +1,20 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_09.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_09.py new file mode 100644 index 0000000..2ac02f3 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_09.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_10.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_10.py new file mode 100644 index 0000000..619f1d9 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_10.py @@ -0,0 +1,19 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_11.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_11.py new file mode 100644 index 0000000..e87ca9c --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_11.py @@ -0,0 +1,17 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_12.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_12.py new file mode 100644 index 0000000..fb8a5f9 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_12.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_13.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_13.py new file mode 100644 index 0000000..248e84f --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_13.py @@ -0,0 +1,17 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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) diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_14.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_14.py new file mode 100644 index 0000000..6672cc7 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_14.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_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 diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_15.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_15.py new file mode 100644 index 0000000..cb748dd --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_15.py @@ -0,0 +1,16 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_15.py + +import numpy as np +A = np.array([[3, 1], + [2, 4]]) + +# calculate determinant of A +det_A = np.linalg.det(A) diff --git a/Book4_Ch03_矩阵__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch03_矩阵__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..3172e43 Binary files /dev/null and b/Book4_Ch03_矩阵__数学要素__从加减乘除到机器学习.pdf differ