From 3e550e510486bce7f71dd18cc6537c6f55ac23e3 Mon Sep 17 00:00:00 2001 From: "ASHWIN.S" <96253622+ashwin3005@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:36:15 +0530 Subject: [PATCH] Update 08-linear-algebra.md (#241) --- course-zoomcamp/01-intro/08-linear-algebra.md | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/course-zoomcamp/01-intro/08-linear-algebra.md b/course-zoomcamp/01-intro/08-linear-algebra.md index 45d458e97..673fa5986 100644 --- a/course-zoomcamp/01-intro/08-linear-algebra.md +++ b/course-zoomcamp/01-intro/08-linear-algebra.md @@ -7,6 +7,96 @@ ## Notes +### Linear Algebra Refresher +* Vector operations +* Multiplication + * Vector-vector multiplication + * Matrix-vector multiplication + * Matrix-matrix multiplication +* Identity matrix +* Inverse + +### Vector operations +~~~~python +u = np.array([2, 7, 5, 6]) +v = np.array([3, 4, 8, 6]) + +# addition +u + v + +# subtraction +u - v + +# scalar multiplication +2 * v +~~~~ +### Multiplication + +##### Vector-vector multiplication + +~~~~python +def vector_vector_multiplication(u, v): + assert u.shape[0] == v.shape[0] + + n = u.shape[0] + + result = 0.0 + + for i in range(n): + result = result + u[i] * v[i] + + return result +~~~~ + +##### Matrix-vector multiplication + +~~~~python +def matrix_vector_multiplication(U, v): + assert U.shape[1] == v.shape[0] + + num_rows = U.shape[0] + + result = np.zeros(num_rows) + + for i in range(num_rows): + result[i] = vector_vector_multiplication(U[i], v) + + return result +~~~~ + +##### Matrix-matrix multiplication + +~~~~python +def matrix_matrix_multiplication(U, V): + assert U.shape[1] == V.shape[0] + + num_rows = U.shape[0] + num_cols = V.shape[1] + + result = np.zeros((num_rows, num_cols)) + + for i in range(num_cols): + vi = V[:, i] + Uvi = matrix_vector_multiplication(U, vi) + result[:, i] = Uvi + + return result +~~~~ +### Identity matrix + +~~~~python +I = np.eye(3) +~~~~ +### Inverse +~~~~python +V = np.array([ + [1, 1, 2], + [0, 0.5, 1], + [0, 2, 1], +]) +inv = np.linalg.inv(V) +~~~~ + Add notes here (PRs are welcome).