I'm using Eigenvectors in GAP to diagonalize a matrix $M$. The matrix is guaranteed to be diagonalizable and in fact it should be so by an orthogonal transformation. This works fine and $D$ is the desired diagonal matrix :
C:=Eigenvectors(CF(4),M);
D:=C*M*C^-1;
However $C$ is usually not orthogonal. There are several methods to orthogonalize it. For example this matlab code https://blogs.mathworks.com/cleve/2016/07/25/compare-gram-schmidt-and-householder-orthogonalization-algorithms/ should be translatable to GAP without much difficulty; but my guess is that it will be slow. Is there anything already built in GAP for this?
I gathered an example before some of the comments and answers were written. I'll post here for reference; (I usually work with much larger matrices). I added a simple Gram-Schmidt orthogonalization translated from the referenced link:
M:=[
[1,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2],
[0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,2,0,0,2,0,0,2,0,0,0],
[0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0],
[2,0,0,0,0,1,0,0,0,0,2,0,0,0,0,2],
[0,0,0,2,0,0,1,0,0,2,0,0,2,0,0,0],
[0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0],
[0,0,0,2,0,0,2,0,0,1,0,0,2,0,0,0],
[2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,2],
[0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0],
[0,0,0,2,0,0,2,0,0,2,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0],
[2,0,0,0,0,2,0,0,0,0,2,0,0,0,0,1]];
C:=Eigenvectors(CF(4),M);
Print("CMC^-1 diagonal? : ",IsDiagonalMat(CMC^-1),"\n");
Print("C orthonal? : ",TransposedMat(C)=C^-1,"\n");
gramschmidt:=function(V)local U,n,m,T,norm,k,h;
norm:=v->Sqrt(vv);
T:=DimensionsMat(V);
m:=T[1];n:=T[2];U:=NullMat(m,n);
U[1]:=V[1]/norm(V[1]);
for k in [2..m] do
U[k]:=V[k];
for h in [1..k-1] do
U[k]:=U[k]-((U[h]U[k])/(U[h]U[h]))U[h];
od;
U[k]:=U[k]/norm(U[k]);
od;
return U;end;
R:=gramschmidt(C);
Print("RMR^-1 diagonal? : ",IsDiagonalMat(RMR^-1),"\n");
Print("R orthonal? : ",TransposedMat(R)=R^-1,"\n");
norm := v -> 1;and I think yours is fine. It would be faster if it sorted by eigenvalue (since they are already orthogonal and don't need any updates) – Jack Schmidt May 27 '21 at 21:16