9
$\begingroup$

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.

$\endgroup$
11
$\begingroup$

I think you need CoefficientArrays:

mat = Last@CoefficientArrays[qf, {x, y, z}, "Symmetric"->True];
{x, y, z}.mat.{x, y, z} == qf // Simplify
(* True *)
$\endgroup$
  • $\begingroup$ I need mat be symmetric (and I re-edit the question). $\endgroup$ – van abel May 6 '16 at 4:57
  • 3
    $\begingroup$ Ok, I find the option "Symmetric" -> True, please update your answer? $\endgroup$ – van abel May 6 '16 at 5:01
  • 1
    $\begingroup$ @vanabel thank you for pointing out this option. Embarrassing ignorance. :) $\endgroup$ – ubpdqn May 6 '16 at 5:15
  • $\begingroup$ @vanabel Edited :) $\endgroup$ – xzczd May 6 '16 at 5:34
16
$\begingroup$

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.

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

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.

$\endgroup$

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.