-3
$\begingroup$

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[[1]]];
    AppendTo[e, u[[1]]/Norm[u[[1]]]];
    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[[2]]];
    R = Ak[[2]].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.

$\endgroup$

1 Answer 1

4
$\begingroup$

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

QR[m_] := Module[{t, r, q},
   t = GS[m][[2]];
   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.

$\endgroup$

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