Opened 12 years ago

Closed 12 years ago

#1577 closed enhancement (fixed)

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

Reported by: ncalexan Owned by: malb
Priority: minor Milestone: sage-2.10.1
Component: commutative algebra Keywords: multi polynomial rings coefficients monomials
Cc: jbmohler Merged in:
Authors: Reviewers:
Report Upstream: Work issues:
Branch: Commit:
Dependencies: Stopgaps:

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.

Attachments (2)

trac_1577.patch (3.3 KB) - added by malb 12 years ago.
trac_1577_comment_4.patch (2.8 KB) - added by malb 12 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 12 years ago by rlm

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

comment:2 Changed 12 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'>

Changed 12 years ago by malb

comment:3 Changed 12 years ago by malb

  • Status changed from new to assigned
  • Summary changed from .coefficients() and .monomials() differ in order in multivariate polynomial rings to [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings

comment:4 Changed 12 years ago by jbmohler

  • Summary changed from [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings to [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 12 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 12 years ago by malb

  • Cc jbmohler added

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()"

Changed 12 years ago by malb

comment:7 Changed 12 years ago by malb

Joel's suggested change is in trac_1577_comment_4.patch.

comment:8 Changed 12 years ago by malb

  • Summary changed from [with patch, with mostly positive review] .coefficients() and .monomials() differ in order in multivariate polynomial rings to [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 12 years ago by ncalexan

  • Summary changed from [with patch, needs review] .coefficients() and .monomials() differ in order in multivariate polynomial rings to [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 12 years ago by mabshoff

  • Resolution set to fixed
  • Status changed from assigned to closed

Merged both patches in Sage 2.10.1.alpha1

Note: See TracTickets for help on using tickets.