Search code examples
polynomialsparipari-gp

Can you get a list of the powers in a polynomial? Pari GP


I'm working with single variable polynomials with coefficients +1/-1 (and zero). These can be very long and the range of powers can be quite big. It would be convenient for me to view the powers as a vector - is there any way of doing this quickly? I had hoped there would be a command already in Pari to do this, but I can't seem to see one?

Just an example to confirm what I'm trying to do...

Input:x^10 - x^8 + x^5 - x^2 + x + 1

Desired output: [10, 8, 5, 2, 1, 0]


Solution

  • You can use Vecrev to get the polynomial coefficients. After that just enumerate them to select the zero-based positions of non-zeros. You want the following one-liner:

    nonzeros(xs) = Vecrev([x[2]-1 | x <- select(x -> x[1] != 0, vector(#xs, i, [xs[i], i]))])
    

    Now you can easily get the list of polynomial powers:

    p = x^10 - x^8 + x^5 - x^2 + x + 1
    nonzeros(Vecrev(p))
    >> [10, 8, 5, 2, 1, 0]