# How to transform symmetric matrix to diagonal?

I wonder whether there is a way how to transform symmetric matrix to diagonal matrix. I could not find any function that performs symmetric transformation in Mathematica.

E.g. I have matrix {{1,a},{a,2}} and I want to do TransformToDiagonal[{{1,a},{a,2}}]. The result should be {{1,0},{0,2-a^2}}.

• How did you get the this result{{1,0},{0,2-a^2}} ? – L.K. Feb 13 '17 at 13:24
• I added (-a)*1st row to 2nd row and then (-a)*1st column to 2nd column. – velblúd Feb 13 '17 at 13:25
• Are you talking about row reduced echelon? – L.K. Feb 13 '17 at 13:26
• Kind of, but I need to use symmetric transformation. At the end, I should have zeros everywhere except for the diagonal. – velblúd Feb 13 '17 at 13:32
• You wrote wrong matrix in question? – L.K. Feb 13 '17 at 13:53

First we need to check whether the matrix in question is symmetric or not.

SymmetricMatrixQ[{{1, a}, {a, 2}}]


True

Now we will go for row reduction to see what we get,

RowReduce[{{1, a}, {a, 2}}] // MatrixForm


\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

This is not what you want. Now I will try Nassers approach to use LUDecomposition

{lu, p, c} = LUDecomposition[{{1, a}, {a, 2}}]
(u = lu SparseArray[{i_, j_} /; j >= i -> 1, {2, 2}]) // MatrixForm


\begin{pmatrix} 1 & a \\ 0 & 2-a^2 \end{pmatrix}

I maybe wrong but it seems what you are after is impossible for me to produce.

Edit

Following @george2079 suggestions, we can get the OPs desired result,

Transpose[u];
{lu, p, c} = LUDecomposition[%];
(u = lu SparseArray[{i_, j_} /; j >= i -> 1, {2, 2}]) // MatrixForm


\begin{pmatrix} 1 & 0 \\ 0 & 2-a^2 \end{pmatrix}

• This seems to be working well-enough for 2x2 matrices (probably because in this case the "column" part of a symmetric transformation does not change the diagonal) , but gives completely wrong results for bigger ones. Anyway thanks for your answer. – velblúd Feb 13 '17 at 14:05
• Transpose and repeat the LUDecomposition approach.. gives the same result as my answer. – george2079 Feb 13 '17 at 15:36
• @george2079 Wow!. Thanks – zhk Feb 13 '17 at 15:51
• @velblúd You should accept george2079 response as an answer not mine. I adopted his idea. – zhk Feb 13 '17 at 18:04
• Thank you guys. I accepted this answer as it is nicer and more robust that the procedural approach. – velblúd Feb 13 '17 at 18:06

kind of ugly procedural approach:

n = 3
m = Table[ a[Min[i, j], Max[i, j] ], {i, n}, {j, n}] ;
Do[ m[[row]] = m[[row]] - m[[prow]]  m[[row, prow]]/m[[prow, prow]]
, {prow, 1, n - 1} , {row, {prow + 1, n} }];
m = Transpose[m];
Do[ m[[row]] = m[[row]] - m[[prow]]  m[[row, prow]]/m[[prow, prow]]
, {prow, 1, n - 1} , {row, {prow + 1, n} }];


m // MatrixForm

of course for m={{1, a}, {a, 2}} this yields {{1, 0}, {0, 2 - a^2}}

• Thank you, this is what I need! Just one note - it fails if there is zero on the diagonal. (e.g. {{0,1},{0,1}}). But this is good enough for me. – velblúd Feb 13 '17 at 18:01

another approach

m = {{1, a}, {a, 2}};
Nest[(Transpose@UpperTriangularize@First@LUDecomposition@#) &, m, 2]
{{1, 0}, {0, 2 - a^2}}


With @george2079 matrix:

n = 3
m = Table[a[Min[i, j], Max[i, j]], {i, n}, {j, n}];
Nest[(Transpose@UpperTriangularize@First@LUDecomposition@#) &, m,
2] // MatrixForm