# 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) *)
{"ft_lang_label":"__label__en","ft_lang_prob":0.5111054,"math_prob":0.9998049,"size":1239,"snap":"2020-34-2020-40","text_gpt3_token_len":571,"char_repetition_ratio":0.15303643,"word_repetition_ratio":0.2580645,"special_character_ratio":0.5552865,"punctuation_ratio":0.13043478,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99891734,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-03T15:03:21Z\",\"WARC-Record-ID\":\"<urn:uuid:d95be537-f79f-48f7-acc4-0b3f1e6ef0e5>\",\"Content-Length\":\"135191\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:17b836eb-5b42-4dc2-b7c5-24507fff3382>\",\"WARC-Concurrent-To\":\"<urn:uuid:9d43486a-1349-4f48-96f4-e985e5f8701f>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/208099/quadratic-form-for-specific-formula-as-matrix-reprezentation\",\"WARC-Payload-Digest\":\"sha1:I4GXGVG6BGLQJREXXOFYJKADV5CNZQQH\",\"WARC-Block-Digest\":\"sha1:WMNS3QHEZ5GY2S3ZA2CBRYBYQAIXVNJU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735812.88_warc_CC-MAIN-20200803140840-20200803170840-00291.warc.gz\"}"}