Gerhard Niklasch on Fri, 11 May 2001 17:31:43 +0200 (MEST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Changing PARI types


In response to:
> Message-ID: <Pine.SOL.3.96.1010511082001.8135A-100000@elios>
> Date: Fri, 11 May 2001 08:28:52 +0100 (BST)
> From: Mark Chimley <M.Chimley@bristol.ac.uk>
> 
> I have used "ground" to round the coefficients of a polynomial to the
> nearest integers, however the type of the coefficients remains at 6
> (probably real or complex). This means that rootsmod (GEN) cannot handle
> the polynomial (the error actually comes from factormod, which is used
> by rootsmod). Is it possible to change the type of the coefficients so
> that they can be handled by rootsmod?
> 
> For example, can I do something like:
> 
> settyp (H [i], 1);
> 
> for each coefficient, or would this be horribly dangerous?

That would change the type marker of the object whilst leaving
the bit pattern as it is, and is very unlikely to result in a
recognizable PARI integer!  (Look up "type" in the manual or
online help.)  But you can get an integer directly:

===8<---
(17:23) gp > ??round
round(x,{&e}):

   If  x  is in R,  rounds x to the nearest integer and sets e to the number of
error bits,  that is the binary exponent of the difference between the original
and  the  rounded value  (the "fractional part").   If the exponent of x is too
large  compared to its precision  (i.e. e > 0),  the result is undefined and an
error occurs if e was not given.

   Important  remark:  note  that,  contrary to the other truncation functions,
this  function  operates  on every coefficient at every level of a PARI object.
For example truncate((2.4*X^2-1.7)/(X)) = 2.4*X, whereas
round((2.4*X^2-1.7)/(X))  =  (2*X^2-2)/(X). An important use of round is to get
exact results after a long approximate computation,  when theory tells you that
the coefficients must be integers.

   The  library  syntax  is  grndtoi(x,&e),   where e is a long integer.   Also
available is ground(x).

(17:23) gp > 
--->8===

So, grndtoi() is what you're looking for.

Moral:  Many gp functions xxyyzz have library equivalents gxxyyzz, but
this is not always the whole truth, and sometimes it's not even the
truth.

(Side note / this weeks exercise:
 how do you look up a library function in gp online help?

 Answer:  E.g., ???settyp at the gp prompt will tell you that "type" is
 what you should look at, so follow it up with ??type .  This works for
 all sorts of keywords, not only library functions, so ???truncation
 as well as ???truncate will let you know that ??round may be worth
 looking at.)

Cheers, Gerhard