@@ -32,40 +32,28 @@ def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
32
32
>>> solution = solve_linear_system(np.column_stack((A, B)))
33
33
>>> np.allclose(solution, np.array([2., 3., -1.]))
34
34
True
35
- >>> solve_linear_system(np.array([[0, 0], [0, 0]], dtype=float))
36
- array([nan, nan])
35
+ >>> solve_linear_system(np.array([[0, 0, 0], [0, 0, 0]], dtype=float))
36
+ Traceback (most recent call last):
37
+ ...
38
+ ValueError: Matrix is not correct
37
39
"""
38
40
ab = np .copy (matrix )
39
41
num_of_rows = ab .shape [0 ]
40
42
num_of_columns = ab .shape [1 ] - 1
41
43
x_lst : list [float ] = []
42
44
43
- # Lead element search
44
- for column_num in range (num_of_rows ):
45
- for i in range (column_num , num_of_columns ):
46
- if abs (ab [i ][column_num ]) > abs (ab [column_num ][column_num ]):
47
- ab [[column_num , i ]] = ab [[i , column_num ]]
48
- if ab [column_num , column_num ] == 0.0 :
49
- raise ValueError ("Matrix is not correct" )
50
- else :
51
- pass
52
- if column_num != 0 :
53
- for i in range (column_num , num_of_rows ):
54
- ab [i , :] -= (
55
- ab [i , column_num - 1 ]
56
- / ab [column_num - 1 , column_num - 1 ]
57
- * ab [column_num - 1 , :]
58
- )
45
+ assert num_of_rows == num_of_columns
59
46
60
- # Upper triangular matrix
61
47
for column_num in range (num_of_rows ):
48
+ # Lead element search
62
49
for i in range (column_num , num_of_columns ):
63
50
if abs (ab [i ][column_num ]) > abs (ab [column_num ][column_num ]):
64
51
ab [[column_num , i ]] = ab [[i , column_num ]]
65
- if ab [column_num , column_num ] == 0.0 :
66
- raise ValueError ("Matrix is not correct" )
67
- else :
68
- pass
52
+
53
+ # Upper triangular matrix
54
+ if ab [column_num , column_num ] == 0.0 :
55
+ raise ValueError ("Matrix is not correct" )
56
+
69
57
if column_num != 0 :
70
58
for i in range (column_num , num_of_rows ):
71
59
ab [i , :] -= (
0 commit comments