mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-26 02:48:40 +00:00
Replaced loops in jacobi_iteration_method function with vector operations. That gives a reduction in the time for calculating the algorithm.
This commit is contained in:
parent
ae0fc85401
commit
cc1455088b
@ -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
|
||||
"""
|
||||
|
||||
return [float(i) for i in 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 init_val.tolist()
|
||||
|
||||
|
||||
# Checks if the given matrix is strictly diagonally dominant
|
||||
|
Loading…
x
Reference in New Issue
Block a user