0
$\begingroup$

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[1] r[1] - q[2] r[1] - q[3] r[1] - q[4] r[1] - q[1] r[2] - 
 q[2] r[2] - q[3] r[2] - q[4] r[2] - q[1] r[3] - q[2] r[3] - 
 q[3] r[3] - q[4] r[3] - q[1] r[4] - q[2] r[4] - q[3] r[4] - 
 q[4] r[4] - q[1] r[5] - q[2] r[5] - q[3] r[5] - q[4] r[5] - 
 q[1] r[6] - q[2] r[6] - q[3] r[6] - q[4] r[6] - q[1] r[7] - 
 q[2] r[7] - q[3] r[7] - q[4] r[7] - r[1] s[1] - r[2] s[1] - 
 r[3] s[1] - r[4] s[1] - r[5] s[1] - r[6] s[1] - r[7] s[1] - 
 r[8] s[1] - r[1] s[2] - r[2] s[2] - r[3] s[2] - r[4] s[2] - 
 r[5] s[2] - r[6] s[2] - r[7] s[2] - r[8] s[2] - r[1] s[3] - 
 r[2] s[3] - r[3] s[3] - r[4] s[3] - r[5] s[3] - r[6] s[3] - 
 r[7] s[3] - r[8] s[3] - r[1] s[4] - r[2] s[4] - r[3] s[4] - 
 r[4] s[4] - r[5] s[4] - r[6] s[4] - r[7] s[4] - r[8] s[4] - 
 r[1] s[5] - r[2] s[5] - r[3] s[5] - r[4] s[5] - r[5] s[5] - 
 r[6] s[5] - r[7] s[5] - r[8] s[5] - r[1] s[6] - r[2] s[6] - 
 r[3] s[6] - r[4] s[6] - r[5] s[6] - r[6] s[6] - r[7] s[6] - 
 r[8] s[6] - r[1] s[7] - r[2] s[7] - r[3] s[7] - r[4] s[7] - 
 r[5] s[7] - r[6] s[7] - r[7] s[7] - r[8] s[7] - r[1] s[8] - 
 r[2] s[8] - r[3] s[8] - r[4] s[8] - r[5] s[8] - r[6] s[8] - 
 r[7] s[8] - r[8] s[8] - r[1] s[9] - r[2] s[9] - r[3] s[9] - 
 r[4] s[9] - r[5] s[9] - r[6] s[9] - r[7] s[9] - r[8] s[9] - 
 r[1] s[10] - r[2] s[10] - r[3] s[10] - r[4] s[10] - r[5] s[10] - 
 r[6] s[10] - r[7] s[10] - r[8] s[10] - r[1] s[11] - r[2] s[11] - 
 r[3] s[11] - r[4] s[11] - r[5] s[11] - r[6] s[11] - r[7] s[11] - 
 r[8] s[11] - r[1] s[12] - r[2] s[12] - r[3] s[12] - r[4] s[12] - 
 r[5] s[12] - r[6] s[12] - r[7] s[12] - r[8] s[12] - r[1] s[13] - 
 r[2] s[13] - r[3] s[13] - r[4] s[13] - r[5] s[13] - r[6] s[13] - 
 r[7] s[13] - r[8] s[13] - r[1] s[14] - r[2] s[14] - r[3] s[14] - 
 r[4] s[14] - r[5] s[14] - r[6] s[14] - r[7] s[14] - r[8] s[14] - 
 r[1] s[15] - r[2] s[15] - r[3] s[15] - r[4] s[15] - r[5] s[15] - 
 r[6] s[15] - r[7] s[15] - r[8] s[15] + 6s[15]

In this example, there is only 1 1-local term, (6s[15]) 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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – corey979 Dec 9 '16 at 8:47
  • $\begingroup$ Hi corey, the q,r,s sort of represent shifts in the index, so r = +4, s = +12, so r[1] = q[5] and s[1] = q[13], 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. $\endgroup$ – Neil Philip Dec 9 '16 at 19:34
