Skip to content

Commit 47917f0

Browse files
authored
Merge pull request mpmath#1005 from baranwalayush/fix/matrix_ValueError
Fix mpmath#1004: Matrix should raise ValueError in case of negative dimensions
2 parents 23b8bbf + de1e15d commit 47917f0

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

mpmath/matrices/matrices.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,14 @@ def __init__(self, *args, **kwargs):
320320
elif isinstance(args[0], int):
321321
# create empty matrix of given dimensions
322322
if len(args) == 1:
323+
if args[0] < 0:
324+
raise ValueError("expected non-negative int")
323325
self._rows = self._cols = args[0]
324326
else:
325327
if not isinstance(args[1], int):
326328
raise TypeError("expected int")
329+
if args[0] < 0 or args[1] < 0:
330+
raise ValueError("expected non-negative int")
327331
self._rows = args[0]
328332
self._cols = args[1]
329333
elif isinstance(args[0], _matrix):

mpmath/tests/test_matrices.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def test_matrix_basic():
7575
A10[-3, -2] = 1
7676
assert A10[-3, -2] == 1
7777

78+
pytest.raises(ValueError, lambda: matrix(-2))
79+
pytest.raises(ValueError, lambda: matrix(2, -3))
80+
7881
def test_matmul():
7982
"""
8083
Test the PEP465 "@" matrix multiplication syntax.

0 commit comments

Comments
 (0)