# Get the coefficient matrix from a quadratic form

Suppose I have a quadratic form of

qf = a x^2 + b y^2 + c z^2 + 2 d x y + 2 e x z + 2 f y z


How can I easily to get the symmetric matrix A, such that $X^TAX=qf$? where $X^T=(x,y,z)$.

I hope that the method will work for general quadratic form (i.e., for 2nd-homogenous polynomial of variable x,y,z,w,...)

I have tried the function CoefficientRules, but it seem there needs a step to transform the order of each term into the "position" of matrix.

I think you need CoefficientArrays:

mat = Last@CoefficientArrays[qf, {x, y, z}, "Symmetric"->True];
{x, y, z}.mat.{x, y, z} == qf // Simplify
(* True *)

• I need mat be symmetric (and I re-edit the question). – van abel May 6 '16 at 4:57
• Ok, I find the option "Symmetric" -> True, please update your answer? – van abel May 6 '16 at 5:01
• @vanabel thank you for pointing out this option. Embarrassing ignorance. :) – ubpdqn May 6 '16 at 5:15
• @vanabel Edited :) – xzczd May 6 '16 at 5:34

Here is a very short solution:

qf = a x^2 + b y^2 + c z^2 + 2 d x y + 2 e x z + 2 f y z;

1/2 D[qf, {{x, y, z}, 2}]
(* ==> {{a, d, e}, {d, b, f}, {e, f, c}} *)


This is just an application of the answer to Quick Hessian matrix and gradient calculation.

• Very slick! (+1) – Chip Hurst May 6 '16 at 5:54
• @Jens neat +1 :) – ubpdqn May 6 '16 at 6:05
• good idea (and trick)! – van abel May 6 '16 at 8:29

Here is a way that yields symmetric matrix (for this example you could just write it down):

m=Module[{r = {x -> 1, y -> 2, z -> 3}, tu = Tuples[{x, y, z}, 2]},
Normal@SparseArray[(## /. r) ->
Coefficient[qf, Times @@ ##]/(2 - Boole[#[[1]] === #[[2]]]) & /@
tu, {3, 3}]]


yields:

{{a, d, e}, {d, b, f}, {e, f, c}}


Check:

Expand[{x, y, z}.m.{x, y, z}]


yields qf

CoefficientArrays as per @xzczd yields:

{{a, 2 d, 2 e}, {0, b, 2 f}, {0, 0, c}}


which is also a valid representation.