1
$\begingroup$

This should do what you want:

polynomial = -q[1] r[1] - q[2] r[1] - q[3] r[1] - q[4] r[1] - q[1] r[2] - q[2] r[2] - q[3] r[2] - q[4] r[2] - q[1] r[3] - q[2] r[3] - q[3] r[3] - q[4] r[3] - q[1] r[4] - q[2] r[4] - q[3] r[4] - q[4] r[4] - q[1] r[5] - q[2] r[5] - q[3] r[5] - q[4] r[5] - q[1] r[6] - q[2] r[6] - q[3] r[6] - q[4] r[6] - q[1] r[7] - q[2] r[7] - q[3] r[7] - q[4] r[7] - r[1] s[1] - r[2] s[1] - r[3] s[1] - r[4] s[1] - r[5] s[1] - r[6] s[1] - r[7] s[1] - r[8] s[1] - r[1] s[2] - r[2] s[2] - r[3] s[2] - r[4] s[2] - r[5] s[2] - r[6] s[2] - r[7] s[2] - r[8] s[2] - r[1] s[3] - r[2] s[3] - r[3] s[3] - r[4] s[3] - r[5] s[3] - r[6] s[3] - r[7] s[3] - r[8] s[3] - r[1] s[4] - r[2] s[4] - r[3] s[4] - r[4] s[4] - r[5] s[4] - r[6] s[4] - r[7] s[4] - r[8] s[4] - r[1] s[5] - r[2] s[5] - r[3] s[5] - r[4] s[5] - r[5] s[5] - r[6] s[5] - r[7] s[5] - r[8] s[5] - r[1] s[6] - r[2] s[6] - r[3] s[6] - r[4] s[6] - r[5] s[6] - r[6] s[6] - r[7] s[6] - r[8] s[6] - r[1] s[7] - r[2] s[7] - r[3] s[7] - r[4] s[7] - r[5] s[7] - r[6] s[7] - r[7] s[7] - r[8] s[7] - r[1] s[8] - r[2] s[8] - r[3] s[8] - r[4] s[8] - r[5] s[8] - r[6] s[8] - r[7] s[8] - r[8] s[8] - r[1] s[9] - r[2] s[9] - r[3] s[9] - r[4] s[9] - r[5] s[9] - r[6] s[9] - r[7] s[9] - r[8] s[9] - r[1] s[10] - r[2] s[10] - r[3] s[10] - r[4] s[10] - r[5] s[10] - r[6] s[10] - r[7] s[10] - r[8] s[10] - r[1] s[11] - r[2] s[11] - r[3] s[11] - r[4] s[11] - r[5] s[11] - r[6] s[11] - r[7] s[11] - r[8] s[11] - r[1] s[12] - r[2] s[12] - r[3] s[12] - r[4] s[12] - r[5] s[12] - r[6] s[12] - r[7] s[12] - r[8] s[12] - r[1] s[13] - r[2] s[13] - r[3] s[13] - r[4] s[13] - r[5] s[13] -  r[6] s[13] - r[7] s[13] - r[8] s[13] - r[1] s[14] - r[2] s[14] - r[3] s[14] - r[4] s[14] - r[5] s[14] - r[6] s[14] - r[7] s[14] - r[8] s[14] - r[1] s[15] - r[2] s[15] - r[3] s[15] - r[4] s[15] - r[5] s[15] - r[6] s[15] - r[7] s[15] - r[8] s[15] + 6 s[15]

variables = Sort@Variables[polynomial]
(* {q[1], q[2], q[3], q[4], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]} *)

arrays = CoefficientArrays[polynomial, variables];

(matrix = DiagonalMatrix@arrays[[2]] + arrays[[3]]) // MatrixForm

Mathematica graphics

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[15] + 6 s[15]^2 *)
$\endgroup$
  • $\begingroup$ Wow, very cool! Thanks Simon. $\endgroup$ – Neil Philip Dec 9 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.