Bill Allombert on Fri, 06 Nov 2015 16:29:53 +0100


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

Re: real polynomials


On Fri, Nov 06, 2015 at 02:45:34PM +0000, John Cremona wrote:
> I am so embarrassed at having such trivially elementary questions, so
> let me first say that I do not ask on the list until spending at least
> an hour experimenting and reading the library reference manual.
> 
> If I have a rational polynomial f created by
> 
> GEN f = mkpoln(ncoeffs,g0,g1,g2,g3,g4);
> where the g0, ..., g4 are rationals, and then I do
>
> GEN fdash = derivpol(f);
> GEN g = ggcd(f,fdash);
> 
> I get a run-time error:
> 
>   ***   incorrect type in Q_divi_to_int (t_FRAC).
> which I do not understand. 

You code is correct so I assume f is not defined properly.
The above code works:

#include <pari/pari.h>
int
main(void)
{
  GEN f, fdash, g;
  pari_init(8000000,500000);
  f = mkpoln(3,ghalf,gen_2,gen_1);
  fdash = derivpol(f);
  g = ggcd(f,fdash);
  pari_printf("g=%Ps\n",g);
  pari_close();
}

> Obstacles to debugging include (1) not knowing how to output a GEN; 

In C do:
pari_printf("g=%Ps\n",g);

inside gdb, you can do
p output(g)
or
p dbgGEN(g,-1)

> (2) asking for the type (or "typ") of a
> GEN gives a number (both the above have type 10) when I was expecting
> t_POL or similar.

10 is equal to t_POL (it is an enum value).

you can use type_name(10) to convert 10 to the string "t_POL".

> While I am here, in the code above I am clearly defining a polynomial
> of degree 4.  I really want to have polynomials of arbitrary degree
> but can I do that with the mkpoln() function?  i.e. can I replace have
> n separate GEN arguments for the coefficients with a single GEN*
> array?

Probably it is simpler to start with a vector and the convert it to a polynomial.
You need to allocate a vector with cgetg(...,t_VEC), and then fill it.

GEN P;
long n = ...
GEN V = cgetg(n+1,t_VEC);
for(i=1; i<=n; i++)
  gel(V,i) = ...
P = gtopolyrev(V, 0);

you can always use gp2c to generate example of C code.

Cheers,
Bill.