Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Write a quadratic expression as a sum of squares by eliminating its mixed terms and then completing squares
ResourceFunction["DiagonalizeQuadratic"][quad,vars,newvars] returns the diagonalized quadratic in newvars, the eigenvalues, corresponding orthonormal eigenvectors and a list of substitution rules relating newvars to vars.  | 
Rewrite a quadratic with no linear terms:
| In[1]:= | 
| Out[1]= | 
Confirm the result:
| In[2]:= | 
| Out[2]= | 
Rewrite a quadratic with linear terms:
| In[3]:= | 
| Out[3]= | 
Confirm the result:
| In[4]:= | ![]()  | 
| Out[4]= | 
Rewrite a quadratic in three variables:
| In[5]:= | 
| Out[5]= | ![]()  | 
Confirm the result:
| In[6]:= | 
| Out[6]= | 
When Mathematica returns Root objects in determining the eigensystem of the matrix, DiagonalizeQuadratic returns numerical approximations of the eigenvalues and eigenvectors:
| In[7]:= | 
| Out[6]= | 
Check the result:
| In[8]:= | 
| Out[8]= | 
Mathematica returns numerical approximations for the eigensystem of symmat:
| In[9]:= | 
| Out[9]= | 
This explains why DiagonalizeQuadratic returns numerical approximations for this quadratic:
| In[10]:= | 
| Out[10]= | ![]()  | 
| In[11]:= | 
| Out[11]= | ![]()  | 
The input can be given as an equation:
| In[12]:= | 
| Out[12]= | ![]()  | 
The names of the original variables and the new variables must be disjoint:
| In[13]:= | 
The list of variables and new variables must have the same length:
| In[14]:= | 
If new variables are not specified, an error message is returned:
| In[15]:= | 
Note that if the input quadratic is numericized by applying N to it, then the diagonalized quadratic returned is different from the diagonalized quadratic returned by diagonalizing the non-numericized quadratic. The reason is that Mathematica's Eigensystem returns a different (but equivalent) list of orthonormal eigenvectors:
| In[16]:= | 
| Out[16]= | ![]()  | 
This output is different from the previous output:
| In[17]:= | 
| Out[17]= | ![]()  | 
| In[18]:= | 
| Out[18]= | 
This work is licensed under a Creative Commons Attribution 4.0 International License