# Quadratic forms

A *quadratic form* is a polynomial in $n$ variables with all terms of degree two. Therefore, a quadratic form is a mapping $q:\mathbb{R}^n \rightarrow \mathbb{R}$. The term "form" is another name for "homogeneous polynomial", which is a polynomial whose all nonzero terms have the same degree. 

Here are some examples of quadratic forms in one, two and three variables.

<br>

$$ q(x) = a x^2 $$

<br>

$$
\begin{split}
q(x,y) &= a x^2 + 2b xy + c y^2 \\
       &= x(ax + by) + y(bx + cy)              
\end{split}
$$

<br>

$$
\begin{split}
q(x,y,z) &= a x^2 + d y^2 + f z^2  + 2b xy + 2c xz + 2e yz \\
         &= x(ax + by + cz) + y (bx + dy + ez) + z (cx + ey + fz)         
\end{split}
$$

<br>


As the number of variables increases, more and more terms arise in the polynomial. Linear algebra allows to express quadratic forms with a more compact notation which is invariant from the number of variables.

The examples above can then be expressed as follows.

<br>

$$ 
q(x) = 
\begin{bmatrix}
x
\end{bmatrix}
\begin{bmatrix}
a
\end{bmatrix}
\begin{bmatrix}
x
\end{bmatrix}
= \boldsymbol{x}^\intercal A \boldsymbol{x}
$$

<br>

$$ 
q(x,y) = 
\begin{bmatrix}
x & y
\end{bmatrix}
\begin{bmatrix}
a & b \\
b & c
\end{bmatrix}
\begin{bmatrix}
x \\
y
\end{bmatrix}
= \boldsymbol{x}^\intercal A \boldsymbol{x}
$$ 

<br>

$$ 
q(x,y,z) =
\begin{bmatrix}
x & y & z
\end{bmatrix}
\begin{bmatrix}
a & b & c \\
b & d & e \\
c & e & f
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
z
\end{bmatrix}
= \boldsymbol{x}^\intercal A \boldsymbol{x}
$$ 

<br>

Notice that the matrices $A$s are all symmetric.

Let's verify the correctness of the vectorized expressions above using the *sympy* module.



```python
from sympy import *
x, y, z = symbols('x y z')
a, b, c, d, e, f = symbols('a b c d e f')
```

### q(x,y)


```python
x_2d = Matrix([x, y])
A_2x2 = Matrix([[a, b], [b, c]])

x_2d.T * A_2x2 * x_2d
```




$\displaystyle \left[\begin{matrix}x \left(a x + b y\right) + y \left(b x + c y\right)\end{matrix}\right]$



### q(x,y,z)


```python
x_3d = Matrix([x, y, z])
A_3x3 = Matrix([[a, b, c], [b, d, e], [c, e, f]])

x_3d.T * A_3x3 * x_3d
```




$\displaystyle \left[\begin{matrix}x \left(a x + b y + c z\right) + y \left(b x + d y + e z\right) + z \left(c x + e y + f z\right)\end{matrix}\right]$



In the general case of an arbitrary number of variables $n$, the quadratic form can still be written using the same compact notation.

<br>

$$
\begin{split}
q(x_1,\dots,x_n) &= &\, x_{1} (a_{11} {x_1} + \, &\dots + a_{1n} {x_n}) \, + \\
& &\, x_{2} (a_{21} {x_1} + \, &\dots + a_{2n} {x_n}) \, + \\  
& & &\,\, \vdots \\
& &\, x_{n} (a_{n1} {x_1} + \, &\dots + a_{nn} {x_n}) \\
& = &\, \boldsymbol{x}^\intercal A \boldsymbol{x} &
\end{split}
$$

<br>

Where

$$
A =
\begin{split}
\begin{bmatrix} 
    a_{11} & \dots  & a_{1n} \\
    \vdots & \ddots & \vdots \\
    a_{n1} & \dots  & a_{nn} 
    \end{bmatrix}
\end{split}
$$

<br>

and

<br>

$$
\boldsymbol{x} =
\begin{split}
\begin{bmatrix} 
    x_1  \\
    \vdots \\
    x_n
    \end{bmatrix}
\end{split}
$$

<br>



```python

```

5b281d114b05d4b33c375f4fef3a51b7ced1e399
ipynb
Jupyter Notebook
_notebooks/2021-10-03-Quadratic_Forms.ipynb
lorebucs/zenzo
7ac6f1eb6ca9bdded0636e309dfb8ce99e456fa0
Apache-2.0
_notebooks/2021-10-03-Quadratic_Forms.ipynb
lorebucs/zenzo
7ac6f1eb6ca9bdded0636e309dfb8ce99e456fa0
Apache-2.0
_notebooks/2021-10-03-Quadratic_Forms.ipynb
lorebucs/zenzo
7ac6f1eb6ca9bdded0636e309dfb8ce99e456fa0
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES