Skip to content

Commit 7d476ed

Browse files
committed
fix case not zero-testable + fix bug issue #36065
1 parent b1dae92 commit 7d476ed

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/sage/matrix/args.pyx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,24 @@ cdef class MatrixArgs:
848848
Traceback (most recent call last):
849849
...
850850
ValueError: sequence too short (expected length 6, got 1)
851+
852+
Check github issue #36065:
853+
854+
sage: class MyAlgebraicNumber(sage.rings.qqbar.AlgebraicNumber):
855+
....: def __bool__(self):
856+
....: raise ValueError
857+
sage: matrix(1, 1, MyAlgebraicNumber(0))
858+
[0]
859+
sage: matrix(1, 1, MyAlgebraicNumber(3))
860+
[3]
861+
sage: matrix(1, 2, MyAlgebraicNumber(0))
862+
Traceback (most recent call last):
863+
...
864+
TypeError: scalar matrix must be square if the value cannot be determined to be zero
865+
sage: matrix(1, 2, MyAlgebraicNumber(3))
866+
Traceback (most recent call last):
867+
...
868+
TypeError: scalar matrix must be square if the value cannot be determined to be zero
851869
"""
852870
self.finalize()
853871
return self
@@ -925,12 +943,18 @@ cdef class MatrixArgs:
925943
raise AssertionError(f"nrows={self.nrows} ncols={self.ncols} base={self.base} type={self.typ}")
926944

927945
# Non-zero scalar matrices must be square
946+
# also ensure type is MA_ENTRIES_ZERO for scalar zero matrices
928947
if self.typ == MA_ENTRIES_SCALAR:
929-
if self.entries:
948+
try:
949+
if not self.entries:
950+
self.typ = MA_ENTRIES_ZERO
951+
except Exception:
952+
# "not self.entries" has failed, self.entries cannot be determined to be zero
930953
if self.nrows != self.ncols:
931-
raise TypeError("nonzero scalar matrix must be square")
932-
else:
933-
self.typ = MA_ENTRIES_ZERO
954+
raise TypeError("scalar matrix must be square if the value cannot be determined to be zero")
955+
if self.typ == MA_ENTRIES_SCALAR and self.nrows != self.ncols:
956+
# self.typ is still SCALAR -> "not self.entries" has successfully evaluated, to False
957+
raise TypeError("nonzero scalar matrix must be square")
934958

935959
if self.sparse == -1:
936960
self.sparse = (self.typ & MA_FLAG_SPARSE) != 0

src/sage/rings/padics/relaxed_template.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ cdef class RelaxedElement(pAdicGenericElement):
811811

812812
def __bool__(self):
813813
r"""
814-
Return ``True`` if this element is indistinguishable from zero.
814+
Return ``True`` if this element is distinguishable from zero.
815815
816816
TESTS::
817817

0 commit comments

Comments
 (0)