Skip to content

Commit 9b131cd

Browse files
author
Release Manager
committed
gh-37174: fixed bug in `is_linearly_dependent()`, returns true for linearly dependent univariate polynomial. `is_linearly_dependent()` returned wrong results for the univariate polynomial ring because the `coefficients()` method of univariate polynomials returns coefficients in the opposite order of `monomials()`: ``` sage: R.<q> = QQ[] sage: p = q^4 + 3*q + 5 sage: p.monomials() [q^4, q, 1] sage: p.coefficients() [5, 3, 1] ``` We fix this by making the `coefficient_matrix()` method to check for univariate polynomial ring and make the order consistent. Fixes: #37075 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [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. - [x] I have updated the documentation accordingly. URL: #37174 Reported by: Aman Moon Reviewer(s): Travis Scrimshaw
2 parents 97d2ba5 + 6fbb720 commit 9b131cd

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,20 +5941,22 @@ cdef class Polynomial(CommutativePolynomial):
59415941
return a*self
59425942

59435943
def coefficients(self, sparse=True):
5944-
"""
5945-
Return the coefficients of the monomials appearing in self.
5944+
r"""
5945+
Return the coefficients of the monomials appearing in ``self``.
5946+
59465947
If ``sparse=True`` (the default), it returns only the non-zero coefficients.
59475948
Otherwise, it returns the same value as ``self.list()``.
59485949
(In this case, it may be slightly faster to invoke ``self.list()`` directly.)
5950+
In either case, the coefficients are ordered by increasing degree.
59495951
59505952
EXAMPLES::
59515953
59525954
sage: _.<x> = PolynomialRing(ZZ)
5953-
sage: f = x^4 + 2*x^2 + 1
5955+
sage: f = 3*x^4 + 2*x^2 + 1
59545956
sage: f.coefficients()
5955-
[1, 2, 1]
5957+
[1, 2, 3]
59565958
sage: f.coefficients(sparse=False)
5957-
[1, 0, 2, 0, 1]
5959+
[1, 0, 2, 0, 3]
59585960
"""
59595961
zero = self._parent.base_ring().zero()
59605962
if sparse:

src/sage/rings/polynomial/toy_variety.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def coefficient_matrix(polys):
120120
M = matrix(R, len(polys), len(mons))
121121
for i in range(len(polys)):
122122
imons = polys[i].monomials()
123-
icoeffs = polys[i].coefficients()
123+
if polys[0].parent().ngens() == 1:
124+
icoeffs = polys[i].coefficients()[::-1]
125+
else:
126+
icoeffs = polys[i].coefficients()
124127
for j in range(len(imons)):
125128
M[i, mons.index(imons[j])] = icoeffs[j]
126129
return M
@@ -166,6 +169,13 @@ def is_linearly_dependent(polys) -> bool:
166169
False
167170
sage: is_linearly_dependent([])
168171
False
172+
sage: R.<x> = PolynomialRing(QQ)
173+
sage: B = [x^147 + x^99,
174+
....: 2*x^123 + x^75,
175+
....: x^147 + 2*x^123 + 2*x^75,
176+
....: 2*x^147 + x^99 + x^75]
177+
sage: is_linearly_dependent(B)
178+
True
169179
"""
170180
if not polys:
171181
return False

0 commit comments

Comments
 (0)