From 0c62b308ccd432d98c71de7432968d8eff77973d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 23 Jan 2019 14:38:36 +0100 Subject: [PATCH] added some exercises --- ex_41.py | 20 ++++++++++++++++++++ ex_42.py | 25 +++++++++++++++++++++++++ ex_43.py | 15 +++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 ex_41.py create mode 100644 ex_42.py create mode 100644 ex_43.py diff --git a/ex_41.py b/ex_41.py new file mode 100644 index 0000000..25694b0 --- /dev/null +++ b/ex_41.py @@ -0,0 +1,20 @@ +import numpy as np + +A = np.array([ [1, 5, 1] + , [5, 0, -3] + , [1, -3, 7]]) + +print(np.linalg.eig(A)) + +O = np.linalg.eig(A)[1] +D = np.zeros((3, 3)) +for i, v in enumerate(np.linalg.eig(A)[0]): + D[i][i] = v + +I = np.zeros((3, 3)) +for i in range(3): + I[i][i] = 1 + +print("(2)", np.allclose(np.dot(O.T, O), I)) +print("(3)", np.allclose(A, np.dot(np.dot(O, D), O.T))) +print("(4)", np.allclose(np.dot(A, A), np.dot(np.dot(O, np.dot(D, D)), O.T))) diff --git a/ex_42.py b/ex_42.py new file mode 100644 index 0000000..9994fda --- /dev/null +++ b/ex_42.py @@ -0,0 +1,25 @@ +import numpy as np + + +A = np.array( + [ [0, 1, 1, 0, 0, 0] + , [1, 0, 1, 0, 1, 0] + , [1, 1, 0, 1, 0, 0] + , [0, 0, 1, 0, 1, 0] + , [0, 0, 0, 1, 0, 1] + , [0, 0, 0, 0, 1, 0] + ]) + +eigvalues, eigvectors = np.linalg.eig(A) + +l_max_i = eigvalues.argmax() +l_max = eigvalues[l_max_i] +v_max = eigvectors[l_max_i] + +def some_norm(M): + return np.abs(M).max() + +for k in range(1, 21): + print("some kind of error for k =", k, ":", some_norm(A**k - l_max**k * np.outer(v_max, v_max.T))) + + diff --git a/ex_43.py b/ex_43.py new file mode 100644 index 0000000..2c7f569 --- /dev/null +++ b/ex_43.py @@ -0,0 +1,15 @@ +import numpy as np + +P = np.array( + [ [1/6, 1/6, 1/6, 1/6, 1/6, 0] + , [1/6, 1/6, 1/6, 1/6, 1/6, 0] + , [1/6, 1/6, 1/6, 1/6, 1/6, 0] + , [1/6, 1/6, 1/6, 1/6, 1/6, 0] + , [1/6, 1/6, 1/6, 1/6, 1/6, 1/10] + , [1/6, 1/6, 1/6, 1/6, 1/6, 9/10]]) +print(P.shape) + +v = np.array([0, 1, 0, 0, 0, 0]) +w = np.power(np.dot(P**4, v), 1/4) +print(w) +print(w[-1])