On Fri, 13 Oct 2006 23:32:09 -0700, didier deshommes <[EMAIL PROTECTED]> wrote: > Currently abs() is not defined for integers with a modulus:
I don't think it should be defined. Mathematically, it doesn't makes sense to write |n| for n an integer modulo m. To add strength to my argument that this shouldn't be defined in SAGE, note that in both GP/PARI and Magma abs is not defined on integers modulo n: abs(Mod(-3,10)) in PARI: GP/PARI ERROR: *** abs: incorrect type in gabs. Abs(Integers(10)!(-3)); ^ Runtime error in 'Abs': Bad argument types Argument types given: RngIntResElt You can get what you are thinking of as "the absolute value" using the lift method: sage: Mod(7,10).lift() 7 Sorry to bounce your patch -- I hate not incorporating things people send me, but in this case I think it is not good. It might even make sense to have a patch that defines __abs__ for integers modulo N to raise an ArithmeticError that explains that abs shouldn't make sense in that context, and the user should call lift instead. -- William > sage: abs(mod(7,10)) > --------------------------------------------------------------------------- > <type 'exceptions.TypeError'> Traceback (most recent call > last) > <type 'exceptions.TypeError'>: bad operand type for abs(): > 'integer_mod.IntegerMod_int' > > Here is a diff against integer_mod.pyx . The change is small enough so > that I didn't need to make a full bundle (I'm just getting the hang of > hg): > > diff -r 5eba42221f8a -r 6a378a7db7ec sage/rings/integer_mod.pyx > --- a/sage/rings/integer_mod.pyx Thu Oct 05 23:44:44 2006 -0700 > +++ b/sage/rings/integer_mod.pyx Fri Oct 13 23:17:46 2006 -0700 > @@ -944,6 +944,15 @@ cdef class IntegerMod_int(IntegerMod_abs > def __hash__(self): > return hash(self.ivalue) > > + def __abs__(self): > + """ > + EXAMPLES: > + sage: abs(mod(3,10)) > + 3 > + sage: abs(mod(-3,10)) > + 7 > + """ > + return self.ivalue > ### End of class > > > Cheers, > didier > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---