Q1. In his lecture, Prof. Strang begins with the simplest case: the orthogonal projection of a vector b onto a one dimensional subspace S=span⟨a⟩, a being a non-null vector in Rn. The projection vector p should be a multiple of a, so Prof. Strang writes it as p=xa. Here x is a scalar number, not a matrix, and a is a vector, identified with a n×1 matrix. He wants to deduce that p=Pb, where P is the projection matrix
P=1aTaaaT,
which is an n×n matrix. To this end, Prof. Strang writes the projection vector as p=ax. Now, you should see p and a as n×1 matrices and x as a one dimensional vector or a 1×1 matrix, so that a and x can be multiplied. Thus, we have
xa=ax.
This identity does not mean that a and x are two matrices that commute. Just read the left and right sides as stated above. It is true that the notation may be a bit misleading. Perhaps it would be better to write (x) instead of x if x should be seen as a 1×1 matrix (parentheses are used to delimit matrices). For example,
4(123)=(4812)=(123)(4).
The advantage of expressing p as ax is that generalization is then possible. If S is spanned by the linearly independent vectors a1,…,ak, the orthogonal projection p of a vector b onto S should be a linear combination of a1,…,ak, that is,
p=x1a1+⋯+xkak=AX
with
A=(a1|…|ak)
and
X=(x1⋮xk)
The reasoning in Prof. Strang’s lecture would then show that p=Pb, where P is now the projection matrix
P=A(ATA)−1AT.
Returning to your question, you write P=X*A
where, in fact, you should write p=xa and consider x being an scalar, not a matrix. Consequently, since x is a scalar, from aT(b−p)=0, one has
aTb=aTp=aT(xa)=xaTa.
Hence
x=aTbaTa.
Q2. In your code, A.transpose()*A
is a 1×1 matrix. You cannot divide by a matrix. Thus you need to extract the unique element of this matrix, either with the .det()
method or simply writing (A.transpose()*A)[0,0]
. By the way, note that, in the video, the projection matrix is denoted by P, not X.
In SageMath, I think that it is better to use vector
instead of matrix
to represent a, b and p, as shown here.
I know the first question is basic math question not a sagemath question, sorry, tell me if I must post it on a math forum instead. now the code in the link is my last code.