Ticket #9652: smallfix1-arith_valuation.2.patch

File smallfix1-arith_valuation.2.patch, 4.7 KB (added by mderickx, 11 years ago)

Use this one, the other is an old version

  • sage/rings/arith.py

    # HG changeset patch
    # User Maarten Derickx <m.derickx.student@gmail.com>
    # Date 1284195133 -7200
    # Node ID c1686e03aead067786aa7cb3d965e6cfb4465dd0
    # Parent  35297cc2620b7e81805e0f3a529c30af30575149
    This if statement will always return false since fraction fields have an atribute valuation. Might the statement magically return true cause of some buggy other code. Then this will return a buggy restult since the argument p is not passed to the numerator and denominator.
    
    diff -r 35297cc2620b -r c1686e03aead 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        This is not always a valuation in the mathematical sense.
     565        For more information see:
     566        sage.rings.finite_rings.integer_mod.IntegerMod_int.valuation
     567
    568568    EXAMPLES::
    569569   
    570570        sage: valuation(512,2)
     
    595595        5
    596596        sage: valuation(243*10007,10007)
    597597        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
     598        sage: y = QQ['y'].gen()
     599        sage: valuation(y^3, y)
     600        3
     601        sage: x = QQ[['x']].gen()
     602        sage: valuation((x^3-x^2)/(x-4))
     603        2
     604        sage: valuation(4r,2r)
     605        2
     606    """
     607    if isinstance(m,(int,long)):
     608        m=ZZ(m)
     609    return m.valuation(*args1, **args2)
    612610
    613611def prime_powers(start, stop=None):
    614612    r"""
  • sage/rings/finite_rings/integer_mod.pyx

    diff -r 35297cc2620b -r c1686e03aead 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.