@@ -848,6 +848,24 @@ cdef class MatrixArgs:
848
848
Traceback (most recent call last):
849
849
...
850
850
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
851
869
"""
852
870
self .finalize()
853
871
return self
@@ -925,12 +943,18 @@ cdef class MatrixArgs:
925
943
raise AssertionError (f" nrows={self.nrows} ncols={self.ncols} base={self.base} type={self.typ}" )
926
944
927
945
# Non-zero scalar matrices must be square
946
+ # also ensure type is MA_ENTRIES_ZERO for scalar zero matrices
928
947
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
930
953
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" )
934
958
935
959
if self .sparse == - 1 :
936
960
self .sparse = (self .typ & MA_FLAG_SPARSE) != 0
0 commit comments