Ticket #9652: smallfix1-arith_valuation.2.3.patch

File smallfix1-arith_valuation.2.3.patch, 4.5 KB (added by lftabera, 11 years ago)

previous patch with clenaer comment in the header

  • sage/rings/arith.py

    # HG changeset patch
    # User Maarten Derickx <m.derickx.student@gmail.com>
    # Date 1284288712 -7200
    # Node ID b5ecf7f27964d43965fa5cd406ff0a0ebb7b7948
    # Parent  35297cc2620b7e81805e0f3a529c30af30575149
    #9652 Unnecesary and buggy code in arith.py
    
    
    diff -r 35297cc2620b -r b5ecf7f27964 sage/rings/arith.py
    a b  
    554554    """
    555555    return ZZ(n).is_prime_power(flag=flag)
    556556
    557 def valuation(m, p):
    558     """
    559     The exact power of p that divides m.
    560    
    561     m should be an integer or rational (but maybe other types work
    562     too.)
    563    
    564     This actually just calls the m.valuation() method.
    565    
    566     If m is 0, this function returns rings.infinity.
    567    
     557def valuation(m,*args1, **args2):
     558    """   
     559    This actually just calls the m.valuation() method.
     560    See the documentation of m.valuation() for a more precise description.
     561    Use of this function by developers is discouraged. Use m.valuation() instead.
     562   
     563    .. NOTE::
     564
     565        This is not always a valuation in the mathematical sense.
     566        For more information see:
     567        sage.rings.finite_rings.integer_mod.IntegerMod_int.valuation
     568
    568569    EXAMPLES::
    569570   
    570571        sage: valuation(512,2)
     
    595596        5
    596597        sage: valuation(243*10007,10007)
    597598        1
    598     """
    599     if hasattr(m, 'valuation'):
    600         return m.valuation(p)
    601     if m == 0:
    602         import sage.rings.all
    603         return sage.rings.all.infinity
    604     if is_FractionFieldElement(m):
    605         return valuation(m.numerator()) - valuation(m.denominator())
    606     r = 0
    607     power = p
    608     while not (m % power): # m % power == 0
    609         r += 1
    610         power *= p
    611     return r
     599        sage: y = QQ['y'].gen()
     600        sage: valuation(y^3, y)
     601        3
     602        sage: x = QQ[['x']].gen()
     603        sage: valuation((x^3-x^2)/(x-4))
     604        2
     605        sage: valuation(4r,2r)
     606        2
     607    """
     608    if isinstance(m,(int,long)):
     609        m=ZZ(m)
     610    return m.valuation(*args1, **args2)
    612611
    613612def prime_powers(start, stop=None):
    614613    r"""
  • sage/rings/finite_rings/integer_mod.pyx

    diff -r 35297cc2620b -r b5ecf7f27964 sage/rings/finite_rings/integer_mod.pyx
    a b  
    4242
    4343-  William Stein: sqrt
    4444
     45-  Maarten Derickx: moved the valuation code from the global
     46   valuation function to here
     47
    4548
    4649TESTS::
    4750
     
    11411144            raise ArithmeticError, "multiplicative order of %s not defined since it is not a unit modulo %s"%(
    11421145                self, self.__modulus.sageInteger)
    11431146
     1147    def valuation(self, p):
     1148        """
     1149        The largest power r such that m is in the ideal generated by p^r or infinity if there is not a largest such power.
     1150        However it is an error to take the valuation with respect to a unit.
     1151
     1152        .. NOTE::
     1153       
     1154            This is not a valuation in the mathematical sense. As shown with the examples below.
     1155
     1156        EXAMPLES:
     1157       
     1158        This example shows that the (a*b).valuation(n) is not always the same as a.valuation(n) + b.valuation(n)
     1159 
     1160        ::
     1161
     1162            sage: R=ZZ.quo(9)
     1163            sage: a=R(3)
     1164            sage: b=R(6)
     1165            sage: a.valuation(3)
     1166            1
     1167            sage: a.valuation(3) + b.valuation(3)
     1168            2
     1169            sage: (a*b).valuation(3)
     1170            +Infinity
     1171
     1172        The valuation with respect to a unit is an error
     1173
     1174        ::
     1175
     1176            sage: a.valuation(4)
     1177            Traceback (most recent call last):
     1178            ...
     1179            ValueError: Valuation with respect to a unit is not defined.
     1180
     1181        TESTS::
     1182
     1183            sage: R=ZZ.quo(12)
     1184            sage: a=R(2)
     1185            sage: b=R(4)
     1186            sage: a.valuation(2)
     1187            1
     1188            sage: b.valuation(2)
     1189            +Infinity
     1190            sage: ZZ.quo(1024)(16).valuation(4)
     1191            2
     1192     
     1193        """
     1194        p=self.__modulus.sageInteger.gcd(p)
     1195        if p==1:
     1196            raise ValueError("Valuation with respect to a unit is not defined.")
     1197        r = 0
     1198        power = p
     1199        while not (self % power): # self % power == 0
     1200            r += 1
     1201            power *= p
     1202            if not power.divides(self.__modulus.sageInteger):
     1203                from sage.rings.all import infinity
     1204                return infinity
     1205        return r
     1206
    11441207    def __floordiv__(self, other):
    11451208        """
    11461209        Exact division for prime moduli, for compatibility with other fields.