Search code examples
vectorpolynomialsparipari-gp

Returning Powers of a Two-Variable Polynomial as a Vector in PARI/GP?


I'm trying to view the monomials of a two-variable polynomial in vector form. So, for example, if I input x^2 + x^3*y + x*y + y^2 + 1, I would like to view it as [[2;0], [3;1], [1;1], [0;2], [0;0]] - a vector made of column vectors.

If I use Vec on a two-variable polynomial, it simply treats the second variable as a number, giving Vec( x^2 + x^3*y + x*y + y^2 + 1 ) = [ y, 1, y, y^2 + 1 ], which I don't think can then be twisted into something that would work for me.

Any ideas on how this might be able to be done?


Solution

  • You have to calc all the monomials by your own. Your bivariate polynomial can be viewed as the univariate polynomial having polynomial coefficients. Firstly, you select the primary variable (e.g. 'x) and find its exponents together with its nonzero coefficients:

    exponents(f, v=variable(f)) = {
        if(type(f) != "t_POL",
            return([[0, f]])
        );
        my(x = varhigher("#"));
        my(coeffs = Vecrev(subst(f, v, x)));
        my(indexes = select((c) -> c != 0, coeffs, 1));
    
        [[n-1, coeffs[n]] | n <- Vec(indexes)]
    };
    
    exponents(1)
    > [[0, 1]]
    exponents(x^2 + x^3*y + x*y + y^2 + 1)
    > [[0, y^2 + 1], [1, y], [2, 1], [3, y]]
    exponents(x^2 + x^3*y + x*y + y^2 + 1, 'y)
    > [[0, x^2 + 1], [1, x^3 + x], [2, 1]]
    

    Secondly, given such list of exponents and coefficients you easily obtain the complete list of monomials:

    monomial_list(f) = {
        concat(
            apply(
                (xs) -> [[xs[1], p[1]] | p <- exponents(xs[2])],
                exponents(f)
            )
        )
    };
    
    monomial_list(0)
    > [[0, 0]]
    monomial_list(x^2 + x^3*y + x*y + y^2 + 1)
    > [[0, 0], [0, 2], [1, 1], [2, 0], [3, 1]]