#15919: unrank via R[i] conflicts with notation for constructing polynomial
rings
in CartesianProduct
-------------------------------------+-------------------------------------
Reporter: darij | Owner:
Type: defect | Status: positive_review
Priority: major | Milestone: sage-6.2
Component: algebra | Resolution:
Keywords: notation, | Merged in:
algebra, polynomial | Reviewers:
Authors: | Work issues:
Report Upstream: N/A | Commit:
Branch: | 79d469861431ac8d6be5ad4edeace1c6ac8a62c8
u/tscrim/ticket/15919 | Stopgaps:
Dependencies: |
-------------------------------------+-------------------------------------
Changes (by tscrim):
* status: needs_review => positive_review
Old description:
> {{{
> FF = IntegerModRing(29)
> tester = FF._tester()
> list(tester.some_elements(CartesianProduct(FF, FF, FF)))
> }}}
> This should give a list of 3-arrays of elements of Z/29. Instead I get:
> {{{
> sage: FF = IntegerModRing(29)
> sage: tester = FF._tester()
> sage: list(tester.some_elements(CartesianProduct(FF, FF, FF)))
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call
> last)
> <ipython-input-3-5d7e71a13340> in <module>()
> ----> 1 list(tester.some_elements(CartesianProduct(FF, FF, FF)))
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/misc/sage_unittest.pyc in some_elements(self, S)
> 498 if n > self._max_runs:
> 499 from random import sample
> --> 500 S = sample(S, self._max_runs)
> 501 except (TypeError, AttributeError):
> 502 # We already can't tell what the length of n is, so
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python/random.pyc in
> sample(self, population, k)
> 342 j = _int(random() * n)
> 343 selected_add(j)
> --> 344 result[i] = population[j]
> 345 except (TypeError, KeyError): # handle (at least)
> sets
> 346 if isinstance(population, list):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/combinat/combinat.pyc in __getitem__(self, i)
> 1220 ValueError: the value must be between 0 and 2
> inclusive
> 1221 """
> -> 1222 return self.unrank(i)
> 1223
> 1224 def __str__(self):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/combinat/cartesian_product.pyc in unrank(self, x)
> 258 raise IndexError("x larger than the size of the
> Cartesian Product")
> 259 positions.reverse()
> --> 260 return [L[i] for L,i in zip(self.iters, positions)]
> 261
> 262 def random_element(self):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent.so in
> sage.structure.parent.Parent.__getitem__
> (sage/structure/parent.c:11060)()
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/categories/rings.pyc in __getitem__(self, arg)
> 859
> 860 from
> sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
> --> 861 return PolynomialRing(self, elts)
> 862
> 863 class ElementMethods:
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
> PolynomialRing(base_ring, arg1, arg2, sparse, order, names, name,
> var_array, implementation)
> 467 raise TypeError("if second arguments is a string
> with no commas, then there must be no other non-optional arguments")
> 468 name = arg1
> --> 469 R = _single_variate(base_ring, name, sparse,
> implementation)
> 470 else:
> 471 # 2-4. PolynomialRing(base_ring, names,
> order='degrevlex'):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
> _single_variate(base_ring, name, sparse, implementation)
> 518 def _single_variate(base_ring, name, sparse, implementation):
> 519 import sage.rings.polynomial.polynomial_ring as m
> --> 520 name = normalize_names(1, name)
> 521 key = (base_ring, name, sparse, implementation if not sparse
> else None)
> 522 R = _get_from_cache(key)
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent_gens.so in
> sage.structure.parent_gens.normalize_names
> (sage/structure/parent_gens.c:2759)()
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent_gens.so in
> sage.structure.parent_gens._certify_names
> (sage/structure/parent_gens.c:2322)()
>
> ValueError: first letter of variable name must be a letter
> }}}
> And 29 is the smallest number where this seems to throw this error; also,
> it doesn't fail if I only take the cartesian product of two copies of the
> field.
>
> I get a similar error if I do `CartesianProduct(FF, FF).unrank(3)`, and
> this has been around for a while (not just since beta4):
> {{{
> sage: FF = IntegerModRing(3)
> sage: CartesianProduct(FF, FF).unrank(3)
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call
> last)
> <ipython-input-15-98d0a571cc61> in <module>()
> ----> 1 CartesianProduct(FF, FF).unrank(Integer(3))
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/combinat/cartesian_product.pyc in unrank(self, x)
> 258 raise IndexError("x larger than the size of the
> Cartesian Product")
> 259 positions.reverse()
> --> 260 return [L[i] for L,i in zip(self.iters, positions)]
> 261
> 262 def random_element(self):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent.so in
> sage.structure.parent.Parent.__getitem__
> (sage/structure/parent.c:11060)()
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/categories/rings.pyc in __getitem__(self, arg)
> 859
> 860 from
> sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
> --> 861 return PolynomialRing(self, elts)
> 862
> 863 class ElementMethods:
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
> PolynomialRing(base_ring, arg1, arg2, sparse, order, names, name,
> var_array, implementation)
> 467 raise TypeError("if second arguments is a string
> with no commas, then there must be no other non-optional arguments")
> 468 name = arg1
> --> 469 R = _single_variate(base_ring, name, sparse,
> implementation)
> 470 else:
> 471 # 2-4. PolynomialRing(base_ring, names,
> order='degrevlex'):
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
> _single_variate(base_ring, name, sparse, implementation)
> 518 def _single_variate(base_ring, name, sparse, implementation):
> 519 import sage.rings.polynomial.polynomial_ring as m
> --> 520 name = normalize_names(1, name)
> 521 key = (base_ring, name, sparse, implementation if not sparse
> else None)
> 522 R = _get_from_cache(key)
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent_gens.so in
> sage.structure.parent_gens.normalize_names
> (sage/structure/parent_gens.c:2759)()
>
> /home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
> packages/sage/structure/parent_gens.so in
> sage.structure.parent_gens._certify_names
> (sage/structure/parent_gens.c:2322)()
>
> ValueError: first letter of variable name must be a letter
> }}}
> So it seems that both bugs are due to the fancy `R[x]` syntax for
> polynomial rings conflicting with the `R[i]` syntax for the i-th element
> of an enumerated set `R`, and #8389 didn't cause the error, but at most
> exposed it better.
New description:
{{{
FF = IntegerModRing(29)
tester = FF._tester()
list(tester.some_elements(CartesianProduct(FF, FF, FF)))
}}}
This should give a list of 3-arrays of elements of Z/29. Instead I get:
{{{
sage: FF = IntegerModRing(29)
sage: tester = FF._tester()
sage: list(tester.some_elements(CartesianProduct(FF, FF, FF)))
---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
<ipython-input-3-5d7e71a13340> in <module>()
----> 1 list(tester.some_elements(CartesianProduct(FF, FF, FF)))
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/misc/sage_unittest.pyc in some_elements(self, S)
498 if n > self._max_runs:
499 from random import sample
--> 500 S = sample(S, self._max_runs)
501 except (TypeError, AttributeError):
502 # We already can't tell what the length of n is, so
/home/darij/gitsage/sage-5.13.beta1/local/lib/python/random.pyc in
sample(self, population, k)
342 j = _int(random() * n)
343 selected_add(j)
--> 344 result[i] = population[j]
345 except (TypeError, KeyError): # handle (at least)
sets
346 if isinstance(population, list):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/combinat/combinat.pyc in __getitem__(self, i)
1220 ValueError: the value must be between 0 and 2
inclusive
1221 """
-> 1222 return self.unrank(i)
1223
1224 def __str__(self):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/combinat/cartesian_product.pyc in unrank(self, x)
258 raise IndexError("x larger than the size of the
Cartesian Product")
259 positions.reverse()
--> 260 return [L[i] for L,i in zip(self.iters, positions)]
261
262 def random_element(self):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent.so in
sage.structure.parent.Parent.__getitem__ (sage/structure/parent.c:11060)()
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/categories/rings.pyc in __getitem__(self, arg)
859
860 from sage.rings.polynomial.polynomial_ring_constructor
import PolynomialRing
--> 861 return PolynomialRing(self, elts)
862
863 class ElementMethods:
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
PolynomialRing(base_ring, arg1, arg2, sparse, order, names, name,
var_array, implementation)
467 raise TypeError("if second arguments is a string
with no commas, then there must be no other non-optional arguments")
468 name = arg1
--> 469 R = _single_variate(base_ring, name, sparse,
implementation)
470 else:
471 # 2-4. PolynomialRing(base_ring, names,
order='degrevlex'):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
_single_variate(base_ring, name, sparse, implementation)
518 def _single_variate(base_ring, name, sparse, implementation):
519 import sage.rings.polynomial.polynomial_ring as m
--> 520 name = normalize_names(1, name)
521 key = (base_ring, name, sparse, implementation if not sparse
else None)
522 R = _get_from_cache(key)
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent_gens.so in
sage.structure.parent_gens.normalize_names
(sage/structure/parent_gens.c:2759)()
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent_gens.so in
sage.structure.parent_gens._certify_names
(sage/structure/parent_gens.c:2322)()
ValueError: first letter of variable name must be a letter
}}}
And 29 is the smallest number where this seems to throw this error; also,
it doesn't fail if I only take the cartesian product of two copies of the
field.
I get a similar error if I do `CartesianProduct(FF, FF).unrank(3)`, and
this has been around for a while (not just since beta4):
{{{
sage: FF = IntegerModRing(3)
sage: CartesianProduct(FF, FF).unrank(3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
<ipython-input-15-98d0a571cc61> in <module>()
----> 1 CartesianProduct(FF, FF).unrank(Integer(3))
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/combinat/cartesian_product.pyc in unrank(self, x)
258 raise IndexError("x larger than the size of the
Cartesian Product")
259 positions.reverse()
--> 260 return [L[i] for L,i in zip(self.iters, positions)]
261
262 def random_element(self):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent.so in
sage.structure.parent.Parent.__getitem__ (sage/structure/parent.c:11060)()
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/categories/rings.pyc in __getitem__(self, arg)
859
860 from sage.rings.polynomial.polynomial_ring_constructor
import PolynomialRing
--> 861 return PolynomialRing(self, elts)
862
863 class ElementMethods:
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
PolynomialRing(base_ring, arg1, arg2, sparse, order, names, name,
var_array, implementation)
467 raise TypeError("if second arguments is a string
with no commas, then there must be no other non-optional arguments")
468 name = arg1
--> 469 R = _single_variate(base_ring, name, sparse,
implementation)
470 else:
471 # 2-4. PolynomialRing(base_ring, names,
order='degrevlex'):
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in
_single_variate(base_ring, name, sparse, implementation)
518 def _single_variate(base_ring, name, sparse, implementation):
519 import sage.rings.polynomial.polynomial_ring as m
--> 520 name = normalize_names(1, name)
521 key = (base_ring, name, sparse, implementation if not sparse
else None)
522 R = _get_from_cache(key)
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent_gens.so in
sage.structure.parent_gens.normalize_names
(sage/structure/parent_gens.c:2759)()
/home/darij/gitsage/sage-5.13.beta1/local/lib/python2.7/site-
packages/sage/structure/parent_gens.so in
sage.structure.parent_gens._certify_names
(sage/structure/parent_gens.c:2322)()
ValueError: first letter of variable name must be a letter
}}}
So it seems that both bugs are due to the fancy `R[x]` syntax for
polynomial rings conflicting with the `R[i]` syntax for the i-th element
of an enumerated set `R`, and #8389 didn't cause the error, but at most
exposed it better.
This reworks the `unrank` function in `sage.combinat.ranker` and uses it
for the Cartesian product (this doesn't work for `ZZ`, see #16239). In
followup tickets, we also want to be more systematic about using unrank in
`TestSuite` and do better construction of `some_elements()` #16244.
--
Comment:
Done and done.
--
Ticket URL: <http://trac.sagemath.org/ticket/15919#comment:20>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.