-
-
Notifications
You must be signed in to change notification settings - Fork 670
Description
Steps To Reproduce
Creating a matrix from a scalar (MA_ENTRIES_SCALAR
flag from args.pyx
), using code such as mat = matrix(ZZ, nrows, ncols, value)
, will unexpectedly fail in some cases. Such a call is supposed to build a diagonal matrix with value
on the diagonal, and requires the matrix to be square (nrows == ncols
) if value
is nonzero. The issue is mainly that we cannot always successfully compare value
to zero...
Consider the following example, inspired from a test in the doc for get_is_zero_unsafe
in matrix0.pyx
:
sage: class MyAlgebraicNumber(sage.rings.qqbar.AlgebraicNumber):
....: def __bool__(self):
....: raise ValueError
....:
sage: mat = matrix(1,1,MyAlgebraicNumber(1))
sage: mat
[1]
sage: mat = matrix(1,2,MyAlgebraicNumber(1))
### FAILS with ValueError ###
The failure is the same if using MyAlgebraicNumber(0)
.
Expected Behavior
The wanted behaviour is that such a call fails when nrows != ncols
and value
is not zero. The current error, for example if one does matrix(ZZ, 1, 2, 1)
is TypeError("nonzero scalar matrix must be square")
. If value
happens to be zero, then the code just creates the zero matrix (even when nrows != ncols
).
Actual Behavior
We get a value error, as below.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [4], line 1
----> 1 mat = matrix(Integer(1),Integer(2),MyAlgebraicNumber(Integer(1)))
File ~/repositories/software/sage/src/sage/matrix/constructor.pyx:643, in sage.matrix.constructor.matrix()
641 """
642 immutable = kwds.pop('immutable', False)
--> 643 M = MatrixArgs(*args, **kwds).matrix()
644 if immutable:
645 M.set_immutable()
File ~/repositories/software/sage/src/sage/matrix/args.pyx:655, in sage.matrix.args.MatrixArgs.matrix()
653 True
654 """
--> 655 self.finalize()
656
657 cdef Matrix M
File ~/repositories/software/sage/src/sage/matrix/args.pyx:929, in sage.matrix.args.MatrixArgs.finalize()
927 if self.typ == MA_ENTRIES_SCALAR:
928 if self.nrows != self.ncols:
--> 929 if self.entries:
930 raise TypeError("nonzero scalar matrix must be square")
931 self.typ = MA_ENTRIES_ZERO
Cell In [1], line 3, in MyAlgebraicNumber.__bool__(self)
2 def __bool__(self):
----> 3 raise ValueError
ValueError:
Additional Information
The culprit is the piece of finalize
in args.pyx
as highlighted in the above error message. I would suggest to correct the code so that it fails if the matrix is rectangular and value
cannot be determined to be zero. So, on the above examples with both and , it would fail with a TypeError
with a message such as "a scalar matrix must be square if it is nonzero or cannot be determined to be zero".
Environment
- **OS**: Ubuntu 22.04
- **Sage Version**: 10.1.beta9
Checklist
- I have searched the existing issues for a bug report that matches the one I want to file, without success.
- I have read the documentation and troubleshoot guide