Opened 7 months ago

Closed 5 months ago

#31752 closed defect (fixed)

error when creating random polynomials

Reported by: gh-mwageringel Owned by:
Priority: major Milestone: sage-9.4
Component: commutative algebra Keywords:
Cc: Merged in:
Authors: Markus Wageringel Reviewers: Samuel Lelièvre
Report Upstream: N/A Work issues:
Branch: 7dfe75c (Commits, GitHub, GitLab) Commit: 7dfe75cf48bf41b432e189774a7a5144fb735907
Dependencies: Stopgaps:

Status badges


The choose_degree option fails frequently.

sage: R.<x,y,z> = QQ[]
sage: set_random_seed(0)
sage: f = R.random_element(5, choose_degree=True)  # ok
sage: set_random_seed(1)
sage: f = R.random_element(5, choose_degree=True)  # fails
TypeError                                 Traceback (most recent call last)
<ipython-input-12-241bf39f07c2> in <module>
----> 1 f = R.random_element(Integer(5), choose_degree=True)

/usr/lib/python3.9/site-packages/sage/rings/polynomial/multi_polynomial_ring_base.pyx in sage.rings.polynomial.multi_polynomial_ring_base.MPolynomialRing_base.random_element (build/cythonized/sage/rings/polynomial/multi_polynomial_ring_base.c:14338)()
   1062             else:
   1063                 while terms:
-> 1064                     m = self._random_monomial_upto_degree_class(n, degree)
   1065                     if not m in M:
   1066                         M.add(m)

/usr/lib/python3.9/site-packages/sage/rings/polynomial/multi_polynomial_ring_base.pyx in sage.rings.polynomial.multi_polynomial_ring_base.MPolynomialRing_base._random_monomial_upto_degree_class (build/cythonized/sage/rings/polynomial/multi_polynomial_ring_base.c:12939)()
    875         #Select random monomial of degree d
--> 876         random_index = ZZ.random_element(0, total-1)
    877         #Generate the corresponding monomial
    878         return self._to_monomial(random_index, n, d)

/usr/lib/python3.9/site-packages/sage/rings/integer_ring.pyx in sage.rings.integer_ring.IntegerRing_class.random_element (build/cythonized/sage/rings/integer_ring.c:6514)()
    715             raise TypeError("x must be > 0")
    716         if x is not None and y is not None and x >= y:
--> 717             raise TypeError("x must be < y")
    718         self._randomize_mpz(z.value, x, y, distribution)
    719         return z

TypeError: x must be < y

Change History (5)

comment:1 Changed 7 months ago by gh-mwageringel

Tested with Sage 9.2 and 9.3rc4.

comment:2 Changed 7 months ago by gh-mwageringel

  • Authors set to Markus Wageringel
  • Branch set to u/gh-mwageringel/31752
  • Commit set to 7dfe75cf48bf41b432e189774a7a5144fb735907
  • Status changed from new to needs_review

This is a simple off-by-one error. I have not added a doctest, as it will automatically be covered by #29979.

New commits:

7dfe75c31752: fix off-by-1 error creating random polynomials

comment:3 Changed 7 months ago by slelievre

  • Reviewers set to Samuel Lelièvre
  • Status changed from needs_review to positive_review

I was coming to the same conclusion. The confusion was likely because

  • ZZ.random_element(a, b) includes a but excludes b,
  • similar to range(a, b) which works that way too,
  • but contrary to randint(a, b) which includes both a and b.

comment:4 Changed 7 months ago by gh-mwageringel


comment:5 Changed 5 months ago by vbraun

  • Branch changed from u/gh-mwageringel/31752 to 7dfe75cf48bf41b432e189774a7a5144fb735907
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.