Repository: sagemath/sage

Issue:
PolynomialSequence_gf2.coefficients_monomials is broken
### Steps To Reproduce

Run

```python
for F in (GF(3), GF(2)):
    R = PolynomialRing(F, ['a', 'b', 'c'])
    a, b, c = R.gens()
    f = a + b + c
    M, monos = Sequence([f]).coefficients_monomials()
    print(F)
    print(M)
    print(monos)
```

### Expected Behavior

Print

```
[1 1 1]
(a, b, c)
```

for both `GF(3)` and `GF(2)`

### Actual Behavior

`GF(3)` works correctly
`GF(2)` result in this error:

```
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /usr/lib/python3.11/site-packages/sage/rings/polynomial/multi_polynomial_sequence.py:1746, in PolynomialSequence_gf2.coefficients_monomials(self, order, sparse)
   1745 try:
-> 1746     A[x, y[m]] = one
   1747 except KeyError:

KeyError: (1, a)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[209], line 5
      3 a, b, c = R.gens()
      4 f = a + b + c
----> 5 M, monos = Sequence([f]).coefficients_monomials()
      6 print(F)
      7 print(M)

File /usr/lib/python3.11/site-packages/sage/rings/polynomial/multi_polynomial_sequence.py:1748, in PolynomialSequence_gf2.coefficients_monomials(self, order, sparse)
   1746             A[x, y[m]] = one
   1747         except KeyError:
-> 1748             raise ValueError("order argument does not contain all monomials")
   1749 return A, vector(v)

ValueError: order argument does not contain all monomials
```

### Additional Information

I think it comes from [here](https://github.com/sagemath/sage/blob/15fcf52fbd6a72f0d0f226c02fe1a74b21bbfaf5/src/sage/rings/polynomial/multi_polynomial_sequence.py#L1745), iterating directly on a `GF(2)` polynomial is still a pair of `(coefficient, monomial)` instead of just `monomial`.

### Environment

```markdown
- **OS**: Arch Linux on WSL2
- **Sage Version**: 10.3
```


### Checklist

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.
- [X] I have read the documentation and troubleshoot guide


Discussion:
> I think it comes from [here](https://github.com/sagemath/sage/blob/15fcf52fbd6a72f0d0f226c02fe1a74b21bbfaf5/src/sage/rings/polynomial/multi_polynomial_sequence.py#L1745), iterating directly on a GF(2) polynomial is still a pair of (coefficient, monomial) instead of just monomial.

Yes, that is exactly the reason; unfortunately, this behavior differs between a boolean polynomial from polybori and a multipolynomial from Singular, as the example from the code shows:
```
B.<x,y,z> = BooleanPolynomialRing()
F = Sequence([x*y + y + 1, z + 1])
for x, poly in enumerate(F):
    for m in poly:
        print("m:", m)
```
returns
```
m: x*y
m: y
m: 1
m: z
m: 1
```
One could fix this by checking whether `m` is a tuple (in which case its second entry needs to be chosen), but this might not be the most elegant approach.

Resolving patch:
diff --git a/src/sage/rings/polynomial/multi_polynomial_sequence.py b/src/sage/rings/polynomial/multi_polynomial_sequence.py
--- a/src/sage/rings/polynomial/multi_polynomial_sequence.py
+++ b/src/sage/rings/polynomial/multi_polynomial_sequence.py
@@ -1725,9 +1725,24 @@ def coefficients_monomials(self, order=None, sparse=True):
             (x*y, y, z, 1)
             sage: A*v
             (x*y + y + 1, z + 1)
+
+        TESTS:
+
+        Check that :issue:`37837` has been fixed::
+
+            sage: R.<a,b,c> = PolynomialRing(GF(2), ['a', 'b', 'c'])
+            sage: A, v = Sequence([a+b+c]).coefficients_monomials()
+            sage: A
+            [1 1 1]
+            sage: v
+            (a, b, c)
+            sage: A*v
+            (a + b + c)
         """
         from sage.modules.free_module_element import vector
         from sage.matrix.constructor import Matrix
+        from sage.rings.polynomial.multi_polynomial_ring_base import \
+            BooleanPolynomialRing_base
 
         if order is None:
             v = sorted(self.monomials(), reverse=True)
@@ -1737,16 +1752,27 @@ def coefficients_monomials(self, order=None, sparse=True):
             else:
                 raise ValueError("order argument can only accept list or tuple")
 
-        R = self.ring().base_ring()
-        one = R.one()
+        R = self.ring()
+        K = R.base_ring()
         y = dict(zip(v, range(len(v))))  # construct dictionary for fast lookups
-        A = Matrix(R, len(self), len(v), sparse=sparse)
-        for x, poly in enumerate(self):
-            for m in poly:
-                try:
-                    A[x, y[m]] = one
-                except KeyError:
-                    raise ValueError("order argument does not contain all monomials")
+        A = Matrix(K, len(self), len(v), sparse=sparse)
+
+        if isinstance(R, BooleanPolynomialRing_base):
+            one = K.one()
+            for x, poly in enumerate(self):
+                for m in poly:
+                    try:
+                        A[x, y[m]] = one
+                    except KeyError:
+                        raise ValueError("order argument does not contain all monomials")
+        else:
+            for x, poly in enumerate(self):
+                for c, m in poly:
+                    try:
+                        A[x, y[m]] = c
+                    except KeyError:
+                        raise ValueError("order argument does not contain all monomials")
+
         return A, vector(v)
 
 


Test patch: