Question: Extracting Coefficients of Multivariate Polynomial Vectors

I have a vector consisting of taylor expansions of multiple variables, i.e. each element of the vector is a multivariate polynomial in X[1], X[2], etc. Not all the polynomials are of the same order, not all contain all the X[i]s.

I want to extract the coefficients of each component of the vector and store them in a Matrix.

I thought I had done it, but sadly the order of the coefficients is not what I expected.

Here is an example of a vector:

V := Vector([[a4*X[2]^2+a1*X[1]+a5*X[1]*X[2]+a2*X[2]+a3*X[1]^2],[(b1*X[1]+b2*X[2])^2]]);

In general, I will need to 1) expand, 2) collect, 3) sort, and 4) extract coefficients, for each element of the Vector V.

Here's my best effort:

ExtractCoeffs := (V,X) -> Matrix([seq([coeffs(sort(collect(
expand(V[i]),[X[1],X[2]],'distributed'),
order=plex(X[1],X[2]),'ascending'),
[X[1],X[2]])],i=1..2)]):
[[a5, a2, a4, a1, a3],
[2*b1*b2, b2^2, b1^2, 0, 0]]

But I would like the coefficient on X[1] first, on X[2] second, on X[1]*X[2] third, on X[1]^2 fourth, etc. I thought that would have been achieved by the sort options plex(X[1],X[2]) and 'ascending.' I would even have expected it to be sorted that way simply by calling sort without any option.

What am I doing wrong?

As a supplementary question: anyway to write the above more succinctly and while keeping the size of the vector V and the number of variables X[i] unspecified?

I did try things like this, with the distributive tilde:

coeffs~(sort~(collect~(expand~(V),X,'distributed'),X,'ascending'),X);

but no, that's not right...

Sadly, I didn't manage to adapt MatrixPolynomialAlgebra[Coeff] or PolynomialTools[CoefficientVector] to my purpose either...

Please Wait...