From dd1a7630b0e5477db0587586710b257159540ef2 Mon Sep 17 00:00:00 2001 From: ruxandravr Date: Sat, 20 Oct 2018 16:37:23 +0300 Subject: [PATCH 1/3] Add SST.py --- python/ad-hoc/SST.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 python/ad-hoc/SST.py diff --git a/python/ad-hoc/SST.py b/python/ad-hoc/SST.py new file mode 100644 index 0000000..25a5b08 --- /dev/null +++ b/python/ad-hoc/SST.py @@ -0,0 +1,20 @@ +import numpy as np +def SST(A, b): + # if the matrix is not upper triangular, we cannot solve the system + if np.allclose(A, np.triu(A)) == False: + print("The matrix A is not upper triangular!") + x = np.nan + return + + n = len(b) + x = np.zeros((n, 1)) + + # calculate xn + x[n - 1, 0] = b[n - 1, 0] / A[n - 1, n - 1] + + # calculate x(i) backwards + for i in range(n - 2, -1, -1): + sum_of_xs = np.dot(A[i, (i + 1) : n], x[(i + 1) : n, 0]) + x[i, 0] = (b[i, 0] - sum_of_xs) / A[i, i] + + return x \ No newline at end of file From 0280d55b9affdf37d65217a65fda8a13175978c0 Mon Sep 17 00:00:00 2001 From: ruxandravr Date: Sat, 20 Oct 2018 17:03:16 +0300 Subject: [PATCH 2/3] Add SIT.py --- python/ad-hoc/SIT.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 python/ad-hoc/SIT.py diff --git a/python/ad-hoc/SIT.py b/python/ad-hoc/SIT.py new file mode 100644 index 0000000..e02d22c --- /dev/null +++ b/python/ad-hoc/SIT.py @@ -0,0 +1,22 @@ +import numpy as np +def SIT(A, b): + # if the matrix is not lower triangular, we cannot solve the system + if np.allclose(A, np.tril(A)) == False: + print("The matrix A is not lower triangular!") + x = np.nan + return + + n = len(b) + x = np.zeros((n, 1)) + + # calculate x1 + x[0, 0] = b[0, 0] / A[0, 0] + + # calculate x(i) forwards + for i in range (1, n): + sum_of_xs = np.dot(A[i, 0 : i], x[0 : i, 0]) + x[i, 0] = (b[i, 0] - sum_of_xs) / A[i, i] + + return x + +print(SIT(np.tril(np.matrix([[1,2,3], [4,5,6], [7,8,9]])), np.array([[1], [14], [50]]))) \ No newline at end of file From 2eb85d34cb3eebfdc07b336d13c86025305de2d5 Mon Sep 17 00:00:00 2001 From: ruxandravr Date: Sat, 20 Oct 2018 17:17:33 +0300 Subject: [PATCH 3/3] Refactor SIT.py --- python/ad-hoc/SIT.py | 29 +++++++++++++---------------- python/ad-hoc/SST.py | 20 -------------------- 2 files changed, 13 insertions(+), 36 deletions(-) delete mode 100644 python/ad-hoc/SST.py diff --git a/python/ad-hoc/SIT.py b/python/ad-hoc/SIT.py index e02d22c..1faf8b4 100644 --- a/python/ad-hoc/SIT.py +++ b/python/ad-hoc/SIT.py @@ -1,22 +1,19 @@ import numpy as np def SIT(A, b): - # if the matrix is not lower triangular, we cannot solve the system - if np.allclose(A, np.tril(A)) == False: - print("The matrix A is not lower triangular!") - x = np.nan - return + # if the matrix is not lower triangular, we cannot solve the system + if not np.allclose(A, np.tril(A)): + print("The matrix A is not lower triangular!") + return np.nan - n = len(b) - x = np.zeros((n, 1)) + n = len(b) + x = np.zeros((n, 1)) - # calculate x1 - x[0, 0] = b[0, 0] / A[0, 0] + # calculate x1 + x[0, 0] = b[0, 0] / A[0, 0] - # calculate x(i) forwards - for i in range (1, n): - sum_of_xs = np.dot(A[i, 0 : i], x[0 : i, 0]) - x[i, 0] = (b[i, 0] - sum_of_xs) / A[i, i] + # calculate x(i) forwards + for i in range (1, n): + sum_of_xs = np.dot(A[i, 0 : i], x[0 : i, 0]) + x[i, 0] = (b[i, 0] - sum_of_xs) / A[i, i] - return x - -print(SIT(np.tril(np.matrix([[1,2,3], [4,5,6], [7,8,9]])), np.array([[1], [14], [50]]))) \ No newline at end of file + return x \ No newline at end of file diff --git a/python/ad-hoc/SST.py b/python/ad-hoc/SST.py deleted file mode 100644 index 25a5b08..0000000 --- a/python/ad-hoc/SST.py +++ /dev/null @@ -1,20 +0,0 @@ -import numpy as np -def SST(A, b): - # if the matrix is not upper triangular, we cannot solve the system - if np.allclose(A, np.triu(A)) == False: - print("The matrix A is not upper triangular!") - x = np.nan - return - - n = len(b) - x = np.zeros((n, 1)) - - # calculate xn - x[n - 1, 0] = b[n - 1, 0] / A[n - 1, n - 1] - - # calculate x(i) backwards - for i in range(n - 2, -1, -1): - sum_of_xs = np.dot(A[i, (i + 1) : n], x[(i + 1) : n, 0]) - x[i, 0] = (b[i, 0] - sum_of_xs) / A[i, i] - - return x \ No newline at end of file