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

Function retroactive_resolution

linear_algebra/gaussian_elimination.py:11–43  ·  view source on GitHub ↗

This function performs a retroactive linear system resolution for triangular matrix Examples: 1. * 2x1 + 2x2 - 1x3 = 5 * 0x1 - 2x2 - 1x3 = -7 * 0x1 + 0x2 + 5x3 = 15 2. * 2x1 + 2x2 = -1 * 0x1 - 2x2 = -1 >>>

(
    coefficients: NDArray[float64], vector: NDArray[float64]
)

Source from the content-addressed store, hash-verified

9
10
11def retroactive_resolution(
12 coefficients: NDArray[float64], vector: NDArray[float64]
13) -> NDArray[float64]:
14 """
15 This function performs a retroactive linear system resolution
16 for triangular matrix
17
18 Examples:
19 1.
20 * 2x1 + 2x2 - 1x3 = 5
21 * 0x1 - 2x2 - 1x3 = -7
22 * 0x1 + 0x2 + 5x3 = 15
23 2.
24 * 2x1 + 2x2 = -1
25 * 0x1 - 2x2 = -1
26
27 >>> gaussian_elimination([[2, 2, -1], [0, -2, -1], [0, 0, 5]], [[5], [-7], [15]])
28 array([[2.],
29 [2.],
30 [3.]])
31 >>> gaussian_elimination([[2, 2], [0, -2]], [[-1], [-1]])
32 array([[-1. ],
33 [ 0.5]])
34 """
35
36 rows, _columns = np.shape(coefficients)
37
38 x: NDArray[float64] = np.zeros((rows, 1), dtype=float)
39 for row in reversed(range(rows)):
40 total = np.dot(coefficients[row, row + 1 :], x[row + 1 :])
41 x[row, 0] = (vector[row][0] - total[0]) / coefficients[row, row]
42
43 return x
44
45
46def gaussian_elimination(

Callers 1

gaussian_eliminationFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected