On Thursday, September 29, 2011, Chris Smith <smi...@gmail.com> wrote: > Maybe during break I can write a little tutorial about doing modular > arithmetic to include in the docs. Your example, Aaron, is exactly > what I was looking for with the arithmetic (though the repr form is > rather ugly -- see below). And Hector's work will allow one to answer > the other question (about finding a number that has a certain residual > profile for a given set of bases) though perhaps the routine I > presented could be used in the case where the moduli are not all > prime. That could go in a solve_congruence routine in solve (to answer > your question, Hector) and that routine could call the gf_csolve when > the moduli are prime or else do the other calculation that I gave: > > ```python >>>> def div(a,b,m): > ... m=FF(m) > ... try: return m(a)/m(b) > ... except: > ... if m(a): return oo # perhaps this, rather than an error would be > more sympy like > ... return S.NaN # ditto > ... >>>> for i in range(5): > ... for j in range(i,5): > ... print '%i/%i =%s'%(i,j,div(i,j,5)) > ... > 0/0 =nan > 0/1 =0 mod 5 > 0/2 =0 mod 5 > 0/3 =0 mod 5 > 0/4 =0 mod 5 > 1/1 =1 mod 5 > 1/2 =3 mod 5 > 1/3 =2 mod 5 > 1/4 =4 mod 5 > 2/2 =1 mod 5 > 2/3 =4 mod 5 > 2/4 =3 mod 5 > 3/3 =1 mod 5 > 3/4 =2 mod 5 > 4/4 =1 mod 5 >>>> for i in range(6): > ... for j in range(i,6): > ... print '%i/%i =%s'%(i,j,div(i,j,6)) > ... > 0/0 =nan > 0/1 =0 mod 6 > 0/2 =nan > 0/3 =nan > 0/4 =nan > 0/5 =0 mod 6 > 1/1 =1 mod 6 > 1/2 =oo > 1/3 =oo > 1/4 =oo > 1/5 =5 mod 6 > 2/2 =oo > 2/3 =oo > 2/4 =oo > 2/5 =4 mod 6 > 3/3 =oo > 3/4 =oo > 3/5 =3 mod 6 > 4/4 =oo > 4/5 =2 mod 6 > 5/5 =1 mod 6 >
Neither oo nor Nan really makes sense here. oo particularily makes littles sense as there is not really a way to have a concept like oo in a finite ring. Nan actually kind of makes sense. Perhaps we should create a subclass of Nan with a name like ZeroDivisor or something to make it clear what it represents in this context. Aaron Meurer >>>> m7=FF(7) >>>> [(m7(i)) for i in range(7)] > [SymmetricModularIntegerMod7(0), SymmetricModularIntegerMod7(1), SymmetricModula > rIntegerMod7(2), SymmetricModularIntegerMod7(3), SymmetricModularIntegerMod7(4), > SymmetricModularIntegerMod7(5), SymmetricModularIntegerMod7(6)] >>>> [sqrt(m7(i)) for i in range(7)] > [0, 1, sqrt(2), sqrt(3), sqrt(3)*I, sqrt(2)*I, I] >>>> [str(m7(i)) for i in range(7)] > ['0 mod 7', '1 mod 7', '2 mod 7', '3 mod 7', '4 mod 7', '5 mod 7', '6 mod 7'] >>>> str(m7(4)+m7(3)) > '0 mod 7' >>>> str(m7(4)-m7(3)) > '1 mod 7' >>>> str(m7(4)*m7(3)) > '5 mod 7' >>>> str(m7(4)/m7(3)) > '6 mod 7' >>>> str(m7(4)**3) > '1 mod 7' >>>> str(sqrt(m7(4))) > 'sqrt(3)*I' >>>> sqrt(m7(4))**2 > -3 >>>> m7(_)==m7(4) > True > ``` > > I wonder if the positive value for the sqrt should be returned. > http://ptrow.com/perl/calculator.pl lists the square roots of 4 mod 7 > as being 2 and 5 (half of the numbers between 1 and modulus - 1 -- in > this case half of 2, 3, 4, 5, or 2 and 5 -- are square roots mod n). > sympy returned sqrt(-3) rather than 2 or 5. > > Also, I wonder if the str form should be the default printed in > interactive sessions rather than the repr form. > > /c > > -- > You received this message because you are subscribed to the Google Groups "sympy" group. > To post to this group, send email to sympy@googlegroups.com. > To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/sympy?hl=en. > > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.