Skip to content

Commit 4246da3

Browse files
jacobi_iteration_method.py the use of vector operations, which reduces the calculation time by dozens of times (TheAlgorithms#8938)
* Replaced loops in jacobi_iteration_method function with vector operations. That gives a reduction in the time for calculating the algorithm. * Replaced loops in jacobi_iteration_method function with vector operations. That gives a reduction in the time for calculating the algorithm. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Delete main.py * Update jacobi_iteration_method.py Changed a line that was too long. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update jacobi_iteration_method.py Changed the type of the returned list as required. * Update jacobi_iteration_method.py Replaced init_val with new_val. * 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]. * Update jacobi_iteration_method.py Changed comments, made variable names more understandable. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update jacobi_iteration_method.py left the old algorithm commented out, as it clearly shows what is being done. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update jacobi_iteration_method.py Edits upon request. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 97e2de0 commit 4246da3

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Diff for: arithmetic_analysis/jacobi_iteration_method.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def jacobi_iteration_method(
1313
coefficient_matrix: NDArray[float64],
1414
constant_matrix: NDArray[float64],
15-
init_val: list[int],
15+
init_val: list[float],
1616
iterations: int,
1717
) -> list[float]:
1818
"""
@@ -115,6 +115,7 @@ def jacobi_iteration_method(
115115

116116
strictly_diagonally_dominant(table)
117117

118+
"""
118119
# Iterates the whole matrix for given number of times
119120
for _ in range(iterations):
120121
new_val = []
@@ -130,8 +131,37 @@ def jacobi_iteration_method(
130131
temp = (temp + val) / denom
131132
new_val.append(temp)
132133
init_val = new_val
134+
"""
135+
136+
# denominator - a list of values along the diagonal
137+
denominator = np.diag(coefficient_matrix)
138+
139+
# val_last - values of the last column of the table array
140+
val_last = table[:, -1]
141+
142+
# masks - boolean mask of all strings without diagonal
143+
# elements array coefficient_matrix
144+
masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool)
145+
146+
# no_diagonals - coefficient_matrix array values without diagonal elements
147+
no_diagonals = coefficient_matrix[masks].reshape(-1, rows - 1)
148+
149+
# Here we get 'i_col' - these are the column numbers, for each row
150+
# without diagonal elements, except for the last column.
151+
i_row, i_col = np.where(masks)
152+
ind = i_col.reshape(-1, rows - 1)
153+
154+
#'i_col' is converted to a two-dimensional list 'ind', which will be
155+
# used to make selections from 'init_val' ('arr' array see below).
156+
157+
# Iterates the whole matrix for given number of times
158+
for _ in range(iterations):
159+
arr = np.take(init_val, ind)
160+
sum_product_rows = np.sum((-1) * no_diagonals * arr, axis=1)
161+
new_val = (sum_product_rows + val_last) / denominator
162+
init_val = new_val
133163

134-
return [float(i) for i in new_val]
164+
return new_val.tolist()
135165

136166

137167
# Checks if the given matrix is strictly diagonally dominant

0 commit comments

Comments
 (0)