sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.2k stars 414 forks source link

Lack of comparisons between polydic.ETuple and tuples and consequences #12040

Open fafcb8fb-f679-4d5a-b5d7-a3b49c3076ce opened 12 years ago

fafcb8fb-f679-4d5a-b5d7-a3b49c3076ce commented 12 years ago
sage: P.<t> = PolynomialRing(QQ)
sage: F.<a> = FractionField(P)
sage: Pxy.<x,y> = PolynomialRing(F,2)
sage: f = y^2 - x*(x-1)*(x-2)
sage: E = EllipticCurve(f)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/kohel/<ipython console> in <module>()

/usr/local/sage/local/lib/python2.6/site-packages/sage/schemes/elliptic_curves/constructor.pyc in EllipticCurve(x, y, j)
    235         if f.degree() != 3:
    236             raise ValueError, "Elliptic curves must be defined by a cubic polynomial."
--> 237         if f.degrees() == (3,2):
    238             x, y = f.parent().gens()
    239         elif f.degree() == (2,3):

/usr/local/sage/local/lib/python2.6/site-packages/sage/rings/polynomial/polydict.so in sage.rings.polynomial.polydict.ETuple.__richcmp__ (sage/rings/polynomial/polydict.c:11114)()

TypeError: Argument 'other' has incorrect type (expected sage.rings.polynomial.polydict.ETuple, got tuple)

The problem arises from the type of the return argument for f.degrees():

sage: type(f.degrees())
<type 'sage.rings.polynomial.polydict.ETuple'>

In contrast for a polynomial over QQ:

sage: Pxy.<x,y> = PolynomialRing(QQ,2)
sage: f = y^2 - x*(x-1)*(x-2)
sage: type(f.degrees())
<type 'tuple'>

The elliptic curve example above is a red herring, except to show that this unexpected and inconsistent return type is likely to create major issues elsewhere. I can imagine a resolution to this particular issue by defining a coercion from tuples so that the comparison does not fail, but someone knowledgeable about the design of polynomial rings and/or coercions needs to look at this. Note that a coercion does exist and parent(x.degrees())((1,0)) == x.degrees() returns True.

Component: number theory

Author: David Kohel

Issue created by migration from https://trac.sagemath.org/ticket/12040

fchapoton commented 6 years ago

Description changed:

--- 
+++ 
@@ -1,10 +1,11 @@
+
+```
 sage: P.<t> = PolynomialRing(QQ)
 sage: F.<a> = FractionField(P)
 sage: Pxy.<x,y> = PolynomialRing(F,2)
 sage: f = y^2 - x*(x-1)*(x-2)
 sage: E = EllipticCurve(f)
-
----
+---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)

 /Users/kohel/<ipython console> in <module>()
@@ -31,5 +32,5 @@
 sage: f = y^2 - x*(x-1)*(x-2)
 sage: type(f.degrees())
 <type 'tuple'>
-
+```
 The elliptic curve example above is a red herring, except to show that this unexpected and inconsistent return type is likely to create major issues elsewhere.  I can imagine a resolution to this particular issue by defining a coercion from tuples so that the comparison does not fail, but someone knowledgeable about the design of polynomial rings and/or coercions needs to look at this.  Note that a coercion does exist and parent(x.degrees())((1,0)) == x.degrees() returns True.
fchapoton commented 6 years ago
comment:6

Simplified issue:

sage: F = FractionField(PolynomialRing(QQ,'t'))
sage: Pxy.<x,y> = PolynomialRing(F,2)
sage: f = y^2 - x*(x-1)*(x-2)
sage: f.degrees()
(3, 2)
sage: f.degrees() == (3,2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-94-a14e4537ede5> in <module>()
----> 1 f.degrees() == (Integer(3),Integer(2))

TypeError: Argument 'other' has incorrect type (expected 
sage.rings.polynomial.polydict.ETuple, got tuple)