@@ -22,17 +22,22 @@ def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
22
22
>>> solution = solve_linear_system(np.column_stack((A, B)))
23
23
>>> np.allclose(solution, np.array([2., 3., -1.]))
24
24
True
25
- >>> solve_linear_system(np.array([[0, 0, 0], [0, 0, 0]], dtype=float))
25
+ >>> solve_linear_system(np.array([[0, 0, 0]], dtype=float))
26
26
Traceback (most recent call last):
27
27
...
28
- ValueError: Matrix is not correct
28
+ ValueError: Matrix is not square
29
+ >>> solve_linear_system(np.array([[0, 0, 0], [0, 0, 0]], dtype=float))
30
+ Traceback (most recent call last):
31
+ ...
32
+ ValueError: Matrix is singular
29
33
"""
30
34
ab = np .copy (matrix )
31
35
num_of_rows = ab .shape [0 ]
32
36
num_of_columns = ab .shape [1 ] - 1
33
37
x_lst : list [float ] = []
34
38
35
- assert num_of_rows == num_of_columns
39
+ if num_of_rows != num_of_columns :
40
+ raise ValueError ("Matrix is not square" )
36
41
37
42
for column_num in range (num_of_rows ):
38
43
# Lead element search
@@ -41,8 +46,8 @@ def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
41
46
ab [[column_num , i ]] = ab [[i , column_num ]]
42
47
43
48
# Upper triangular matrix
44
- if ab [column_num , column_num ] == 0.0 :
45
- raise ValueError ("Matrix is not correct " )
49
+ if abs ( ab [column_num , column_num ]) < 1e-8 :
50
+ raise ValueError ("Matrix is singular " )
46
51
47
52
if column_num != 0 :
48
53
for i in range (column_num , num_of_rows ):
0 commit comments