I am having difficulty in improving some code. I have written the following code, which I think is not efficient at all. First I define some variables using table:
vars = Table[
If[i <= j + 2 && Mod[i + j, 2] == 0, Subscript[p, i, j], 0], {i, 1,
7}, {j, 1, 3}]
The output is
{{Subscript[p, 1, 1], 0, Subscript[p, 1, 3]}, {0, Subscript[p, 2, 2],
0}, {Subscript[p, 3, 1], 0, Subscript[p, 3, 3]}, {0, Subscript[p, 4,
2], 0}, {0, 0, Subscript[p, 5, 3]}, {0, 0, 0}, {0, 0, 0}}
The point here is that I am only allowed to use $p_{i,j}$ for some specific values of $i$ and $j$.
Now that I have defined my variables, I want to find all monomials of degree 2 in these variables, such that the sum of the first indices is 8 and the sum of the second indices is 6. I am not allowed to use $p_{1,1}$ by the way. So $p_{3,3}p_{5,3}$ is an example (actually this is the only possibility). The code I am using is
polynomials1 =
DeleteDuplicates[
DeleteCases[
Flatten@Table[
If[i + k == 8 && j + l == 6 && i + j > 2 && k + l > 2 ,
vars[[i, j]]*vars[[k, l]], 0], {i, 1, 7}, {j, 1,
3}, {k, 1, 7}, {l, 1, 3}], 0]]
The first two conditions in the If statement are the ones I stated above, the last two statements prohibits the usage of $p_{1,1}$.
This gives the desired output, namely $p_{3,3}p_{5,3}$, but I can see that it is not efficient. This is because the variables $p_{i,j}$ commute with themselves. For example, $i=3,j=3$ and $k=5,l=3$ and $i=5,j=3$ and $k=3,l=3$ give the same solution.
If I want to this for monomials of higher degree, then it becomes slow very quickly (at degree 5 it becomes really slow). How can we calculate this in a more efficient way?