mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
4 Commits
442d8ed531
...
43a39c1025
Author | SHA1 | Date | |
---|---|---|---|
|
43a39c1025 | ||
|
d288cbd864 | ||
|
cc545c5b49 | ||
|
4904dea1c4 |
@ -116,19 +116,6 @@ def jacobi_iteration_method(
|
|||||||
strictly_diagonally_dominant(table)
|
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
|
# Iterates the whole matrix for given number of times
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
new_val = []
|
new_val = []
|
||||||
@ -146,18 +133,40 @@ def jacobi_iteration_method(
|
|||||||
init_val = new_val
|
init_val = new_val
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
denom - a list of values along the diagonal
|
||||||
|
"""
|
||||||
denom = np.diag(coefficient_matrix)
|
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)
|
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)
|
i_row, i_col = np.where(masks)
|
||||||
ind = i_col.reshape(-1, rows - 1)
|
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
|
# Iterates the whole matrix for given number of times
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
arr = np.take(init_val, ind)
|
arr = np.take(init_val, ind)
|
||||||
temp = np.sum((-1) * ttt * arr, axis=1)
|
temp = np.sum((-1) * no_diag * arr, axis=1)
|
||||||
new_val = (temp + val) / denom
|
new_val = (temp + val_last) / denom
|
||||||
init_val = new_val
|
init_val = new_val
|
||||||
|
|
||||||
return new_val.tolist()
|
return new_val.tolist()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user