# Having trouble coding my own QR decomposition using Gram Schmidt [closed]

I have this code for Gram Schmidt:

GS[A_] :=
Module[{u = {}, col, e = {}, ae, t},
col = Transpose[A]; (* lista dos vectores coluna de A *)
AppendTo[u, col[]];
AppendTo[e, u[]/Norm[u[]]];
For[i = 2, i <=  Length[col], i++,
ae = 0;
t = 1;
While[t <= i - 1, ae = ae - (col[[i]].e[[t]])*e[[t]]; t++];
AppendTo[u, col[[i]] + ae];
AppendTo[e, u[[i]]/Norm[u[[i]]]]];
{u, e}]


And this is my code for QR decomposition:

QR[A_] :=
Module[{Ak, i, Q, R, a = {}, col, k},
Ak = FullSimplify[GS[A]];
col = Transpose[A];
Q = Transpose[Ak[]];
R = Ak[].A;
R = UpperTriangularize[FullSimplify[R]];
{Q, R}]


I want to test this code with this matrix:

SJorge =
Import[
StringJoin["https://c2.staticflickr.com/4/3463/3823583611_c80bf5a375_b.jpg"]];
imagem = ColorConvert[SJorge, "Grayscale"];
M = ImageData[imagem, "Byte"];


It's giving results different than from what I get when I do

QRDecompoition[N[M]]


op

QR[N[M]]


Need some help.

Your QR routine is wrong. Here is a working QR routine:

QR[m_] := Module[{t, r, q},
t = GS[m][];
r = t.m;
q = t;
{q, r}
];


To test it we create a random 3x3 matrix:

m = RandomReal[{-1, 1}, {3, 3}];


Then we get q and r and test it against m==Transpose[q].r:

{q, r} = QR[m];
m == Transpose[q].r
(*True*)
`

Note that QRDecomposition returns the transpose of Q. I adapted the QR routine to this. Note further that the QR decomposition is not unique. We may multiply the rows of q and r by +/-1. Therefore, the output of QR and QRDecomposition may differ in sign.