-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Jacobi Iteration Method #5113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Jacobi Iteration Method #5113
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6a79b61
Added Jacobi Iteration Method
eviltypha 2a3fd8e
Added comments
eviltypha d9b9e1d
Added reference link
eviltypha ff09c1b
Update jacobi_iteration_method.py
eviltypha cca524b
Changes for codespell test
eviltypha 58503a3
Update jacobi_iteration_method.py
eviltypha 4e81fa9
Update jacobi_iteration_method.py
eviltypha 047d159
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha d06ce84
Merge branch 'TheAlgorithms:master' into master
eviltypha f06339c
updating DIRECTORY.md
97b2ad4
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha 01bd5d1
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha 4f3f8ca
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha b5cd6f2
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha 5144650
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha e9c9f36
Update arithmetic_analysis/jacobi_iteration_method.py
eviltypha 40185a1
Update jacobi_iteration_method.py
eviltypha f491ca0
Update jacobi_iteration_method.py
eviltypha b214e3f
Update jacobi_iteration_method.py
eviltypha 570ecc4
fix styles
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
""" | ||
Jacobi Iteration Method - https://en.wikipedia.org/wiki/Jacobi_method | ||
""" | ||
|
||
from typing import List | ||
|
||
import numpy as np | ||
|
||
|
||
# Method to find solution of system of linear equations | ||
def jacobi_iteration_method( | ||
coefficient_matrix: np.ndarray, | ||
constant_matrix: np.ndarray, | ||
init_val: list, | ||
iterations: int, | ||
) -> List[float]: | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Jacobi Iteration Method: | ||
An iterative algorithm to determine the solutions of strictly diagonally dominant | ||
system of linear equations | ||
|
||
4x1 + x2 + x3 = 2 | ||
x1 + 5x2 + 2x3 = -6 | ||
x1 + 2x2 + 4x3 = -4 | ||
|
||
x_init = [0.5, -0.5 , -0.5] | ||
|
||
Examples: | ||
|
||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]]) | ||
>>> constant = np.array([[2], [-6], [-4]]) | ||
>>> init_val = [0.5, -0.5, -0.5] | ||
>>> iterations = 3 | ||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations) | ||
[0.909375, -1.14375, -0.7484375] | ||
|
||
|
||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2]]) | ||
>>> constant = np.array([[2], [-6], [-4]]) | ||
>>> init_val = [0.5, -0.5, -0.5] | ||
>>> iterations = 3 | ||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Coefficient matrix dimensions must be nxn but received 2x3 | ||
|
||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]]) | ||
>>> constant = np.array([[2], [-6]]) | ||
>>> init_val = [0.5, -0.5, -0.5] | ||
>>> iterations = 3 | ||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Coefficient and constant matrices dimensions must be nxn and nx1 but | ||
received 3x3 and 2x1 | ||
|
||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]]) | ||
>>> constant = np.array([[2], [-6], [-4]]) | ||
>>> init_val = [0.5, -0.5] | ||
>>> iterations = 3 | ||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Number of initial values must be equal to number of rows in coefficient | ||
matrix but received 2 and 3 | ||
|
||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]]) | ||
>>> constant = np.array([[2], [-6], [-4]]) | ||
>>> init_val = [0.5, -0.5, -0.5] | ||
>>> iterations = 0 | ||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Iterations must be at least 1 | ||
|
||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
rows1, cols1 = coefficient_matrix.shape | ||
rows2, cols2 = constant_matrix.shape | ||
|
||
if rows1 != cols1: | ||
raise ValueError( | ||
f"Coefficient matrix dimensions must be nxn but received {rows1}x{cols1}" | ||
) | ||
|
||
if cols2 != 1: | ||
raise ValueError(f"Constant matrix must be nx1 but received {rows2}x{cols2}") | ||
|
||
if rows1 != rows2: | ||
raise ValueError( | ||
f"""Coefficient and constant matrices dimensions must be nxn and nx1 but | ||
received {rows1}x{cols1} and {rows2}x{cols2}""" | ||
) | ||
|
||
if len(init_val) != rows1: | ||
raise ValueError( | ||
f"""Number of initial values must be equal to number of rows in coefficient | ||
matrix but received {len(init_val)} and {rows1}""" | ||
) | ||
|
||
if iterations <= 0: | ||
raise ValueError("Iterations must be at least 1") | ||
|
||
table = np.concatenate((coefficient_matrix, constant_matrix), axis=1) | ||
|
||
rows, cols = table.shape | ||
|
||
strictly_diagonally_dominant(table) | ||
|
||
# Iterates the whole matrix for given number of times | ||
for i in range(0, iterations): | ||
new_val = [] | ||
for row in range(0, rows): | ||
temp = 0 | ||
for col in range(0, cols): | ||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if col == row: | ||
denom = table[row][col] | ||
elif col == cols - 1: | ||
val = table[row][col] | ||
else: | ||
temp = temp + (-1) * table[row][col] * init_val[col] | ||
temp = (temp + val) / denom | ||
new_val.append(temp) | ||
init_val = new_val | ||
|
||
return new_val | ||
|
||
|
||
# Checks if the given matrix is strictly diagonally dominant | ||
def strictly_diagonally_dominant(table: np.ndarray) -> bool: | ||
""" | ||
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 4, -4]]) | ||
>>> strictly_diagonally_dominant(table) | ||
True | ||
|
||
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 3, -4]]) | ||
>>> strictly_diagonally_dominant(table) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Coefficient matrix is not strictly diagonally dominant | ||
|
||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
rows, cols = table.shape | ||
|
||
is_diagonally_dominant = True | ||
|
||
for i in range(0, rows): | ||
sum = 0 | ||
for j in range(0, cols - 1): | ||
if i == j: | ||
continue | ||
else: | ||
sum = sum + table[i][j] | ||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if table[i][i] <= sum: | ||
is_diagonally_dominant = False | ||
break | ||
|
||
if is_diagonally_dominant is False: | ||
raise ValueError("Coefficient matrix is not strictly diagonally dominant") | ||
eviltypha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return is_diagonally_dominant | ||
|
||
|
||
# Test Cases | ||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.