Getting the constant portion of an expression in Pari-gp

87 Views Asked by At

This is probably a pretty stupid question--but I can't seem to find a relevant explanation in the pari-gp literature. I'm looking for a function in the library that takes a polynomial, or a series, or a constant, and spits out the constant portion (in the constant case, it'll just spit out the input).

The tool I've been using is polcoef(Expression,0) but this doesn't work for multiple variables--we have to call the function again.

For example,

polcoef(1+z+z^2,0)
%2 = 1

polcoef(1+y+z,0)
%3 = z + 1

polcoef(polcoef(1+y+z,0),0)
%4 = 1

Is there a built in function that'll do this for an arbitrary number of variables? So that, I don't have to iterate the polcoef function. This would clean up my code considerably; and I know I could probably build a recursive function myself, but I'm wondering if there's something built-in that does this.

Any help, comments, requests to edit and clarify are greatly appreciated.

Regards.

Edit:

Forgot to add, my desired output is,

Const(1+x+y+z)
1

Const(Pi+x^2+p)
Pi

1

There are 1 best solutions below

1
Piotr Semenov On BEST ANSWER

You can use the built-in substvec function to replace several variables at once. For example:

substvec(1+y+z, ['y,'z], [0,0])
%1 = 1

You even can easily implement a short-cut function not to create the list of zeros of proper length:

Const(poly: t_POL) = {
    my(vars = variables(poly));
    substvec(poly, vars, vector(#vars))
};
Const(1+x+y+z)
%1 = 1

Const(Pi+x^2+p)
%2 = Pi