From cc1455088bd28c3627e7e57fb714aecc5939120d Mon Sep 17 00:00:00 2001 From: quant12345 Date: Wed, 9 Aug 2023 15:39:11 +0500 Subject: [PATCH 01/15] Replaced loops in jacobi_iteration_method function with vector operations. That gives a reduction in the time for calculating the algorithm. --- .../jacobi_iteration_method.py | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 17edf4bf4b8b..042b5dab8f4e 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -10,10 +10,10 @@ # Method to find solution of system of linear equations def jacobi_iteration_method( - coefficient_matrix: NDArray[float64], - constant_matrix: NDArray[float64], - init_val: list[int], - iterations: int, + coefficient_matrix: NDArray[float64], + constant_matrix: NDArray[float64], + init_val: list[int], + iterations: int, ) -> list[float]: """ Jacobi Iteration Method: @@ -115,6 +115,17 @@ def jacobi_iteration_method( strictly_diagonally_dominant(table) + """ + denom - a list of values along the diagonal + val - values of the last column of the table array + masks - boolean mask of all strings without diagonal elements array coefficient_matrix + ttt - coefficient_matrix array values without diagonal elements + ind - column indexes for each row without diagonal elements + arr - list obtained by column indexes from the list init_val + + the code below uses vectorized operations based on + the previous algorithm on loopss: + # Iterates the whole matrix for given number of times for _ in range(iterations): new_val = [] @@ -130,8 +141,23 @@ def jacobi_iteration_method( temp = (temp + val) / denom new_val.append(temp) init_val = new_val + """ + + denom = np.diag(coefficient_matrix) + val = table[:, -1] + masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool) + ttt = coefficient_matrix[masks].reshape(-1, rows - 1) + i_row, i_col = np.where(masks) + ind = i_col.reshape(-1, rows - 1) + + # Iterates the whole matrix for given number of times + for _ in range(iterations): + arr = np.take(init_val, ind) + temp = np.sum((-1) * ttt * arr, axis=1) + new_val = (temp + val) / denom + init_val = new_val - return [float(i) for i in new_val] + return init_val.tolist() # Checks if the given matrix is strictly diagonally dominant From 5fa2a6e7b6523b529042f74e929c1ca4c8da2aad Mon Sep 17 00:00:00 2001 From: quant12345 Date: Wed, 9 Aug 2023 15:40:34 +0500 Subject: [PATCH 02/15] Replaced loops in jacobi_iteration_method function with vector operations. That gives a reduction in the time for calculating the algorithm. --- main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 000000000000..5596b44786f0 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ From 8e21133d25471100da6b15e82a2f310308bf6e9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:36:35 +0000 Subject: [PATCH 03/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/jacobi_iteration_method.py | 12 ++++++------ main.py | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 042b5dab8f4e..8d5786949375 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -10,10 +10,10 @@ # Method to find solution of system of linear equations def jacobi_iteration_method( - coefficient_matrix: NDArray[float64], - constant_matrix: NDArray[float64], - init_val: list[int], - iterations: int, + coefficient_matrix: NDArray[float64], + constant_matrix: NDArray[float64], + init_val: list[int], + iterations: int, ) -> list[float]: """ Jacobi Iteration Method: @@ -118,12 +118,12 @@ def jacobi_iteration_method( """ denom - a list of values along the diagonal val - values of the last column of the table array - masks - boolean mask of all strings without diagonal elements array coefficient_matrix + masks - boolean mask of all strings without diagonal elements array coefficient_matrix ttt - coefficient_matrix array values without diagonal elements ind - column indexes for each row without diagonal elements arr - list obtained by column indexes from the list init_val - the code below uses vectorized operations based on + the code below uses vectorized operations based on the previous algorithm on loopss: # Iterates the whole matrix for given number of times diff --git a/main.py b/main.py index 5596b44786f0..c937c7cff1be 100644 --- a/main.py +++ b/main.py @@ -6,11 +6,11 @@ def print_hi(name): # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + print(f"Hi, {name}") # Press Ctrl+F8 to toggle the breakpoint. # Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') +if __name__ == "__main__": + print_hi("PyCharm") # See PyCharm help at https://www.jetbrains.com/help/pycharm/ From b86d0435f2066f34647c8e2fe0f2ef0d0352f056 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Wed, 9 Aug 2023 17:23:17 +0500 Subject: [PATCH 04/15] Delete main.py --- main.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 main.py diff --git a/main.py b/main.py deleted file mode 100644 index c937c7cff1be..000000000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press Shift+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f"Hi, {name}") # Press Ctrl+F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == "__main__": - print_hi("PyCharm") - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ From a735995360ac3a458a9ff36e7ba22c189bc4aa97 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Wed, 9 Aug 2023 17:50:34 +0500 Subject: [PATCH 05/15] Update jacobi_iteration_method.py Changed a line that was too long. --- arithmetic_analysis/jacobi_iteration_method.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 8d5786949375..31ed2be26341 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -118,7 +118,10 @@ def jacobi_iteration_method( """ denom - a list of values along the diagonal val - values of the last column of the table array - masks - boolean mask of all strings without diagonal elements array coefficient_matrix + + masks - boolean mask of all strings without diagonal + elements array coefficient_matrix + ttt - coefficient_matrix array values without diagonal elements ind - column indexes for each row without diagonal elements arr - list obtained by column indexes from the list init_val From 34b0a121edddf27620a05cc672bf8fdc956db289 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 12:53:15 +0000 Subject: [PATCH 06/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/jacobi_iteration_method.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 31ed2be26341..21317245ed7a 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -118,10 +118,10 @@ def jacobi_iteration_method( """ denom - a list of values along the diagonal val - values of the last column of the table array - - masks - boolean mask of all strings without diagonal + + masks - boolean mask of all strings without diagonal elements array coefficient_matrix - + ttt - coefficient_matrix array values without diagonal elements ind - column indexes for each row without diagonal elements arr - list obtained by column indexes from the list init_val From 2be3f22321f14c2c19679171e4605270200d0ddd Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:10:09 +0500 Subject: [PATCH 07/15] Update jacobi_iteration_method.py Changed the type of the returned list as required. --- arithmetic_analysis/jacobi_iteration_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 21317245ed7a..5c23b1e8ac15 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -14,7 +14,7 @@ def jacobi_iteration_method( constant_matrix: NDArray[float64], init_val: list[int], iterations: int, -) -> list[float]: +) -> NDArray[float64]: """ Jacobi Iteration Method: An iterative algorithm to determine the solutions of strictly diagonally dominant From cb4ea2002b62d2aa25c10687d7ee99a9aacc3cd6 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:24:29 +0500 Subject: [PATCH 08/15] Update jacobi_iteration_method.py Replaced init_val with new_val. --- arithmetic_analysis/jacobi_iteration_method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 5c23b1e8ac15..563dd56cb951 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -14,7 +14,7 @@ def jacobi_iteration_method( constant_matrix: NDArray[float64], init_val: list[int], iterations: int, -) -> NDArray[float64]: +) -> list[float]: """ Jacobi Iteration Method: An iterative algorithm to determine the solutions of strictly diagonally dominant @@ -160,7 +160,7 @@ def jacobi_iteration_method( new_val = (temp + val) / denom init_val = new_val - return init_val.tolist() + return new_val.tolist() # Checks if the given matrix is strictly diagonally dominant From 442d8ed531fcfbba68cd823c14e694afe7e63d68 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:39:19 +0500 Subject: [PATCH 09/15] Update jacobi_iteration_method.py Fixed bug: init_val: list[int] to list[float]. Since the numbers are fractional: init_val = [0.5, -0.5, -0.5]. --- arithmetic_analysis/jacobi_iteration_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 563dd56cb951..44bcec273ab0 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -12,7 +12,7 @@ def jacobi_iteration_method( coefficient_matrix: NDArray[float64], constant_matrix: NDArray[float64], - init_val: list[int], + init_val: list[float], iterations: int, ) -> list[float]: """ From 4904dea1c4870ea97f6df712ac62482912e024d1 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Sun, 13 Aug 2023 19:06:59 +0500 Subject: [PATCH 10/15] Update jacobi_iteration_method.py Changed comments, made variable names more understandable. --- .../jacobi_iteration_method.py | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 44bcec273ab0..cfbf88f1e57c 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -117,47 +117,38 @@ def jacobi_iteration_method( """ denom - a list of values along the diagonal - val - values of the last column of the table array - - masks - boolean mask of all strings without diagonal - elements array coefficient_matrix - - ttt - coefficient_matrix array values without diagonal elements - ind - column indexes for each row without diagonal elements - arr - list obtained by column indexes from the list init_val - - the code below uses vectorized operations based on - the previous algorithm on loopss: - - # Iterates the whole matrix for given number of times - for _ in range(iterations): - new_val = [] - for row in range(rows): - temp = 0 - for col in range(cols): - if col == row: - denom = table[row][col] - elif col == cols - 1: - val = table[row][col] - else: - temp += (-1) * table[row][col] * init_val[col] - temp = (temp + val) / denom - new_val.append(temp) - init_val = new_val """ - denom = np.diag(coefficient_matrix) - val = table[:, -1] + """ + val_last - values of the last column of the table array + """ + val_last = table[:, -1] + """ + masks - boolean mask of all strings without diagonal + elements array coefficient_matrix + """ masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool) - ttt = coefficient_matrix[masks].reshape(-1, rows - 1) + """ + no_diag - coefficient_matrix array values without diagonal elements + """ + no_diag = coefficient_matrix[masks].reshape(-1, rows - 1) + """ + Here we get 'i_col' - these are the column numbers, for each row + without diagonal elements, except for the last column. + """ i_row, i_col = np.where(masks) ind = i_col.reshape(-1, rows - 1) + """ + 'i_col' is converted to a two-dimensional list 'ind', + which will be used to make selections from 'init_val' + ('arr' array see below). + """ # Iterates the whole matrix for given number of times for _ in range(iterations): arr = np.take(init_val, ind) - temp = np.sum((-1) * ttt * arr, axis=1) - new_val = (temp + val) / denom + temp = np.sum((-1) * no_diag * arr, axis=1) + new_val = (temp + val_last) / denom init_val = new_val return new_val.tolist() From cc545c5b493f94e89917cd5b758f77511f8b4458 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Aug 2023 14:08:08 +0000 Subject: [PATCH 11/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/jacobi_iteration_method.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index cfbf88f1e57c..7bb285b68b5e 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -124,8 +124,8 @@ def jacobi_iteration_method( """ val_last = table[:, -1] """ - masks - boolean mask of all strings without diagonal - elements array coefficient_matrix + masks - boolean mask of all strings without diagonal + elements array coefficient_matrix """ masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool) """ @@ -133,13 +133,13 @@ def jacobi_iteration_method( """ no_diag = coefficient_matrix[masks].reshape(-1, rows - 1) """ - Here we get 'i_col' - these are the column numbers, for each row + Here we get 'i_col' - these are the column numbers, for each row without diagonal elements, except for the last column. """ i_row, i_col = np.where(masks) ind = i_col.reshape(-1, rows - 1) """ - 'i_col' is converted to a two-dimensional list 'ind', + 'i_col' is converted to a two-dimensional list 'ind', which will be used to make selections from 'init_val' ('arr' array see below). """ From d288cbd86490452172b7e2896c055538a002cdcf Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Sun, 13 Aug 2023 19:14:20 +0500 Subject: [PATCH 12/15] Update jacobi_iteration_method.py left the old algorithm commented out, as it clearly shows what is being done. --- arithmetic_analysis/jacobi_iteration_method.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 7bb285b68b5e..ad722ef36bf8 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -114,6 +114,24 @@ def jacobi_iteration_method( rows, cols = table.shape strictly_diagonally_dominant(table) + + """ + # Iterates the whole matrix for given number of times + for _ in range(iterations): + new_val = [] + for row in range(rows): + temp = 0 + for col in range(cols): + if col == row: + denom = table[row][col] + elif col == cols - 1: + val = table[row][col] + else: + temp += (-1) * table[row][col] * init_val[col] + temp = (temp + val) / denom + new_val.append(temp) + init_val = new_val + """ """ denom - a list of values along the diagonal From 43a39c102540ddaa2b655bcfd75c0a3224d727d8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Aug 2023 14:14:52 +0000 Subject: [PATCH 13/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/jacobi_iteration_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index ad722ef36bf8..4b587e7c39e2 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -114,7 +114,7 @@ def jacobi_iteration_method( rows, cols = table.shape strictly_diagonally_dominant(table) - + """ # Iterates the whole matrix for given number of times for _ in range(iterations): From 5c1a12bf01e6ab11595aac10fc517b98cc0297ab Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:09:34 +0500 Subject: [PATCH 14/15] Update jacobi_iteration_method.py Edits upon request. --- .../jacobi_iteration_method.py | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 4b587e7c39e2..d2a9c95e4e1e 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -133,40 +133,33 @@ def jacobi_iteration_method( init_val = new_val """ - """ - denom - a list of values along the diagonal - """ - denom = np.diag(coefficient_matrix) - """ - val_last - values of the last column of the table array - """ + # denominator - a list of values along the diagonal + denominator = np.diag(coefficient_matrix) + + #val_last - values of the last column of the table array val_last = table[:, -1] - """ - masks - boolean mask of all strings without diagonal - elements array coefficient_matrix - """ + + #masks - boolean mask of all strings without diagonal + #elements array coefficient_matrix masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool) - """ - no_diag - coefficient_matrix array values without diagonal elements - """ - no_diag = coefficient_matrix[masks].reshape(-1, rows - 1) - """ - Here we get 'i_col' - these are the column numbers, for each row - without diagonal elements, except for the last column. - """ + + #no_diagonals - coefficient_matrix array values without diagonal elements + no_diagonals = coefficient_matrix[masks].reshape(-1, rows - 1) + + #Here we get 'i_col' - these are the column numbers, for each row + #without diagonal elements, except for the last column. i_row, i_col = np.where(masks) ind = i_col.reshape(-1, rows - 1) - """ - 'i_col' is converted to a two-dimensional list 'ind', - which will be used to make selections from 'init_val' - ('arr' array see below). - """ + + #'i_col' is converted to a two-dimensional list 'ind', which will be + #used to make selections from 'init_val' ('arr' array see below). + # Iterates the whole matrix for given number of times for _ in range(iterations): arr = np.take(init_val, ind) - temp = np.sum((-1) * no_diag * arr, axis=1) - new_val = (temp + val_last) / denom + sum_product_rows = np.sum((-1) * no_diagonals * arr, axis=1) + new_val = (sum_product_rows + val_last) / denominator init_val = new_val return new_val.tolist() From 240f388a6af3561c8bb9f1913dba8e84ac49ba2a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Sep 2023 10:10:09 +0000 Subject: [PATCH 15/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/jacobi_iteration_method.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index d2a9c95e4e1e..0f185e38a98f 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -136,24 +136,23 @@ def jacobi_iteration_method( # denominator - a list of values along the diagonal denominator = np.diag(coefficient_matrix) - #val_last - values of the last column of the table array + # val_last - values of the last column of the table array val_last = table[:, -1] - #masks - boolean mask of all strings without diagonal - #elements array coefficient_matrix + # masks - boolean mask of all strings without diagonal + # elements array coefficient_matrix masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool) - #no_diagonals - coefficient_matrix array values without diagonal elements + # no_diagonals - coefficient_matrix array values without diagonal elements no_diagonals = coefficient_matrix[masks].reshape(-1, rows - 1) - #Here we get 'i_col' - these are the column numbers, for each row - #without diagonal elements, except for the last column. + # Here we get 'i_col' - these are the column numbers, for each row + # without diagonal elements, except for the last column. i_row, i_col = np.where(masks) ind = i_col.reshape(-1, rows - 1) #'i_col' is converted to a two-dimensional list 'ind', which will be - #used to make selections from 'init_val' ('arr' array see below). - + # used to make selections from 'init_val' ('arr' array see below). # Iterates the whole matrix for given number of times for _ in range(iterations):