Skip to content

Commit

Permalink
gh-38453: Enhance augment method in Matrix_gf2e
Browse files Browse the repository at this point in the history
    
This pull request modified `augment()` method defined in
`matrix_gf2e_dense` to allow vectors to be augmented.
See issue: #36761

Earlier it accepted only matrix of type `matrix_gf2e_dense`.
I removed that dependency.
Added check for vector.
If vector then try to change the base ring of vector to self.base_ring
and create matrix of type `matrix_gf2e_dense` from that vector
Rest of the flow remains same.

The parent class Matrix1 defined generic augment() method which allowed
vectors to be augmented but `matrix_gf2e_dense` provides new definition
of augment() and do not provide support for vectors.
For consistency it would be good to have vector augmentation in this
class too.

Fixes #36761



### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] 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: #38453
Reported by: Animesh Shree
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Sep 27, 2024
2 parents 6a039da + 605feb4 commit c66fe84
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/sage/matrix/matrix_gf2e_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ cdef class Matrix_gf2e_dense(matrix_dense.Matrix_dense):
"""
mzed_col_swap(self._entries, col1, col2)

def augment(self, Matrix_gf2e_dense right):
def augment(self, right):
"""
Augments ``self`` with ``right``.
Expand Down Expand Up @@ -1167,21 +1167,50 @@ cdef class Matrix_gf2e_dense(matrix_dense.Matrix_dense):
sage: N = Matrix(K, 0, 1, 0)
sage: M.augment(N)
[]
sage: A = matrix(K, 3, range(12))
sage: B = vector(QQ, [2,5/7,1.2]) # see issue: 38448
sage: A.augment(B).ncols()
5
sage: B = vector([])
sage: A.augment(B) == A
True
"""
cdef Matrix_gf2e_dense _right
cdef Matrix_gf2e_dense A

if self._nrows != right._nrows:
if not isinstance(right, Matrix_gf2e_dense):
# See issue: #36761 - Allow Vectors to be augmented
if hasattr(right, '_vector_'):
rsize = len(right)
if rsize==0:
return self.__copy__()
if self._nrows != rsize:
raise TypeError("Both numbers of rows must match.")
if self.base_ring() is not right.base_ring():
right = right.change_ring(self.base_ring())
from sage.matrix.matrix_space import MatrixSpace
M = MatrixSpace(self.base_ring(), nrows=rsize, ncols=1)
_right = <Matrix_gf2e_dense>(M(right))
else:
raise TypeError("a matrix must be augmented with another matrix, "
"or a vector")
else:
_right = <Matrix_gf2e_dense>right

if self._nrows != _right._nrows:
raise TypeError("Both numbers of rows must match.")

if self._ncols == 0:
return right.__copy__()
if right._ncols == 0:
return _right.__copy__()
if _right._ncols == 0:
return self.__copy__()

A = self.new_matrix(ncols = self._ncols + right._ncols)
A = self.new_matrix(ncols = self._ncols + _right._ncols)
if self._nrows == 0:
return A
A._entries = mzed_concat(A._entries, self._entries, right._entries)
A._entries = mzed_concat(A._entries, self._entries, _right._entries)
return A

cdef _stack_impl(self, bottom):
Expand Down

0 comments on commit c66fe84

Please sign in to comment.