0
$\begingroup$

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}}.

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

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}

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – velblúd Feb 13 '17 at 14:05
  • 1
    $\begingroup$ Transpose and repeat the LUDecomposition approach.. gives the same result as my answer. $\endgroup$ – george2079 Feb 13 '17 at 15:36
  • $\begingroup$ @george2079 Wow!. Thanks $\endgroup$ – zhk Feb 13 '17 at 15:51
  • $\begingroup$ @velblúd You should accept george2079 response as an answer not mine. I adopted his idea. $\endgroup$ – zhk Feb 13 '17 at 18:04
  • $\begingroup$ Thank you guys. I accepted this answer as it is nicer and more robust that the procedural approach. $\endgroup$ – velblúd Feb 13 '17 at 18:06
1
$\begingroup$

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

enter image description here

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – velblúd Feb 13 '17 at 18:01
1
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.