# Stable Method of orthogonal projection onto a subspace with the help of Moore-Penrose inverse,

Projection of a vector $v$ onto the column space of a matrix $A$ is given by $AA^\dagger v$. From the definition of Moore-Penrose Inverse we know that $AA^\dagger v = (A^T)^\dagger A^T v$.

Below is the code for implementing the projection of a random vector onto the space of a random matrix. This is related to my other question Backward stable algorithm to get orthogonal projection onto the column space of a matrix. I didn't get an answer to that question.

I would like to know why is there a huge difference between the two methods of calculating the projection.

  % testingprojfrostackexchange
clear;
M = 1400;
N = 1300;
r = 1;
A = rand(M,N);
u = rand(M,r);

projLN = pinv(A')*(A'*u);%This is projection through Least Norm
projLS = A*(pinv(A)*u);%This is projection through Least Square

[Q R] = qr(A);
Q = Q(:,1:N);
z1 = Q*(Q'*u);%This is the actual projection

display('(projection through QQT) - projLS');
norm(z1-projLN)/norm(projLN)

display('(projection through QQT) - projLN');
norm(z1-projLS)/norm(projLS)


Output

>> stackexchange
(projection through QQT) - projLS

ans =

2.1569e-13

(projection through QQT) - projLN

ans =

8.3546e-15


The actual projection which is given by $Q(:,1:N)Q(:,1:N)^T$, where $Q$ is from the Housholder decomposition of $A$. We get the projections from $AA^\dagger$ and $(A^T)^\dagger A^T$ and find that $AA^\dagger$ is much better when $A$ is a tall matrix.