Ticket #9764: 9764_ideal_repr_new.patch

File 9764_ideal_repr_new.patch, 17.4 KB (added by Jeroen Demeyer, 12 years ago)

Patch against sage-4.6.alpha0 + #9400 + #9898 + #9753

  • sage/libs/pari/gen.pyx

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1282168698 -7200
    # Node ID cd0623e7596fe89d62fe973fb649b2e50c3530fd
    # Parent  cd88cc8db5eeb3d9da9c0378669c922fe9ca70f9
    #9764: implement hashing for number field ideals that uses the HNF form instead of a string
    #9764: make number field ideals *not* print in reduced form, use gens_two() if the discriminant is large
    #9764: Use PARI's hash_GEN() for gen.__hash__
    
    diff -r cd88cc8db5ee -r cd0623e7596f sage/libs/pari/gen.pyx
    a b  
    416416        return P.new_gen_to_string(self.g)
    417417
    418418    def __hash__(self):
    419         _sig_on
    420         return hash(P.new_gen_to_string(self.g))
     419        """
     420        Return the hash of self, computed using PARI's hash_GEN().
     421       
     422        TESTS::
     423       
     424            sage: type(pari('1 + 2.0*I').__hash__())
     425            <type 'int'>
     426        """
     427        cdef long h
     428        _sig_on
     429        h = hash_GEN(self.g)
     430        _sig_off
     431        return h
    421432
    422433    def _testclass(self):
    423434        import test
  • sage/rings/number_field/number_field.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/number_field/number_field.py
    a b  
    42854285            sage: K.<a> = NumberField(x^4+3*x^2-17)
    42864286            sage: P = K.ideal(61).factor()[0][0]
    42874287            sage: K.residue_field(P)
    4288             Residue field in abar of Fractional ideal (-2*a^2 + 1)
     4288            Residue field in abar of Fractional ideal (61, a^2 + 30)
    42894289       
    42904290        ::
    42914291       
     
    42994299            sage: L.residue_field(P)
    43004300            Traceback (most recent call last):
    43014301            ...
    4302             ValueError: Fractional ideal (-2*a^2 + 1) is not an ideal of Number Field in b with defining polynomial x^2 + 5
     4302            ValueError: Fractional ideal (61, a^2 + 30) is not an ideal of Number Field in b with defining polynomial x^2 + 5
    43034303            sage: L.residue_field(2)
    43044304            Traceback (most recent call last):
    43054305            ...
     
    70437043
    70447044            sage: C20 = CyclotomicField(20)
    70457045            sage: C20.different()
    7046             Fractional ideal (-4*zeta20^7 + 8*zeta20^5 - 12*zeta20^3 + 6*zeta20)
     7046            Fractional ideal (10, 2*zeta20^6 - 4*zeta20^4 - 4*zeta20^2 + 2)
    70477047            sage: C18 = CyclotomicField(18)
    70487048            sage: D = C18.different().norm()
    70497049            sage: D == C18.discriminant().abs()
  • sage/rings/number_field/number_field_ideal.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/number_field/number_field_ideal.py
    a b  
    3636#                  http://www.gnu.org/licenses/
    3737#*****************************************************************************
    3838
     39SMALL_DISC = 1000000
     40
    3941import operator
    4042
    4143import sage.misc.latex as latex
     
    178180        if len(gens)==0:
    179181            raise ValueError, "gens must have length at least 1 (zero ideal is not a fractional ideal)"
    180182        Ideal_generic.__init__(self, field, gens, coerce)
     183
     184    def __hash__(self):
     185        """
     186        EXAMPLES::
     187
     188            sage: NumberField(x^2 + 1, 'a').ideal(7).__hash__()
     189            -9223372036854775779                 # 64-bit
     190            -2147483619                          # 32-bit
     191        """
     192        try: return self._hash
     193        # At some point in the future (e.g., for relative extensions), we'll likely
     194        # have to consider other hashes, like the following.
     195        #except AttributeError: self._hash = hash(self.gens())
     196        except AttributeError: self._hash = self.pari_hnf().__hash__()
     197        return self._hash
    181198       
    182199    def _latex_(self):
    183200        """
     
    188205            '\\left(2, \\frac{1}{2} a - \\frac{1}{2}\\right)'
    189206            sage: latex(K.ideal([2, 1/2*a - 1/2]))
    190207            \left(2, \frac{1}{2} a - \frac{1}{2}\right)
     208
     209        The gens are reduced only if the norm of the discriminant of
     210        the defining polynomial is at most
     211        sage.rings.number_field.number_field_ideal.SMALL_DISC::
     212
     213            sage: K.<a> = NumberField(x^2 + 902384092834); K
     214            Number Field in a with defining polynomial x^2 + 902384092834
     215            sage: I = K.factor(19)[0][0]; I._latex_()
     216            '\\left(19\\right)'
     217
     218        We can make the generators reduced by increasing SMALL_DISC.
     219        We had better also set proof to False, or computing reduced
     220        gens could take too long::
     221       
     222            sage: proof.number_field(False)
     223            sage: sage.rings.number_field.number_field_ideal.SMALL_DISC = 10^20
     224            sage: K.<a> = NumberField(x^4 + 3*x^2 - 17)
     225            sage: K.ideal([17*a,17,17,17*a])._latex_()
     226            '\\left(17\\right)'
    191227        """
    192         return '\\left(%s\\right)'%(", ".join(map(latex.latex, self.gens_reduced())))
     228        return '\\left(%s\\right)'%(", ".join(map(latex.latex, self._gens_repr())))
    193229       
    194230    def __cmp__(self, other):
    195231        """
     
    363399
    364400    def _repr_short(self):
    365401        """
    366         Efficient string representation of this fraction ideal.
     402        Compact string representation of this ideal.  When the norm of
     403        the discriminant of the defining polynomial of the number field
     404        is less than
     405
     406            sage.rings.number_field.number_field_ideal.SMALL_DISC
     407           
     408        then display reduced generators.  Otherwise display two
     409        generators.
    367410
    368411        EXAMPLES::
    369412
     
    372415            sage: I = K.factor(17)[0][0]; I
    373416            Fractional ideal (17, a^2 - 6)
    374417            sage: I._repr_short()
    375             '(17, a^2 - 6)'       
     418            '(17, a^2 - 6)'
     419
     420        We use reduced gens, because the discriminant is small::
     421       
     422            sage: K.<a> = NumberField(x^2 + 17); K
     423            Number Field in a with defining polynomial x^2 + 17
     424            sage: I = K.factor(17)[0][0]; I
     425            Fractional ideal (-a)
     426
     427        Here the discriminant is 'large', so the gens aren't reduced::
     428
     429            sage: sage.rings.number_field.number_field_ideal.SMALL_DISC
     430            1000000
     431            sage: K.<a> = NumberField(x^2 + 902384094); K
     432            Number Field in a with defining polynomial x^2 + 902384094
     433            sage: I = K.factor(19)[0][0]; I
     434            Fractional ideal (19, a + 14)
     435            sage: I.gens_reduced()                 # long time
     436            (19, a + 14)
    376437        """
    377         #NOTE -- we will *have* to not reduce the gens soon, since this
    378         # makes things insanely slow in general.
    379         # When I fix this, I *have* to also change the _latex_ method.
    380         return '(%s)'%(', '.join(map(str, self.gens_reduced())))
    381 #         return '(%s)'%(', '.join(map(str, self.gens())))
     438        return '(%s)'%(', '.join(map(str, self._gens_repr())))
     439
     440    def _gens_repr(self):
     441        """
     442        Returns tuple of generators to be used for printing this number
     443        field ideal. The gens are reduced only if the absolute value of
     444        the norm of the discriminant of the defining polynomial is at
     445        most sage.rings.number_field.number_field_ideal.SMALL_DISC.
     446       
     447        EXAMPLES::
     448
     449            sage: sage.rings.number_field.number_field_ideal.SMALL_DISC
     450            1000000
     451            sage: K.<a> = NumberField(x^4 + 3*x^2 - 17)
     452            sage: K.discriminant()  # too big
     453            -1612688
     454            sage: I = K.ideal([17*a*(2*a-2),17*a*(2*a-3)]); I._gens_repr()
     455            (289, 17*a)
     456            sage: I.gens_reduced()
     457            (17*a,)
     458        """
     459        # If the discriminant is small, it is easy to find nice gens.
     460        # Otherwise it is potentially very hard.
     461        try:
     462            if abs(self.number_field().defining_polynomial().discriminant().norm()) <= SMALL_DISC:
     463                return self.gens_reduced()
     464        except TypeError:
     465            # In some cases with relative extensions, computing the
     466            # discriminant of the defining polynomial is not
     467            # supported.
     468            pass
     469        # Return two generators unless the second one is zero
     470        two_gens = self.gens_two()
     471        if two_gens[1]:
     472            return two_gens
     473        else:
     474            return (two_gens[0],)
    382475
    383476    def _pari_(self):
    384477        """
     
    604697            sage: J = K.ideal([a+2, 9])
    605698            sage: J.gens()
    606699            (a + 2, 9)
    607             sage: J.gens_reduced()
     700            sage: J.gens_reduced()  # random sign
    608701            (a + 2,)
    609702            sage: K.ideal([a+2, 3]).gens_reduced()
    610703            (3, a + 2)
    611704
    612705        TESTS::
    613706
     707            sage: len(J.gens_reduced()) == 1
     708            True
     709           
    614710            sage: all(j.parent() is K for j in J.gens())
    615711            True
    616712            sage: all(j.parent() is K for j in J.gens_reduced())
    617713            True
    618 
     714           
    619715            sage: K.<a> = NumberField(x^4 + 10*x^2 + 20)
    620716            sage: J = K.prime_above(5)
    621717            sage: J.is_principal()
     
    13281424            sage: K.<a> = CyclotomicField(11); K
    13291425            Cyclotomic Field of order 11 and degree 10
    13301426            sage: I = K.factor(31)[0][0]; I
    1331             Fractional ideal (-3*a^7 - 4*a^5 - 3*a^4 - 3*a^2 - 3*a - 3)
     1427            Fractional ideal (31, a^5 + 10*a^4 - a^3 + a^2 + 9*a - 1)
    13321428            sage: I.divides(I)
    13331429            True
    13341430            sage: I.divides(31)
     
    13511447            sage: I = K.ideal(19); I
    13521448            Fractional ideal (19)
    13531449            sage: F = I.factor(); F
    1354             (Fractional ideal (a^2 + 2*a + 2)) * (Fractional ideal (a^2 - 2*a + 2))
     1450            (Fractional ideal (19, 1/2*a^2 + a - 17/2)) * (Fractional ideal (19, 1/2*a^2 - a - 17/2))
    13551451            sage: type(F)
    13561452            <class 'sage.structure.factorization.Factorization'>
    13571453            sage: list(F)
    1358             [(Fractional ideal (a^2 + 2*a + 2), 1), (Fractional ideal (a^2 - 2*a + 2), 1)]
     1454            [(Fractional ideal (19, 1/2*a^2 + a - 17/2), 1), (Fractional ideal (19, 1/2*a^2 - a - 17/2), 1)]
    13591455            sage: F.prod()
    13601456            Fractional ideal (19)
    13611457        """
  • sage/rings/number_field/number_field_ideal_rel.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/number_field/number_field_ideal_rel.py
    a b  
    192192            sage: J.absolute_norm()
    193193            22584817
    194194            sage: J.absolute_ideal()
    195             Fractional ideal (188/812911*a^5 - 1/812911*a^4 + 45120/812911*a^3 - 56/73901*a^2 + 3881638/812911*a + 50041/812911)
     195            Fractional ideal (22584817, -4417/2438733*a^5 - 2867/1625822*a^4 - 1307249/4877466*a^3 - 112151/443406*a^2 - 22904674/2438733*a - 13720435234111/2438733)  # 32-bit
     196            Fractional ideal (22584817, -1473/812911*a^5 + 8695/4877466*a^4 - 1308209/4877466*a^3 + 117415/443406*a^2 - 22963264/2438733*a - 13721081784272/2438733)   # 64-bit
    196197            sage: J.absolute_ideal().norm()
    197198            22584817
    198199
     
    414415            sage: K.<c> = F.extension(Y^2 - (1 + a)*(a + b)*a*b)
    415416            sage: I = K.ideal(3, c)
    416417            sage: J = I.ideal_below(); J
    417             Fractional ideal (b)   # 32-bit
    418             Fractional ideal (-b)  # 64-bit
     418            Fractional ideal (b)
    419419            sage: J.number_field() == F
    420420            True
    421421        """
     
    593593            sage: I.relative_ramification_index()
    594594            2
    595595            sage: I.ideal_below()
    596             Fractional ideal (b)   # 32-bit
    597             Fractional ideal (-b)  # 64-bit
     596            Fractional ideal (-b)  # 32-bit
     597            Fractional ideal (b)   # 64-bit
    598598            sage: K.ideal(b) == I^2
    599599            True
    600600        """
     
    770770        sage: K.<a> = NumberField(x^2+6)
    771771        sage: L.<b> = K.extension(K['x'].gen()^4 + a)
    772772        sage: I = L.ideal(b); I
    773         Fractional ideal (b)
     773        Fractional ideal (6, b)
    774774        sage: is_NumberFieldFractionalIdeal_rel(I)
    775775        True
    776776        sage: N = I.relative_norm(); N
  • sage/rings/number_field/order.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/number_field/order.py
    a b  
    673673            sage: P = K.ideal(61).factor()[0][0]
    674674            sage: OK = K.maximal_order()
    675675            sage: OK.residue_field(P)
    676             Residue field in abar of Fractional ideal (-2*a^2 + 1)
     676            Residue field in abar of Fractional ideal (61, a^2 + 30)
    677677        """
    678678        if self.is_maximal():
    679679            return self.number_field().residue_field(prime, name, check)
  • sage/rings/number_field/small_primes_of_degree_one.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/number_field/small_primes_of_degree_one.py
    a b  
    4646EXAMPLES::
    4747
    4848    sage: x = ZZ['x'].gen()
    49     sage: F.<a> = NumberField(x^2 - 2, 'a')
     49    sage: F.<a> = NumberField(x^2 - 2)
    5050    sage: Ps = F.primes_of_degree_one_list(3)
    5151    sage: Ps # random
    5252    [Fractional ideal (2*a + 1), Fractional ideal (-3*a + 1), Fractional ideal (-a + 5)]
     
    5959
    6060The next two examples are for relative number fields.::
    6161
    62     sage: L.<b> = F.extension(x^3 - a, 'b')
     62    sage: L.<b> = F.extension(x^3 - a)
    6363    sage: Ps = L.primes_of_degree_one_list(3)
    6464    sage: Ps # random
    6565    [Fractional ideal (17, b - 5), Fractional ideal (23, b - 4), Fractional ideal (31, b - 2)]
     
    6969    True
    7070    sage: all(P.residue_class_degree() == 1 for P in Ps)
    7171    True
    72     sage: M.<c> = NumberField(x^2 - x*b^2 + b, 'c')
     72    sage: M.<c> = NumberField(x^2 - x*b^2 + b)
    7373    sage: Ps = M.primes_of_degree_one_list(3)
    7474    sage: Ps # random
    7575    [Fractional ideal (17, c - 2), Fractional ideal (c - 1), Fractional ideal (41, c + 15)]
  • sage/rings/polynomial/polynomial_quotient_ring.py

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/polynomial/polynomial_quotient_ring.py
    a b  
    689689        `x^2 + 31` from 12 to 2, i.e. we lose a generator of order 6::
    690690       
    691691            sage: S.S_class_group([K.ideal(a)])
    692             [((1/4*xbar^2 + 31/4, (-1/8*a + 1/8)*xbar^2 - 31/8*a + 31/8, 1/16*xbar^3 + 1/16*xbar^2 + 31/16*xbar + 31/16, -1/16*a*xbar^3 + (1/16*a + 1/8)*xbar^2 - 31/16*a*xbar + 31/16*a + 31/8), 6, 1/16*xbar^3 - 5/16*xbar^2 + 31/16*xbar - 139/16), ((-1/4*xbar^2 - 23/4, (1/8*a - 1/8)*xbar^2 + 23/8*a - 23/8, -1/16*xbar^3 - 1/16*xbar^2 - 23/16*xbar - 23/16, 1/16*a*xbar^3 + (-1/16*a - 1/8)*xbar^2 + 23/16*a*xbar - 23/16*a - 23/8), 2, -1/8*xbar^2 - 15/8)]     # 32-bit
    693             [((1/4*xbar^2 + 31/4, (-1/8*a + 1/8)*xbar^2 - 31/8*a + 31/8, 1/16*xbar^3 + 1/16*xbar^2 + 31/16*xbar + 31/16, -1/16*a*xbar^3 + (1/16*a + 1/8)*xbar^2 - 31/16*a*xbar + 31/16*a + 31/8), 6, 1/16*xbar^3 - 5/16*xbar^2 + 31/16*xbar - 139/16), ((-1/4*xbar^2 - 23/4, (1/8*a - 1/8)*xbar^2 + 23/8*a - 23/8, -1/16*xbar^3 - 1/16*xbar^2 - 23/16*xbar - 23/16, 1/16*a*xbar^3 + (-1/16*a - 1/8)*xbar^2 + 23/16*a*xbar - 23/16*a - 23/8), 2, -1/8*xbar^2 - 15/8)] # 64-bit
     692            [((1/4*xbar^2 + 31/4, (-1/8*a + 1/8)*xbar^2 - 31/8*a + 31/8, 1/16*xbar^3 + 1/16*xbar^2 + 31/16*xbar + 31/16, -1/16*a*xbar^3 + (1/16*a + 1/8)*xbar^2 - 31/16*a*xbar + 31/16*a + 31/8), 6, 1/16*xbar^3 - 5/16*xbar^2 + 31/16*xbar - 139/16), ((-1/4*xbar^2 - 23/4, (1/8*a - 1/8)*xbar^2 + 23/8*a - 23/8, -1/16*xbar^3 - 1/16*xbar^2 - 23/16*xbar - 23/16, 1/16*a*xbar^3 + (-1/16*a - 1/8)*xbar^2 + 23/16*a*xbar - 23/16*a - 23/8), 2, -1/8*xbar^2 - 15/8)]  # 32-bit
     693            [((1/4*xbar^2 + 31/4, (-1/8*a + 1/8)*xbar^2 - 31/8*a + 31/8, 1/16*xbar^3 + 1/16*xbar^2 + 31/16*xbar + 31/16, -1/16*a*xbar^3 + (1/16*a + 1/8)*xbar^2 - 31/16*a*xbar + 31/16*a + 31/8), 6, -1/16*xbar^3 + 1/16*xbar^2 - 31/16*xbar + 47/16), ((-1/4*xbar^2 - 23/4, (1/8*a - 1/8)*xbar^2 + 23/8*a - 23/8, -1/16*xbar^3 - 1/16*xbar^2 - 23/16*xbar - 23/16, 1/16*a*xbar^3 + (-1/16*a - 1/8)*xbar^2 + 23/16*a*xbar - 23/16*a - 23/8), 2, -1/8*xbar^2 - 15/8)] # 64-bit
    694694
    695695        Note that all the returned values live where we expect them to::
    696696       
  • sage/rings/residue_field.pyx

    diff -r cd88cc8db5ee -r cd0623e7596f sage/rings/residue_field.pyx
    a b  
    172172    An example where the residue class field is large but of degree 1::
    173173   
    174174        sage: K.<a> = NumberField(x^3-875); P = K.ideal(2007).factor()[0][0]; k = K.residue_field(P); k
    175         Residue field of Fractional ideal (-2/25*a^2 - 2/5*a - 3)
     175        Residue field of Fractional ideal (223, 1/5*a + 11)
    176176        sage: k(a)
    177177        168
    178178        sage: k(a)^3 - 875
     
    468468       
    469469            sage: K.<a> = NumberField(x^3-11)
    470470            sage: F = K.ideal(37).factor(); F
    471             (Fractional ideal (37, a + 12)) * (Fractional ideal (2*a - 5)) * (Fractional ideal (37, a + 9))   # 32-bit
    472             (Fractional ideal (37, a + 12)) * (Fractional ideal (-2*a + 5)) * (Fractional ideal (37, a + 9))  # 64-bit
     471            (Fractional ideal (37, a + 12)) * (Fractional ideal (-2*a + 5)) * (Fractional ideal (37, a + 9))
    473472            sage: k = K.residue_field(F[0][0])
    474473            sage: l = K.residue_field(F[1][0])
    475474            sage: k == l