Skip to content

Commit df3a091

Browse files
linear_algebra: make matrix_inversion doctest deterministic (#15130)
numpy.linalg.inv can return platform-dependent float representations (e.g. 0.6 vs 0.6000000000000001), which made the doctest fragile across BLAS/LAPACK backends. Round the results in the doctests so the expected output is deterministic, and add a second invertible-matrix example.
1 parent 64646c5 commit df3a091

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

linear_algebra/matrix_inversion.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ def invert_matrix(matrix: list[list[float]]) -> list[list[float]]:
1111
Returns:
1212
list[list[float]]: Inverted matrix if invertible, else raises error.
1313
14-
>>> invert_matrix([[4.0, 7.0], [2.0, 6.0]])
15-
[[0.6000000000000001, -0.7000000000000001], [-0.2, 0.4]]
14+
The exact floating-point representation returned by ``numpy.linalg.inv``
15+
can vary slightly across platforms and BLAS/LAPACK backends
16+
(e.g. ``0.6`` vs ``0.6000000000000001``), so the doctests below round the
17+
result to make the expected output deterministic.
18+
19+
>>> [[round(x, 6) for x in row] for row in invert_matrix([[4.0, 7.0], [2.0, 6.0]])]
20+
[[0.6, -0.7], [-0.2, 0.4]]
21+
>>> [[round(x, 6) for x in row] for row in invert_matrix([[1.0, 0.0], [0.0, 2.0]])]
22+
[[1.0, 0.0], [0.0, 0.5]]
1623
>>> invert_matrix([[1.0, 2.0], [0.0, 0.0]])
1724
Traceback (most recent call last):
1825
...

0 commit comments

Comments
 (0)