Bill Allombert on Thu, 30 May 2002 16:38:07 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: detecing non quadratic residue in sqrt/trapping errors |
On Thu, May 30, 2002 at 12:32:10PM +0300, Tuukka Toivonen wrote: > I have a line in my Pari function similar to > f(c) = > { > x = sqrt(c); > ... something more ... > } > > c can be, for example, an integer modulo p. It either has or has not the > solution. If there isn't a solution, pari says "non-quadratic residue in > gsqrt" and halts the function. I want to have instead a test, so that if > the square root doesn't exist, the function does something else instead of > halting. So, effectively, > x = sqrt(c) > if(error happened, > do something else, > else no error > ); For this particular case, you can use issquare(). > Is it possible to trap errors in Pari somehow? Yes, with the trap(,,) function, but it is an experimental feature. > I'd like to let the function handle also reals and complex numbers, so I'd > like to assume as little as possible about the variable type. > > I'm writing a function to compute square roots from a number of the form > Mod(a,p) + Mod(b,p)*I This is already implemented internally in factorff(): You can do something like mysqrt(a,b,p)= { local(F); if (p%4==1,return(sqrt((a+sqrt(Mod(-1,p))*b)*Mod(1,p)))); F=factorff('x^2-(a+'Z*b),p,'Z^2+1)[,1]; if(length(F)==1,error("not a square"), subst(subst(lift(lift(F[1])),'x,0),'Z,I)) } Cheers, Bill.