Skip to content

Commit c2372a9

Browse files
author
Release Manager
committed
gh-38235: Fix diagonal matrix construction from base ring elements that have `_matrix_` methods <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes #12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes #12345". --> The matrix constructors are designed to accept a scalar as the `entries` and then construct a diagonal matrix; special case: constructing the zero matrix from 0. However, when the scalar is a base ring element that happens to have a `_matrix_` method, that is tried first, leading to the errors reported in #38221. Here we change it so that if the base ring (or matrix space) has been specified already and a scalar from that base ring is passed, that takes precedence over using a `_matrix_` method. (Coercion is not attempted.) For the special case of `zero_matrix` and `MatrixSpace.zero_matrix`, we now use `entries=None`, which is the fastest and most robust path in `MatrixArgs.entries_type` to designate a zero matrix. Fixes #38221 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #38235 Reported by: Matthias Köppe Reviewer(s): Matthias Köppe, Travis Scrimshaw
2 parents 2f9da96 + 6ed0045 commit c2372a9

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/sage/matrix/args.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,21 @@ cdef class MatrixArgs:
15001500
Traceback (most recent call last):
15011501
...
15021502
NameError: name 'a' is not defined
1503+
1504+
Check that :issue:`38221` is fixed::
1505+
1506+
sage: # needs sage.groups
1507+
sage: G = CyclicPermutationGroup(7)
1508+
sage: R = GF(2)
1509+
sage: A = G.algebra(R)
1510+
sage: matrix(A, 3, 3, A.zero())
1511+
[0 0 0]
1512+
[0 0 0]
1513+
[0 0 0]
1514+
sage: matrix(A, 3, 3, A.one())
1515+
[() 0 0]
1516+
[ 0 () 0]
1517+
[ 0 0 ()]
15031518
"""
15041519
# Check basic Python types. This is very fast, so it doesn't
15051520
# hurt to do these first.
@@ -1524,6 +1539,8 @@ cdef class MatrixArgs:
15241539
cdef bint is_elt = isinstance(self.entries, Element)
15251540
if is_elt and isinstance(self.entries, Matrix):
15261541
return MA_ENTRIES_MATRIX
1542+
if is_elt and self.base is not None and self.entries.parent() == self.base:
1543+
return MA_ENTRIES_SCALAR
15271544
t = type(self.entries)
15281545
try:
15291546
f = t._matrix_

src/sage/matrix/matrix_space.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,9 +2170,20 @@ def zero_matrix(self):
21702170
False
21712171
sage: MM.zero().is_mutable()
21722172
False
2173+
2174+
Check that :issue:`38221` is fixed::
2175+
2176+
sage: # needs sage.groups
2177+
sage: G = CyclicPermutationGroup(7)
2178+
sage: R = GF(2)
2179+
sage: A = G.algebra(R)
2180+
sage: S = MatrixSpace(A, 3, 3)
2181+
sage: S.zero_matrix()
2182+
[0 0 0]
2183+
[0 0 0]
2184+
[0 0 0]
21732185
"""
2174-
zero = self.base_ring().zero()
2175-
res = self.element_class(self, zero, False, False)
2186+
res = self.element_class(self, None, False, False)
21762187
res.set_immutable()
21772188
return res
21782189

src/sage/matrix/special.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,23 @@ def zero_matrix(ring, nrows=None, ncols=None, sparse=False):
10001000
[0 0 0 0 0]
10011001
[0 0 0 0 0]
10021002
1003+
TESTS:
1004+
1005+
Check that :issue:`38221` is fixed::
1006+
1007+
sage: # needs sage.groups
1008+
sage: G = CyclicPermutationGroup(7)
1009+
sage: R = GF(2)
1010+
sage: A = G.algebra(R)
1011+
sage: zero_matrix(A, 3, 3)
1012+
[0 0 0]
1013+
[0 0 0]
1014+
[0 0 0]
10031015
"""
10041016
if isinstance(ring, (Integer, int)):
10051017
nrows, ncols = (ring, nrows)
10061018
ring = ZZ
1007-
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse)(0)
1019+
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse).matrix(None)
10081020

10091021

10101022
@matrix_method

0 commit comments

Comments
 (0)