I wrote down this code in mathematica:
A = {{1, 2}, {2, 1}};
A // MatrixForm
L = Eigenvalues[A]
V = Eigenvectors[A]
Do[If[L[[i]] < 0, {L[[i]] = -L[[i]], V[[i]] = -V[[i]]}], {i,
Length[L]}]
L
V
And it simply gives the correct outputs:
Out:{{1,2},{2,1}}
Out:{3, -1}
Out:{{1, 1}, {-1, 1}}
Out:{3, 1}
Out:{{1, 1}, {1, -1}}
But if I write the matrix as a function of some variable and do the same, it's not giving the same answer:
In: A[a_] = {{a, 2}, {2, a}};
(*Compute eigenvalues and eigenvectors*)
S[a_] = Eigenvalues[A[a]];
T[a_] = Eigenvectors[A[a]];
(*Loop to modify eigenvalues and eigenvectors*)
Do[If[S[a][[i]] < 0, {S[a][[i]] = -S[a][[i]],
T[a][[i]] = -T[a][[i]]}], {i, Length[S[a]]}]
S[1]
T[1]
Out:{-1, 3}
Out: {{-1, 1}, {1, 1}}
Can anyone resolve the issue?