11
$\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$

3 Answers 3

14
$\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$
4
  • 1
    $\begingroup$ I need mat be symmetric (and I re-edit the question). $\endgroup$
    – van abel
    May 6, 2016 at 4:57
  • 4
    $\begingroup$ Ok, I find the option "Symmetric" -> True, please update your answer? $\endgroup$
    – van abel
    May 6, 2016 at 5:01
  • 2
    $\begingroup$ @vanabel thank you for pointing out this option. Embarrassing ignorance. :) $\endgroup$
    – ubpdqn
    May 6, 2016 at 5:15
  • 1
    $\begingroup$ @vanabel Edited :) $\endgroup$
    – xzczd
    May 6, 2016 at 5:34
23
$\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$
4
  • 1
    $\begingroup$ Very slick! (+1) $\endgroup$
    – Greg Hurst
    May 6, 2016 at 5:54
  • 1
    $\begingroup$ @Jens neat +1 :) $\endgroup$
    – ubpdqn
    May 6, 2016 at 6:05
  • $\begingroup$ good idea (and trick)! $\endgroup$
    – van abel
    May 6, 2016 at 8:29
  • $\begingroup$ Can anyone explain to me the rationality of this answer ? $\endgroup$ Jan 28, 2021 at 7:59
8
$\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 and acknowledge you have read our privacy policy.

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