Opened 15 years ago

Closed 15 years ago

# [with patch, with positive review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

Reported by: Owned by: ncalexan Martin Albrecht minor sage-2.10.1 commutative algebra multi polynomial rings coefficients monomials jbmohler N/A

### Description

A small annoyance -- the ordering on the lists below is different:

```sage: R.<fx,fy,gx,gy> = ZZ[]
sage: F = ((fx*gy - fy*gx)^3)
sage: F
-1*fy^3*gx^3 + 3*fx*fy^2*gx^2*gy - 3*fx^2*fy*gx*gy^2 + fx^3*gy^3
sage: F.monomials()
[fx^2*fy*gx*gy^2, fy^3*gx^3, fx*fy^2*gx^2*gy, fx^3*gy^3]
sage: F.coefficients()
[-3, -1, 3, 1]
```

`F.coefficients?` says "The order the coefficients appear in depends on the ordering used on self's parent." `F.monomials?` says "Returns list of all monomials which occure in this multivariate polynomial."

I think the latter should be changed.

### comment:1 Changed 15 years ago by Robert Miller

Looks like monomials and coefficients line up, but it's not in the same order as they print?

### comment:2 Changed 15 years ago by ncalexan

While we're here:

The elements of list() don't have the correct types -- the final line should be a libsingular poly as well:

```sage: R.<x, y> = QQ[]
sage: (x + y).monomials()
[x, y]
sage: type((x + y).monomials()[0])
<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: list(x + y)[0]
(1, x)
sage: type(list(x + y)[0][-1])
<class 'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>
```

### comment:3 Changed 15 years ago by Martin Albrecht

Status: new → assigned .coefficients() and .monomials() differ in order in multivariate polynomial rings → [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

### comment:4 Changed 15 years ago by jbmohler

Summary: [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings → [with patch, with mostly positive review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

I entirely agree with the actual code of this patch. One of the doc-string changes is not representative though -- we are not sorting largest to smallest (whatever that might mean for a monomial).

I think that ZZ mpolys doc-string for monomials should read

```        Return the list of monomials in self. The returned list is
ordered by the term ordering of self.parent().
```

just like for QQ mpolys.

### comment:5 Changed 15 years ago by jbmohler

Oh, perhaps I should add that I doc-tested 'sage/rings' and verified that the patch fixes the original bug as well as the bug mentioned in the comments.

### comment:6 Changed 15 years ago by Martin Albrecht

I am okay with changing the docstring but want to point out that "from largest to smallest" is well defined for a multivariate polynomial in a given ring. It means to sort according to the monomial ordering of the ring (which is a property of that ring) but in _descending_ order. This fact is not clear -- though probably 'natural' -- when writing "The returned list is ordered by the term ordering of self.parent()"

### comment:7 Changed 15 years ago by Martin Albrecht

Joel's suggested change is in `trac_1577_comment_4.patch`.

### comment:8 Changed 15 years ago by Martin Albrecht

Summary: [with patch, with mostly positive review] .coefficients() and .monomials() differ in order in multivariate polynomial rings → [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

jbmohler can you verify this is correct now?

### comment:9 Changed 15 years ago by ncalexan

Summary: [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings → [with patch, with positive review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

I reported this bug, and I approved this patch! Apply.

### comment:10 Changed 15 years ago by Michael Abshoff

Resolution: → fixed assigned → closed

Merged both patches in Sage 2.10.1.alpha1

Note: See TracTickets for help on using tickets.