Compare commits

..

4 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
43a39c1025 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-13 14:14:53 +00:00
Kamil
d288cbd864
Update jacobi_iteration_method.py
left the old algorithm commented out, as it clearly shows what is being done.
2023-08-13 19:14:20 +05:00
pre-commit-ci[bot]
cc545c5b49 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-13 14:08:09 +00:00
Kamil
4904dea1c4
Update jacobi_iteration_method.py
Changed comments, made variable names more understandable.
2023-08-13 19:06:59 +05:00

View File

@ -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()