Get the coefficient matrix from a quadratic form

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.


I think you need CoefficientArrays:

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

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.