# Quadratic form for specific formula as matrix reprezentation [closed]

So I have problem for specific formula to create Quadratic_form form from $$y = 3 + x_1 - 2 x_4 + 2 x_3 x_4 + x_2 (-x_3 + x_4)$$ as matrix $$\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1/2 & 1/2 \\ 0 & -1/2 & 0 & 1 \\ 0 & 1/2 & 1 & -2 \end{bmatrix}$$ We have in Mathematica special function for this?

• I don't understand the relationship between the coefficients and the values in the matrix, so I can't help. The reference doesn't explain the form either. – mikado Oct 17 '19 at 20:19
• @mikado of course, I fixed this issue. The representation of quadratic form as matrix, more is here: en.wikipedia.org/wiki/Quadratic_form – blabla Oct 17 '19 at 21:01
• Suggest you look at CoefficientArray – mikado Oct 17 '19 at 22:08
• Quoting the first line from the linked page, "In mathematics, a quadratic form is a polynomial with terms all of degree two." The example has terms not of degree two. So it needs to be made clear what exactly is wanted. – Daniel Lichtblau Oct 17 '19 at 22:24
• Have a look at SolveAlways. – yarchik Oct 18 '19 at 15:09

If you want to determine a symmetric matrix $$A$$ such that if $$x=(1,x_1,x_2,x_3,x_4)$$ and

$$x^T A x = 3 + x_1 - 2 x_4 + 2 x_3 x_4 + x_2 (-x_3 + x_4)$$

then the following (based on the comment by @yarchik) should do that:

(* Create a symbolic symmetric matrix *)
A = Table[a[Min[i, j], Max[i, j]], {i, 0, 4}, {j, 0, 4}];

y = 3 + x1 - 2 x4 + 2 x3 x4 + x2 (-x3 + x4)

(* Solve for the values of the symmetric matrix *)
x = {1, x1, x2, x3, x4};
(A = A /. SolveAlways[x.A.x == y, {x1, x2, x3, x4}][]) // MatrixForm


$$\left( \begin{array}{ccccc} 3 & \frac{1}{2} & 0 & 0 & -1 \\ \frac{1}{2} & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & -\frac{1}{2} & \frac{1}{2} \\ 0 & 0 & -\frac{1}{2} & 0 & 1 \\ -1 & 0 & \frac{1}{2} & 1 & 0 \\ \end{array} \right)$$

As a check...

x.A.x // Simplify
(* 3 + x1 - 2 x4 + 2 x3 x4 + x2 (-x3 + x4) *)