MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / jacobi_iteration_method

Function jacobi_iteration_method

linear_algebra/jacobi_iteration_method.py:13–165  ·  view source on GitHub ↗

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.arr

(
    coefficient_matrix: NDArray[float64],
    constant_matrix: NDArray[float64],
    init_val: list[float],
    iterations: int,
)

Source from the content-addressed store, hash-verified

11
12# Method to find solution of system of linear equations
13def jacobi_iteration_method(
14 coefficient_matrix: NDArray[float64],
15 constant_matrix: NDArray[float64],
16 init_val: list[float],
17 iterations: int,
18) -> list[float]:
19 """
20 Jacobi Iteration Method:
21 An iterative algorithm to determine the solutions of strictly diagonally dominant
22 system of linear equations
23
24 4x1 + x2 + x3 = 2
25 x1 + 5x2 + 2x3 = -6
26 x1 + 2x2 + 4x3 = -4
27
28 x_init = [0.5, -0.5 , -0.5]
29
30 Examples:
31
32 >>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
33 >>> constant = np.array([[2], [-6], [-4]])
34 >>> init_val = [0.5, -0.5, -0.5]
35 >>> iterations = 3
36 >>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
37 [0.909375, -1.14375, -0.7484375]
38
39
40 >>> coefficient = np.array([[4, 1, 1], [1, 5, 2]])
41 >>> constant = np.array([[2], [-6], [-4]])
42 >>> init_val = [0.5, -0.5, -0.5]
43 >>> iterations = 3
44 >>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
45 Traceback (most recent call last):
46 ...
47 ValueError: Coefficient matrix dimensions must be nxn but received 2x3
48
49 >>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
50 >>> constant = np.array([[2], [-6]])
51 >>> init_val = [0.5, -0.5, -0.5]
52 >>> iterations = 3
53 >>> jacobi_iteration_method(
54 ... coefficient, constant, init_val, iterations
55 ... ) # doctest: +NORMALIZE_WHITESPACE
56 Traceback (most recent call last):
57 ...
58 ValueError: Coefficient and constant matrices dimensions must be nxn and nx1 but
59 received 3x3 and 2x1
60
61 >>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
62 >>> constant = np.array([[2], [-6], [-4]])
63 >>> init_val = [0.5, -0.5]
64 >>> iterations = 3
65 >>> jacobi_iteration_method(
66 ... coefficient, constant, init_val, iterations
67 ... ) # doctest: +NORMALIZE_WHITESPACE
68 Traceback (most recent call last):
69 ...
70 ValueError: Number of initial values must be equal to number of rows in coefficient

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected