diff --git a/linear_algebra/matrices/pt1/ex5.py b/linear_algebra/matrices/pt1/ex5.py new file mode 100644 index 0000000000000000000000000000000000000000..dbda58f8df606aadb1ab249e174e061ffde515c1 --- /dev/null +++ b/linear_algebra/matrices/pt1/ex5.py @@ -0,0 +1,67 @@ +import numpy as np + + +def ex5(): + M = np.array([[1, 2], [-3, 4], [-5, -6]]) + N = np.array([[-1, 0, 1], [2, -2, 0]]) + v = np.array([3, -1, 5]) + + print(M) + print(N) + + # x = 1, y = 1 (aka 0, 0) of m + print(f"1, 1 of m = {M[0][0]}") + + # Shape + print(f"M.shape = {M.shape}") + print(f"N.shape = {N.shape}") + print(f"v.shape = {v.shape}") + + print(f"M * N = {M @ N}") + print(f"N * M = {N @ M}") + + # Testing different dot or matrix products + + # 3x1 3x2 => should not exist; v will be adjusted so that matmul will work + # (v from 3x1 to 1x3) + print(f"v * M = {v @ M}") + + # will not work even after adjusting v + # print(f"v * N = {v @ N}") + + # will not work even after adjusting v + # print(f"M * v = {M @ v}") + + # will work (3x2 X 3x1) + print(f"N * v = {N @ v}") + + # will work: 1x3 X 3x2 => 1x2 X 2x3 => 1x3 + print(f"v * M * N = {v @ M @ N}") + + # will not work: 1x3 X 2x3 => will fail + # print(f"v * N * M = {v @ N @ M}") + + # will work: 3x2 X 2x3 => 3x3 X 3x1 => 3x1 + print(f"M * N * v = {M @ N @ v}") + + # will fail: 2x3 X 3x2 => 2x2 X 3x1 => fail + # print(f"N * M * v = {N @ M @ v}") + + print(f"M^T = {np.transpose(M)}") + print(f"N^T = {np.transpose(N)}") + print(f"(M * N)^T = {np.transpose(M @ N)}") + print(f"N^T * M^T = {np.transpose(N) @ np.transpose(M)}") + + print(f"np.identity(3) = {np.identity(3)}") + print(f"np.diag([1, 2, 3]) = {np.diag([1, 2, 3])}") + print(f"np.diag(M) = {np.diag(M)}") + print(f"np.diag(N) = {np.diag(N)}") + print(f"np.diag(N @ M) = {np.diag(N @ M)}") + + # mdr, jsp ce qui se passe + print(f"v^T * v = {np.transpose(v) @ v}") + print(f"v * v^T = {v @ np.transpose(v)}") + + +if __name__ == "__main__": + ex5() diff --git a/linear_algebra/vecteurs/ex1.py b/linear_algebra/vecteurs/ex1.py new file mode 100644 index 0000000000000000000000000000000000000000..c4cea712d7e0f4c06ccd91b29e1aaee4dd025dd4 --- /dev/null +++ b/linear_algebra/vecteurs/ex1.py @@ -0,0 +1,64 @@ +import numpy as np + + +def ex1(): + v = np.array([3, -1, 5]) + # 2. + v_bis = v.copy() + + print("Before: ") + print(f"v = {v}") + print(f"v_bis = {v_bis}") + + # 1. + v[1] = 5 + + print("After: ") + print(f"v = {v}") + print(f"v_bis = {v_bis}") + + w = np.array([-2, -4, 5]) + v = np.array([3, -1, 5]) + print(f"w = {w}") + print(f"v = {v}") + + # 3. + res = 2 * v - 3 * w + print(f"2v - 3w = {res}") + + # 4. On ajoute 1 à chaque composante du vecteur v. Non cela ne correspond + # pas une répresentation mathématique quelconque + print(f"{v + 1}") + + # 5. Les comparaisons se font composantes par composantes + print(f"v > w: {v > w}") + print(f"v < w: {v < w}") + print(f"v == w {v == w}") + + # 6. Les comparaisons se font composantes par composantes mais agi comme + # un ET logique (AND) => retourne qu'une seule valeur + v_lst = v.tolist() + w_lst = w.tolist() + print(f"v_lst == w_lst: {v_lst == w_lst}") + print(f"v_lst >= w_lst: {v_lst >= w_lst}") + print(f"v_lst < w_lst: {v_lst < w_lst}") + + # 7. + dot_product = np.dot(v, w) + print(f"v * w = {dot_product}") + + # 8. + z = np.concatenate([v, w]) + print(f"z: {z}") + + # 9. + u = z[2:5] + print(f"u: {u}") + + # 10. + z[1:4] = 0 + print(f"z[1:4] = 0: {z}") + + +if __name__ == "__main__": + ex1() diff --git a/linear_algebra/vecteurs/ex2.py b/linear_algebra/vecteurs/ex2.py new file mode 100644 index 0000000000000000000000000000000000000000..914a0ff03b14aa8a6c1748a404c034808ba41d4e --- /dev/null +++ b/linear_algebra/vecteurs/ex2.py @@ -0,0 +1,18 @@ +import numpy as np + + +def ex2(): + u = np.array([1, 2, 3, 4]) + v = np.array([-3, 0, 1, 0]) + w = np.array([-1, 1, -1, 2]) + + print(f"||u|| = {np.linalg.norm(u)}") + print(f"||v|| = {np.linalg.norm(v)}") + print(f"||w|| = {np.linalg.norm(w)}") + + print(f"dist(u, v) = {np.linalg.norm(u - v)}") + print(f"dist(u, w) = {np.linalg.norm(u - w)}") + + +if __name__ == "__main__": + ex2() diff --git a/linear_algebra/vecteurs/ex3.py b/linear_algebra/vecteurs/ex3.py new file mode 100644 index 0000000000000000000000000000000000000000..d9c64317e69ef645a4da3bc461ba49b962de51f6 --- /dev/null +++ b/linear_algebra/vecteurs/ex3.py @@ -0,0 +1,17 @@ +import numpy as np + + +def ex3(): + # Angle between vectors: + # np.dot(u, v) / np.linalg.norm(u) * np.linalg.norm(v) = cos(theta) + u = np.array([1, 2, 3, 4]) + v = np.array([-3, 0, 1, 0]) + + cos_theta = np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v)) + theta = np.arccos(cos_theta) + + print(f"theta = {np.rad2deg(theta)}") + + +if __name__ == "__main__": + ex3() diff --git a/linear_algebra/vecteurs/ex4.py b/linear_algebra/vecteurs/ex4.py new file mode 100644 index 0000000000000000000000000000000000000000..495e06fe99cc7f7a305b90558490232d9d47e4c4 --- /dev/null +++ b/linear_algebra/vecteurs/ex4.py @@ -0,0 +1,33 @@ +import numpy as np + + +def ex4(): + v = np.ones(10) + std = np.std(v) + avg = np.average(v) + + print(f"std = {std}") + + lhs = np.sqrt(np.mean(np.square(v - avg * 1))) + print(f"lhs = {lhs}") + + print(f"std == lhs: {std == lhs}") + + +def avg(n: int): + return np.average(np.ones(n)) + + +def rms(n: int): + return np.sqrt(np.mean(np.square(np.ones(n)))) + + +def std(n: int): + return np.std(np.ones(n)) + + +if __name__ == "__main__": + ex4() + print(avg(10)) + print(rms(10)) + print(std(10))