# I need to organize a sum of terms in a pseudo-boolean function into a matrix!

I have a long 2-local summation of terms that I want to organize into a matrix such that the coefficients of the 1-local terms (a term with just a scalar coefficient and a single variable) are on the diagonal and the 2 local coefficients (term with scalar and two variables) are in the upper triangular portion of the matrix. This can be in any configuration, which should make the problem much simpler. Here is an portion of the kind of functions I am working with:

-q r - q r - q r - q r - q r -
q r - q r - q r - q r - q r -
q r - q r - q r - q r - q r -
q r - q r - q r - q r - q r -
q r - q r - q r - q r - q r -
q r - q r - q r - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s - r s - r s -
r s - r s - r s + 6s


In this example, there is only 1 1-local term, (6s) so there would only be a 6 on the diagonal. I'm new to Mathematica, so I was wondering if there was a nice/elegant solution, besides just parsing the function and throwing the coefficients into 2 arrays (one for 1-local, one for two) and smooshing it into a matrix.

• You have three types of variables - q, r, s - so it's not clear to me how do you want the coefficients to be organized in a matrix. I suggest to 1) make a much smaller expression to work on, and 2) show what the result for such should look like. – corey979 Dec 9 '16 at 8:47
• Hi corey, the q,r,s sort of represent shifts in the index, so r = +4, s = +12, so r = q and s = q, but in the context of the problem it was more efficient to groups these bits as their own vectors (treat first 4 bits as a q vector, next 8 bits as r vector, etc) so with this in mind, Simon's organization is quite nice, particularly because the original polynomial can be recovered. And it satisfies the main criteria, 1-local on diagonal and 2-local everywhere in the upper triangular portion. – Neil Philip Dec 9 '16 at 19:34

This should do what you want:

polynomial = -q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - q r - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s -  r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s - r s + 6 s

variables = Sort@Variables[polynomial]
(* {q, q, q, q, r, r, r, r, r, r, r, r, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s} *)

arrays = CoefficientArrays[polynomial, variables];

(matrix = DiagonalMatrix@arrays[] + arrays[]) // MatrixForm We can recover the original polynomial by dotting with the vector of variables, except the diagonal terms will have an extra factor:

variables.matrix.variables - polynomial // Expand
(* -6 s + 6 s^2 *)

• Wow, very cool! Thanks Simon. – Neil Philip Dec 9 '16 at 19:28