# Computing basis for the intersection of two vector spaces represented as polynomial subspaces in multiple variables

I was trying to compute intersection of two vector spaces. I did find this link Intersection of two vector spaces with a nifty function being made for this given two lists of basis vectors.

I am having trouble finding an efficient way of putting basis vectors into a list that start as polynomials. For example, I have the vector spaces spanned by {z1, z2, z3} and {z4, z5, z6}

z1 = (1/6 p ((x1 + 2 y1)^2 + (2 x1 + y1)^2) + ((p - 1)/p) (x2 + y2)) (x1 + 2 y1);

z2 = (x1 + 2 y1)^3;

z3 = (x1 + 2 y1) (x2 + 2 y2);


And another given by

z4 = (1/6 p ((x1 + 2 y1)^2 + (2 x1 + y1)^2) + ((p - 1)/p) (x2 + y2)) (2 x1 + y1);

z5 = (2 x1 + y1)^3;

z6 = (2 x1 + y1) (2 x2 + y2);


I am interested in the intersection of these vector spaces. When I try to find an efficient way to put them into list, I was thinking something like

w1 = Flatten[CoefficientList[z1, {x1, y1, x2, y2}];


would work, but I don't get vectors of the same size, as illustrated below

Flatten[CoefficientList[z1, {x1, y1, x2, y2}]]

{0, 0, 0, 0, 0, (2 (-1 + p))/p, (2 (-1 + p))/p, 0, 0, 0, 0, 0, (5 p)/3, 0, 0, 0,
0, (-1 + p)/p, (-1 + p)/p, 0, 0, 0, 0, 0, (7 p)/2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3 p, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (5 p)/6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Flatten[CoefficientList[z2, {x1, y1, x2, y2}]]

{0, 0, 0, 8, 0, 0, 12, 0, 0, 6, 0, 0, 1, 0, 0, 0}


Any suggestions?

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jul 30 '16 at 2:53
• You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this meta Q&A helpful – Michael E2 Jul 30 '16 at 2:53
• Are you looking to compute the intersection of two polynomial ideals? It's not clear to me what are the "vectors" (or, more accurately, what is the vector space) if this is really meant to be a linear algebra question. – Daniel Lichtblau Jul 30 '16 at 18:50
• If it is an issue of dimensions from CoefficientList, just use the optional third argument, with settings based on highest exponents appearing for a given variable. – Daniel Lichtblau Jul 31 '16 at 16:08

This way is perhaps a little kluge-y, and it can be certainly be elegant-ized, but here's a first pass at the problem.

We first construct a generic polynomial with all of the possible monomials in your polynomials using my answer here:

polynomial[vars_List, degree_Integer, coeff_] :=
#.Array[coeff, Length@#] &@ DeleteDuplicates[Times @@@ Tuples[Prepend[vars, 1], degree]]
Protect[temp];
poly = polynomial[{x1, x2, y1, y2}, 3, temp[] &];


We then add this polynomial to each of yours to guarantee that it has all of the possible monomials. We then use CoefficientRules on the polynomials, set temp[] to zero, and then extract the entries. For instance, using one of your example polynomials from your post:

Last /@ CoefficientRules[
z1 + polynomial[{x1, x2, y1, y2}, 3, temp[] &],
{x1, x2, y1, y2}
] /. temp[] -> 0
(* {(5 p)/6, 0, 3 p, 0, 0, 0, 0, 0, 1 - 1/p, (7 p)/2, 0, 0, 0, 1 - 1/p, 0, 0, 0,
0, 0, 0, 0, 2 - 2/p, 0, 0, 0, (5 p)/3, 0, 0, 0, 2 - 2/p, 0, 0, 0, 0, 0} *)


This vector is of length 35 because there are 35 monomials (including 1) of degree up to 3 in four variables.

• @Nate. I fixed it just now. (I had to get rid of the degree_Integer on the right-hand side.) Please check it again and see if it suits your purpose! – march Jul 30 '16 at 18:47
• Thanks, it works well, and used it to to find a nontrivial intersecton of two vector spaces. I am also looking for a way to rewrite the vector I get from the intersection of 2 vector spaces in terms of one of the original basis, or/and back to a polynomial format. I tried LinearSolve[ basis list for first vector space, vector in intersection], but I get the error LinearSolve::lslc: Coefficient matrix and target vector(s) or matrix do not have the same dimensions. >> – Nate Jul 30 '16 at 20:20
• @Nate . I should be able to figure that out. I'll work on it. – march Jul 30 '16 at 20:29
• Thanks, let me know if you do. I am wondering if one could just add a bunch of zero vectors to the list for the linear solve problem. Still haven't found an easy way to change it back to a polynomial either. – Nate Aug 1 '16 at 2:39