Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix diagonal matrix construction from base ring elements that have _matrix_ methods #38235

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/sage/matrix/args.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,21 @@ cdef class MatrixArgs:
Traceback (most recent call last):
...
NameError: name 'a' is not defined

Check that :issue:`38221` is fixed::

sage: # needs sage.groups
sage: G = CyclicPermutationGroup(7)
sage: R = GF(2)
sage: A = G.algebra(R)
sage: matrix(A, 3, 3, A.zero())
[0 0 0]
[0 0 0]
[0 0 0]
sage: matrix(A, 3, 3, A.one())
[() 0 0]
[ 0 () 0]
[ 0 0 ()]
"""
# Check basic Python types. This is very fast, so it doesn't
# hurt to do these first.
Expand All @@ -1524,6 +1539,8 @@ cdef class MatrixArgs:
cdef bint is_elt = isinstance(self.entries, Element)
if is_elt and isinstance(self.entries, Matrix):
return MA_ENTRIES_MATRIX
if is_elt and self.base is not None and self.entries.parent() == self.base:
return MA_ENTRIES_SCALAR
t = type(self.entries)
try:
f = t._matrix_
Expand Down
15 changes: 13 additions & 2 deletions src/sage/matrix/matrix_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,9 +2170,20 @@ def zero_matrix(self):
False
sage: MM.zero().is_mutable()
False

Check that :issue:`38221` is fixed::

sage: # needs sage.groups
sage: G = CyclicPermutationGroup(7)
sage: R = GF(2)
sage: A = G.algebra(R)
sage: S = MatrixSpace(A, 3, 3)
sage: S.zero_matrix()
[0 0 0]
[0 0 0]
[0 0 0]
"""
zero = self.base_ring().zero()
res = self.element_class(self, zero, False, False)
res = self.element_class(self, None, False, False)
res.set_immutable()
return res

Expand Down
14 changes: 13 additions & 1 deletion src/sage/matrix/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,11 +1000,23 @@ def zero_matrix(ring, nrows=None, ncols=None, sparse=False):
[0 0 0 0 0]
[0 0 0 0 0]

TESTS:

Check that :issue:`38221` is fixed::

sage: # needs sage.groups
sage: G = CyclicPermutationGroup(7)
sage: R = GF(2)
sage: A = G.algebra(R)
sage: zero_matrix(A, 3, 3)
tscrim marked this conversation as resolved.
Show resolved Hide resolved
[0 0 0]
[0 0 0]
[0 0 0]
"""
if isinstance(ring, (Integer, int)):
nrows, ncols = (ring, nrows)
ring = ZZ
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse)(0)
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse).matrix(None)


@matrix_method
Expand Down
Loading